/test/test_document.rb

https://gitlab.com/gregtyka/jekyll · Ruby · 441 lines · 377 code · 64 blank · 0 comment · 11 complexity · 8670d5bdbc1ca2a1d0f054a8a9d5eb91 MD5 · raw file

  1. require 'helper'
  2. class TestDocument < JekyllUnitTest
  3. def assert_equal_value(key, one, other)
  4. assert_equal(one[key], other[key])
  5. end
  6. context "a document in a collection" do
  7. setup do
  8. @site = fixture_site({
  9. "collections" => ["methods"]
  10. })
  11. @site.process
  12. @document = @site.collections["methods"].docs.detect {|d| d.relative_path == "_methods/configuration.md" }
  13. end
  14. should "exist" do
  15. assert !@document.nil?
  16. end
  17. should "know its relative path" do
  18. assert_equal "_methods/configuration.md", @document.relative_path
  19. end
  20. should "knows its extname" do
  21. assert_equal ".md", @document.extname
  22. end
  23. should "know its basename" do
  24. assert_equal "configuration.md", @document.basename
  25. end
  26. should "know its basename without extname" do
  27. assert_equal "configuration", @document.basename_without_ext
  28. end
  29. should "know whether its a yaml file" do
  30. assert_equal false, @document.yaml_file?
  31. end
  32. should "know its data" do
  33. assert_equal "Jekyll.configuration", @document.data["title"]
  34. assert_equal "foo.bar", @document.data["whatever"]
  35. end
  36. context "with YAML ending in three dots" do
  37. setup do
  38. @site = fixture_site({"collections" => ["methods"]})
  39. @site.process
  40. @document = @site.collections["methods"].docs.detect {|d| d.relative_path == "_methods/yaml_with_dots.md" }
  41. end
  42. should "know its data" do
  43. assert_equal "YAML with Dots", @document.data["title"]
  44. assert_equal "foo.bar", @document.data["whatever"]
  45. end
  46. end
  47. should "output the collection name in the #to_liquid method" do
  48. assert_equal @document.to_liquid['collection'], "methods"
  49. end
  50. should "output its relative path as path in Liquid" do
  51. assert_equal @document.to_liquid['path'], "_methods/configuration.md"
  52. end
  53. end
  54. context "a document as part of a collection with frontmatter defaults" do
  55. setup do
  56. @site = fixture_site({
  57. "collections" => ["slides"],
  58. "defaults" => [{
  59. "scope"=> {"path"=>"", "type"=>"slides"},
  60. "values"=> {
  61. "nested"=> {
  62. "key"=>"myval",
  63. }
  64. }
  65. }]
  66. })
  67. @site.process
  68. @document = @site.collections["slides"].docs.select{|d| d.is_a?(Document) }.first
  69. end
  70. should "know the frontmatter defaults" do
  71. assert_equal "Example slide", @document.data["title"]
  72. assert_equal "slide", @document.data["layout"]
  73. assert_equal({"key"=>"myval"}, @document.data["nested"])
  74. end
  75. end
  76. context "a document as part of a collection with overriden default values" do
  77. setup do
  78. @site = fixture_site({
  79. "collections" => ["slides"],
  80. "defaults" => [{
  81. "scope"=> {"path"=>"", "type"=>"slides"},
  82. "values"=> {
  83. "nested"=> {
  84. "test1"=>"default1",
  85. "test2"=>"default1"
  86. }
  87. }
  88. }]
  89. })
  90. @site.process
  91. @document = @site.collections["slides"].docs[1]
  92. end
  93. should "override default values in the document frontmatter" do
  94. assert_equal "Override title", @document.data["title"]
  95. assert_equal "slide", @document.data["layout"]
  96. assert_equal({"test1"=>"override1","test2"=>"override2"}, @document.data["nested"])
  97. end
  98. end
  99. context "a document as part of a collection with valid path" do
  100. setup do
  101. @site = fixture_site({
  102. "collections" => ["slides"],
  103. "defaults" => [{
  104. "scope"=> {"path"=>"_slides", "type"=>"slides"},
  105. "values"=> {
  106. "nested"=> {
  107. "key"=>"value123",
  108. }
  109. }
  110. }]
  111. })
  112. @site.process
  113. @document = @site.collections["slides"].docs.first
  114. end
  115. should "know the frontmatter defaults" do
  116. assert_equal "Example slide", @document.data["title"]
  117. assert_equal "slide", @document.data["layout"]
  118. assert_equal({"key"=>"value123"}, @document.data["nested"])
  119. end
  120. end
  121. context "a document as part of a collection with invalid path" do
  122. setup do
  123. @site = fixture_site({
  124. "collections" => ["slides"],
  125. "defaults" => [{
  126. "scope"=> {"path"=>"somepath", "type"=>"slides"},
  127. "values"=> {
  128. "nested"=> {
  129. "key"=>"myval",
  130. }
  131. }
  132. }]
  133. })
  134. @site.process
  135. @document = @site.collections["slides"].docs.first
  136. end
  137. should "not know the specified frontmatter defaults" do
  138. assert_equal "Example slide", @document.data["title"]
  139. assert_equal "slide", @document.data["layout"]
  140. assert_equal nil, @document.data["nested"]
  141. end
  142. end
  143. context "a document in a collection with a custom permalink" do
  144. setup do
  145. @site = fixture_site({
  146. "collections" => ["slides"]
  147. })
  148. @site.process
  149. @document = @site.collections["slides"].docs[2]
  150. @dest_file = dest_dir("slide/3/index.html")
  151. end
  152. should "know its permalink" do
  153. assert_equal "/slide/3/", @document.permalink
  154. end
  155. should "produce the right URL" do
  156. assert_equal "/slide/3/", @document.url
  157. end
  158. end
  159. context "a document in a collection with custom filename permalinks" do
  160. setup do
  161. @site = fixture_site({
  162. "collections" => {
  163. "slides" => {
  164. "output" => true,
  165. "permalink" => "/slides/test/:name"
  166. }
  167. },
  168. "permalink" => "pretty"
  169. })
  170. @site.process
  171. @document = @site.collections["slides"].docs[0]
  172. @dest_file = dest_dir("slides/test/example-slide-1.html")
  173. end
  174. should "produce the right URL" do
  175. assert_equal "/slides/test/example-slide-1", @document.url
  176. end
  177. should "produce the right destination file" do
  178. assert_equal @dest_file, @document.destination(dest_dir)
  179. end
  180. should "honor the output extension of its permalink" do
  181. assert_equal ".html", @document.output_ext
  182. end
  183. end
  184. context "a document in a collection with pretty permalink style" do
  185. setup do
  186. @site = fixture_site({
  187. "collections" => {
  188. "slides" => {
  189. "output" => true,
  190. }
  191. },
  192. })
  193. @site.permalink_style = :pretty
  194. @site.process
  195. @document = @site.collections["slides"].docs[0]
  196. @dest_file = dest_dir("slides/example-slide-1/index.html")
  197. end
  198. should "produce the right URL" do
  199. assert_equal "/slides/example-slide-1/", @document.url
  200. end
  201. should "produce the right destination file" do
  202. assert_equal @dest_file, @document.destination(dest_dir)
  203. end
  204. end
  205. context "a document in a collection with cased file name" do
  206. setup do
  207. @site = fixture_site({
  208. "collections" => {
  209. "slides" => {
  210. "output" => true,
  211. }
  212. },
  213. })
  214. @site.permalink_style = :pretty
  215. @site.process
  216. @document = @site.collections["slides"].docs[7]
  217. @dest_file = dest_dir("slides/example-slide-Upper-Cased/index.html")
  218. end
  219. should "produce the right cased URL" do
  220. assert_equal "/slides/example-slide-Upper-Cased/", @document.url
  221. end
  222. end
  223. context "a document in a collection with cased file name" do
  224. setup do
  225. @site = fixture_site({
  226. "collections" => {
  227. "slides" => {
  228. "output" => true
  229. }
  230. }
  231. })
  232. @site.process
  233. @document = @site.collections["slides"].docs[6]
  234. @dest_file = dest_dir("slides/example-slide-7.php")
  235. end
  236. should "be written out properly" do
  237. assert_exist @dest_file
  238. end
  239. should "produce the permalink as the url" do
  240. assert_equal "/slides/example-slide-7.php", @document.url
  241. end
  242. should "be written to the proper directory" do
  243. assert_equal @dest_file, @document.destination(dest_dir)
  244. end
  245. should "honor the output extension of its permalink" do
  246. assert_equal ".php", @document.output_ext
  247. end
  248. end
  249. context "documents in a collection with custom title permalinks" do
  250. setup do
  251. @site = fixture_site({
  252. "collections" => {
  253. "slides" => {
  254. "output" => true,
  255. "permalink" => "/slides/:title"
  256. }
  257. },
  258. })
  259. @site.process
  260. @document = @site.collections["slides"].docs[3]
  261. @document_without_slug = @site.collections["slides"].docs[4]
  262. @document_with_strange_slug = @site.collections["slides"].docs[5]
  263. end
  264. should "produce the right URL if they have a slug" do
  265. assert_equal "/slides/so-what-is-jekyll-exactly", @document.url
  266. end
  267. should "produce the right destination file if they have a slug" do
  268. dest_file = dest_dir("slides/so-what-is-jekyll-exactly.html")
  269. assert_equal dest_file, @document.destination(dest_dir)
  270. end
  271. should "produce the right URL if they don't have a slug" do
  272. assert_equal "/slides/example-slide-5", @document_without_slug.url
  273. end
  274. should "produce the right destination file if they don't have a slug" do
  275. dest_file = dest_dir("slides/example-slide-5.html")
  276. assert_equal dest_file, @document_without_slug.destination(dest_dir)
  277. end
  278. should "produce the right URL if they have a wild slug" do
  279. assert_equal "/slides/Well,-so-what-is-Jekyll,-then", @document_with_strange_slug.url
  280. end
  281. should "produce the right destination file if they have a wild slug" do
  282. dest_file = dest_dir("/slides/Well,-so-what-is-Jekyll,-then.html")
  283. assert_equal dest_file, @document_with_strange_slug.destination(dest_dir)
  284. end
  285. end
  286. context "document with a permalink with dots & a trailing slash" do
  287. setup do
  288. @site = fixture_site({"collections" => {
  289. "with.dots" => { "output" => true }
  290. }})
  291. @site.process
  292. @document = @site.collections["with.dots"].docs.last
  293. @dest_file = dest_dir("with.dots", "permalink.with.slash.tho", "index.html")
  294. end
  295. should "yield an HTML document" do
  296. assert_equal @dest_file, @document.destination(dest_dir)
  297. end
  298. should "be written properly" do
  299. assert_exist @dest_file
  300. end
  301. should "get the right output_ext" do
  302. assert_equal ".html", @document.output_ext
  303. end
  304. end
  305. context "documents in a collection" do
  306. setup do
  307. @site = fixture_site({
  308. "collections" => {
  309. "slides" => {
  310. "output" => true
  311. }
  312. },
  313. })
  314. @site.process
  315. @files = @site.collections["slides"].docs
  316. end
  317. context "without output overrides" do
  318. should "be output according to collection defaults" do
  319. refute_nil @files.find { |doc| doc.relative_path == "_slides/example-slide-4.html" }
  320. end
  321. end
  322. context "with output overrides" do
  323. should "be output according its front matter" do
  324. assert_nil @files.find { |doc| doc.relative_path == "_slides/non-outputted-slide.html" }
  325. end
  326. end
  327. end
  328. context "a static file in a collection" do
  329. setup do
  330. @site = fixture_site({
  331. "collections" => {
  332. "slides" => {
  333. "output" => true
  334. }
  335. }
  336. })
  337. @site.process
  338. @document = @site.collections["slides"].files.find { |doc| doc.relative_path == "_slides/octojekyll.png" }
  339. @dest_file = dest_dir("slides/octojekyll.png")
  340. end
  341. should "be a static file" do
  342. assert_equal true, @document.is_a?(StaticFile)
  343. end
  344. should "be set to write" do
  345. assert @document.write?
  346. end
  347. should "be in the list of docs_to_write" do
  348. assert @site.docs_to_write.include?(@document)
  349. end
  350. should "be output in the correct place" do
  351. assert File.file?(@dest_file)
  352. end
  353. end
  354. context "a document in a collection with non-alphabetic file name" do
  355. setup do
  356. @site = fixture_site({
  357. "collections" => {
  358. "methods" => {
  359. "output" => true
  360. }
  361. },
  362. })
  363. @site.process
  364. @document = @site.collections["methods"].docs.find { |doc| doc.relative_path == "_methods/escape-+ #%20[].md" }
  365. @dest_file = dest_dir("methods/escape-+ #%20[].html")
  366. end
  367. should "produce the right URL" do
  368. assert_equal "/methods/escape-+%20%23%2520%5B%5D.html", @document.url
  369. end
  370. should "produce the right destination" do
  371. assert_equal @dest_file, @document.destination(dest_dir)
  372. end
  373. should "be output in the correct place" do
  374. assert_equal true, File.file?(@dest_file)
  375. end
  376. end
  377. end