Compare commits

...
Author SHA1 Message Date
Guilherme Carreiro a7c4c3ae06 * 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
2025-03-05 15:13:02 +01:00
Guilherme Carreiro a3a0cf9b81 Update the parser to use the new tokens 2025-03-05 14:49:59 +01:00
Guilherme Carreiro 5dceb83db1 Introduce support to boolean operators in the lexer 2025-03-05 14:49:10 +01:00
Albert Chu 76d628ddc7 Added a lot more boolean unit tests 2025-03-04 17:52:16 -07:00
Albert Chu 9be1b65bcc Rough support for parenthesis. Also better respect for and/or order precedence. 2025-03-04 17:19:27 -07:00
Albert Chu 19e9ba36ec Support for simple boolean comparisons and boolean assignments 2025-03-04 17:08:23 -07:00
Albert Chu a2de2dcdb6 TDD: Improved unit tests in boolean_unit_test.rb
Added tests for existing usage cases to avoid breaking important logic when introducing changes in subsequent commits.
2025-03-04 16:58:36 -07:00
Albert Chu 0667bfaa2e Removed infix operators from this PR 2025-03-04 16:33:27 -07:00
Albert Chu de669a3415 TDD: Unit tests for new liquid syntax 2025-03-04 12:59:45 -07:00
8 changed files with 335 additions and 1 deletions
+2
View File
@@ -80,6 +80,8 @@ require 'liquid/variable_lookup'
require 'liquid/range_lookup'
require 'liquid/resource_limits'
require 'liquid/expression'
require 'liquid/expression/comparison_expression'
require 'liquid/expression/logical_expression'
require 'liquid/template'
require 'liquid/condition'
require 'liquid/utils'
+3
View File
@@ -51,6 +51,9 @@ module Liquid
end
def inner_parse(markup, ss, cache)
return LogicalExpression.parse(markup, ss, cache) if LogicalExpression.logical?(markup)
return ComparisonExpression.parse(markup, ss, cache) if ComparisonExpression.comparison?(markup)
if (markup.start_with?("(") && markup.end_with?(")")) && markup =~ RANGES_REGEX
return RangeLookup.parse(
Regexp.last_match(1),
@@ -0,0 +1,29 @@
# frozen_string_literal: true
module Liquid
class Expression
class ComparisonExpression
COMPARISON_REGEX = /\A\s*(.+?)\s*(==|!=|<>|<=|>=|<|>|contains)\s*(.+)\s*\z/
class << self
def comparison?(markup)
markup =~ COMPARISON_REGEX
end
def parse(markup, ss, cache)
match = markup.match(COMPARISON_REGEX)
if match
left = Expression.parse(match[1].strip, ss, cache)
operator = match[2].strip
right = Expression.parse(match[3].strip, ss, cache)
return Condition.new(left, operator, right)
end
Condition.new(parse(markup, ss, cache), nil, nil)
end
end
end
end
end
@@ -0,0 +1,56 @@
# frozen_string_literal: true
module Liquid
class Expression
class LogicalExpression
LOGICAL_REGEX = /\A\s*(.+?)\s+(and|or)\s+(.+)\s*\z/i
EXPRESSIONS_AND_OPERATORS = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QuotedFragment}|\S+)\s*)+)/o
BOOLEAN_OPERATORS = ['and', 'or'].freeze
class << self
def logical?(markup)
markup =~ LOGICAL_REGEX
end
def parse(markup, ss, cache)
expressions = markup.scan(EXPRESSIONS_AND_OPERATORS)
last_expr = expressions.pop
condition = if ComparisonExpression.comparison?(last_expr)
ComparisonExpression.parse(last_expr, ss, cache)
elsif LOGICAL_REGEX.match?(last_expr)
LogicalExpression.parse(last_expr, ss, cache)
else
Condition.new(Expression.parse(last_expr, ss, cache), nil, nil)
end
until expressions.empty?
operator = expressions.pop.to_s.strip
next unless BOOLEAN_OPERATORS.include?(operator)
expr = expressions.pop.to_s.strip
new_condition = if ComparisonExpression.comparison?(expr)
ComparisonExpression.parse(expr, ss, cache)
elsif LOGICAL_REGEX.match?(expr)
LogicalExpression.parse(expr, ss, cache)
else
Condition.new(Expression.parse(expr, ss, cache), nil, nil)
end
if operator == 'and'
new_condition.and(condition)
else # operator == 'or'
new_condition.or(condition)
end
condition = new_condition
end
condition
end
end
end
end
end
+6
View File
@@ -14,6 +14,8 @@ module Liquid
COMPARISON_LESS_THAN = [:comparison, "<"].freeze
COMPARISON_LESS_THAN_OR_EQUAL = [:comparison, "<="].freeze
COMPARISON_NOT_EQUAL_ALT = [:comparison, "<>"].freeze
BOOLEAN_AND = [:boolean_operator, "and"].freeze
BOOLEAN_OR = [:boolean_operator, "or"].freeze
DASH = [:dash, "-"].freeze
DOT = [:dot, "."].freeze
DOTDOT = [:dotdot, ".."].freeze
@@ -151,6 +153,10 @@ module Liquid
# Special case for "contains"
output << if type == :id && t == "contains" && output.last&.first != :dot
COMPARISON_CONTAINS
elsif type == :id && t == "and" && output.last&.first != :dot
BOOLEAN_AND
elsif type == :id && t == "or" && output.last&.first != :dot
BOOLEAN_OR
else
[type, t]
end
+16 -1
View File
@@ -48,7 +48,7 @@ module Liquid
def expression
token = @tokens[@p]
case token[0]
expr = case token[0]
when :id
str = consume
str << variable_lookups
@@ -69,6 +69,21 @@ module Liquid
else
raise SyntaxError, "#{token} is not a valid expression"
end
if look(:comparison)
operator = consume(:comparison)
left = expr
right = expression
"#{left} #{operator} #{right}"
elsif look(:boolean_operator)
operator = consume(:boolean_operator)
left = expr
right = expression
"#{left} #{operator} #{right}"
else
expr
end
end
def argument
+151
View File
@@ -0,0 +1,151 @@
# frozen_string_literal: true
require 'test_helper'
class BooleanUnitTest < Minitest::Test
include Liquid
def test_simple_boolean_comparison
assert_parity("1 > 0", "true")
assert_parity("1 < 0", "false")
end
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_equality_operators
assert_parity_todo!("1 == 1", "true")
assert_parity_todo!("1 != 2", "true")
assert_parity_todo!("'hello' == 'hello'", "true")
end
def test_nil_renders_as_empty_string
assert_parity_todo!("nil", "false")
end
def test_nil_comparison_with_blank
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
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
assert_parity_todo!("x and true", "false", { "x" => nil })
end
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 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
+72
View File
@@ -141,6 +141,78 @@ class LexerUnitTest < Minitest::Test
)
end
def test_boolean_and_operator
exp = [
[:id, "true"],
[:boolean_operator, "and"],
[:id, "false"],
[:end_of_string],
]
act = tokenize("true and false")
assert_equal(exp, act)
end
def test_boolean_or_operator
exp = [
[:id, "false"],
[:boolean_operator, "or"],
[:id, "true"],
[:end_of_string],
]
act = tokenize("false or true")
assert_equal(exp, act)
end
def test_boolean_operators_in_complex_expressions
exp = [
[:id, "a"],
[:boolean_operator, "and"],
[:id, "b"],
[:boolean_operator, "or"],
[:id, "c"],
[:end_of_string],
]
act = tokenize("a and b or c")
assert_equal(exp, act)
end
def test_boolean_operators_with_comparisons
exp = [
[:id, "a"],
[:comparison, ">"],
[:number, "5"],
[:boolean_operator, "and"],
[:id, "b"],
[:comparison, "<"],
[:number, "10"],
[:end_of_string],
]
act = tokenize("a > 5 and b < 10")
assert_equal(exp, act)
end
def test_boolean_operators_as_property_names
exp = [
[:id, "obj"],
[:dot, "."],
[:id, "and"],
[:dot, "."],
[:id, "property"],
[:end_of_string],
]
act = tokenize("obj.and.property")
assert_equal(exp, act)
exp = [
[:id, "obj"],
[:dot, "."],
[:id, "or"],
[:end_of_string],
]
act = tokenize("obj.or")
assert_equal(exp, act)
end
private
def tokenize(input)