diff --git a/lib/docs/core/models/entry.rb b/lib/docs/core/models/entry.rb index 7d5a404d..9e5c6282 100644 --- a/lib/docs/core/models/entry.rb +++ b/lib/docs/core/models/entry.rb @@ -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) diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index 2316ce8e..8c3937e2 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/test/lib/docs/core/doc_test.rb @@ -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 diff --git a/test/lib/docs/core/models/entry_test.rb b/test/lib/docs/core/models/entry_test.rb index bbf811ff..d5258910 100644 --- a/test/lib/docs/core/models/entry_test.rb +++ b/test/lib/docs/core/models/entry_test.rb @@ -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