PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_post.rb

http://github.com/mojombo/jekyll
Ruby | 456 lines | 372 code | 84 blank | 0 comment | 2 complexity | 0c2b0d6ab1d6e8f38d5a70e78aeebcdb 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::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 categories" do
  108. setup do
  109. @post.categories << "food"
  110. @post.categories << "beer"
  111. @post.process(@fake_file)
  112. end
  113. should "process the url correctly" do
  114. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  115. assert_equal "/food/beer/2008/09/09/foo-bar.html", @post.url
  116. end
  117. end
  118. context "with space (categories)" do
  119. setup do
  120. @post.categories << "French cuisine"
  121. @post.categories << "Belgian beer"
  122. @post.process(@fake_file)
  123. end
  124. should "process the url correctly" do
  125. assert_equal "/:categories/:year/:month/:day/:title.html", @post.template
  126. assert_equal "/French%20cuisine/Belgian%20beer/2008/09/09/foo-bar.html", @post.url
  127. end
  128. end
  129. context "with none style" do
  130. setup do
  131. @post.site.permalink_style = :none
  132. @post.process(@fake_file)
  133. end
  134. should "process the url correctly" do
  135. assert_equal "/:categories/:title.html", @post.template
  136. assert_equal "/foo-bar.html", @post.url
  137. end
  138. end
  139. context "with pretty style" do
  140. setup do
  141. @post.site.permalink_style = :pretty
  142. @post.process(@fake_file)
  143. end
  144. should "process the url correctly" do
  145. assert_equal "/:categories/:year/:month/:day/:title/", @post.template
  146. assert_equal "/2008/09/09/foo-bar/", @post.url
  147. end
  148. end
  149. context "with custom date permalink" do
  150. setup do
  151. @post.site.permalink_style = '/:categories/:year/:i_month/:i_day/:title/'
  152. @post.process(@fake_file)
  153. end
  154. should "process the url correctly" do
  155. assert_equal "/2008/9/9/foo-bar/", @post.url
  156. end
  157. end
  158. context "with prefix style and no extension" do
  159. setup do
  160. @post.site.permalink_style = "/prefix/:title"
  161. @post.process(@fake_file)
  162. end
  163. should "process the url correctly" do
  164. assert_equal "/prefix/:title", @post.template
  165. assert_equal "/prefix/foo-bar", @post.url
  166. end
  167. end
  168. end
  169. should "read yaml front-matter" do
  170. @post.read_yaml(@source, @real_file)
  171. assert_equal({"title" => "Foo Bar", "layout" => "default"}, @post.data)
  172. assert_equal "h1. {{ page.title }}\n\nBest *post* ever", @post.content
  173. end
  174. should "transform textile" do
  175. @post.process(@real_file)
  176. @post.read_yaml(@source, @real_file)
  177. @post.transform
  178. assert_equal "<h1>{{ page.title }}</h1>\n<p>Best <strong>post</strong> ever</p>", @post.content
  179. end
  180. end
  181. context "when in a site" do
  182. setup do
  183. clear_dest
  184. stub(Jekyll).configuration { Jekyll::DEFAULTS }
  185. @site = Site.new(Jekyll.configuration)
  186. @site.posts = [setup_post('2008-02-02-published.textile'),
  187. setup_post('2009-01-27-categories.textile')]
  188. end
  189. should "have next post" do
  190. assert_equal(@site.posts.last, @site.posts.first.next)
  191. end
  192. should "have previous post" do
  193. assert_equal(@site.posts.first, @site.posts.last.previous)
  194. end
  195. should "not have previous post if first" do
  196. assert_equal(nil, @site.posts.first.previous)
  197. end
  198. should "not have next post if last" do
  199. assert_equal(nil, @site.posts.last.next)
  200. end
  201. end
  202. context "initializing posts" do
  203. should "publish when published yaml is no specified" do
  204. post = setup_post("2008-02-02-published.textile")
  205. assert_equal true, post.published
  206. end
  207. should "not published when published yaml is false" do
  208. post = setup_post("2008-02-02-not-published.textile")
  209. assert_equal false, post.published
  210. end
  211. should "recognize date in yaml" do
  212. post = setup_post("2010-01-09-date-override.textile")
  213. do_render(post)
  214. assert_equal Time, post.date.class
  215. assert_equal Time, post.to_liquid["date"].class
  216. assert_equal "/2010/01/10/date-override.html", post.url
  217. assert_equal "<p>Post with a front matter date</p>\n<p>10 Jan 2010</p>", post.output
  218. end
  219. should "recognize time in yaml" do
  220. post = setup_post("2010-01-09-time-override.textile")
  221. do_render(post)
  222. assert_equal Time, post.date.class
  223. assert_equal Time, post.to_liquid["date"].class
  224. assert_equal "/2010/01/10/time-override.html", post.url
  225. assert_equal "<p>Post with a front matter time</p>\n<p>10 Jan 2010</p>", post.output
  226. end
  227. should "recognize time with timezone in yaml" do
  228. post = setup_post("2010-01-09-timezone-override.textile")
  229. do_render(post)
  230. assert_equal Time, post.date.class
  231. assert_equal Time, post.to_liquid["date"].class
  232. assert_equal "/2010/01/10/timezone-override.html", post.url
  233. assert_equal "<p>Post with a front matter time with timezone</p>\n<p>10 Jan 2010</p>", post.output
  234. end
  235. should "to_liquid prioritizes post attributes over data" do
  236. post = setup_post("2010-01-16-override-data.textile")
  237. assert_equal Array, post.tags.class
  238. assert_equal Array, post.to_liquid["tags"].class
  239. assert_equal Time, post.date.class
  240. assert_equal Time, post.to_liquid["date"].class
  241. end
  242. should "recognize category in yaml" do
  243. post = setup_post("2009-01-27-category.textile")
  244. assert post.categories.include?('foo')
  245. end
  246. should "recognize several categories in yaml" do
  247. post = setup_post("2009-01-27-categories.textile")
  248. assert post.categories.include?('foo')
  249. assert post.categories.include?('bar')
  250. assert post.categories.include?('baz')
  251. end
  252. should "recognize empty category in yaml" do
  253. post = setup_post("2009-01-27-empty-category.textile")
  254. assert_equal [], post.categories
  255. end
  256. should "recognize empty categories in yaml" do
  257. post = setup_post("2009-01-27-empty-categories.textile")
  258. assert_equal [], post.categories
  259. end
  260. should "recognize tag in yaml" do
  261. post = setup_post("2009-05-18-tag.textile")
  262. assert post.tags.include?('code')
  263. end
  264. should "recognize tags in yaml" do
  265. post = setup_post("2009-05-18-tags.textile")
  266. assert post.tags.include?('food')
  267. assert post.tags.include?('cooking')
  268. assert post.tags.include?('pizza')
  269. end
  270. should "recognize empty tag in yaml" do
  271. post = setup_post("2009-05-18-empty-tag.textile")
  272. assert_equal [], post.tags
  273. end
  274. should "recognize empty tags in yaml" do
  275. post = setup_post("2009-05-18-empty-tags.textile")
  276. assert_equal [], post.tags
  277. end
  278. should "allow no yaml" do
  279. post = setup_post("2009-06-22-no-yaml.textile")
  280. assert_equal "No YAML.", post.content
  281. end
  282. should "allow empty yaml" do
  283. post = setup_post("2009-06-22-empty-yaml.textile")
  284. assert_equal "Empty YAML.", post.content
  285. end
  286. context "rendering" do
  287. setup do
  288. clear_dest
  289. end
  290. should "render properly" do
  291. post = setup_post("2008-10-18-foo-bar.textile")
  292. do_render(post)
  293. assert_equal "<<< <h1>Foo Bar</h1>\n<p>Best <strong>post</strong> ever</p> >>>", post.output
  294. end
  295. should "write properly" do
  296. post = setup_post("2008-10-18-foo-bar.textile")
  297. do_render(post)
  298. post.write(dest_dir)
  299. assert File.directory?(dest_dir)
  300. assert File.exists?(File.join(dest_dir, '2008', '10', '18', 'foo-bar.html'))
  301. end
  302. should "write properly without html extension" do
  303. post = setup_post("2008-10-18-foo-bar.textile")
  304. post.site.permalink_style = ":title"
  305. do_render(post)
  306. post.write(dest_dir)
  307. assert File.directory?(dest_dir)
  308. assert File.exists?(File.join(dest_dir, 'foo-bar', 'index.html'))
  309. end
  310. should "insert data" do
  311. post = setup_post("2008-11-21-complex.textile")
  312. do_render(post)
  313. 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
  314. end
  315. should "include templates" do
  316. post = setup_post("2008-12-13-include.markdown")
  317. post.site.source = File.join(File.dirname(__FILE__), 'source')
  318. do_render(post)
  319. assert_equal "<<< <hr />\n<p>Tom Preston-Werner github.com/mojombo</p>\n\n<p>This <em>is</em> cool</p> >>>", post.output
  320. end
  321. should "render date specified in front matter properly" do
  322. post = setup_post("2010-01-09-date-override.textile")
  323. do_render(post)
  324. assert_equal "<p>Post with a front matter date</p>\n<p>10 Jan 2010</p>", post.output
  325. end
  326. should "render time specified in front matter properly" do
  327. post = setup_post("2010-01-09-time-override.textile")
  328. do_render(post)
  329. assert_equal "<p>Post with a front matter time</p>\n<p>10 Jan 2010</p>", post.output
  330. end
  331. end
  332. end
  333. should "generate categories and topics" do
  334. post = Post.new(@site, File.join(File.dirname(__FILE__), *%w[source]), 'foo', 'bar/2008-12-12-topical-post.textile')
  335. assert_equal ['foo'], post.categories
  336. end
  337. end
  338. context "converter file extension settings" do
  339. setup do
  340. stub(Jekyll).configuration { Jekyll::DEFAULTS }
  341. @site = Site.new(Jekyll.configuration)
  342. end
  343. should "process .md as markdown under default configuration" do
  344. post = setup_post '2011-04-12-md-extension.md'
  345. conv = post.converter
  346. assert conv.kind_of? Jekyll::MarkdownConverter
  347. end
  348. should "process .text as indentity under default configuration" do
  349. post = setup_post '2011-04-12-text-extension.text'
  350. conv = post.converter
  351. assert conv.kind_of? Jekyll::IdentityConverter
  352. end
  353. should "process .text as markdown under alternate configuration" do
  354. @site.config['markdown_ext'] = 'markdown,mdw,mdwn,md,text'
  355. post = setup_post '2011-04-12-text-extension.text'
  356. conv = post.converter
  357. assert conv.kind_of? Jekyll::MarkdownConverter
  358. end
  359. should "process .md as markdown under alternate configuration" do
  360. @site.config['markdown_ext'] = 'markdown,mkd,mkdn,md,text'
  361. post = setup_post '2011-04-12-text-extension.text'
  362. conv = post.converter
  363. assert conv.kind_of? Jekyll::MarkdownConverter
  364. end
  365. should "process .text as textile under alternate configuration" do
  366. @site.config['textile_ext'] = 'textile,text'
  367. post = setup_post '2011-04-12-text-extension.text'
  368. conv = post.converter
  369. assert conv.kind_of? Jekyll::TextileConverter
  370. end
  371. end
  372. end