PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_post.rb

https://gitlab.com/Rockyspade/jekyll
Ruby | 581 lines | 473 code | 108 blank | 0 comment | 3 complexity | e7979af1f46d21dc85d4a18813470c9a MD5 | raw file
  1. require 'helper'
  2. class TestPost < Test::Unit::TestCase
  3. def setup_post(file)
  4. Post.new(@site, source_dir, '', file)
  5. end
  6. def do_render(post)
  7. layouts = { "default" => Layout.new(@site, source_dir('_layouts'), "simple.html")}
  8. post.render(layouts, {"site" => {"posts" => []}})
  9. end
  10. context "A Post" do
  11. setup do
  12. clear_dest
  13. stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS }
  14. @site = Site.new(Jekyll.configuration)
  15. end
  16. should "ensure valid posts are valid" do
  17. assert Post.valid?("2008-09-09-foo-bar.textile")
  18. assert Post.valid?("foo/bar/2008-09-09-foo-bar.textile")
  19. assert !Post.valid?("lol2008-09-09-foo-bar.textile")
  20. assert !Post.valid?("blah")
  21. end
  22. context "processing posts" do
  23. setup do
  24. @post = Post.allocate
  25. @post.site = @site
  26. @real_file = "2008-10-18-foo-bar.textile"
  27. @fake_file = "2008-09-09-foo-bar.textile"
  28. @source = source_dir('_posts')
  29. end
  30. should "keep date, title, and markup type" do
  31. @post.categories = []
  32. @post.process(@fake_file)
  33. assert_equal Time.parse("2008-09-09"), @post.date
  34. assert_equal "foo-bar", @post.slug
  35. assert_equal ".textile", @post.ext
  36. assert_equal "/2008/09/09", @post.dir
  37. assert_equal "/2008/09/09/foo-bar", @post.id
  38. end
  39. should "create url based on date and title" do
  40. @post.categories = []
  41. @post.process(@fake_file)
  42. assert_equal "/2008/09/09/foo-bar.html", @post.url
  43. end
  44. should "raise a good error on invalid post date" do
  45. assert_raise Jekyll::FatalException do
  46. @post.process("2009-27-03-foo-bar.textile")
  47. end
  48. end
  49. should "CGI escape urls" do
  50. @post.categories = []
  51. @post.process("2009-03-12-hash-#1.markdown")
  52. assert_equal "/2009/03/12/hash-%231.html", @post.url
  53. assert_equal "/2009/03/12/hash-#1", @post.id
  54. end
  55. should "respect permalink in yaml front matter" do
  56. file = "2008-12-03-permalinked-post.textile"
  57. @post.process(file)
  58. @post.read_yaml(@source, file)
  59. assert_equal "my_category/permalinked-post", @post.permalink
  60. assert_equal "/my_category", @post.dir
  61. assert_equal "/my_category/permalinked-post", @post.url
  62. end
  63. context "with CRLF linebreaks" do
  64. setup do
  65. @real_file = "2009-05-24-yaml-linebreak.markdown"
  66. @source = source_dir('win/_posts')
  67. end
  68. should "read yaml front-matter" do
  69. @post.read_yaml(@source, @real_file)
  70. assert_equal({"title" => "Test title", "layout" => "post", "tag" => "Ruby"}, @post.data)
  71. assert_equal "This is the content", @post.content
  72. end
  73. end
  74. context "with embedded triple dash" do
  75. setup do
  76. @real_file = "2010-01-08-triple-dash.markdown"
  77. end
  78. should "consume the embedded dashes" do
  79. @post.read_yaml(@source, @real_file)
  80. assert_equal({"title" => "Foo --- Bar"}, @post.data)
  81. assert_equal "Triple the fun!", @post.content
  82. end
  83. end
  84. context "with site wide permalink" do
  85. setup do
  86. @post.categories = []
  87. end
  88. context "with unspecified (date) style" do
  89. setup do
  90. @post.process(@fake_file)
  91. end
  92. should "process the url correctly" do
  93. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  94. assert_equal "/2008/09/09/foo-bar.html", @post.url
  95. end
  96. end
  97. context "with unspecified (date) style and a category" do
  98. setup do
  99. @post.categories << "beer"
  100. @post.process(@fake_file)
  101. end
  102. should "process the url correctly" do
  103. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  104. assert_equal "/beer/2008/09/09/foo-bar.html", @post.url
  105. end
  106. end
  107. context "with unspecified (date) style and a numeric category" do
  108. setup do
  109. @post.categories << 2013
  110. @post.process(@fake_file)
  111. end
  112. should "process the url correctly" do
  113. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  114. assert_equal "/2013/2008/09/09/foo-bar.html", @post.url
  115. end
  116. end
  117. context "with specified layout of nil" do
  118. setup do
  119. file = '2013-01-12-nil-layout.textile'
  120. @post = setup_post(file)
  121. @post.process(file)
  122. end
  123. should "layout of nil is respected" do
  124. assert_equal "nil", @post.data["layout"]
  125. end
  126. end
  127. context "with unspecified (date) style and categories" do
  128. setup do
  129. @post.categories << "food"
  130. @post.categories << "beer"
  131. @post.process(@fake_file)
  132. end
  133. should "process the url correctly" do
  134. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  135. assert_equal "/food/beer/2008/09/09/foo-bar.html", @post.url
  136. end
  137. end
  138. context "with space (categories)" do
  139. setup do
  140. @post.categories << "French cuisine"
  141. @post.categories << "Belgian beer"
  142. @post.process(@fake_file)
  143. end
  144. should "process the url correctly" do
  145. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  146. assert_equal "/French%20cuisine/Belgian%20beer/2008/09/09/foo-bar.html", @post.url
  147. end
  148. end
  149. context "with none style" do
  150. setup do
  151. @post.site.permalink_style = :none
  152. @post.process(@fake_file)
  153. end
  154. should "process the url correctly" do
  155. assert_equal "/:categories/:title.html", @post.template
  156. assert_equal "/foo-bar.html", @post.url
  157. end
  158. end
  159. context "with pretty style" do
  160. setup do
  161. @post.site.permalink_style = :pretty
  162. @post.process(@fake_file)
  163. end
  164. should "process the url correctly" do
  165. assert_equal "/:categories/:year/:month/:day/:title/", @post.template
  166. assert_equal "/2008/09/09/foo-bar/", @post.url
  167. end
  168. end
  169. context "with ordinal style" do
  170. setup do
  171. @post.site.permalink_style = :ordinal
  172. @post.process(@fake_file)
  173. end
  174. should "process the url correctly" do
  175. assert_equal "/:categories/:year/:y_day/:title.html", @post.template
  176. assert_equal "/2008/253/foo-bar.html", @post.url
  177. end
  178. end
  179. context "with custom date permalink" do
  180. setup do
  181. @post.site.permalink_style = '/:categories/:year/:i_month/:i_day/:title/'
  182. @post.process(@fake_file)
  183. end
  184. should "process the url correctly" do
  185. assert_equal "/2008/9/9/foo-bar/", @post.url
  186. end
  187. end
  188. context "with custom abbreviated month date permalink" do
  189. setup do
  190. @post.site.permalink_style = '/:categories/:year/:short_month/:day/:title/'
  191. @post.process(@fake_file)
  192. end
  193. should "process the url correctly" do
  194. assert_equal "/2008/Sep/09/foo-bar/", @post.url
  195. end
  196. end
  197. context "with prefix style and no extension" do
  198. setup do
  199. @post.site.permalink_style = "/prefix/:title"
  200. @post.process(@fake_file)
  201. end
  202. should "process the url correctly" do
  203. assert_equal "/prefix/:title", @post.template
  204. assert_equal "/prefix/foo-bar", @post.url
  205. end
  206. end
  207. end
  208. should "read yaml front-matter" do
  209. @post.read_yaml(@source, @real_file)
  210. assert_equal({"title" => "Foo Bar", "layout" => "default"}, @post.data)
  211. assert_equal "h1. {{ page.title }}\n\nBest *post* ever", @post.content
  212. end
  213. should "transform textile" do
  214. @post.process(@real_file)
  215. @post.read_yaml(@source, @real_file)
  216. @post.transform
  217. assert_equal "<h1>{{ page.title }}</h1>\n<p>Best <strong>post</strong> ever</p>", @post.content
  218. end
  219. context "#excerpt" do
  220. setup do
  221. file = "2013-01-02-post-excerpt.markdown"
  222. @post = setup_post(file)
  223. @post.process(file)
  224. @post.read_yaml(@source, file)
  225. do_render(@post)
  226. end
  227. should "return first paragraph by default" do
  228. assert @post.excerpt.include?("First paragraph"), "contains first paragraph"
  229. assert !@post.excerpt.include?("Second paragraph"), "does not contains second paragraph"
  230. assert !@post.excerpt.include?("Third paragraph"), "does not contains third paragraph"
  231. end
  232. should "correctly resolve link references" do
  233. assert @post.excerpt.include?("www.jekyllrb.com"), "contains referenced link URL"
  234. end
  235. should "return rendered HTML" do
  236. assert_equal "<p>First paragraph with <a href='http://www.jekyllrb.com/'>link ref</a>.</p>",
  237. @post.excerpt
  238. end
  239. context "with excerpt_separator setting" do
  240. setup do
  241. file = "2013-01-02-post-excerpt.markdown"
  242. @post.site.config['excerpt_separator'] = "\n---\n"
  243. @post.process(file)
  244. @post.read_yaml(@source, file)
  245. @post.transform
  246. end
  247. should "respect given separator" do
  248. assert @post.excerpt.include?("First paragraph"), "contains first paragraph"
  249. assert @post.excerpt.include?("Second paragraph"), "contains second paragraph"
  250. assert !@post.excerpt.include?("Third paragraph"), "does not contains third paragraph"
  251. end
  252. should "replace separator with new-lines" do
  253. assert !@post.excerpt.include?("---"), "does not contains separator"
  254. end
  255. end
  256. context "with custom excerpt" do
  257. setup do
  258. file = "2013-04-11-custom-excerpt.markdown"
  259. @post = setup_post(file)
  260. do_render(@post)
  261. end
  262. should "use custom excerpt" do
  263. assert_equal("I can set a custom excerpt", @post.excerpt)
  264. end
  265. should "expose custom excerpt to liquid" do
  266. assert @post.content.include?("I can use the excerpt: <quote>I can set a custom excerpt</quote>"), "Exposes incorrect excerpt to liquid."
  267. end
  268. end
  269. end
  270. end
  271. context "when in a site" do
  272. setup do
  273. clear_dest
  274. stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS }
  275. @site = Site.new(Jekyll.configuration)
  276. @site.posts = [setup_post('2008-02-02-published.textile'),
  277. setup_post('2009-01-27-categories.textile')]
  278. end
  279. should "have next post" do
  280. assert_equal(@site.posts.last, @site.posts.first.next)
  281. end
  282. should "have previous post" do
  283. assert_equal(@site.posts.first, @site.posts.last.previous)
  284. end
  285. should "not have previous post if first" do
  286. assert_equal(nil, @site.posts.first.previous)
  287. end
  288. should "not have next post if last" do
  289. assert_equal(nil, @site.posts.last.next)
  290. end
  291. end
  292. context "initializing posts" do
  293. should "publish when published yaml is no specified" do
  294. post = setup_post("2008-02-02-published.textile")
  295. assert_equal true, post.published
  296. end
  297. should "not published when published yaml is false" do
  298. post = setup_post("2008-02-02-not-published.textile")
  299. assert_equal false, post.published
  300. end
  301. should "recognize date in yaml" do
  302. post = setup_post("2010-01-09-date-override.textile")
  303. do_render(post)
  304. assert_equal Time, post.date.class
  305. assert_equal Time, post.to_liquid["date"].class
  306. assert_equal "/2010/01/10/date-override.html", post.url
  307. assert_equal "<p>Post with a front matter date</p>\n<p>10 Jan 2010</p>", post.output
  308. end
  309. should "recognize time in yaml" do
  310. post = setup_post("2010-01-09-time-override.textile")
  311. do_render(post)
  312. assert_equal Time, post.date.class
  313. assert_equal Time, post.to_liquid["date"].class
  314. assert_equal "/2010/01/10/time-override.html", post.url
  315. assert_equal "<p>Post with a front matter time</p>\n<p>10 Jan 2010</p>", post.output
  316. end
  317. should "recognize time with timezone in yaml" do
  318. post = setup_post("2010-01-09-timezone-override.textile")
  319. do_render(post)
  320. assert_equal Time, post.date.class
  321. assert_equal Time, post.to_liquid["date"].class
  322. assert_equal "/2010/01/10/timezone-override.html", post.url
  323. assert_equal "<p>Post with a front matter time with timezone</p>\n<p>10 Jan 2010</p>", post.output
  324. end
  325. should "to_liquid prioritizes post attributes over data" do
  326. post = setup_post("2010-01-16-override-data.textile")
  327. assert_equal Array, post.tags.class
  328. assert_equal Array, post.to_liquid["tags"].class
  329. assert_equal Time, post.date.class
  330. assert_equal Time, post.to_liquid["date"].class
  331. end
  332. should "recognize category in yaml" do
  333. post = setup_post("2009-01-27-category.textile")
  334. assert post.categories.include?('foo')
  335. end
  336. should "recognize several categories in yaml" do
  337. post = setup_post("2009-01-27-categories.textile")
  338. assert post.categories.include?('foo')
  339. assert post.categories.include?('bar')
  340. assert post.categories.include?('baz')
  341. end
  342. should "recognize empty category in yaml" do
  343. post = setup_post("2009-01-27-empty-category.textile")
  344. assert_equal [], post.categories
  345. end
  346. should "recognize empty categories in yaml" do
  347. post = setup_post("2009-01-27-empty-categories.textile")
  348. assert_equal [], post.categories
  349. end
  350. should "recognize number category in yaml" do
  351. post = setup_post("2013-05-10-number-category.textile")
  352. assert post.categories.include?('2013')
  353. assert !post.categories.include?(2013)
  354. end
  355. should "recognize tag in yaml" do
  356. post = setup_post("2009-05-18-tag.textile")
  357. assert post.tags.include?('code')
  358. end
  359. should "recognize tags in yaml" do
  360. post = setup_post("2009-05-18-tags.textile")
  361. assert post.tags.include?('food')
  362. assert post.tags.include?('cooking')
  363. assert post.tags.include?('pizza')
  364. end
  365. should "recognize empty tag in yaml" do
  366. post = setup_post("2009-05-18-empty-tag.textile")
  367. assert_equal [], post.tags
  368. end
  369. should "recognize empty tags in yaml" do
  370. post = setup_post("2009-05-18-empty-tags.textile")
  371. assert_equal [], post.tags
  372. end
  373. should "allow no yaml" do
  374. post = setup_post("2009-06-22-no-yaml.textile")
  375. assert_equal "No YAML.", post.content
  376. end
  377. should "allow empty yaml" do
  378. post = setup_post("2009-06-22-empty-yaml.textile")
  379. assert_equal "Empty YAML.", post.content
  380. end
  381. context "rendering" do
  382. setup do
  383. clear_dest
  384. end
  385. should "render properly" do
  386. post = setup_post("2008-10-18-foo-bar.textile")
  387. do_render(post)
  388. assert_equal "<<< <h1>Foo Bar</h1>\n<p>Best <strong>post</strong> ever</p> >>>", post.output
  389. end
  390. should "write properly" do
  391. post = setup_post("2008-10-18-foo-bar.textile")
  392. do_render(post)
  393. post.write(dest_dir)
  394. assert File.directory?(dest_dir)
  395. assert File.exists?(File.join(dest_dir, '2008', '10', '18', 'foo-bar.html'))
  396. end
  397. should "write properly without html extension" do
  398. post = setup_post("2008-10-18-foo-bar.textile")
  399. post.site.permalink_style = ":title"
  400. do_render(post)
  401. post.write(dest_dir)
  402. assert File.directory?(dest_dir)
  403. assert File.exists?(File.join(dest_dir, 'foo-bar', 'index.html'))
  404. end
  405. should "insert data" do
  406. post = setup_post("2008-11-21-complex.textile")
  407. do_render(post)
  408. assert_equal "<<< <p>url: /2008/11/21/complex.html<br />\ndate: #{Time.parse("2008-11-21")}<br />\nid: /2008/11/21/complex</p> >>>", post.output
  409. end
  410. should "include templates" do
  411. post = setup_post("2008-12-13-include.markdown")
  412. post.site.source = File.join(File.dirname(__FILE__), 'source')
  413. do_render(post)
  414. assert_equal "<<< <hr />\n<p>Tom Preston-Werner github.com/mojombo</p>\n\n<p>This <em>is</em> cool</p> >>>", post.output
  415. end
  416. should "render date specified in front matter properly" do
  417. post = setup_post("2010-01-09-date-override.textile")
  418. do_render(post)
  419. assert_equal "<p>Post with a front matter date</p>\n<p>10 Jan 2010</p>", post.output
  420. end
  421. should "render time specified in front matter properly" do
  422. post = setup_post("2010-01-09-time-override.textile")
  423. do_render(post)
  424. assert_equal "<p>Post with a front matter time</p>\n<p>10 Jan 2010</p>", post.output
  425. end
  426. end
  427. end
  428. should "generate categories and topics" do
  429. post = Post.new(@site, File.join(File.dirname(__FILE__), *%w[source]), 'foo', 'bar/2008-12-12-topical-post.textile')
  430. assert_equal ['foo'], post.categories
  431. end
  432. end
  433. context "converter file extension settings" do
  434. setup do
  435. stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS }
  436. @site = Site.new(Jekyll.configuration)
  437. end
  438. should "process .md as markdown under default configuration" do
  439. post = setup_post '2011-04-12-md-extension.md'
  440. conv = post.converter
  441. assert conv.kind_of? Jekyll::Converters::Markdown
  442. end
  443. should "process .text as identity under default configuration" do
  444. post = setup_post '2011-04-12-text-extension.text'
  445. conv = post.converter
  446. assert conv.kind_of? Jekyll::Converters::Identity
  447. end
  448. should "process .text as markdown under alternate configuration" do
  449. @site.config['markdown_ext'] = 'markdown,mdw,mdwn,md,text'
  450. post = setup_post '2011-04-12-text-extension.text'
  451. conv = post.converter
  452. assert conv.kind_of? Jekyll::Converters::Markdown
  453. end
  454. should "process .md as markdown under alternate configuration" do
  455. @site.config['markdown_ext'] = 'markdown,mkd,mkdn,md,text'
  456. post = setup_post '2011-04-12-text-extension.text'
  457. conv = post.converter
  458. assert conv.kind_of? Jekyll::Converters::Markdown
  459. end
  460. should "process .mkdn under text if it is not in the markdown config" do
  461. @site.config['markdown_ext'] = 'markdown,mkd,md,text'
  462. post = setup_post '2013-08-01-mkdn-extension.mkdn'
  463. conv = post.converter
  464. assert conv.kind_of? Jekyll::Converters::Identity
  465. end
  466. should "process .text as textile under alternate configuration" do
  467. @site.config['textile_ext'] = 'textile,text'
  468. post = setup_post '2011-04-12-text-extension.text'
  469. conv = post.converter
  470. assert conv.kind_of? Jekyll::Converters::Textile
  471. end
  472. end
  473. end