entry_test.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsEntryTest < MiniTest::Spec
  4. Entry = Docs::Entry
  5. let :entry do
  6. Entry.new
  7. end
  8. describe ".new" do
  9. it "stores a name" do
  10. assert_equal 'name', Entry.new('name').name
  11. end
  12. it "stores a path" do
  13. assert_equal 'path', Entry.new(nil, 'path').path
  14. end
  15. it "stores a type" do
  16. assert_equal 'type', Entry.new(nil, nil, 'type').type
  17. end
  18. end
  19. describe "#name=" do
  20. it "removes surrounding whitespace" do
  21. entry.name = " \n\rname "
  22. assert_equal 'name', entry.name
  23. end
  24. it "accepts nil" do
  25. entry.name = nil
  26. assert_nil entry.name
  27. end
  28. end
  29. describe "#type=" do
  30. it "removes surrounding whitespace" do
  31. entry.type = " \n\rtype "
  32. assert_equal 'type', entry.type
  33. end
  34. it "accepts nil" do
  35. entry.type = nil
  36. assert_nil entry.type
  37. end
  38. end
  39. describe "#==" do
  40. it "returns true when the other has the same name, path and type" do
  41. assert_equal Entry.new, Entry.new
  42. end
  43. it "returns false when the other has a different name" do
  44. entry.name = 'name'
  45. refute_equal Entry.new, entry
  46. end
  47. it "returns false when the other has a different path" do
  48. entry.path = 'path'
  49. refute_equal Entry.new, entry
  50. end
  51. it "returns false when the other has a different type" do
  52. entry.type = 'type'
  53. refute_equal Entry.new, entry
  54. end
  55. end
  56. describe "#<=>" do
  57. it "returns 1 when the other's name is less" do
  58. assert_equal 1, Entry.new('b') <=> Entry.new('a')
  59. end
  60. it "returns -1 when the other's name is greater" do
  61. assert_equal -1, Entry.new('a') <=> Entry.new('b')
  62. end
  63. it "returns 0 when the other's name is equal" do
  64. assert_equal 0, Entry.new('a') <=> Entry.new('a')
  65. end
  66. it "is case-insensitive" do
  67. assert_equal 0, Entry.new('a') <=> Entry.new('A')
  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