PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lib/liquid/tags/standard_tag_test.rb

https://github.com/peertransfer/liquid
Ruby | 461 lines | 380 code | 80 blank | 1 comment | 1 complexity | 64a8333e7a084ed777a845d68d4b5619 MD5 | raw file
  1. require 'test_helper'
  2. class StandardTagTest < Test::Unit::TestCase
  3. include Liquid
  4. def test_tag
  5. tag = Tag.new('tag', [], [])
  6. assert_equal 'liquid::tag', tag.name
  7. assert_equal '', tag.render(Context.new)
  8. end
  9. def test_no_transform
  10. assert_template_result('this text should come out of the template without change...',
  11. 'this text should come out of the template without change...')
  12. assert_template_result('blah','blah')
  13. assert_template_result('<blah>','<blah>')
  14. assert_template_result('|,.:','|,.:')
  15. assert_template_result('','')
  16. text = %|this shouldnt see any transformation either but has multiple lines
  17. as you can clearly see here ...|
  18. assert_template_result(text,text)
  19. end
  20. def test_has_a_block_which_does_nothing
  21. assert_template_result(%|the comment block should be removed .. right?|,
  22. %|the comment block should be removed {%comment%} be gone.. {%endcomment%} .. right?|)
  23. assert_template_result('','{%comment%}{%endcomment%}')
  24. assert_template_result('','{%comment%}{% endcomment %}')
  25. assert_template_result('','{% comment %}{%endcomment%}')
  26. assert_template_result('','{% comment %}{% endcomment %}')
  27. assert_template_result('','{%comment%}comment{%endcomment%}')
  28. assert_template_result('','{% comment %}comment{% endcomment %}')
  29. assert_template_result('foobar','foo{%comment%}comment{%endcomment%}bar')
  30. assert_template_result('foobar','foo{% comment %}comment{% endcomment %}bar')
  31. assert_template_result('foobar','foo{%comment%} comment {%endcomment%}bar')
  32. assert_template_result('foobar','foo{% comment %} comment {% endcomment %}bar')
  33. assert_template_result('foo bar','foo {%comment%} {%endcomment%} bar')
  34. assert_template_result('foo bar','foo {%comment%}comment{%endcomment%} bar')
  35. assert_template_result('foo bar','foo {%comment%} comment {%endcomment%} bar')
  36. assert_template_result('foobar','foo{%comment%}
  37. {%endcomment%}bar')
  38. end
  39. def test_for
  40. assert_template_result(' yo yo yo yo ','{%for item in array%} yo {%endfor%}','array' => [1,2,3,4])
  41. assert_template_result('yoyo','{%for item in array%}yo{%endfor%}','array' => [1,2])
  42. assert_template_result(' yo ','{%for item in array%} yo {%endfor%}','array' => [1])
  43. assert_template_result('','{%for item in array%}{%endfor%}','array' => [1,2])
  44. expected = <<HERE
  45. yo
  46. yo
  47. yo
  48. HERE
  49. template = <<HERE
  50. {%for item in array%}
  51. yo
  52. {%endfor%}
  53. HERE
  54. assert_template_result(expected,template,'array' => [1,2,3])
  55. end
  56. def test_for_with_range
  57. assert_template_result(' 1 2 3 ','{%for item in (1..3) %} {{item}} {%endfor%}')
  58. end
  59. def test_for_with_variable
  60. assert_template_result(' 1 2 3 ','{%for item in array%} {{item}} {%endfor%}','array' => [1,2,3])
  61. assert_template_result('123','{%for item in array%}{{item}}{%endfor%}','array' => [1,2,3])
  62. assert_template_result('123','{% for item in array %}{{item}}{% endfor %}','array' => [1,2,3])
  63. assert_template_result('abcd','{%for item in array%}{{item}}{%endfor%}','array' => ['a','b','c','d'])
  64. assert_template_result('a b c','{%for item in array%}{{item}}{%endfor%}','array' => ['a',' ','b',' ','c'])
  65. assert_template_result('abc','{%for item in array%}{{item}}{%endfor%}','array' => ['a','','b','','c'])
  66. end
  67. def test_for_helpers
  68. assigns = {'array' => [1,2,3] }
  69. assert_template_result(' 1/3 2/3 3/3 ',
  70. '{%for item in array%} {{forloop.index}}/{{forloop.length}} {%endfor%}',
  71. assigns)
  72. assert_template_result(' 1 2 3 ', '{%for item in array%} {{forloop.index}} {%endfor%}', assigns)
  73. assert_template_result(' 0 1 2 ', '{%for item in array%} {{forloop.index0}} {%endfor%}', assigns)
  74. assert_template_result(' 2 1 0 ', '{%for item in array%} {{forloop.rindex0}} {%endfor%}', assigns)
  75. assert_template_result(' 3 2 1 ', '{%for item in array%} {{forloop.rindex}} {%endfor%}', assigns)
  76. assert_template_result(' true false false ', '{%for item in array%} {{forloop.first}} {%endfor%}', assigns)
  77. assert_template_result(' false false true ', '{%for item in array%} {{forloop.last}} {%endfor%}', assigns)
  78. end
  79. def test_for_and_if
  80. assigns = {'array' => [1,2,3] }
  81. assert_template_result('+--',
  82. '{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}',
  83. assigns)
  84. end
  85. def test_for_else
  86. assert_template_result('+++', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>[1,2,3])
  87. assert_template_result('-', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>[])
  88. assert_template_result('-', '{%for item in array%}+{%else%}-{%endfor%}', 'array'=>nil)
  89. end
  90. def test_limiting
  91. assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
  92. assert_template_result('12', '{%for i in array limit:2 %}{{ i }}{%endfor%}', assigns)
  93. assert_template_result('1234', '{%for i in array limit:4 %}{{ i }}{%endfor%}', assigns)
  94. assert_template_result('3456', '{%for i in array limit:4 offset:2 %}{{ i }}{%endfor%}', assigns)
  95. assert_template_result('3456', '{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}', assigns)
  96. end
  97. def test_dynamic_variable_limiting
  98. assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
  99. assigns['limit'] = 2
  100. assigns['offset'] = 2
  101. assert_template_result('34', '{%for i in array limit: limit offset: offset %}{{ i }}{%endfor%}', assigns)
  102. end
  103. def test_nested_for
  104. assigns = {'array' => [[1,2],[3,4],[5,6]] }
  105. assert_template_result('123456', '{%for item in array%}{%for i in item%}{{ i }}{%endfor%}{%endfor%}', assigns)
  106. end
  107. def test_offset_only
  108. assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
  109. assert_template_result('890', '{%for i in array offset:7 %}{{ i }}{%endfor%}', assigns)
  110. end
  111. def test_pause_resume
  112. assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
  113. markup = <<-MKUP
  114. {%for i in array.items limit: 3 %}{{i}}{%endfor%}
  115. next
  116. {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
  117. next
  118. {%for i in array.items offset:continue limit: 3 %}{{i}}{%endfor%}
  119. MKUP
  120. expected = <<-XPCTD
  121. 123
  122. next
  123. 456
  124. next
  125. 789
  126. XPCTD
  127. assert_template_result(expected,markup,assigns)
  128. end
  129. def test_pause_resume_limit
  130. assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
  131. markup = <<-MKUP
  132. {%for i in array.items limit:3 %}{{i}}{%endfor%}
  133. next
  134. {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
  135. next
  136. {%for i in array.items offset:continue limit:1 %}{{i}}{%endfor%}
  137. MKUP
  138. expected = <<-XPCTD
  139. 123
  140. next
  141. 456
  142. next
  143. 7
  144. XPCTD
  145. assert_template_result(expected,markup,assigns)
  146. end
  147. def test_pause_resume_BIG_limit
  148. assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
  149. markup = <<-MKUP
  150. {%for i in array.items limit:3 %}{{i}}{%endfor%}
  151. next
  152. {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
  153. next
  154. {%for i in array.items offset:continue limit:1000 %}{{i}}{%endfor%}
  155. MKUP
  156. expected = <<-XPCTD
  157. 123
  158. next
  159. 456
  160. next
  161. 7890
  162. XPCTD
  163. assert_template_result(expected,markup,assigns)
  164. end
  165. def test_pause_resume_BIG_offset
  166. assigns = {'array' => {'items' => [1,2,3,4,5,6,7,8,9,0]}}
  167. markup = %q({%for i in array.items limit:3 %}{{i}}{%endfor%}
  168. next
  169. {%for i in array.items offset:continue limit:3 %}{{i}}{%endfor%}
  170. next
  171. {%for i in array.items offset:continue limit:3 offset:1000 %}{{i}}{%endfor%})
  172. expected = %q(123
  173. next
  174. 456
  175. next
  176. )
  177. assert_template_result(expected,markup,assigns)
  178. end
  179. def test_assign
  180. assigns = {'var' => 'content' }
  181. assert_template_result('var2: var2:content', 'var2:{{var2}} {%assign var2 = var%} var2:{{var2}}', assigns)
  182. end
  183. def test_hyphenated_assign
  184. assigns = {'a-b' => '1' }
  185. assert_template_result('a-b:1 a-b:2', 'a-b:{{a-b}} {%assign a-b = 2 %}a-b:{{a-b}}', assigns)
  186. end
  187. def test_assign_with_colon_and_spaces
  188. assigns = {'var' => {'a:b c' => {'paged' => '1' }}}
  189. assert_template_result('var2: 1', '{%assign var2 = var["a:b c"].paged %}var2: {{var2}}', assigns)
  190. end
  191. def test_capture
  192. assigns = {'var' => 'content' }
  193. assert_template_result('content foo content foo ',
  194. '{{ var2 }}{% capture var2 %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
  195. assigns)
  196. end
  197. def test_capture_detects_bad_syntax
  198. assert_raise(SyntaxError) do
  199. assert_template_result('content foo content foo ',
  200. '{{ var2 }}{% capture %}{{ var }} foo {% endcapture %}{{ var2 }}{{ var2 }}',
  201. {'var' => 'content' })
  202. end
  203. end
  204. def test_case
  205. assigns = {'condition' => 2 }
  206. assert_template_result(' its 2 ',
  207. '{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
  208. assigns)
  209. assigns = {'condition' => 1 }
  210. assert_template_result(' its 1 ',
  211. '{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
  212. assigns)
  213. assigns = {'condition' => 3 }
  214. assert_template_result('',
  215. '{% case condition %}{% when 1 %} its 1 {% when 2 %} its 2 {% endcase %}',
  216. assigns)
  217. assigns = {'condition' => "string here" }
  218. assert_template_result(' hit ',
  219. '{% case condition %}{% when "string here" %} hit {% endcase %}',
  220. assigns)
  221. assigns = {'condition' => "bad string here" }
  222. assert_template_result('',
  223. '{% case condition %}{% when "string here" %} hit {% endcase %}',\
  224. assigns)
  225. end
  226. def test_case_with_else
  227. assigns = {'condition' => 5 }
  228. assert_template_result(' hit ',
  229. '{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
  230. assigns)
  231. assigns = {'condition' => 6 }
  232. assert_template_result(' else ',
  233. '{% case condition %}{% when 5 %} hit {% else %} else {% endcase %}',
  234. assigns)
  235. assigns = {'condition' => 6 }
  236. assert_template_result(' else ',
  237. '{% case condition %} {% when 5 %} hit {% else %} else {% endcase %}',
  238. assigns)
  239. end
  240. def test_case_on_size
  241. assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [])
  242. assert_template_result('1', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1])
  243. assert_template_result('2', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1])
  244. assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1])
  245. assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1])
  246. assert_template_result('', '{% case a.size %}{% when 1 %}1{% when 2 %}2{% endcase %}', 'a' => [1, 1, 1, 1, 1])
  247. end
  248. def test_case_on_size_with_else
  249. assert_template_result('else',
  250. '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
  251. 'a' => [])
  252. assert_template_result('1',
  253. '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
  254. 'a' => [1])
  255. assert_template_result('2',
  256. '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
  257. 'a' => [1, 1])
  258. assert_template_result('else',
  259. '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
  260. 'a' => [1, 1, 1])
  261. assert_template_result('else',
  262. '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
  263. 'a' => [1, 1, 1, 1])
  264. assert_template_result('else',
  265. '{% case a.size %}{% when 1 %}1{% when 2 %}2{% else %}else{% endcase %}',
  266. 'a' => [1, 1, 1, 1, 1])
  267. end
  268. def test_case_on_length_with_else
  269. assert_template_result('else',
  270. '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
  271. {})
  272. assert_template_result('false',
  273. '{% case false %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
  274. {})
  275. assert_template_result('true',
  276. '{% case true %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
  277. {})
  278. assert_template_result('else',
  279. '{% case NULL %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
  280. {})
  281. end
  282. def test_assign_from_case
  283. # Example from the shopify forums
  284. code = %q({% case collection.handle %}{% when 'menswear-jackets' %}{% assign ptitle = 'menswear' %}{% when 'menswear-t-shirts' %}{% assign ptitle = 'menswear' %}{% else %}{% assign ptitle = 'womenswear' %}{% endcase %}{{ ptitle }})
  285. template = Liquid::Template.parse(code)
  286. assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-jackets'})
  287. assert_equal "menswear", template.render("collection" => {'handle' => 'menswear-t-shirts'})
  288. assert_equal "womenswear", template.render("collection" => {'handle' => 'x'})
  289. assert_equal "womenswear", template.render("collection" => {'handle' => 'y'})
  290. assert_equal "womenswear", template.render("collection" => {'handle' => 'z'})
  291. end
  292. def test_case_when_or
  293. code = '{% case condition %}{% when 1 or 2 or 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
  294. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
  295. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 2 })
  296. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
  297. assert_template_result(' its 4 ', code, {'condition' => 4 })
  298. assert_template_result('', code, {'condition' => 5 })
  299. code = '{% case condition %}{% when 1 or "string" or null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
  300. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
  301. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
  302. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
  303. assert_template_result('', code, {'condition' => 'something else' })
  304. end
  305. def test_case_when_comma
  306. code = '{% case condition %}{% when 1, 2, 3 %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
  307. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
  308. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 2 })
  309. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 3 })
  310. assert_template_result(' its 4 ', code, {'condition' => 4 })
  311. assert_template_result('', code, {'condition' => 5 })
  312. code = '{% case condition %}{% when 1, "string", null %} its 1 or 2 or 3 {% when 4 %} its 4 {% endcase %}'
  313. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 1 })
  314. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => 'string' })
  315. assert_template_result(' its 1 or 2 or 3 ', code, {'condition' => nil })
  316. assert_template_result('', code, {'condition' => 'something else' })
  317. end
  318. def test_assign
  319. assert_equal 'variable', Liquid::Template.parse( '{% assign a = "variable"%}{{a}}' ).render
  320. end
  321. def test_assign_an_empty_string
  322. assert_equal '', Liquid::Template.parse( '{% assign a = ""%}{{a}}' ).render
  323. end
  324. def test_assign_is_global
  325. assert_equal 'variable',
  326. Liquid::Template.parse( '{%for i in (1..2) %}{% assign a = "variable"%}{% endfor %}{{a}}' ).render
  327. end
  328. def test_case_detects_bad_syntax
  329. assert_raise(SyntaxError) do
  330. assert_template_result('', '{% case false %}{% when %}true{% endcase %}', {})
  331. end
  332. assert_raise(SyntaxError) do
  333. assert_template_result('', '{% case false %}{% huh %}true{% endcase %}', {})
  334. end
  335. end
  336. def test_cycle
  337. assert_template_result('one','{%cycle "one", "two"%}')
  338. assert_template_result('one two','{%cycle "one", "two"%} {%cycle "one", "two"%}')
  339. assert_template_result(' two','{%cycle "", "two"%} {%cycle "", "two"%}')
  340. assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
  341. assert_template_result('text-align: left text-align: right',
  342. '{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}')
  343. end
  344. def test_multiple_cycles
  345. assert_template_result('1 2 1 1 2 3 1',
  346. '{%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%} {%cycle 1,2,3%}')
  347. end
  348. def test_multiple_named_cycles
  349. assert_template_result('one one two two one one',
  350. '{%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %} {%cycle 1: "one", "two" %} {%cycle 2: "one", "two" %}')
  351. end
  352. def test_multiple_named_cycles_with_names_from_context
  353. assigns = {"var1" => 1, "var2" => 2 }
  354. assert_template_result('one one two two one one',
  355. '{%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %} {%cycle var1: "one", "two" %} {%cycle var2: "one", "two" %}', assigns)
  356. end
  357. def test_size_of_array
  358. assigns = {"array" => [1,2,3,4]}
  359. assert_template_result('array has 4 elements', "array has {{ array.size }} elements", assigns)
  360. end
  361. def test_size_of_hash
  362. assigns = {"hash" => {:a => 1, :b => 2, :c=> 3, :d => 4}}
  363. assert_template_result('hash has 4 elements', "hash has {{ hash.size }} elements", assigns)
  364. end
  365. def test_illegal_symbols
  366. assert_template_result('', '{% if true == empty %}?{% endif %}', {})
  367. assert_template_result('', '{% if true == null %}?{% endif %}', {})
  368. assert_template_result('', '{% if empty == true %}?{% endif %}', {})
  369. assert_template_result('', '{% if null == true %}?{% endif %}', {})
  370. end
  371. def test_for_reversed
  372. assigns = {'array' => [ 1, 2, 3] }
  373. assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
  374. end
  375. def test_ifchanged
  376. assigns = {'array' => [ 1, 1, 2, 2, 3, 3] }
  377. assert_template_result('123','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
  378. assigns = {'array' => [ 1, 1, 1, 1] }
  379. assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
  380. end
  381. end # StandardTagTest