History

Make EntryIndex a unique index (don't add the same entry twice)

Thibaut committed 11 years ago

Commit 7de19cf1 parent cb0b2e03 files · +32 −14Browse files

Unified · Split

3 files
  1. lib/docs/core/entry_index.rb +5 −2
  2. test/lib/docs/core/doc_test.rb +5 −2
  3. test/lib/docs/core/entry_index_test.rb +22 −10
lib/docs/core/entry_index.rb+5−2
@@ -6,6 +6,7 @@ module Docs · 5 unmodified lines
66
77 def initialize
88 @entries = []
9+ @index = Set.new
910 @types = Hash.new { |hash, key| hash[key] = Type.new key }
1011 end
1112
@@ -38,8 +39,10 @@ module Docs · 26 unmodified lines
3839 private
3940
4041 def add_entry(entry)
41− @entries << entry.dup
42− @types[entry.type].count += 1 if entry.type
42+ if @index.add?(entry.as_json.to_s)
43+ @entries << entry.dup
44+ @types[entry.type].count += 1 if entry.type
45+ end
4346 end
4447
4548 def entries_as_json
test/lib/docs/core/doc_test.rb+5−2
@@ -205,7 +205,10 @@ class DocsDocTest < MiniTest::Spec · 204 unmodified lines
205205
206206 context "when pages are built successfully" do
207207 let :pages do
208− [page.dup, page.dup]
208+ [
209+ page.deep_dup.tap { |p| page[:entries].first.tap { |e| e.name = 'one' } },
210+ page.deep_dup.tap { |p| page[:entries].first.tap { |e| e.name = 'two' } }
211+ ]
209212 end
210213
211214 before do
@@ -233,7 +236,7 @@ class DocsDocTest < MiniTest::Spec · 21 unmodified lines
233236 mock(store).write('index.json', anything) do |path, json|
234237 json = JSON.parse(json)
235238 assert_equal pages.length, json['entries'].length
236− assert_includes json['entries'], entry.as_json.stringify_keys
239+ assert_includes json['entries'], Docs::Entry.new('one').as_json.stringify_keys
237240 end
238241 doc.store_pages(store)
239242 end
test/lib/docs/core/entry_index_test.rb+22−10
@@ -3,7 +3,7 @@ require 'docs' · 2 unmodified lines
33
44 class DocsEntryIndexTest < MiniTest::Spec
55 let :entry do
6− Docs::Entry.new 'name', 'type', 'path'
6+ Docs::Entry.new 'name', 'path', 'type'
77 end
88
99 let :index do
@@ -29,7 +29,11 @@ class DocsEntryIndexTest < MiniTest::Spec · 19 unmodified lines
2929 end
3030
3131 it "stores an array of entries" do
32− entries = [entry, entry]
32+ entries = [
33+ Docs::Entry.new('one', 'path', 'type'),
34+ Docs::Entry.new('two', 'path', 'type')
35+ ]
36+
3337 index.add(entries)
3438 assert_equal entries, index.entries
3539 end
@@ -46,11 +50,17 @@ class DocsEntryIndexTest < MiniTest::Spec · 10 unmodified lines
4650 assert_empty index.types
4751 end
4852
53+ it "doesn't store the same entry twice" do
54+ 2.times { index.add(entry.dup) }
55+ assert_equal [entry], index.entries
56+ end
57+
4958 it "creates and indexes the type" do
50− entry.type = 'one'; index.add(entry)
51− entry.type = 'two'; 2.times { index.add(entry) }
52− assert_equal ['one', 'two'], index.types.keys
53− assert_instance_of Docs::Type, index.types['one']
59+ index.add Docs::Entry.new('one', 'path', 'a')
60+ index.add Docs::Entry.new('two', 'path', 'b')
61+ index.add Docs::Entry.new('three', 'path', 'b')
62+ assert_equal ['a', 'b'], index.types.keys
63+ assert_instance_of Docs::Type, index.types['a']
5464 end
5565
5666 it "doesn't index the nil type" do
@@ -59,8 +69,9 @@ class DocsEntryIndexTest < MiniTest::Spec · 2 unmodified lines
5969 end
6070
6171 it "increments the type's count" do
62− 2.times { index.add(entry) }
63− assert_equal 2, index.types[entry.type].count
72+ index.add Docs::Entry.new('one', 'path', 'type')
73+ index.add Docs::Entry.new('two', 'path', 'type')
74+ assert_equal 2, index.types['type'].count
6475 end
6576 end
6677
@@ -90,8 +101,9 @@ class DocsEntryIndexTest < MiniTest::Spec · 23 unmodified lines
90101 end
91102
92103 it "includes the json representation of the #entries" do
93− index.add [entry, entry]
94− assert_equal [entry.as_json, entry.as_json], index.as_json[:entries]
104+ index.add one = Docs::Entry.new('one', 'path', 'type')
105+ index.add two = Docs::Entry.new('two', 'path', 'type')
106+ assert_equal [one.as_json, two.as_json], index.as_json[:entries]
95107 end
96108
97109 it "is sorted by name, case-insensitive" do