entry_index_test.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsEntryIndexTest < MiniTest::Spec
  4. let :entry do
  5. Docs::Entry.new 'name', 'type', 'path'
  6. end
  7. let :index do
  8. Docs::EntryIndex.new
  9. end
  10. describe "#entries" do
  11. it "returns an Array" do
  12. assert_instance_of Array, index.entries
  13. end
  14. end
  15. describe "#types" do
  16. it "returns a hash" do
  17. assert_instance_of Hash, index.types
  18. end
  19. end
  20. describe "#add" do
  21. it "stores an entry" do
  22. index.add(entry)
  23. assert_includes index.entries, entry
  24. end
  25. it "stores an array of entries" do
  26. entries = [entry, entry]
  27. index.add(entries)
  28. assert_equal entries, index.entries
  29. end
  30. it "duplicates the entry" do
  31. index.add(entry)
  32. refute_same entry, index.entries.first
  33. end
  34. it "doesn't store the root entry" do
  35. mock(entry).root? { true }
  36. index.add(entry)
  37. assert_empty index.entries
  38. assert_empty index.types
  39. end
  40. it "creates and indexes the type" do
  41. entry.type = 'one'; index.add(entry)
  42. entry.type = 'two'; 2.times { index.add(entry) }
  43. assert_equal ['one', 'two'], index.types.keys
  44. assert_instance_of Docs::Type, index.types['one']
  45. end
  46. it "doesn't index the nil type" do
  47. entry.type = nil; index.add(entry)
  48. assert_empty index.types
  49. end
  50. it "increments the type's count" do
  51. 2.times { index.add(entry) }
  52. assert_equal 2, index.types[entry.type].count
  53. end
  54. end
  55. describe "#empty?" do
  56. it "returns true when entries have been added" do
  57. assert index.empty?
  58. end
  59. it "returns false when an entry has been added" do
  60. index.add(entry)
  61. refute index.empty?
  62. end
  63. end
  64. describe "#as_json" do
  65. it "returns a Hash" do
  66. assert_instance_of Hash, index.as_json
  67. end
  68. describe ":entries" do
  69. it "is an empty array by default" do
  70. assert_instance_of Array, index.as_json[:entries]
  71. end
  72. it "includes the json representation of the #entries" do
  73. index.add [entry, entry]
  74. assert_equal [entry.as_json, entry.as_json], index.as_json[:entries]
  75. end
  76. it "is sorted by name, case-insensitive" do
  77. entry.name = 'B'; index.add(entry)
  78. entry.name = 'a'; index.add(entry)
  79. entry.name = 'c'; index.add(entry)
  80. entry.name = nil; index.add(entry)
  81. assert_equal [nil, 'a', 'B', 'c'], index.as_json[:entries].map { |e| e[:name] }
  82. end
  83. end
  84. describe ":types" do
  85. it "is an empty array by default" do
  86. assert_instance_of Array, index.as_json[:types]
  87. end
  88. it "includes the json representation of the #types" do
  89. type = Docs::Type.new 'one', 1
  90. entry.type = 'one'; index.add(entry)
  91. assert_equal type.as_json, index.as_json[:types].first
  92. end
  93. it "is sorted by name, case-insensitive" do
  94. entry.type = 'B'; index.add(entry)
  95. entry.type = 'a'; index.add(entry)
  96. entry.type = 'c'; index.add(entry)
  97. assert_equal ['a', 'B', 'c'], index.as_json[:types].map { |e| e[:name] }
  98. end
  99. end
  100. end
  101. describe "#to_json" do
  102. it "returns the JSON string for #as_json" do
  103. stub(index).as_json { { entries: [1], types: [2] } }
  104. assert_equal '{"entries":[1],"types":[2]}', index.to_json
  105. end
  106. end
  107. end