public
Rubygem
Description: Liquid markup language. Save, customer facing template language for flexible web apps.
Homepage: http://www.liquidmarkup.org
Clone URL: git://github.com/tobi/liquid.git
Click here to lend your support to: liquid and make a donation at www.pledgie.com !
liquid / test / if_else_test.rb
100644 150 lines (123 sloc) 8.713 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require File.dirname(__FILE__) + '/helper'
 
class IfElseTest < Test::Unit::TestCase
  include Liquid
 
  def test_if
    assert_template_result(' ',' {% if false %} this text should not go into the output {% endif %} ')
    assert_template_result(' this text should go into the output ',
              ' {% if true %} this text should go into the output {% endif %} ')
    assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
  end
 
  def test_if_else
    assert_template_result(' YES ','{% if false %} NO {% else %} YES {% endif %}')
    assert_template_result(' YES ','{% if true %} YES {% else %} NO {% endif %}')
    assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}')
  end
  
  def test_if_boolean
    assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
  end
  
  def test_if_or
    assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
    assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
    assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
    assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
 
    assert_template_result(' YES ','{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
    assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
  end
  
  def test_if_or_with_operators
    assert_template_result(' YES ','{% if a == true or b == true %} YES {% endif %}', 'a' => true, 'b' => true)
    assert_template_result(' YES ','{% if a == true or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
    assert_template_result('','{% if a == false or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
  end
  
  def test_if_and
    assert_template_result(' YES ','{% if true and true %} YES {% endif %}')
    assert_template_result('','{% if false and true %} YES {% endif %}')
    assert_template_result('','{% if false and true %} YES {% endif %}')
  end
  
  
  def test_hash_miss_generates_false
    assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
  end
  
  def test_if_from_variable
    assert_template_result('','{% if var %} NO {% endif %}', 'var' => false)
    assert_template_result('','{% if var %} NO {% endif %}', 'var' => nil)
    assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {'bar' => false})
    assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
    assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => nil)
    assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => true)
    
    assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => "text")
    assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
    assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => 1)
    assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => {})
    assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => [])
    assert_template_result(' YES ','{% if "foo" %} YES {% endif %}')
    assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => true})
    assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => "text"})
    assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => 1 })
    assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => {} })
    assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => [] })
    
    assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => false)
    assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => nil)
    assert_template_result(' YES ','{% if var %} YES {% else %} NO {% endif %}', 'var' => true)
    assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}', 'var' => "text")
    
    assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'bar' => false})
    assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => true})
    assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => "text"})
    assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'notbar' => true})
    assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {})
    assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'notfoo' => {'bar' => true})
  end
  
  def test_nested_if
    assert_template_result('', '{% if false %}{% if false %} NO {% endif %}{% endif %}')
    assert_template_result('', '{% if false %}{% if true %} NO {% endif %}{% endif %}')
    assert_template_result('', '{% if true %}{% if false %} NO {% endif %}{% endif %}')
    assert_template_result(' YES ', '{% if true %}{% if true %} YES {% endif %}{% endif %}')
    
    assert_template_result(' YES ', '{% if true %}{% if true %} YES {% else %} NO {% endif %}{% else %} NO {% endif %}')
    assert_template_result(' YES ', '{% if true %}{% if false %} NO {% else %} YES {% endif %}{% else %} NO {% endif %}')
    assert_template_result(' YES ', '{% if false %}{% if true %} NO {% else %} NONO {% endif %}{% else %} YES {% endif %}')
    
  end
  
  def test_comparisons_on_null
    assert_template_result('','{% if null < 10 %} NO {% endif %}')
    assert_template_result('','{% if null <= 10 %} NO {% endif %}')
    assert_template_result('','{% if null >= 10 %} NO {% endif %}')
    assert_template_result('','{% if null > 10 %} NO {% endif %}')
 
    assert_template_result('','{% if 10 < null %} NO {% endif %}')
    assert_template_result('','{% if 10 <= null %} NO {% endif %}')
    assert_template_result('','{% if 10 >= null %} NO {% endif %}')
    assert_template_result('','{% if 10 > null %} NO {% endif %}')
  end
  
  def test_else_if
    assert_template_result('0','{% if 0 == 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
    assert_template_result('1','{% if 0 != 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
    assert_template_result('2','{% if 0 != 0 %}0{% elsif 1 != 1%}1{% else %}2{% endif %}')
    
    assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
  end
  
  def test_with_filtered_expressions
    assert_template_result('yes','{% if "BLAH"|downcase == "blah" %}yes{% endif %}')
    assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--" == "FOO--" %}yes{% endif %}')
    assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--"|downcase == "foo--" %}yes{% endif %}')
    assert_template_result('yes','{% if "foo--" == "FOO BAR"|truncatewords:1,"--"|downcase %}yes{% endif %}')
    # array transformation, to make sure we aren't converting arrays to strings somewhere along the way:
    assert_template_result('yes','{% if values|sort == sorted %}yes{% endif %}', 'values' => %w{foo bar baz}, 'sorted' => %w{bar baz foo})
  end
  
  def test_allow_no_spaces_in_filtered_expressions
    assert_template_result('','{% if "foo--" == "FOO BAR" |truncatewords:1,"--"|downcase %}yes{% endif %}')
    assert_template_result('','{% if "foo--" == "FOO BAR"| truncatewords:1,"--"|downcase %}yes{% endif %}')
    assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords :1,"--"|downcase %}yes{% endif %}')
    assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords: 1,"--"|downcase %}yes{% endif %}')
    assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1 ,"--"|downcase %}yes{% endif %}')
    assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1, "--"|downcase %}yes{% endif %}')
    assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1,"--" |downcase %}yes{% endif %}')
  end
  
  def test_syntax_error_no_variable
    assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
  end
  
  def test_syntax_error_no_expression
    assert_raise(SyntaxError) { assert_template_result('', '{% if %}') }
  end
  
  def test_if_with_custom_condition
    Condition.operators['contains'] = :[]
    
    assert_template_result('yes', %({% if 'bob' contains 'o' %}yes{% endif %}))
    assert_template_result('no', %({% if 'bob' contains 'f' %}yes{% else %}no{% endif %}))
  ensure
    Condition.operators.delete 'contains'
  end
end