mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-30 23:55:12 -07:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc04892e54 | ||
|
|
f676e699a4 | ||
|
|
fc0f7f44c1 | ||
|
|
b459eb656f | ||
|
|
c6dcf3e714 | ||
|
|
4dae678c63 | ||
|
|
4c07ff920b | ||
|
|
dbe709c3bf | ||
|
|
2b75bfaff4 | ||
|
|
87bc6e7cfa | ||
|
|
7f122aeed2 | ||
|
|
aa1640035f | ||
|
|
f5d6a36574 | ||
|
|
c5711c095f | ||
|
|
284f5fb647 | ||
|
|
21432928d0 | ||
|
|
1783c0c084 | ||
|
|
e38f730c00 |
@@ -80,8 +80,6 @@ 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'
|
||||
|
||||
@@ -51,9 +51,6 @@ 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),
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
# 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
|
||||
@@ -1,56 +0,0 @@
|
||||
# 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
|
||||
@@ -14,8 +14,6 @@ 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
|
||||
@@ -153,10 +151,6 @@ 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
|
||||
|
||||
+1
-16
@@ -48,7 +48,7 @@ module Liquid
|
||||
|
||||
def expression
|
||||
token = @tokens[@p]
|
||||
expr = case token[0]
|
||||
case token[0]
|
||||
when :id
|
||||
str = consume
|
||||
str << variable_lookups
|
||||
@@ -69,21 +69,6 @@ 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
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
require 'cgi'
|
||||
require 'base64'
|
||||
require 'bigdecimal'
|
||||
|
||||
module Liquid
|
||||
module StandardFilters
|
||||
MAX_I32 = (1 << 31) - 1
|
||||
@@ -712,7 +711,16 @@ module Liquid
|
||||
input.gsub(/\r?\n/, "<br />\n")
|
||||
end
|
||||
|
||||
# Reformat a date using Ruby's core Time#strftime( string ) -> string
|
||||
# @liquid_public_docs
|
||||
# @liquid_type filter
|
||||
# @liquid_category date
|
||||
# @liquid_summary
|
||||
# Formats a date according to a specified format string.
|
||||
# @liquid_description
|
||||
# This filter formats a date using various format specifiers. If the format string is empty,
|
||||
# the original input is returned. If the input cannot be converted to a date, the original input is returned.
|
||||
#
|
||||
# The following format specifiers can be used:
|
||||
#
|
||||
# %a - The abbreviated weekday name (``Sun'')
|
||||
# %A - The full weekday name (``Sunday'')
|
||||
@@ -741,8 +749,8 @@ module Liquid
|
||||
# %Y - Year with century
|
||||
# %Z - Time zone name
|
||||
# %% - Literal ``%'' character
|
||||
#
|
||||
# See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
|
||||
# @liquid_syntax date | date: string
|
||||
# @liquid_return [string]
|
||||
def date(input, format)
|
||||
str_format = Utils.to_s(format)
|
||||
return input if str_format.empty?
|
||||
@@ -974,6 +982,8 @@ module Liquid
|
||||
|
||||
return default_value if ary.empty?
|
||||
|
||||
property = Utils.to_s(property)
|
||||
|
||||
block.call(ary) do |item|
|
||||
if target_value.nil?
|
||||
item[property]
|
||||
@@ -989,7 +999,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def raise_property_error(property)
|
||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
||||
raise Liquid::ArgumentError, "cannot select the property '#{Utils.to_s(property)}'"
|
||||
end
|
||||
|
||||
def apply_operation(input, operand, operation)
|
||||
@@ -1061,7 +1071,10 @@ module Liquid
|
||||
end
|
||||
|
||||
def uniq(&block)
|
||||
to_a.uniq(&block)
|
||||
to_a.uniq do |item|
|
||||
item = Utils.to_liquid_value(item)
|
||||
block ? yield(item) : item
|
||||
end
|
||||
end
|
||||
|
||||
def compact
|
||||
|
||||
@@ -10,7 +10,7 @@ module Liquid
|
||||
# @liquid_description
|
||||
# Variables that are declared with `decrement` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
||||
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
||||
# [snippets](/themes/architecture#snippets) included in the file.
|
||||
# [snippets](/themes/architecture/snippets) included in the file.
|
||||
#
|
||||
# Similarly, variables that are created with `decrement` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
||||
# and [`capture`](/docs/api/liquid/tags/capture). However, `decrement` and [`increment`](/docs/api/liquid/tags/increment) share
|
||||
|
||||
@@ -13,12 +13,16 @@ module Liquid
|
||||
# Liquid code inside will be parsed but not executed. This facilitates
|
||||
# tooling support for features like code completion, linting, and inline
|
||||
# documentation.
|
||||
#
|
||||
# For detailed documentation syntax and examples, see the
|
||||
# [`LiquidDoc` reference](/docs/storefronts/themes/tools/liquid-doc).
|
||||
#
|
||||
# @liquid_syntax
|
||||
# {% doc %}
|
||||
# Renders a message.
|
||||
#
|
||||
# @param {string} foo - A foo value.
|
||||
# @param {string} [bar] - An optional bar value.
|
||||
# @param {string} foo - A string value.
|
||||
# @param {string} [bar] - An optional string value.
|
||||
#
|
||||
# @example
|
||||
# {% render 'message', foo: 'Hello', bar: 'World' %}
|
||||
|
||||
@@ -6,7 +6,7 @@ module Liquid
|
||||
# @liquid_category theme
|
||||
# @liquid_name include
|
||||
# @liquid_summary
|
||||
# Renders a [snippet](/themes/architecture#snippets).
|
||||
# Renders a [snippet](/themes/architecture/snippets).
|
||||
# @liquid_description
|
||||
# Inside the snippet, you can access and alter variables that are [created](/docs/api/liquid/tags/variable-tags) outside of the
|
||||
# snippet.
|
||||
|
||||
@@ -10,7 +10,7 @@ module Liquid
|
||||
# @liquid_description
|
||||
# Variables that are declared with `increment` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
|
||||
# or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
|
||||
# [snippets](/themes/architecture#snippets) included in the file.
|
||||
# [snippets](/themes/architecture/snippets) included in the file.
|
||||
#
|
||||
# Similarly, variables that are created with `increment` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
|
||||
# and [`capture`](/docs/api/liquid/tags/capture). However, `increment` and [`decrement`](/docs/api/liquid/tags/decrement) share
|
||||
|
||||
@@ -6,7 +6,7 @@ module Liquid
|
||||
# @liquid_category theme
|
||||
# @liquid_name render
|
||||
# @liquid_summary
|
||||
# Renders a [snippet](/themes/architecture#snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
||||
# Renders a [snippet](/themes/architecture/snippets) or [app block](/themes/architecture/sections/section-schema#render-app-blocks).
|
||||
# @liquid_description
|
||||
# Inside snippets and app blocks, you can't directly access variables that are [created](/docs/api/liquid/tags/variable-tags) outside
|
||||
# of the snippet or app block. However, you can [specify variables as parameters](/docs/api/liquid/tags/render#render-passing-variables-to-a-snippet)
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
VERSION = "5.8.1"
|
||||
VERSION = "5.8.5"
|
||||
end
|
||||
|
||||
@@ -560,12 +560,23 @@ class StandardFiltersTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_map_returns_empty_with_no_property
|
||||
def test_map_with_value_property
|
||||
array = [
|
||||
{ "handle" => "alpha", "value" => "A" },
|
||||
{ "handle" => "beta", "value" => "B" },
|
||||
{ "handle" => "gamma", "value" => "C" }
|
||||
]
|
||||
|
||||
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
|
||||
end
|
||||
|
||||
def test_map_returns_input_with_no_property
|
||||
foo = [
|
||||
[1],
|
||||
[2],
|
||||
[3],
|
||||
]
|
||||
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
@filters.map(foo, nil)
|
||||
end
|
||||
@@ -1033,6 +1044,22 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result(expected_output, template, { "array" => array })
|
||||
end
|
||||
|
||||
def test_where_with_empty_string_is_a_no_op
|
||||
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||
expected_output = "alpha beta gamma"
|
||||
template = "{{ array | where: '' | join: ' ' }}"
|
||||
|
||||
assert_template_result(expected_output, template, environment)
|
||||
end
|
||||
|
||||
def test_where_with_nil_is_a_no_op
|
||||
environment = { "array" => ["alpha", "beta", "gamma"] }
|
||||
expected_output = "alpha beta gamma"
|
||||
template = "{{ array | where: nil | join: ' ' }}"
|
||||
|
||||
assert_template_result(expected_output, template, environment)
|
||||
end
|
||||
|
||||
def test_where_with_value
|
||||
array = [
|
||||
{ "handle" => "alpha", "ok" => true },
|
||||
@@ -1268,6 +1295,33 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
|
||||
end
|
||||
|
||||
def test_sum_with_non_string_property
|
||||
input = [{ true => 1 }, { 1.0 => 0.2, 1 => -0.3 }, { 1..5 => 0.4 }]
|
||||
|
||||
assert_equal(1, @filters.sum(input, true))
|
||||
assert_equal(0.2, @filters.sum(input, 1.0))
|
||||
assert_equal(-0.3, @filters.sum(input, 1))
|
||||
assert_equal(0.4, @filters.sum(input, (1..5)))
|
||||
assert_equal(0, @filters.sum(input, nil))
|
||||
assert_equal(0, @filters.sum(input, ""))
|
||||
end
|
||||
|
||||
def test_uniq_with_to_liquid_value
|
||||
input = [StringDrop.new("foo"), StringDrop.new("bar"), "foo"]
|
||||
expected = [StringDrop.new("foo"), StringDrop.new("bar")]
|
||||
result = @filters.uniq(input)
|
||||
|
||||
assert_equal(expected, result)
|
||||
end
|
||||
|
||||
def test_uniq_with_to_liquid_value_pick_correct_classes
|
||||
input = ["foo", StringDrop.new("foo"), StringDrop.new("bar")]
|
||||
expected = [String, StringDrop]
|
||||
result = @filters.uniq(input).map(&:class)
|
||||
|
||||
assert_equal(expected, result)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_timezone(tz)
|
||||
|
||||
@@ -146,6 +146,35 @@ class BooleanDrop < Liquid::Drop
|
||||
end
|
||||
end
|
||||
|
||||
class StringDrop < Liquid::Drop
|
||||
include Comparable
|
||||
|
||||
def initialize(value)
|
||||
super()
|
||||
@value = value
|
||||
end
|
||||
|
||||
def to_liquid_value
|
||||
@value
|
||||
end
|
||||
|
||||
def to_s
|
||||
@value
|
||||
end
|
||||
|
||||
def to_str
|
||||
@value
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<StringDrop @value=#{@value.inspect}>"
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
to_liquid_value <=> Liquid::Utils.to_liquid_value(other)
|
||||
end
|
||||
end
|
||||
|
||||
class ErrorDrop < Liquid::Drop
|
||||
def standard_error
|
||||
raise Liquid::StandardError, 'standard error'
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
# 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
|
||||
@@ -141,78 +141,6 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user