1
0

entry_test.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require 'test_helper'
  2. require '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 "#<=>" do
  71. it "returns 1 when the other's name is less" do
  72. assert_equal 1, build_entry('b') <=> build_entry('a')
  73. end
  74. it "returns -1 when the other's name is greater" do
  75. assert_equal -1, build_entry('a') <=> build_entry('b')
  76. end
  77. it "returns 0 when the other's name is equal" do
  78. assert_equal 0, build_entry('a') <=> build_entry('a')
  79. end
  80. it "is case-insensitive" do
  81. assert_equal 0, build_entry('a') <=> build_entry('A')
  82. end
  83. end
  84. describe "#root?" do
  85. it "returns true when #path is 'index'" do
  86. entry.path = 'index'
  87. assert entry.root?
  88. end
  89. it "returns false when #path is 'path'" do
  90. entry.path = 'path'
  91. refute entry.root?
  92. end
  93. end
  94. describe "#as_json" do
  95. it "returns a hash with the name, path and type" do
  96. as_json = Entry.new('name', 'path', 'type').as_json
  97. assert_instance_of Hash, as_json
  98. assert_equal [:name, :path, :type], as_json.keys
  99. assert_equal %w(name path type), as_json.values
  100. end
  101. end
  102. end