Skip to content

Commit 5bb96f8

Browse files
committed
Require all entries to have a name, path and type
1 parent 1819910 commit 5bb96f8

3 files changed

Lines changed: 45 additions & 20 deletions

File tree

lib/docs/core/models/entry.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
# frozen_string_literal: true
2+
13
module Docs
24
class Entry
5+
class Invalid < StandardError; end
6+
37
attr_accessor :name, :type, :path
48

59
def initialize(name = nil, path = nil, type = nil)
610
self.name = name
711
self.path = path
812
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
919
end
1020

1121
def ==(other)

test/lib/docs/core/doc_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DocsDocTest < MiniTest::Spec
1414
end
1515

1616
let :entry do
17-
Docs::Entry.new
17+
Docs::Entry.new 'name', 'path', 'type'
1818
end
1919

2020
let :store do
@@ -262,7 +262,7 @@ class DocsDocTest < MiniTest::Spec
262262
mock(store).write('index.json', anything) do |path, json|
263263
json = JSON.parse(json)
264264
assert_equal pages.length, json['entries'].length
265-
assert_includes json['entries'], Docs::Entry.new('one').as_json.stringify_keys
265+
assert_includes json['entries'], Docs::Entry.new('one', 'path', 'type').as_json.stringify_keys
266266
end
267267
doc.store_pages(store)
268268
end

test/lib/docs/core/models/entry_test.rb

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@ class DocsEntryTest < MiniTest::Spec
55
Entry = Docs::Entry
66

77
let :entry do
8-
Entry.new
8+
Entry.new('name', 'path', 'type')
9+
end
10+
11+
def build_entry(name = 'name', path = 'path', type = 'type')
12+
Entry.new(name, path, type)
913
end
1014

1115
describe ".new" do
12-
it "stores a name" do
13-
assert_equal 'name', Entry.new('name').name
16+
it "stores #name, #path and #type" do
17+
entry = Entry.new('name', 'path', 'type')
18+
assert_equal 'name', entry.name
19+
assert_equal 'path', entry.path
20+
assert_equal 'type', entry.type
1421
end
1522

16-
it "stores a path" do
17-
assert_equal 'path', Entry.new(nil, 'path').path
23+
it "raises an error when #name, #path or #type is nil or empty" do
24+
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', '') }
1830
end
1931

20-
it "stores a type" do
21-
assert_equal 'type', Entry.new(nil, nil, 'type').type
32+
it "don't raise an error when #path is 'index' and #name or #type is nil or empty" do
33+
Entry.new(nil, 'index', 'type')
34+
Entry.new('', 'index', 'type')
35+
Entry.new('name', 'index', nil)
36+
Entry.new('name', 'index', '')
2237
end
2338
end
2439

@@ -48,40 +63,40 @@ class DocsEntryTest < MiniTest::Spec
4863

4964
describe "#==" do
5065
it "returns true when the other has the same name, path and type" do
51-
assert_equal Entry.new, Entry.new
66+
assert_equal build_entry, build_entry
5267
end
5368

5469
it "returns false when the other has a different name" do
55-
entry.name = 'name'
56-
refute_equal Entry.new, entry
70+
entry.name = 'other_name'
71+
refute_equal build_entry, entry
5772
end
5873

5974
it "returns false when the other has a different path" do
60-
entry.path = 'path'
61-
refute_equal Entry.new, entry
75+
entry.path = 'other_path'
76+
refute_equal build_entry, entry
6277
end
6378

6479
it "returns false when the other has a different type" do
65-
entry.type = 'type'
66-
refute_equal Entry.new, entry
80+
entry.type = 'other_type'
81+
refute_equal build_entry, entry
6782
end
6883
end
6984

7085
describe "#<=>" do
7186
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')
7388
end
7489

7590
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')
7792
end
7893

7994
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')
8196
end
8297

8398
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')
85100
end
86101
end
87102

0 commit comments

Comments
 (0)