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

pull/205/head
Thibaut 10 years ago
parent cb0b2e02d7
commit 7de19cf800

@ -6,6 +6,7 @@ module Docs
def initialize def initialize
@entries = [] @entries = []
@index = Set.new
@types = Hash.new { |hash, key| hash[key] = Type.new key } @types = Hash.new { |hash, key| hash[key] = Type.new key }
end end
@ -38,9 +39,11 @@ module Docs
private private
def add_entry(entry) def add_entry(entry)
if @index.add?(entry.as_json.to_s)
@entries << entry.dup @entries << entry.dup
@types[entry.type].count += 1 if entry.type @types[entry.type].count += 1 if entry.type
end end
end
def entries_as_json def entries_as_json
@entries.sort!.map { |entry| entry.as_json } @entries.sort!.map { |entry| entry.as_json }

@ -205,7 +205,10 @@ class DocsDocTest < MiniTest::Spec
context "when pages are built successfully" do context "when pages are built successfully" do
let :pages 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 end
before do before do
@ -233,7 +236,7 @@ class DocsDocTest < MiniTest::Spec
mock(store).write('index.json', anything) do |path, json| mock(store).write('index.json', anything) do |path, json|
json = JSON.parse(json) json = JSON.parse(json)
assert_equal pages.length, json['entries'].length 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 end
doc.store_pages(store) doc.store_pages(store)
end end

@ -3,7 +3,7 @@ require 'docs'
class DocsEntryIndexTest < MiniTest::Spec class DocsEntryIndexTest < MiniTest::Spec
let :entry do let :entry do
Docs::Entry.new 'name', 'type', 'path' Docs::Entry.new 'name', 'path', 'type'
end end
let :index do let :index do
@ -29,7 +29,11 @@ class DocsEntryIndexTest < MiniTest::Spec
end end
it "stores an array of entries" do 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) index.add(entries)
assert_equal entries, index.entries assert_equal entries, index.entries
end end
@ -46,11 +50,17 @@ class DocsEntryIndexTest < MiniTest::Spec
assert_empty index.types assert_empty index.types
end 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 it "creates and indexes the type" do
entry.type = 'one'; index.add(entry) index.add Docs::Entry.new('one', 'path', 'a')
entry.type = 'two'; 2.times { index.add(entry) } index.add Docs::Entry.new('two', 'path', 'b')
assert_equal ['one', 'two'], index.types.keys index.add Docs::Entry.new('three', 'path', 'b')
assert_instance_of Docs::Type, index.types['one'] assert_equal ['a', 'b'], index.types.keys
assert_instance_of Docs::Type, index.types['a']
end end
it "doesn't index the nil type" do it "doesn't index the nil type" do
@ -59,8 +69,9 @@ class DocsEntryIndexTest < MiniTest::Spec
end end
it "increments the type's count" do it "increments the type's count" do
2.times { index.add(entry) } index.add Docs::Entry.new('one', 'path', 'type')
assert_equal 2, index.types[entry.type].count index.add Docs::Entry.new('two', 'path', 'type')
assert_equal 2, index.types['type'].count
end end
end end
@ -90,8 +101,9 @@ class DocsEntryIndexTest < MiniTest::Spec
end end
it "includes the json representation of the #entries" do it "includes the json representation of the #entries" do
index.add [entry, entry] index.add one = Docs::Entry.new('one', 'path', 'type')
assert_equal [entry.as_json, entry.as_json], index.as_json[:entries] index.add two = Docs::Entry.new('two', 'path', 'type')
assert_equal [one.as_json, two.as_json], index.as_json[:entries]
end end
it "is sorted by name, case-insensitive" do it "is sorted by name, case-insensitive" do

Loading…
Cancel
Save