From 7de19cf80008f3f62b7c20b9d46e39619ca52f46 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sun, 26 Apr 2015 18:25:11 -0400 Subject: [PATCH] Make EntryIndex a unique index (don't add the same entry twice) --- lib/docs/core/entry_index.rb | 7 ++++-- test/lib/docs/core/doc_test.rb | 7 ++++-- test/lib/docs/core/entry_index_test.rb | 32 ++++++++++++++++++-------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/docs/core/entry_index.rb b/lib/docs/core/entry_index.rb index 3c819c70..7c4e7a0e 100644 --- a/lib/docs/core/entry_index.rb +++ b/lib/docs/core/entry_index.rb @@ -6,6 +6,7 @@ module Docs def initialize @entries = [] + @index = Set.new @types = Hash.new { |hash, key| hash[key] = Type.new key } end @@ -38,8 +39,10 @@ module Docs private def add_entry(entry) - @entries << entry.dup - @types[entry.type].count += 1 if entry.type + if @index.add?(entry.as_json.to_s) + @entries << entry.dup + @types[entry.type].count += 1 if entry.type + end end def entries_as_json diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index 2a74d32d..40aebcad 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/test/lib/docs/core/doc_test.rb @@ -205,7 +205,10 @@ class DocsDocTest < MiniTest::Spec context "when pages are built successfully" do let :pages do - [page.dup, page.dup] + [ + page.deep_dup.tap { |p| page[:entries].first.tap { |e| e.name = 'one' } }, + page.deep_dup.tap { |p| page[:entries].first.tap { |e| e.name = 'two' } } + ] end before do @@ -233,7 +236,7 @@ class DocsDocTest < MiniTest::Spec mock(store).write('index.json', anything) do |path, json| json = JSON.parse(json) assert_equal pages.length, json['entries'].length - assert_includes json['entries'], entry.as_json.stringify_keys + assert_includes json['entries'], Docs::Entry.new('one').as_json.stringify_keys end doc.store_pages(store) end diff --git a/test/lib/docs/core/entry_index_test.rb b/test/lib/docs/core/entry_index_test.rb index 98abba3a..f52142bf 100644 --- a/test/lib/docs/core/entry_index_test.rb +++ b/test/lib/docs/core/entry_index_test.rb @@ -3,7 +3,7 @@ require 'docs' class DocsEntryIndexTest < MiniTest::Spec let :entry do - Docs::Entry.new 'name', 'type', 'path' + Docs::Entry.new 'name', 'path', 'type' end let :index do @@ -29,7 +29,11 @@ class DocsEntryIndexTest < MiniTest::Spec end it "stores an array of entries" do - entries = [entry, entry] + entries = [ + Docs::Entry.new('one', 'path', 'type'), + Docs::Entry.new('two', 'path', 'type') + ] + index.add(entries) assert_equal entries, index.entries end @@ -46,11 +50,17 @@ class DocsEntryIndexTest < MiniTest::Spec assert_empty index.types end + it "doesn't store the same entry twice" do + 2.times { index.add(entry.dup) } + assert_equal [entry], index.entries + end + it "creates and indexes the type" do - entry.type = 'one'; index.add(entry) - entry.type = 'two'; 2.times { index.add(entry) } - assert_equal ['one', 'two'], index.types.keys - assert_instance_of Docs::Type, index.types['one'] + index.add Docs::Entry.new('one', 'path', 'a') + index.add Docs::Entry.new('two', 'path', 'b') + index.add Docs::Entry.new('three', 'path', 'b') + assert_equal ['a', 'b'], index.types.keys + assert_instance_of Docs::Type, index.types['a'] end it "doesn't index the nil type" do @@ -59,8 +69,9 @@ class DocsEntryIndexTest < MiniTest::Spec end it "increments the type's count" do - 2.times { index.add(entry) } - assert_equal 2, index.types[entry.type].count + index.add Docs::Entry.new('one', 'path', 'type') + index.add Docs::Entry.new('two', 'path', 'type') + assert_equal 2, index.types['type'].count end end @@ -90,8 +101,9 @@ class DocsEntryIndexTest < MiniTest::Spec end it "includes the json representation of the #entries" do - index.add [entry, entry] - assert_equal [entry.as_json, entry.as_json], index.as_json[:entries] + index.add one = Docs::Entry.new('one', 'path', 'type') + index.add two = Docs::Entry.new('two', 'path', 'type') + assert_equal [one.as_json, two.as_json], index.as_json[:entries] end it "is sorted by name, case-insensitive" do