1
0

entry_test.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. require_relative '../../../../test_helper'
  2. require_relative '../../../../../lib/docs'
  3. class DocsEntryTest < MiniTest::Spec
  4. Entry = Docs::Entry
  5. let :entry do
  6. Entry.new('name', 'path', 'type')
  7. end
  8. def build_entry(name = 'name', path = 'path', type = 'type')
  9. Entry.new(name, path, type)
  10. end
  11. describe ".new" do
  12. it "stores #name, #path and #type" do
  13. entry = Entry.new('name', 'path', 'type')
  14. assert_equal 'name', entry.name
  15. assert_equal 'path', entry.path
  16. assert_equal 'type', entry.type
  17. end
  18. it "raises an error when #name, #path or #type is nil or empty" do
  19. assert_raises(Docs::Entry::Invalid) { Entry.new(nil, 'path', 'type') }
  20. assert_raises(Docs::Entry::Invalid) { Entry.new('', 'path', 'type') }
  21. assert_raises(Docs::Entry::Invalid) { Entry.new('name', nil, 'type') }
  22. assert_raises(Docs::Entry::Invalid) { Entry.new('name', '', 'type') }
  23. assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', nil) }
  24. assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', '') }
  25. end
  26. it "don't raise an error when #path is 'index' and #name or #type is nil or empty" do
  27. Entry.new(nil, 'index', 'type')
  28. Entry.new('', 'index', 'type')
  29. Entry.new('name', 'index', nil)
  30. Entry.new('name', 'index', '')
  31. end
  32. end
  33. describe "#name=" do
  34. it "removes surrounding whitespace" do
  35. entry.name = " \n\rname "
  36. assert_equal 'name', entry.name
  37. end
  38. it "accepts nil" do
  39. entry.name = nil
  40. assert_nil entry.name
  41. end
  42. end
  43. describe "#type=" do
  44. it "removes surrounding whitespace" do
  45. entry.type = " \n\rtype "
  46. assert_equal 'type', entry.type
  47. end
  48. it "accepts nil" do
  49. entry.type = nil
  50. assert_nil entry.type
  51. end
  52. end
  53. describe "#==" do
  54. it "returns true when the other has the same name, path and type" do
  55. assert_equal build_entry, build_entry
  56. end
  57. it "returns false when the other has a different name" do
  58. entry.name = 'other_name'
  59. refute_equal build_entry, entry
  60. end
  61. it "returns false when the other has a different path" do
  62. entry.path = 'other_path'
  63. refute_equal build_entry, entry
  64. end
  65. it "returns false when the other has a different type" do
  66. entry.type = 'other_type'
  67. refute_equal build_entry, entry
  68. end
  69. end
  70. describe "#root?" do
  71. it "returns true when #path is 'index'" do
  72. entry.path = 'index'
  73. assert entry.root?
  74. end
  75. it "returns false when #path is 'path'" do
  76. entry.path = 'path'
  77. refute entry.root?
  78. end
  79. end
  80. describe "#as_json" do
  81. it "returns a hash with the name, path and type" do
  82. as_json = Entry.new('name', 'path', 'type').as_json
  83. assert_instance_of Hash, as_json
  84. assert_equal [:name, :path, :type], as_json.keys
  85. assert_equal %w(name path type), as_json.values
  86. end
  87. end
  88. end