doc_test.rb 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsDocTest < MiniTest::Spec
  4. let :doc do
  5. Class.new Docs::Doc do
  6. self.name = 'name'
  7. self.type = 'type'
  8. end
  9. end
  10. let :page do
  11. { path: 'path', store_path: 'store_path', output: 'output', entries: [entry] }
  12. end
  13. let :entry do
  14. Docs::Entry.new 'name', 'path', 'type'
  15. end
  16. let :store do
  17. Docs::NullStore.new
  18. end
  19. describe ".inherited" do
  20. it "sets .type" do
  21. assert_equal doc.type, Class.new(doc).type
  22. end
  23. end
  24. describe ".name" do
  25. it "returns 'Doc' when the class is Docs::Doc" do
  26. assert_equal 'Doc', Docs::Doc.name
  27. end
  28. end
  29. describe ".name=" do
  30. it "stores .name" do
  31. doc.name = 'test'
  32. assert_equal 'test', doc.name
  33. end
  34. end
  35. describe ".slug" do
  36. it "returns 'doc' when the class is Docs::Doc" do
  37. assert_equal 'doc', Docs::Doc.slug
  38. end
  39. it "returns 'doc~4.2_lts' when the class is Docs::Doc and its #version is '42 LTS'" do
  40. stub(Docs::Doc).version { '4.2 LTS' }
  41. assert_equal 'doc~4.2_lts', Docs::Doc.slug
  42. end
  43. it "returns 'foo~42' when #slug has been set to 'foo' and #version to '42'" do
  44. doc.slug = 'foo'
  45. doc.version = '42'
  46. assert_equal 'foo~42', doc.slug
  47. end
  48. end
  49. describe ".slug=" do
  50. it "stores .slug" do
  51. doc.slug = 'test'
  52. assert_equal 'test', doc.slug
  53. end
  54. end
  55. describe ".version=" do
  56. it "stores .version as a string" do
  57. doc.version = 4815162342
  58. assert_equal '4815162342', doc.version
  59. end
  60. end
  61. describe ".release=" do
  62. it "stores .release" do
  63. doc.release = '1'
  64. assert_equal '1', doc.release
  65. end
  66. end
  67. describe ".links=" do
  68. it "stores .links" do
  69. doc.links = { test: true }
  70. assert_equal({ test: true }, doc.links)
  71. end
  72. end
  73. describe ".abstract" do
  74. it "returns nil" do
  75. assert_nil doc.abstract
  76. end
  77. end
  78. describe ".abstract=" do
  79. it "stores .abstract" do
  80. doc.abstract = true
  81. assert doc.abstract
  82. end
  83. end
  84. describe ".path" do
  85. it "returns .slug" do
  86. doc.slug = 'slug'
  87. assert_equal 'slug', doc.path
  88. end
  89. end
  90. describe ".index_path" do
  91. it "returns .path + ::INDEX_FILENAME" do
  92. stub(doc).path { 'path' }
  93. assert_equal File.join('path', Docs::Doc::INDEX_FILENAME), doc.index_path
  94. end
  95. end
  96. describe ".db_path" do
  97. it "returns .path + ::DB_FILENAME" do
  98. stub(doc).path { 'path' }
  99. assert_equal File.join('path', Docs::Doc::DB_FILENAME), doc.db_path
  100. end
  101. end
  102. describe ".new" do
  103. it "raises an error when .abstract is true" do
  104. doc.abstract = true
  105. assert_raises NotImplementedError do
  106. doc.new
  107. end
  108. end
  109. end
  110. describe ".as_json" do
  111. it "returns a hash" do
  112. assert_instance_of Hash, doc.as_json
  113. end
  114. it "includes the doc's name, slug, type, version, and release" do
  115. assert_equal %i(name slug type), doc.as_json.keys
  116. %w(name slug type version release links).each do |attribute|
  117. eval "stub(doc).#{attribute} { attribute }"
  118. assert_equal attribute, doc.as_json[attribute.to_sym]
  119. end
  120. end
  121. it "includes the doc's version when it's defined and nil" do
  122. refute doc.as_json.key?(:version)
  123. doc.version = nil
  124. assert doc.as_json.key?(:version)
  125. end
  126. end
  127. describe ".store_page" do
  128. it "builds a page" do
  129. any_instance_of(doc) do |instance|
  130. stub(instance).build_page('id') { @called = true; nil }
  131. end
  132. doc.store_page(store, 'id') {}
  133. assert @called
  134. end
  135. context "when the page builds successfully" do
  136. before do
  137. any_instance_of(doc) do |instance|
  138. stub(instance).build_page { page }
  139. end
  140. end
  141. context "and it has :entries" do
  142. it "returns true" do
  143. assert doc.store_page(store, 'id')
  144. end
  145. it "stores a file" do
  146. mock(store).write(page[:store_path], page[:output])
  147. doc.store_page(store, 'id')
  148. end
  149. it "opens the .path directory before storing the file" do
  150. stub(doc).path { 'path' }
  151. stub(store).write { assert false }
  152. mock(store).open('path') do |_, block|
  153. stub(store).write
  154. block.call
  155. end
  156. doc.store_page(store, 'id')
  157. end
  158. end
  159. context "and it doesn't have :entries" do
  160. before do
  161. page[:entries] = []
  162. end
  163. it "returns false" do
  164. refute doc.store_page(store, 'id')
  165. end
  166. it "doesn't store a file" do
  167. dont_allow(store).write
  168. doc.store_page(store, 'id')
  169. end
  170. end
  171. end
  172. context "when the page doesn't build successfully" do
  173. before do
  174. any_instance_of(doc) do |instance|
  175. stub(instance).build_page { nil }
  176. end
  177. end
  178. it "returns false" do
  179. refute doc.store_page(store, 'id')
  180. end
  181. it "doesn't store a file" do
  182. dont_allow(store).write
  183. doc.store_page(store, 'id')
  184. end
  185. end
  186. end
  187. describe ".store_pages" do
  188. it "build the pages" do
  189. any_instance_of(doc) do |instance|
  190. stub(instance).build_pages { @called = true }
  191. end
  192. doc.store_pages(store) {}
  193. assert @called
  194. end
  195. context "when pages are built successfully" do
  196. let :pages do
  197. [
  198. page.deep_dup.tap { |p| page[:entries].first.tap { |e| e.name = 'one' } },
  199. page.deep_dup.tap { |p| page[:entries].first.tap { |e| e.name = 'two' } }
  200. ]
  201. end
  202. before do
  203. any_instance_of(doc) do |instance|
  204. stub(instance).build_pages { |block| pages.each(&block) }
  205. end
  206. end
  207. context "and at least one page has :entries" do
  208. it "returns true" do
  209. assert doc.store_pages(store)
  210. end
  211. it "stores a file for each page that has :entries" do
  212. pages.first.merge!(entries: [], output: '')
  213. mock(store).write(page[:store_path], page[:output])
  214. mock(store).write('index.json', anything)
  215. mock(store).write('db.json', anything)
  216. doc.store_pages(store)
  217. end
  218. it "stores an index that contains all the pages' entries" do
  219. stub(store).write(page[:store_path], page[:output])
  220. stub(store).write('db.json', anything)
  221. mock(store).write('index.json', anything) do |path, json|
  222. json = JSON.parse(json)
  223. assert_equal pages.length, json['entries'].length
  224. assert_includes json['entries'], Docs::Entry.new('one', 'path', 'type').as_json.stringify_keys
  225. end
  226. doc.store_pages(store)
  227. end
  228. it "stores a db that contains all the pages, indexed by path" do
  229. stub(store).write(page[:store_path], page[:output])
  230. stub(store).write('index.json', anything)
  231. mock(store).write('db.json', anything) do |path, json|
  232. json = JSON.parse(json)
  233. assert_equal page[:output], json[page[:path]]
  234. end
  235. doc.store_pages(store)
  236. end
  237. it "replaces the .path directory before storing the files" do
  238. stub(doc).path { 'path' }
  239. stub(store).write { assert false }
  240. mock(store).replace('path') do |_, block|
  241. stub(store).write
  242. block.call
  243. end
  244. doc.store_pages(store)
  245. end
  246. end
  247. context "and no pages have :entries" do
  248. before do
  249. pages.each { |page| page[:entries] = [] }
  250. end
  251. it "returns false" do
  252. refute doc.store_pages(store)
  253. end
  254. it "doesn't store files" do
  255. dont_allow(store).write
  256. doc.store_pages(store)
  257. end
  258. end
  259. end
  260. context "when no pages are built successfully" do
  261. before do
  262. any_instance_of(doc) do |instance|
  263. stub(instance).build_pages
  264. end
  265. end
  266. it "returns false" do
  267. refute doc.store_pages(store)
  268. end
  269. it "doesn't store files" do
  270. dont_allow(store).write
  271. doc.store_pages(store)
  272. end
  273. end
  274. end
  275. describe ".versions" do
  276. it "returns [self] if no versions have been created" do
  277. assert_equal [doc], doc.versions
  278. end
  279. end
  280. describe ".version" do
  281. context "with no args" do
  282. it "returns @version by default" do
  283. doc.version = 'v'
  284. assert_equal 'v', doc.version
  285. end
  286. end
  287. context "with args" do
  288. it "creates a version subclass" do
  289. version = doc.version('4') { self.release = '8'; self.links = ["https://#{self.version}"] }
  290. assert_equal [version], doc.versions
  291. assert_nil doc.version
  292. assert_nil doc.release
  293. refute doc.version?
  294. assert version.version?
  295. assert_equal '4', version.version
  296. assert_equal '8', version.release
  297. assert_equal 'name', version.name
  298. assert_equal 'type', version.type
  299. assert_equal ['https://4'], version.links
  300. end
  301. end
  302. end
  303. end