Require all entries to have a name, path and type

pull/417/head
Thibaut Courouble 9 years ago
parent 18199101d7
commit 5bb96f804a

@ -1,11 +1,21 @@
# frozen_string_literal: true
module Docs
class Entry
class Invalid < StandardError; end
attr_accessor :name, :type, :path
def initialize(name = nil, path = nil, type = nil)
self.name = name
self.path = path
self.type = type
unless root?
raise Invalid, 'missing name' if !name || name.empty?
raise Invalid, 'missing path' if !path || path.empty?
raise Invalid, 'missing type' if !type || type.empty?
end
end
def ==(other)

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

@ -5,20 +5,35 @@ class DocsEntryTest < MiniTest::Spec
Entry = Docs::Entry
let :entry do
Entry.new
Entry.new('name', 'path', 'type')
end
def build_entry(name = 'name', path = 'path', type = 'type')
Entry.new(name, path, type)
end
describe ".new" do
it "stores a name" do
assert_equal 'name', Entry.new('name').name
it "stores #name, #path and #type" do
entry = Entry.new('name', 'path', 'type')
assert_equal 'name', entry.name
assert_equal 'path', entry.path
assert_equal 'type', entry.type
end
it "stores a path" do
assert_equal 'path', Entry.new(nil, 'path').path
it "raises an error when #name, #path or #type is nil or empty" do
assert_raises(Docs::Entry::Invalid) { Entry.new(nil, 'path', 'type') }
assert_raises(Docs::Entry::Invalid) { Entry.new('', 'path', 'type') }
assert_raises(Docs::Entry::Invalid) { Entry.new('name', nil, 'type') }
assert_raises(Docs::Entry::Invalid) { Entry.new('name', '', 'type') }
assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', nil) }
assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', '') }
end
it "stores a type" do
assert_equal 'type', Entry.new(nil, nil, 'type').type
it "don't raise an error when #path is 'index' and #name or #type is nil or empty" do
Entry.new(nil, 'index', 'type')
Entry.new('', 'index', 'type')
Entry.new('name', 'index', nil)
Entry.new('name', 'index', '')
end
end
@ -48,40 +63,40 @@ class DocsEntryTest < MiniTest::Spec
describe "#==" do
it "returns true when the other has the same name, path and type" do
assert_equal Entry.new, Entry.new
assert_equal build_entry, build_entry
end
it "returns false when the other has a different name" do
entry.name = 'name'
refute_equal Entry.new, entry
entry.name = 'other_name'
refute_equal build_entry, entry
end
it "returns false when the other has a different path" do
entry.path = 'path'
refute_equal Entry.new, entry
entry.path = 'other_path'
refute_equal build_entry, entry
end
it "returns false when the other has a different type" do
entry.type = 'type'
refute_equal Entry.new, entry
entry.type = 'other_type'
refute_equal build_entry, entry
end
end
describe "#<=>" do
it "returns 1 when the other's name is less" do
assert_equal 1, Entry.new('b') <=> Entry.new('a')
assert_equal 1, build_entry('b') <=> build_entry('a')
end
it "returns -1 when the other's name is greater" do
assert_equal -1, Entry.new('a') <=> Entry.new('b')
assert_equal -1, build_entry('a') <=> build_entry('b')
end
it "returns 0 when the other's name is equal" do
assert_equal 0, Entry.new('a') <=> Entry.new('a')
assert_equal 0, build_entry('a') <=> build_entry('a')
end
it "is case-insensitive" do
assert_equal 0, Entry.new('a') <=> Entry.new('A')
assert_equal 0, build_entry('a') <=> build_entry('A')
end
end

Loading…
Cancel
Save