Skip to content

Commit 5bba11e

Browse files
committed
TDD: Add more unit test cases to boolean_unit_test.rb
1 parent 0667bfa commit 5bba11e

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

test/unit/boolean_unit_test.rb

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,132 @@ def test_mixed_boolean_expressions
7171
assert_equal(true, template.render("a" => 5, "b" => 3, "c" => 2, "d" => 4))
7272
assert_equal(false, template.render("a" => 5, "b" => 3, "c" => 5, "d" => 4))
7373
end
74+
75+
def test_negation_operator
76+
template = Liquid::Template.parse("{{ !true }}")
77+
assert_equal(false, template.render)
78+
79+
template = Liquid::Template.parse("{{ !false }}")
80+
assert_equal(true, template.render)
81+
82+
template = Liquid::Template.parse("{{ !(true and true) }}")
83+
assert_equal(false, template.render)
84+
end
85+
86+
def test_equality_operators
87+
template = Liquid::Template.parse("{{ 1 == 1 }}")
88+
assert_equal(true, template.render)
89+
90+
template = Liquid::Template.parse("{{ 1 != 2 }}")
91+
assert_equal(true, template.render)
92+
93+
template = Liquid::Template.parse("{{ 'hello' == 'hello' }}")
94+
assert_equal(true, template.render)
95+
end
96+
97+
def test_nil_in_boolean_context
98+
template = Liquid::Template.parse("{{ nil and true }}")
99+
assert_equal(false, template.render)
100+
101+
template = Liquid::Template.parse("{{ nil or true }}")
102+
assert_equal(true, template.render)
103+
104+
template = Liquid::Template.parse("{{ !nil }}")
105+
assert_equal(true, template.render)
106+
end
107+
108+
def test_truthy_falsy_values
109+
template = Liquid::Template.parse("{% if empty_string %}true{% else %}false{% endif %}")
110+
assert_equal(false, template.render("empty_string" => ""))
111+
112+
template = Liquid::Template.parse("{% if zero %}true{% else %}false{% endif %}")
113+
assert_equal(false, template.render("zero" => 0))
114+
115+
template = Liquid::Template.parse("{% if text %}true{% else %}false{% endif %}")
116+
assert_equal(true, template.render("text" => "hello"))
117+
end
118+
119+
def test_complex_precedence
120+
template = Liquid::Template.parse("{{ !a and b or c }}")
121+
assert_equal(true, template.render("a" => false, "b" => true, "c" => false))
122+
assert_equal(true, template.render("a" => true, "b" => false, "c" => true))
123+
assert_equal(false, template.render("a" => true, "b" => false, "c" => false))
124+
end
125+
126+
def test_string_comparison_with_blank
127+
# Non-empty string against blank
128+
template = Liquid::Template.parse("{{ text != blank }}")
129+
assert_equal(true, template.render("text" => "hello"))
130+
131+
template = Liquid::Template.parse("{{ text == blank }}")
132+
assert_equal(false, template.render("text" => "hello"))
133+
134+
# Empty string against blank
135+
template = Liquid::Template.parse("{{ empty_text != blank }}")
136+
assert_equal(false, template.render("empty_text" => ""))
137+
138+
template = Liquid::Template.parse("{{ empty_text == blank }}")
139+
assert_equal(true, template.render("empty_text" => ""))
140+
end
141+
142+
def test_nil_comparison_with_blank
143+
template = Liquid::Template.parse("{{ nil_value != blank }}")
144+
assert_equal(false, template.render("nil_value" => nil))
145+
146+
template = Liquid::Template.parse("{{ nil_value == blank }}")
147+
assert_equal(true, template.render("nil_value" => nil))
148+
149+
# Undefined variable is treated as nil
150+
template = Liquid::Template.parse("{{ undefined != blank }}")
151+
assert_equal(false, template.render)
152+
153+
template = Liquid::Template.parse("{{ undefined == blank }}")
154+
assert_equal(true, template.render)
155+
end
156+
157+
def test_empty_collections_with_blank
158+
template = Liquid::Template.parse("{{ empty_array == blank }}")
159+
assert_equal(true, template.render("empty_array" => []))
160+
161+
template = Liquid::Template.parse("{{ empty_array != blank }}")
162+
assert_equal(false, template.render("empty_array" => []))
163+
164+
template = Liquid::Template.parse("{{ empty_hash == blank }}")
165+
assert_equal(true, template.render("empty_hash" => {}))
166+
167+
template = Liquid::Template.parse("{{ empty_hash != blank }}")
168+
assert_equal(false, template.render("empty_hash" => {}))
169+
170+
# Non-empty collections
171+
template = Liquid::Template.parse("{{ array == blank }}")
172+
assert_equal(false, template.render("array" => [1, 2, 3]))
173+
174+
template = Liquid::Template.parse("{{ hash == blank }}")
175+
assert_equal(false, template.render("hash" => { "key" => "value" }))
176+
end
177+
178+
def test_blank_in_conditional_statements
179+
template = Liquid::Template.parse("{% if text != blank %}not blank{% else %}is blank{% endif %}")
180+
assert_equal("not blank", template.render("text" => "hello"))
181+
assert_equal("is blank", template.render("text" => ""))
182+
183+
template = Liquid::Template.parse("{% if nil_value != blank %}not blank{% else %}is blank{% endif %}")
184+
assert_equal("is blank", template.render("nil_value" => nil))
185+
186+
template = Liquid::Template.parse("{% if array != blank %}not blank{% else %}is blank{% endif %}")
187+
assert_equal("not blank", template.render("array" => [1, 2, 3]))
188+
assert_equal("is blank", template.render("array" => []))
189+
end
190+
191+
def test_blank_with_other_operators
192+
template = Liquid::Template.parse("{{ text != blank and number > 0 }}")
193+
assert_equal(true, template.render("text" => "hello", "number" => 5))
194+
assert_equal(false, template.render("text" => "", "number" => 5))
195+
assert_equal(false, template.render("text" => "hello", "number" => 0))
196+
197+
template = Liquid::Template.parse("{{ text != blank or number > 0 }}")
198+
assert_equal(true, template.render("text" => "hello", "number" => 0))
199+
assert_equal(true, template.render("text" => "", "number" => 5))
200+
assert_equal(false, template.render("text" => "", "number" => 0))
201+
end
74202
end

0 commit comments

Comments
 (0)