entry_index_test.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsEntryIndexTest < MiniTest::Spec
  4. let :entry do
  5. Docs::Entry.new 'name', 'path', 'type'
  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 = [
  27. Docs::Entry.new('one', 'path', 'type'),
  28. Docs::Entry.new('two', 'path', 'type')
  29. ]
  30. index.add(entries)
  31. assert_equal entries, index.entries
  32. end
  33. it "duplicates the entry" do
  34. index.add(entry)
  35. refute_same entry, index.entries.first
  36. end
  37. it "doesn't store the root entry" do
  38. mock(entry).root? { true }
  39. index.add(entry)
  40. assert_empty index.entries
  41. assert_empty index.types
  42. end
  43. it "doesn't store the same entry twice" do
  44. 2.times { index.add(entry.dup) }
  45. assert_equal [entry], index.entries
  46. end
  47. it "creates and indexes the type" do
  48. index.add Docs::Entry.new('one', 'path', 'a')
  49. index.add Docs::Entry.new('two', 'path', 'b')
  50. index.add Docs::Entry.new('three', 'path', 'b')
  51. assert_equal ['a', 'b'], index.types.keys
  52. assert_instance_of Docs::Type, index.types['a']
  53. end
  54. it "doesn't index the nil type" do
  55. entry.type = nil; index.add(entry)
  56. assert_empty index.types
  57. end
  58. it "increments the type's count" do
  59. index.add Docs::Entry.new('one', 'path', 'type')
  60. index.add Docs::Entry.new('two', 'path', 'type')
  61. assert_equal 2, index.types['type'].count
  62. end
  63. end
  64. describe "#empty? / #blank? / #present?" do
  65. it "is #empty? and #blank? when no entries have been added" do
  66. assert index.empty?
  67. assert index.blank?
  68. refute index.present?
  69. end
  70. it "is #present? when an entry has been added" do
  71. index.add(entry)
  72. refute index.empty?
  73. refute index.blank?
  74. assert index.present?
  75. end
  76. end
  77. describe "#as_json" do
  78. it "returns a Hash" do
  79. assert_instance_of Hash, index.as_json
  80. end
  81. describe ":entries" do
  82. it "is an empty array by default" do
  83. assert_instance_of Array, index.as_json[:entries]
  84. end
  85. it "includes the json representation of the #entries" do
  86. index.add one = Docs::Entry.new('one', 'path', 'type')
  87. index.add two = Docs::Entry.new('two', 'path', 'type')
  88. assert_equal [one.as_json, two.as_json], index.as_json[:entries]
  89. end
  90. it "is sorted by name, case-insensitive" do
  91. entry.name = 'B'; index.add(entry)
  92. entry.name = 'a'; index.add(entry)
  93. entry.name = 'c'; index.add(entry)
  94. assert_equal ['a', 'B', 'c'], index.as_json[:entries].map { |e| e[:name] }
  95. end
  96. it "sorts numbered names" do
  97. entry.name = '4.2.2. Test'; index.add(entry)
  98. entry.name = '4.20. Test'; index.add(entry)
  99. entry.name = '4.3. Test'; index.add(entry)
  100. entry.name = '4. Test'; index.add(entry)
  101. entry.name = '2 Test'; index.add(entry)
  102. entry.name = 'Test'; index.add(entry)
  103. assert_equal ['4. Test', '4.2.2. Test', '4.3. Test', '4.20. Test', '2 Test', 'Test'], index.as_json[:entries].map { |e| e[:name] }
  104. end
  105. end
  106. describe ":types" do
  107. it "is an empty array by default" do
  108. assert_instance_of Array, index.as_json[:types]
  109. end
  110. it "includes the json representation of the #types" do
  111. type = Docs::Type.new 'one', 1
  112. entry.type = 'one'; index.add(entry)
  113. assert_equal type.as_json, index.as_json[:types].first
  114. end
  115. it "is sorted by name, case-insensitive" do
  116. entry.type = 'B'; index.add(entry)
  117. entry.type = 'a'; index.add(entry)
  118. entry.type = 'c'; index.add(entry)
  119. assert_equal ['a', 'B', 'c'], index.as_json[:types].map { |e| e[:name] }
  120. end
  121. it "sorts numbered names" do
  122. entry.type = '1.8.2. Test'; index.add(entry)
  123. entry.type = '1.90. Test'; index.add(entry)
  124. entry.type = '1.9. Test'; index.add(entry)
  125. entry.type = '9. Test'; index.add(entry)
  126. entry.type = '1 Test'; index.add(entry)
  127. entry.type = 'Test'; index.add(entry)
  128. assert_equal ['1.8.2. Test', '1.9. Test', '1.90. Test', '9. Test', '1 Test', 'Test'], index.as_json[:types].map { |e| e[:name] }
  129. end
  130. end
  131. end
  132. describe "#to_json" do
  133. it "returns the JSON string for #as_json" do
  134. stub(index).as_json { { entries: [1], types: [2] } }
  135. assert_equal '{"entries":[1],"types":[2]}', index.to_json
  136. end
  137. end
  138. end