tobi / liquid

Liquid markup language. Save, customer facing template language for flexible web apps.

This URL has Read+Write access

liquid / test / if_else_test.rb
1d647361 » tobi 2008-05-08 Initial github import of li... 1 require File.dirname(__FILE__) + '/helper'
2
3 class IfElseTest < Test::Unit::TestCase
4 include Liquid
5
6 def test_if
7 assert_template_result(' ',' {% if false %} this text should not go into the output {% endif %} ')
8 assert_template_result(' this text should go into the output ',
9 ' {% if true %} this text should go into the output {% endif %} ')
10 assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
11 end
12
13 def test_if_else
14 assert_template_result(' YES ','{% if false %} NO {% else %} YES {% endif %}')
15 assert_template_result(' YES ','{% if true %} YES {% else %} NO {% endif %}')
16 assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}')
17 end
18
19 def test_if_boolean
20 assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
21 end
22
23 def test_if_or
24 assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
25 assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
26 assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
27 assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
28
29 assert_template_result(' YES ','{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
30 assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
31 end
32
33 def test_if_or_with_operators
34 assert_template_result(' YES ','{% if a == true or b == true %} YES {% endif %}', 'a' => true, 'b' => true)
35 assert_template_result(' YES ','{% if a == true or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
36 assert_template_result('','{% if a == false or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
37 end
38
39 def test_if_and
40 assert_template_result(' YES ','{% if true and true %} YES {% endif %}')
41 assert_template_result('','{% if false and true %} YES {% endif %}')
42 assert_template_result('','{% if false and true %} YES {% endif %}')
43 end
44
45
46 def test_hash_miss_generates_false
47 assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
48 end
49
50 def test_if_from_variable
51 assert_template_result('','{% if var %} NO {% endif %}', 'var' => false)
52 assert_template_result('','{% if var %} NO {% endif %}', 'var' => nil)
53 assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {'bar' => false})
54 assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
55 assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => nil)
56 assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => true)
57
58 assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => "text")
59 assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
60 assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => 1)
61 assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => {})
62 assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => [])
63 assert_template_result(' YES ','{% if "foo" %} YES {% endif %}')
64 assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => true})
65 assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => "text"})
66 assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => 1 })
67 assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => {} })
68 assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => [] })
69
70 assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => false)
71 assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => nil)
72 assert_template_result(' YES ','{% if var %} YES {% else %} NO {% endif %}', 'var' => true)
73 assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}', 'var' => "text")
74
75 assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'bar' => false})
76 assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => true})
77 assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => "text"})
78 assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'notbar' => true})
79 assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {})
80 assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'notfoo' => {'bar' => true})
81 end
82
83 def test_nested_if
84 assert_template_result('', '{% if false %}{% if false %} NO {% endif %}{% endif %}')
85 assert_template_result('', '{% if false %}{% if true %} NO {% endif %}{% endif %}')
86 assert_template_result('', '{% if true %}{% if false %} NO {% endif %}{% endif %}')
87 assert_template_result(' YES ', '{% if true %}{% if true %} YES {% endif %}{% endif %}')
88
89 assert_template_result(' YES ', '{% if true %}{% if true %} YES {% else %} NO {% endif %}{% else %} NO {% endif %}')
90 assert_template_result(' YES ', '{% if true %}{% if false %} NO {% else %} YES {% endif %}{% else %} NO {% endif %}')
91 assert_template_result(' YES ', '{% if false %}{% if true %} NO {% else %} NONO {% endif %}{% else %} YES {% endif %}')
92
93 end
94
95 def test_comparisons_on_null
96 assert_template_result('','{% if null < 10 %} NO {% endif %}')
97 assert_template_result('','{% if null <= 10 %} NO {% endif %}')
98 assert_template_result('','{% if null >= 10 %} NO {% endif %}')
99 assert_template_result('','{% if null > 10 %} NO {% endif %}')
100
101 assert_template_result('','{% if 10 < null %} NO {% endif %}')
102 assert_template_result('','{% if 10 <= null %} NO {% endif %}')
103 assert_template_result('','{% if 10 >= null %} NO {% endif %}')
104 assert_template_result('','{% if 10 > null %} NO {% endif %}')
105 end
106
107 def test_else_if
108 assert_template_result('0','{% if 0 == 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
109 assert_template_result('1','{% if 0 != 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
110 assert_template_result('2','{% if 0 != 0 %}0{% elsif 1 != 1%}1{% else %}2{% endif %}')
111
112 assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
113 end
114
115 def test_syntax_error_no_variable
116 assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
117 end
118
4c30922d » nbibler 2008-05-21 The if tag now raises Liqui... 119 def test_syntax_error_no_expression
120 assert_raise(SyntaxError) { assert_template_result('', '{% if %}') }
121 end
122
1d647361 » tobi 2008-05-08 Initial github import of li... 123 def test_if_with_custom_condition
124 Condition.operators['contains'] = :[]
125
126 assert_template_result('yes', %({% if 'bob' contains 'o' %}yes{% endif %}))
127 assert_template_result('no', %({% if 'bob' contains 'f' %}yes{% else %}no{% endif %}))
128 ensure
129 Condition.operators.delete 'contains'
130 end
131 end