mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
* Move expression handling from variable.rb to expression.rb
* Update test suite to validate parity * Remove parentheses handling * Split boolean into comparison and logical expressions
This commit is contained in:
+117
-308
@@ -6,337 +6,146 @@ class BooleanUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_simple_boolean_comparison
|
||||
template = Liquid::Template.parse("{{ 1 > 0 }}")
|
||||
assert_equal("true", template.render)
|
||||
assert_parity("1 > 0", "true")
|
||||
assert_parity("1 < 0", "false")
|
||||
end
|
||||
|
||||
template = Liquid::Template.parse("{{ 1 < 0 }}")
|
||||
assert_equal("false", template.render)
|
||||
def test_boolean_and_operator
|
||||
assert_parity("true and true", "true")
|
||||
assert_parity("true and false", "false")
|
||||
end
|
||||
|
||||
def test_boolean_or_operator
|
||||
assert_parity("true or false", "true")
|
||||
assert_parity("false or false", "false")
|
||||
end
|
||||
|
||||
def test_operator_precedence
|
||||
assert_parity("false and false or true", "false")
|
||||
end
|
||||
|
||||
def test_complex_boolean_expressions
|
||||
assert_parity("true and true and true", "true")
|
||||
assert_parity("true and false and true", "false")
|
||||
assert_parity("false or false or true", "true")
|
||||
end
|
||||
|
||||
def test_boolean_with_variables
|
||||
assert_parity("a and b", "true", { "a" => true, "b" => true })
|
||||
assert_parity("a and b", "false", { "a" => true, "b" => false })
|
||||
assert_parity("a or b", "true", { "a" => false, "b" => true })
|
||||
assert_parity("a or b", "false", { "a" => false, "b" => false })
|
||||
end
|
||||
|
||||
def test_nil_equals_nil
|
||||
assert_parity("nil == nil", "true")
|
||||
end
|
||||
|
||||
def test_nil_not_equals_nil
|
||||
assert_parity("nil != nil", "false")
|
||||
end
|
||||
|
||||
def test_nil_not_equals_empty_string
|
||||
assert_parity("nil == ''", "false")
|
||||
assert_parity("nil != ''", "true")
|
||||
end
|
||||
|
||||
def test_undefined_variable_in_comparisons
|
||||
assert_parity("undefined_var == nil", "true")
|
||||
assert_parity("undefined_var != nil", "false")
|
||||
end
|
||||
|
||||
def test_undefined_variable_compared_to_empty_string
|
||||
assert_parity("undefined_var == ''", "false")
|
||||
assert_parity("undefined_var != ''", "true")
|
||||
end
|
||||
|
||||
def test_boolean_variable_in_comparisons
|
||||
assert_parity("t == true", "true", { "t" => true })
|
||||
assert_parity("f == false", "true", { "f" => false })
|
||||
end
|
||||
|
||||
def test_boolean_variable_compared_to_nil
|
||||
assert_parity("t == nil", "false", { "t" => true })
|
||||
assert_parity("f == nil", "false", { "f" => false })
|
||||
assert_parity("f != nil", "true", { "f" => false })
|
||||
end
|
||||
|
||||
def test_nil_and_undefined_variables_in_boolean_expressions
|
||||
assert_parity("x == undefined_var", "true", { "x" => nil })
|
||||
assert_parity("x != undefined_var", "false", { "x" => nil })
|
||||
end
|
||||
|
||||
def test_nil_literal_in_or_expression
|
||||
assert_parity("nil or true", "true")
|
||||
end
|
||||
|
||||
def test_nil_variable_in_or_expression
|
||||
assert_parity("x or false", "false", { "x" => nil })
|
||||
end
|
||||
|
||||
def test_mixed_boolean_expressions
|
||||
assert_parity("a > b and c < d", "true", { "a" => 99, "b" => 0, "c" => 0, "d" => 99 })
|
||||
assert_parity("a > b and c < d", "false", { "a" => 99, "b" => 0, "c" => 99, "d" => 0 })
|
||||
end
|
||||
|
||||
def test_boolean_assignment_shorthand
|
||||
template = Liquid::Template.parse("{% assign lazy_load = media_position > 1 %}{{ lazy_load }}")
|
||||
assert_equal("false", template.render("media_position" => 1))
|
||||
assert_equal("true", template.render("media_position" => 2))
|
||||
end
|
||||
|
||||
def test_boolean_and_operator
|
||||
template = Liquid::Template.parse("{{ true and true }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ true and false }}")
|
||||
assert_equal("false", template.render)
|
||||
end
|
||||
|
||||
def test_boolean_or_operator
|
||||
template = Liquid::Template.parse("{{ true or false }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ false or false }}")
|
||||
assert_equal("false", template.render)
|
||||
end
|
||||
|
||||
def test_operator_precedence_with_parentheses
|
||||
template = Liquid::Template.parse("{{ false and (false or true) }}")
|
||||
assert_equal("false", template.render)
|
||||
end
|
||||
|
||||
def test_operator_precedence_without_parentheses
|
||||
template = Liquid::Template.parse("{{ false and false or true }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
def test_complex_boolean_expressions
|
||||
template = Liquid::Template.parse("{{ true and true and true }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ true and false and true }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ false or false or true }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
def test_boolean_with_variables
|
||||
template = Liquid::Template.parse("{{ a and b }}")
|
||||
assert_equal("true", template.render("a" => true, "b" => true))
|
||||
assert_equal("false", template.render("a" => true, "b" => false))
|
||||
|
||||
template = Liquid::Template.parse("{{ a or b }}")
|
||||
assert_equal("true", template.render("a" => false, "b" => true))
|
||||
assert_equal("false", template.render("a" => false, "b" => false))
|
||||
end
|
||||
|
||||
def test_mixed_boolean_expressions
|
||||
template = Liquid::Template.parse("{{ a > b and c < d }}")
|
||||
assert_equal("true", template.render("a" => 5, "b" => 3, "c" => 2, "d" => 4))
|
||||
assert_equal("false", template.render("a" => 5, "b" => 3, "c" => 5, "d" => 4))
|
||||
assert_equal("true", template.render("media_position" => 2))
|
||||
end
|
||||
|
||||
def test_equality_operators
|
||||
template = Liquid::Template.parse("{{ 1 == 1 }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ 1 != 2 }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ 'hello' == 'hello' }}")
|
||||
assert_equal("true", template.render)
|
||||
assert_parity_todo!("1 == 1", "true")
|
||||
assert_parity_todo!("1 != 2", "true")
|
||||
assert_parity_todo!("'hello' == 'hello'", "true")
|
||||
end
|
||||
|
||||
def test_truthy_falsy_values
|
||||
template = Liquid::Template.parse("{% if empty_string %}truthy{% else %}falsey{% endif %}")
|
||||
assert_equal("falsey", template.render("empty_string" => ""))
|
||||
|
||||
template = Liquid::Template.parse("{% if zero %}truthy{% else %}falsey{% endif %}")
|
||||
assert_equal("falsey", template.render("zero" => 0))
|
||||
|
||||
template = Liquid::Template.parse("{% if text %}truthy{% else %}falsey{% endif %}")
|
||||
assert_equal("true", template.render("text" => "hello"))
|
||||
end
|
||||
|
||||
def test_string_comparison_with_blank
|
||||
# Non-empty string against blank
|
||||
template = Liquid::Template.parse("{{ text != blank }}")
|
||||
assert_equal("true", template.render("text" => "hello"))
|
||||
|
||||
template = Liquid::Template.parse("{{ text == blank }}")
|
||||
assert_equal("false", template.render("text" => "hello"))
|
||||
|
||||
# Empty string against blank
|
||||
template = Liquid::Template.parse("{{ empty_text != blank }}")
|
||||
assert_equal("false", template.render("empty_text" => ""))
|
||||
|
||||
template = Liquid::Template.parse("{{ empty_text == blank }}")
|
||||
assert_equal("true", template.render("empty_text" => ""))
|
||||
def test_nil_renders_as_empty_string
|
||||
assert_parity_todo!("nil", "false")
|
||||
end
|
||||
|
||||
def test_nil_comparison_with_blank
|
||||
template = Liquid::Template.parse("{{ nil_value != blank }}")
|
||||
assert_equal("false", template.render("nil_value" => nil))
|
||||
|
||||
template = Liquid::Template.parse("{{ nil_value == blank }}")
|
||||
assert_equal("true", template.render("nil_value" => nil))
|
||||
|
||||
# Undefined variable is treated as nil
|
||||
template = Liquid::Template.parse("{{ undefined != blank }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ undefined == blank }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
def test_empty_collections_with_blank
|
||||
template = Liquid::Template.parse("{{ empty_array == blank }}")
|
||||
assert_equal("true", template.render("empty_array" => []))
|
||||
|
||||
template = Liquid::Template.parse("{{ empty_array != blank }}")
|
||||
assert_equal("false", template.render("empty_array" => []))
|
||||
|
||||
template = Liquid::Template.parse("{{ empty_hash == blank }}")
|
||||
assert_equal("true", template.render("empty_hash" => {}))
|
||||
|
||||
template = Liquid::Template.parse("{{ empty_hash != blank }}")
|
||||
assert_equal("false", template.render("empty_hash" => {}))
|
||||
|
||||
# Non-empty collections
|
||||
template = Liquid::Template.parse("{{ array == blank }}")
|
||||
assert_equal("false", template.render("array" => [1, 2, 3]))
|
||||
|
||||
template = Liquid::Template.parse("{{ hash == blank }}")
|
||||
assert_equal("false", template.render("hash" => { "key" => "value" }))
|
||||
end
|
||||
|
||||
def test_blank_in_conditional_statements
|
||||
template = Liquid::Template.parse("{% if text != blank %}not blank{% else %}is blank{% endif %}")
|
||||
assert_equal("not blank", template.render("text" => "hello"))
|
||||
assert_equal("is blank", template.render("text" => ""))
|
||||
|
||||
template = Liquid::Template.parse("{% if nil_value != blank %}not blank{% else %}is blank{% endif %}")
|
||||
assert_equal("is blank", template.render("nil_value" => nil))
|
||||
|
||||
template = Liquid::Template.parse("{% if array != blank %}not blank{% else %}is blank{% endif %}")
|
||||
assert_equal("not blank", template.render("array" => [1, 2, 3]))
|
||||
assert_equal("is blank", template.render("array" => []))
|
||||
end
|
||||
|
||||
def test_blank_with_other_operators
|
||||
template = Liquid::Template.parse("{{ text != blank and number > 0 }}")
|
||||
assert_equal("true", template.render("text" => "hello", "number" => 5))
|
||||
assert_equal("false", template.render("text" => "", "number" => 5))
|
||||
assert_equal("false", template.render("text" => "hello", "number" => 0))
|
||||
|
||||
template = Liquid::Template.parse("{{ text != blank or number > 0 }}")
|
||||
assert_equal("true", template.render("text" => "hello", "number" => 0))
|
||||
assert_equal("true", template.render("text" => "", "number" => 5))
|
||||
assert_equal("false", template.render("text" => "", "number" => 0))
|
||||
end
|
||||
|
||||
def test_basic_if_else_conditions
|
||||
template = Liquid::Template.parse("{% if true %}success{% else %}failure{% endif %}")
|
||||
assert_equal("success", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% if false %}failure{% else %}success{% endif %}")
|
||||
assert_equal("success", template.render)
|
||||
end
|
||||
|
||||
def test_if_with_comparisons
|
||||
template = Liquid::Template.parse("{% if 10 > 5 %}greater{% else %}not greater{% endif %}")
|
||||
assert_equal("greater", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% if 5 == 5 %}equal{% else %}not equal{% endif %}")
|
||||
assert_equal("equal", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% if 3 < 2 %}smaller{% else %}not smaller{% endif %}")
|
||||
assert_equal("not smaller", template.render)
|
||||
assert_parity_todo!("nil_value == blank", "false")
|
||||
assert_parity_todo!("nil_value != blank", "true")
|
||||
assert_parity_todo!("undefined != blank", "true")
|
||||
assert_parity_todo!("undefined == blank", "false")
|
||||
end
|
||||
|
||||
def test_if_with_variables
|
||||
template = Liquid::Template.parse("{% if value %}has value{% else %}no value{% endif %}")
|
||||
assert_equal("has value", template.render("value" => true))
|
||||
assert_equal("no value", template.render("value" => false))
|
||||
assert_equal("no value", template.render("value" => nil))
|
||||
assert_equal("has value", template.render("value" => "text"))
|
||||
assert_equal("no value", template.render("value" => ""))
|
||||
end
|
||||
|
||||
def test_if_with_variable_comparisons
|
||||
template = Liquid::Template.parse("{% if count > 5 %}high{% else %}low{% endif %}")
|
||||
assert_equal("high", template.render("count" => 10))
|
||||
assert_equal("low", template.render("count" => 3))
|
||||
end
|
||||
|
||||
def test_nested_if_conditions
|
||||
template = Liquid::Template.parse("{% if a %}{% if b %}both{% else %}a only{% endif %}{% else %}none{% endif %}")
|
||||
assert_equal("both", template.render("a" => true, "b" => true))
|
||||
assert_equal("a only", template.render("a" => true, "b" => false))
|
||||
assert_equal("none", template.render("a" => false, "b" => true))
|
||||
end
|
||||
|
||||
def test_elsif_conditions
|
||||
template = Liquid::Template.parse("{% if a %}a{% elsif b %}b{% else %}c{% endif %}")
|
||||
assert_equal("a", template.render("a" => true, "b" => true))
|
||||
assert_equal("b", template.render("a" => false, "b" => true))
|
||||
assert_equal("c", template.render("a" => false, "b" => false))
|
||||
end
|
||||
|
||||
def test_unless_conditions
|
||||
template = Liquid::Template.parse("{% unless a %}not a{% else %}a{% endunless %}")
|
||||
assert_equal("a", template.render("a" => true))
|
||||
assert_equal("not a", template.render("a" => false))
|
||||
end
|
||||
|
||||
def test_if_with_comparison_and_logical_operator
|
||||
template = Liquid::Template.parse("{% if a > 5 and b < 10 %}valid{% else %}invalid{% endif %}")
|
||||
assert_equal("valid", template.render("a" => 7, "b" => 8))
|
||||
assert_equal("invalid", template.render("a" => 3, "b" => 8))
|
||||
assert_equal("invalid", template.render("a" => 7, "b" => 12))
|
||||
end
|
||||
|
||||
# Basic nil rendering tests
|
||||
def test_nil_renders_as_empty_string
|
||||
template = Liquid::Template.parse("{{ nil }}")
|
||||
assert_equal("", template.render)
|
||||
end
|
||||
|
||||
def test_nil_in_assigned_variable_renders_as_empty_string
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x }}")
|
||||
assert_equal("", template.render)
|
||||
end
|
||||
|
||||
# Nil comparison tests
|
||||
def test_nil_equals_nil
|
||||
template = Liquid::Template.parse("{{ nil == nil }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
def test_nil_not_equals_nil
|
||||
template = Liquid::Template.parse("{{ nil != nil }}")
|
||||
assert_equal("false", template.render)
|
||||
end
|
||||
|
||||
def test_nil_not_equals_empty_string
|
||||
template = Liquid::Template.parse("{{ nil == '' }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ nil != '' }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
# Variable tests with nil values
|
||||
def test_variable_with_nil_value_in_comparisons
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x == nil }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x != nil }}")
|
||||
assert_equal("false", template.render)
|
||||
end
|
||||
|
||||
def test_variable_with_nil_value_compared_to_empty_string
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x == '' }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x != '' }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
# Tests with undefined variables
|
||||
def test_undefined_variable_in_comparisons
|
||||
template = Liquid::Template.parse("{{ undefined_var == nil }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ undefined_var != nil }}")
|
||||
assert_equal("false", template.render)
|
||||
end
|
||||
|
||||
def test_undefined_variable_compared_to_empty_string
|
||||
template = Liquid::Template.parse("{{ undefined_var == '' }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{{ undefined_var != '' }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
# Tests with boolean values
|
||||
def test_boolean_variable_in_comparisons
|
||||
template = Liquid::Template.parse("{% assign t = true %}{{ t == true }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% assign f = false %}{{ f == false }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
def test_boolean_variable_compared_to_nil
|
||||
template = Liquid::Template.parse("{% assign t = true %}{{ t == nil }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% assign f = false %}{{ f == nil }}")
|
||||
assert_equal("false", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% assign f = false %}{{ f != nil }}")
|
||||
assert_equal("true", template.render)
|
||||
end
|
||||
|
||||
# Mixed comparison tests
|
||||
def test_nil_and_undefined_variables_in_boolean_expressions
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x == undefined_var }}")
|
||||
assert_equal("true", template.render)
|
||||
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x != undefined_var }}")
|
||||
assert_equal("false", template.render)
|
||||
assert_parity_todo!("value", "true", { "value" => true })
|
||||
assert_parity_todo!("value", "false", { "value" => false })
|
||||
assert_parity_todo!("value", "false", { "value" => nil })
|
||||
assert_parity_todo!("value", "true", { "value" => "text" })
|
||||
assert_parity_todo!("value", "true", { "value" => "" })
|
||||
end
|
||||
|
||||
def test_nil_variable_in_and_expression
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x and true }}")
|
||||
assert_equal("false", template.render)
|
||||
assert_parity_todo!("x and true", "false", { "x" => nil })
|
||||
end
|
||||
|
||||
def test_nil_literal_in_or_expression
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ nil or true }}")
|
||||
assert_equal("true", template.render)
|
||||
private
|
||||
|
||||
def assert_parity_todo!(liquid_expression, expected_result, args = {})
|
||||
assert_parity_scenario(:condition, "{% if #{liquid_expression} %}true{% else %}false{% endif %}", expected_result, args)
|
||||
test_name = caller_locations(1, 1)[0].label
|
||||
puts "\e[33mTODO: parity for '#{test_name}'\e[0m"
|
||||
end
|
||||
|
||||
def test_nil_variable_in_or_expression
|
||||
template = Liquid::Template.parse("{% assign x = nil %}{{ x or false }}")
|
||||
assert_equal("false", template.render)
|
||||
def assert_parity(liquid_expression, expected_result, args = {})
|
||||
assert_parity_scenario(:condition, "{% if #{liquid_expression} %}true{% else %}false{% endif %}", expected_result, args)
|
||||
assert_parity_scenario(:expression, "{{ #{liquid_expression} }}", expected_result, args)
|
||||
end
|
||||
|
||||
def assert_parity_scenario(kind, template, exp_output, args = {})
|
||||
act_output = Liquid::Template.parse(template).render(args)
|
||||
|
||||
assert_equal(exp_output, act_output, <<~ERROR_MESSAGE)
|
||||
#{kind.to_s.capitalize} template failure:
|
||||
---
|
||||
#{template}
|
||||
---
|
||||
args: #{args.inspect}
|
||||
ERROR_MESSAGE
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user