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
6 6
7 def initialize7 def initialize
8 @entries = []8 @entries = []
9 @index = Set.new
9 @types = Hash.new { |hash, key| hash[key] = Type.new key }10 @types = Hash.new { |hash, key| hash[key] = Type.new key }
10 end11 end
11 12
@@ -38,8 +39,10 @@ module Docs · 26 unmodified lines
38 private39 private
39 40
40 def add_entry(entry)41 def add_entry(entry)
41 @entries << entry.dup42 if @index.add?(entry.as_json.to_s)
42 @types[entry.type].count += 1 if entry.type43 @entries << entry.dup
44 @types[entry.type].count += 1 if entry.type
45 end
43 end46 end
44 47
45 def entries_as_json48 def entries_as_json
test/lib/docs/core/doc_test.rb+5−2
@@ -205,7 +205,10 @@ class DocsDocTest < MiniTest::Spec · 204 unmodified lines
205 205
206 context "when pages are built successfully" do206 context "when pages are built successfully" do
207 let :pages do207 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 ]
209 end212 end
210 213
211 before do214 before do
@@ -233,7 +236,7 @@ class DocsDocTest < MiniTest::Spec · 21 unmodified lines
233 mock(store).write('index.json', anything) do |path, json|236 mock(store).write('index.json', anything) do |path, json|
234 json = JSON.parse(json)237 json = JSON.parse(json)
235 assert_equal pages.length, json['entries'].length238 assert_equal pages.length, json['entries'].length
236 assert_includes json['entries'], entry.as_json.stringify_keys239 assert_includes json['entries'], Docs::Entry.new('one').as_json.stringify_keys
237 end240 end
238 doc.store_pages(store)241 doc.store_pages(store)
239 end242 end
test/lib/docs/core/entry_index_test.rb+22−10
@@ -3,7 +3,7 @@ require 'docs' · 2 unmodified lines
3 3
4class DocsEntryIndexTest < MiniTest::Spec4class DocsEntryIndexTest < MiniTest::Spec
5 let :entry do5 let :entry do
6 Docs::Entry.new 'name', 'type', 'path'6 Docs::Entry.new 'name', 'path', 'type'
7 end7 end
8 8
9 let :index do9 let :index do
@@ -29,7 +29,11 @@ class DocsEntryIndexTest < MiniTest::Spec · 19 unmodified lines
29 end29 end
30 30
31 it "stores an array of entries" do31 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
33 index.add(entries)37 index.add(entries)
34 assert_equal entries, index.entries38 assert_equal entries, index.entries
35 end39 end
@@ -46,11 +50,17 @@ class DocsEntryIndexTest < MiniTest::Spec · 10 unmodified lines
46 assert_empty index.types50 assert_empty index.types
47 end51 end
48 52
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
49 it "creates and indexes the type" do58 it "creates and indexes the type" do
50 entry.type = 'one'; index.add(entry)59 index.add Docs::Entry.new('one', 'path', 'a')
51 entry.type = 'two'; 2.times { index.add(entry) }60 index.add Docs::Entry.new('two', 'path', 'b')
52 assert_equal ['one', 'two'], index.types.keys61 index.add Docs::Entry.new('three', 'path', 'b')
53 assert_instance_of Docs::Type, index.types['one']62 assert_equal ['a', 'b'], index.types.keys
63 assert_instance_of Docs::Type, index.types['a']
54 end64 end
55 65
56 it "doesn't index the nil type" do66 it "doesn't index the nil type" do
@@ -59,8 +69,9 @@ class DocsEntryIndexTest < MiniTest::Spec · 2 unmodified lines
59 end69 end
60 70
61 it "increments the type's count" do71 it "increments the type's count" do
62 2.times { index.add(entry) }72 index.add Docs::Entry.new('one', 'path', 'type')
63 assert_equal 2, index.types[entry.type].count73 index.add Docs::Entry.new('two', 'path', 'type')
74 assert_equal 2, index.types['type'].count
64 end75 end
65 end76 end
66 77
@@ -90,8 +101,9 @@ class DocsEntryIndexTest < MiniTest::Spec · 23 unmodified lines
90 end101 end
91 102
92 it "includes the json representation of the #entries" do103 it "includes the json representation of the #entries" do
93 index.add [entry, entry]104 index.add one = Docs::Entry.new('one', 'path', 'type')
94 assert_equal [entry.as_json, entry.as_json], index.as_json[:entries]105 index.add two = Docs::Entry.new('two', 'path', 'type')
106 assert_equal [one.as_json, two.as_json], index.as_json[:entries]
95 end107 end
96 108
97 it "is sorted by name, case-insensitive" do109 it "is sorted by name, case-insensitive" do