History

Require all entries to have a name, path and type

Thibaut Courouble committed 10 years ago

Commit 5bb96f81 parent 18199103 files · +45 −20Browse files

Unified · Split

3 files
  1. lib/docs/core/models/entry.rb +10 −0
  2. test/lib/docs/core/doc_test.rb +2 −2
  3. test/lib/docs/core/models/entry_test.rb +33 −18
lib/docs/core/models/entry.rb+10−0
@@ -1,11 +1,21 @@
1# frozen_string_literal: true
2
1module Docs3module Docs
2 class Entry4 class Entry
5 class Invalid < StandardError; end
6
3 attr_accessor :name, :type, :path7 attr_accessor :name, :type, :path
4 8
5 def initialize(name = nil, path = nil, type = nil)9 def initialize(name = nil, path = nil, type = nil)
6 self.name = name10 self.name = name
7 self.path = path11 self.path = path
8 self.type = type12 self.type = type
13
14 unless root?
15 raise Invalid, 'missing name' if !name || name.empty?
16 raise Invalid, 'missing path' if !path || path.empty?
17 raise Invalid, 'missing type' if !type || type.empty?
18 end
9 end19 end
10 20
11 def ==(other)21 def ==(other)
test/lib/docs/core/doc_test.rb+2−2
@@ -14,7 +14,7 @@ class DocsDocTest < MiniTest::Spec · 13 unmodified lines
14 end14 end
15 15
16 let :entry do16 let :entry do
17 Docs::Entry.new17 Docs::Entry.new 'name', 'path', 'type'
18 end18 end
19 19
20 let :store do20 let :store do
@@ -262,7 +262,7 @@ class DocsDocTest < MiniTest::Spec · 241 unmodified lines
262 mock(store).write('index.json', anything) do |path, json|262 mock(store).write('index.json', anything) do |path, json|
263 json = JSON.parse(json)263 json = JSON.parse(json)
264 assert_equal pages.length, json['entries'].length264 assert_equal pages.length, json['entries'].length
265 assert_includes json['entries'], Docs::Entry.new('one').as_json.stringify_keys265 assert_includes json['entries'], Docs::Entry.new('one', 'path', 'type').as_json.stringify_keys
266 end266 end
267 doc.store_pages(store)267 doc.store_pages(store)
268 end268 end
test/lib/docs/core/models/entry_test.rb+33−18
@@ -5,20 +5,35 @@ class DocsEntryTest < MiniTest::Spec · 4 unmodified lines
5 Entry = Docs::Entry5 Entry = Docs::Entry
6 6
7 let :entry do7 let :entry do
8 Entry.new8 Entry.new('name', 'path', 'type')
9 end
10
11 def build_entry(name = 'name', path = 'path', type = 'type')
12 Entry.new(name, path, type)
9 end13 end
10 14
11 describe ".new" do15 describe ".new" do
12 it "stores a name" do16 it "stores #name, #path and #type" do
13 assert_equal 'name', Entry.new('name').name17 entry = Entry.new('name', 'path', 'type')
18 assert_equal 'name', entry.name
19 assert_equal 'path', entry.path
20 assert_equal 'type', entry.type
14 end21 end
15 22
16 it "stores a path" do23 it "raises an error when #name, #path or #type is nil or empty" do
17 assert_equal 'path', Entry.new(nil, 'path').path24 assert_raises(Docs::Entry::Invalid) { Entry.new(nil, 'path', 'type') }
25 assert_raises(Docs::Entry::Invalid) { Entry.new('', 'path', 'type') }
26 assert_raises(Docs::Entry::Invalid) { Entry.new('name', nil, 'type') }
27 assert_raises(Docs::Entry::Invalid) { Entry.new('name', '', 'type') }
28 assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', nil) }
29 assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', '') }
18 end30 end
19 31
20 it "stores a type" do32 it "don't raise an error when #path is 'index' and #name or #type is nil or empty" do
21 assert_equal 'type', Entry.new(nil, nil, 'type').type33 Entry.new(nil, 'index', 'type')
34 Entry.new('', 'index', 'type')
35 Entry.new('name', 'index', nil)
36 Entry.new('name', 'index', '')
22 end37 end
23 end38 end
24 39
@@ -48,40 +63,40 @@ class DocsEntryTest < MiniTest::Spec · 23 unmodified lines
48 63
49 describe "#==" do64 describe "#==" do
50 it "returns true when the other has the same name, path and type" do65 it "returns true when the other has the same name, path and type" do
51 assert_equal Entry.new, Entry.new66 assert_equal build_entry, build_entry
52 end67 end
53 68
54 it "returns false when the other has a different name" do69 it "returns false when the other has a different name" do
55 entry.name = 'name'70 entry.name = 'other_name'
56 refute_equal Entry.new, entry71 refute_equal build_entry, entry
57 end72 end
58 73
59 it "returns false when the other has a different path" do74 it "returns false when the other has a different path" do
60 entry.path = 'path'75 entry.path = 'other_path'
61 refute_equal Entry.new, entry76 refute_equal build_entry, entry
62 end77 end
63 78
64 it "returns false when the other has a different type" do79 it "returns false when the other has a different type" do
65 entry.type = 'type'80 entry.type = 'other_type'
66 refute_equal Entry.new, entry81 refute_equal build_entry, entry
67 end82 end
68 end83 end
69 84
70 describe "#<=>" do85 describe "#<=>" do
71 it "returns 1 when the other's name is less" do86 it "returns 1 when the other's name is less" do
72 assert_equal 1, Entry.new('b') <=> Entry.new('a')87 assert_equal 1, build_entry('b') <=> build_entry('a')
73 end88 end
74 89
75 it "returns -1 when the other's name is greater" do90 it "returns -1 when the other's name is greater" do
76 assert_equal -1, Entry.new('a') <=> Entry.new('b')91 assert_equal -1, build_entry('a') <=> build_entry('b')
77 end92 end
78 93
79 it "returns 0 when the other's name is equal" do94 it "returns 0 when the other's name is equal" do
80 assert_equal 0, Entry.new('a') <=> Entry.new('a')95 assert_equal 0, build_entry('a') <=> build_entry('a')
81 end96 end
82 97
83 it "is case-insensitive" do98 it "is case-insensitive" do
84 assert_equal 0, Entry.new('a') <=> Entry.new('A')99 assert_equal 0, build_entry('a') <=> build_entry('A')
85 end100 end
86 end101 end
87 102