mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 19:30:47 -07:00
Introduce support to boolean operators in the lexer
This commit is contained in:
@@ -14,6 +14,8 @@ module Liquid
|
|||||||
COMPARISON_LESS_THAN = [:comparison, "<"].freeze
|
COMPARISON_LESS_THAN = [:comparison, "<"].freeze
|
||||||
COMPARISON_LESS_THAN_OR_EQUAL = [:comparison, "<="].freeze
|
COMPARISON_LESS_THAN_OR_EQUAL = [:comparison, "<="].freeze
|
||||||
COMPARISON_NOT_EQUAL_ALT = [:comparison, "<>"].freeze
|
COMPARISON_NOT_EQUAL_ALT = [:comparison, "<>"].freeze
|
||||||
|
BOOLEAN_AND = [:boolean_operator, "and"].freeze
|
||||||
|
BOOLEAN_OR = [:boolean_operator, "or"].freeze
|
||||||
DASH = [:dash, "-"].freeze
|
DASH = [:dash, "-"].freeze
|
||||||
DOT = [:dot, "."].freeze
|
DOT = [:dot, "."].freeze
|
||||||
DOTDOT = [:dotdot, ".."].freeze
|
DOTDOT = [:dotdot, ".."].freeze
|
||||||
@@ -151,6 +153,10 @@ module Liquid
|
|||||||
# Special case for "contains"
|
# Special case for "contains"
|
||||||
output << if type == :id && t == "contains" && output.last&.first != :dot
|
output << if type == :id && t == "contains" && output.last&.first != :dot
|
||||||
COMPARISON_CONTAINS
|
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
|
else
|
||||||
[type, t]
|
[type, t]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -141,6 +141,78 @@ class LexerUnitTest < Minitest::Test
|
|||||||
)
|
)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def tokenize(input)
|
def tokenize(input)
|
||||||
|
|||||||
Reference in New Issue
Block a user