mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-30 15:45:12 -07:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3282008ba5 | ||
|
|
2b75bfaff4 | ||
|
|
87bc6e7cfa | ||
|
|
7f122aeed2 | ||
|
|
aa1640035f | ||
|
|
f5d6a36574 | ||
|
|
c5711c095f | ||
|
|
284f5fb647 | ||
|
|
21432928d0 | ||
|
|
1783c0c084 |
@@ -25,8 +25,6 @@ group :development do
|
||||
end
|
||||
|
||||
group :test do
|
||||
gem 'ruby-lsp'
|
||||
gem 'debug'
|
||||
gem 'rubocop', '~> 1.61.0'
|
||||
gem 'rubocop-shopify', '~> 2.12.0', require: false
|
||||
gem 'rubocop-performance', require: false
|
||||
|
||||
@@ -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'
|
||||
|
||||
+2
-10
@@ -52,10 +52,6 @@ module Liquid
|
||||
@@method_literals[markup] || parse_context.parse_expression(markup)
|
||||
end
|
||||
|
||||
def self.parse(markup, ss, cache)
|
||||
@@method_literals[markup] || Expression.parse(markup, ss, cache)
|
||||
end
|
||||
|
||||
attr_reader :attachment, :child_condition
|
||||
attr_accessor :left, :operator, :right
|
||||
|
||||
@@ -116,15 +112,11 @@ module Liquid
|
||||
private
|
||||
|
||||
def equal_variables(left, right)
|
||||
if left.is_a?(MethodLiteral) && right.is_a?(MethodLiteral)
|
||||
return left.to_s == right.to_s
|
||||
end
|
||||
|
||||
if left.is_a?(MethodLiteral)
|
||||
if right.respond_to?(left.method_name)
|
||||
return right.send(left.method_name)
|
||||
else
|
||||
return left.to_s == right
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -132,7 +124,7 @@ module Liquid
|
||||
if left.respond_to?(right.method_name)
|
||||
return left.send(right.method_name)
|
||||
else
|
||||
return right.to_s == left
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ module Liquid
|
||||
RANGES_REGEX = /\A\(\s*(?>(\S+)\s*\.\.)\s*(\S+)\s*\)\z/
|
||||
INTEGER_REGEX = /\A(-?\d+)\z/
|
||||
FLOAT_REGEX = /\A(-?\d+)\.\d+\z/
|
||||
QUOTED_STRING = /\A#{QuotedString}\z/
|
||||
|
||||
class << self
|
||||
def parse(markup, ss = StringScanner.new(""), cache = nil)
|
||||
@@ -34,9 +33,12 @@ module Liquid
|
||||
|
||||
markup = markup.strip # markup can be a frozen string
|
||||
|
||||
return markup[1..-2] if QUOTED_STRING.match?(markup)
|
||||
|
||||
return LITERALS[markup] if LITERALS.key?(markup)
|
||||
if (markup.start_with?('"') && markup.end_with?('"')) ||
|
||||
(markup.start_with?("'") && markup.end_with?("'"))
|
||||
return markup[1..-2]
|
||||
elsif LITERALS.key?(markup)
|
||||
return LITERALS[markup]
|
||||
end
|
||||
|
||||
# Cache only exists during parsing
|
||||
if cache
|
||||
@@ -49,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,31 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class Expression
|
||||
class ComparisonExpression
|
||||
# We can improve the resiliency of lax parsing by not expecting whitespace
|
||||
# surrounding the operator (ie \s+ => \s*).
|
||||
# However this is not in parity with existing lax parsing behavior.
|
||||
COMPARISON_REGEX = /\A\s*(.+?)\s+(==|!=|<>|<=|>=|<|>|contains)\s+(.+)\s*\z/
|
||||
|
||||
class << self
|
||||
def comparison?(markup)
|
||||
markup.match(COMPARISON_REGEX)
|
||||
end
|
||||
|
||||
def parse(markup, ss, cache)
|
||||
match = comparison?(markup)
|
||||
|
||||
if match
|
||||
left = Condition.parse(match[1].strip, ss, cache)
|
||||
operator = match[2].strip
|
||||
right = Condition.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,59 +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.match(LOGICAL_REGEX)
|
||||
end
|
||||
|
||||
def boolean_operator?(markup)
|
||||
BOOLEAN_OPERATORS.include?(markup)
|
||||
end
|
||||
|
||||
def parse(markup, ss, cache)
|
||||
expressions = markup.scan(EXPRESSIONS_AND_OPERATORS)
|
||||
|
||||
expression = expressions.pop
|
||||
condition = parse_condition(expression, ss, cache)
|
||||
|
||||
until expressions.empty?
|
||||
operator = expressions.pop.to_s.strip
|
||||
|
||||
next unless boolean_operator?(operator)
|
||||
|
||||
expression = expressions.pop.to_s.strip
|
||||
new_condition = parse_condition(expression, ss, cache)
|
||||
|
||||
case operator
|
||||
when 'and' then new_condition.and(condition)
|
||||
when 'or' then new_condition.or(condition)
|
||||
end
|
||||
|
||||
condition = new_condition
|
||||
end
|
||||
|
||||
condition
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_condition(expr, ss, cache)
|
||||
return ComparisonExpression.parse(expr, ss, cache) if comparison?(expr)
|
||||
return LogicalExpression.parse(expr, ss, cache) if logical?(expr)
|
||||
|
||||
Condition.new(Expression.parse(expr, ss, cache), nil, nil)
|
||||
end
|
||||
|
||||
def comparison?(...)
|
||||
ComparisonExpression.comparison?(...)
|
||||
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
|
||||
@@ -387,6 +386,7 @@ module Liquid
|
||||
end
|
||||
elsif ary.all? { |el| el.respond_to?(:[]) }
|
||||
begin
|
||||
property = Utils.to_s(property)
|
||||
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
|
||||
rescue TypeError
|
||||
raise_property_error(property)
|
||||
@@ -416,6 +416,7 @@ module Liquid
|
||||
end
|
||||
elsif ary.all? { |el| el.respond_to?(:[]) }
|
||||
begin
|
||||
property = Utils.to_s(property)
|
||||
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
|
||||
rescue TypeError
|
||||
raise_property_error(property)
|
||||
@@ -503,6 +504,7 @@ module Liquid
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
else
|
||||
property = Utils.to_s(property)
|
||||
ary.uniq do |item|
|
||||
item[property]
|
||||
rescue TypeError
|
||||
@@ -534,6 +536,11 @@ module Liquid
|
||||
# @liquid_syntax array | map: string
|
||||
# @liquid_return [array[untyped]]
|
||||
def map(input, property)
|
||||
property = Utils.to_s(property)
|
||||
|
||||
# Return the input array if property is empty (no-op)
|
||||
return InputIterator.new(input, context).to_a if property.empty?
|
||||
|
||||
InputIterator.new(input, context).map do |e|
|
||||
e = e.call if e.is_a?(Proc)
|
||||
|
||||
@@ -563,6 +570,7 @@ module Liquid
|
||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||
[]
|
||||
else
|
||||
property = Liquid::Utils.to_s(property)
|
||||
ary.reject do |item|
|
||||
item[property].nil?
|
||||
rescue TypeError
|
||||
@@ -712,7 +720,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 +758,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?
|
||||
@@ -943,6 +960,8 @@ module Liquid
|
||||
# @liquid_syntax array | sum
|
||||
# @liquid_return [number]
|
||||
def sum(input, property = nil)
|
||||
property = property.nil? ? nil : Utils.to_s(property)
|
||||
|
||||
ary = InputIterator.new(input, context)
|
||||
return 0 if ary.empty?
|
||||
|
||||
@@ -971,9 +990,10 @@ module Liquid
|
||||
|
||||
def filter_array(input, property, target_value, default_value = [], &block)
|
||||
ary = InputIterator.new(input, context)
|
||||
|
||||
return default_value if ary.empty?
|
||||
|
||||
property = Utils.to_s(property)
|
||||
|
||||
block.call(ary) do |item|
|
||||
if target_value.nil?
|
||||
item[property]
|
||||
@@ -998,7 +1018,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def nil_safe_compare(a, b)
|
||||
result = a <=> b
|
||||
result = Utils.to_liquid_value(a) <=> Utils.to_liquid_value(b)
|
||||
|
||||
if result
|
||||
result
|
||||
|
||||
@@ -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,6 +13,10 @@ 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.
|
||||
|
||||
@@ -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.3"
|
||||
end
|
||||
|
||||
@@ -560,15 +560,47 @@ class StandardFiltersTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_map_returns_empty_with_no_property
|
||||
foo = [
|
||||
def test_map_with_nil_property
|
||||
array = [
|
||||
{ "handle" => "alpha", "value" => "A" },
|
||||
{ "handle" => "beta", "value" => "B" },
|
||||
{ "handle" => "gamma", "value" => "C" }
|
||||
]
|
||||
|
||||
assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array })
|
||||
end
|
||||
|
||||
def test_map_with_empty_string_property
|
||||
array = [
|
||||
{ "handle" => "alpha", "value" => "A" },
|
||||
{ "handle" => "beta", "value" => "B" },
|
||||
{ "handle" => "gamma", "value" => "C" }
|
||||
]
|
||||
|
||||
assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array })
|
||||
end
|
||||
|
||||
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
|
||||
input = [
|
||||
[1],
|
||||
[2],
|
||||
[3],
|
||||
]
|
||||
assert_raises(Liquid::ArgumentError) do
|
||||
@filters.map(foo, nil)
|
||||
end
|
||||
result = @filters.map(input, nil)
|
||||
assert_equal(input.flatten, result)
|
||||
|
||||
result = @filters.map(input, '')
|
||||
assert_equal(input.flatten, result)
|
||||
end
|
||||
|
||||
def test_sort_works_on_enumerables
|
||||
@@ -1033,6 +1065,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 },
|
||||
@@ -1061,6 +1109,19 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result(expected_output, template, { "array" => array })
|
||||
end
|
||||
|
||||
def test_where_with_non_string_property
|
||||
array = [
|
||||
{ "handle" => "alpha", "{}" => true },
|
||||
{ "handle" => "beta", "{}" => false },
|
||||
{ "handle" => "gamma", "{}" => false },
|
||||
{ "handle" => "delta", "{}" => true },
|
||||
]
|
||||
template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}"
|
||||
expected_output = "alpha delta"
|
||||
|
||||
assert_template_result(expected_output, template, { "array" => array, "some_property" => {} })
|
||||
end
|
||||
|
||||
def test_where_string_keys
|
||||
input = [
|
||||
"alpha", "beta", "gamma", "delta"
|
||||
@@ -1268,6 +1329,17 @@ 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
|
||||
|
||||
private
|
||||
|
||||
def with_timezone(tz)
|
||||
|
||||
@@ -1,546 +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_with_integer_literals
|
||||
assert_expression("1", "1")
|
||||
assert_expression("1 == 1", "true")
|
||||
assert_expression("1 != 1", "false")
|
||||
assert_expression("1 == 2", "false")
|
||||
assert_expression("1 != 2", "true")
|
||||
end
|
||||
|
||||
def test_equality_operators_with_stirng_literals
|
||||
assert_expression("'hello'", "hello")
|
||||
assert_expression("'hello' == 'hello'", "true")
|
||||
assert_expression("'hello' != 'hello'", "false")
|
||||
assert_expression("'hello' == 'world'", "false")
|
||||
assert_expression("'hello' != 'world'", "true")
|
||||
end
|
||||
|
||||
def test_equality_operators_with_float_literals
|
||||
assert_expression("1.5", "1.5")
|
||||
assert_expression("1.5 == 1.5", "true")
|
||||
assert_expression("1.5 != 1.5", "false")
|
||||
assert_expression("1.5 == 2.5", "false")
|
||||
assert_expression("1.5 != 2.5", "true")
|
||||
end
|
||||
|
||||
def test_equality_operators_with_nil_literals
|
||||
assert_expression("nil", "")
|
||||
assert_expression("nil == nil", "true")
|
||||
assert_expression("nil != nil", "false")
|
||||
assert_expression("null == nil", "true")
|
||||
assert_expression("null != nil", "false")
|
||||
end
|
||||
|
||||
def test_equality_operators_with_boolean_literals
|
||||
assert_expression("true", "true")
|
||||
assert_expression("false", "false")
|
||||
assert_expression("true == true", "true")
|
||||
assert_expression("true != true", "false")
|
||||
assert_expression("false == false", "true")
|
||||
assert_expression("false != false", "false")
|
||||
assert_expression("true == false", "false")
|
||||
assert_expression("true != false", "true")
|
||||
end
|
||||
|
||||
def test_equality_operators_with_empty_literals
|
||||
assert_expression("empty", "")
|
||||
assert_expression("empty == ''", "true")
|
||||
assert_expression("empty == empty", "true")
|
||||
assert_expression("empty != empty", "false")
|
||||
assert_expression("blank == blank", "true")
|
||||
assert_expression("blank != blank", "false")
|
||||
assert_expression("empty == blank", "true")
|
||||
assert_expression("empty != blank", "false")
|
||||
end
|
||||
|
||||
def test_nil_renders_as_empty_string
|
||||
# No parity needed here. This is to ensure expressions rendered with {{ }}
|
||||
# will still render as an empty string to preserve pre-existing behavior.
|
||||
assert_expression("nil", "")
|
||||
assert_expression("x", "", { "x" => nil })
|
||||
assert_parity_scenario(:expression, "hello {{ x }}", "hello ", { "x" => nil })
|
||||
end
|
||||
|
||||
def test_nil_comparison_with_blank
|
||||
assert_parity("nil_value == blank", "false")
|
||||
assert_parity("nil_value != blank", "true")
|
||||
assert_parity("undefined != blank", "true")
|
||||
assert_parity("undefined == blank", "false")
|
||||
end
|
||||
|
||||
def test_if_with_variables
|
||||
assert_parity("value", "true", { "value" => true })
|
||||
assert_parity("value", "false", { "value" => false })
|
||||
end
|
||||
|
||||
def test_nil_variable_in_and_expression
|
||||
assert_condition("x and true", "false", { "x" => nil })
|
||||
assert_condition("true and x", "false", { "x" => nil })
|
||||
|
||||
assert_expression("x and true", "", { "x" => nil })
|
||||
assert_expression("true and x", "", { "x" => nil })
|
||||
end
|
||||
|
||||
def test_boolean_variable_in_and_expression
|
||||
assert_parity("true and x", "false", { "x" => false })
|
||||
assert_parity("x and true", "false", { "x" => false })
|
||||
|
||||
assert_parity("true and x", "true", { "x" => true })
|
||||
assert_parity("x and true", "true", { "x" => true })
|
||||
|
||||
assert_parity("true or x", "true", { "x" => false })
|
||||
assert_parity("x or true", "true", { "x" => false })
|
||||
|
||||
assert_parity("true or x", "true", { "x" => true })
|
||||
assert_parity("x or true", "true", { "x" => true })
|
||||
end
|
||||
|
||||
def test_multi_variable_boolean_nil_and_expression
|
||||
assert_condition("x and y", "false", { "x" => nil, "y" => true })
|
||||
assert_condition("y and x", "false", { "x" => true, "y" => nil })
|
||||
|
||||
assert_expression("x and y", "", { "x" => nil, "y" => true })
|
||||
assert_expression("y and x", "", { "x" => true, "y" => nil })
|
||||
end
|
||||
|
||||
def test_multi_truthy_variables_and_expressions
|
||||
assert_condition("x or y", "true", { "x" => nil, "y" => "hello" })
|
||||
assert_condition("y or x", "true", { "x" => "hello", "y" => nil })
|
||||
|
||||
assert_expression("x or y", "hello", { "x" => nil, "y" => "hello" })
|
||||
assert_expression("y or x", "hello", { "x" => "hello", "y" => nil })
|
||||
end
|
||||
|
||||
def test_multi_variable_boolean_nil_or_expression
|
||||
assert_parity("x or y", "true", { "x" => nil, "y" => true })
|
||||
assert_parity("y or x", "true", { "x" => true, "y" => nil })
|
||||
end
|
||||
|
||||
def test_links_not_blank_with_drop_returns_true_for_all_cases
|
||||
link = LinkDrop.new(
|
||||
levels: 0,
|
||||
links: [
|
||||
LinkDrop.new(levels: 1, links: [], title: "About", type: "page_link", url: "/pages/about"),
|
||||
LinkDrop.new(levels: 1, links: [], title: "Contact", type: "page_link", url: "/pages/contact"),
|
||||
],
|
||||
title: "Main Menu",
|
||||
type: "menu",
|
||||
url: nil,
|
||||
)
|
||||
|
||||
template = <<~LIQUID
|
||||
{%- if link.links != blank -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}
|
||||
LIQUID
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link.tap { |l| l.links = [] } })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link.tap { |l| l.links = nil } })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => LinkDrop.new })
|
||||
assert_equal("true", act_output)
|
||||
end
|
||||
|
||||
def test_links_truthy_with_drop_returns_false_for_nil_and_empty_drop
|
||||
link = LinkDrop.new(
|
||||
levels: 0,
|
||||
links: [
|
||||
LinkDrop.new(levels: 1, links: [], title: "About", type: "page_link", url: "/pages/about"),
|
||||
LinkDrop.new(levels: 1, links: [], title: "Contact", type: "page_link", url: "/pages/contact"),
|
||||
],
|
||||
title: "Main Menu",
|
||||
type: "menu",
|
||||
url: nil,
|
||||
)
|
||||
|
||||
template = <<~LIQUID
|
||||
{%- if link.links -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}
|
||||
LIQUID
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link.tap { |l| l.links = [] } })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link.tap { |l| l.links = nil } })
|
||||
assert_equal("false", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => {} })
|
||||
assert_equal("false", act_output)
|
||||
end
|
||||
|
||||
def test_links_not_blank_with_hash_returns_true_for_all_cases
|
||||
link = {
|
||||
"levels" => 0,
|
||||
"links" => [
|
||||
{
|
||||
"levels" => 1,
|
||||
"links" => [],
|
||||
"title" => { "text" => "About" },
|
||||
"type" => "page_link",
|
||||
"url" => "/pages/about",
|
||||
},
|
||||
{
|
||||
"levels" => 1,
|
||||
"links" => [],
|
||||
"title" => { "text" => "Contact" },
|
||||
"type" => "page_link",
|
||||
"url" => "/pages/contact",
|
||||
},
|
||||
],
|
||||
"title" => { "text" => "Main Menu" },
|
||||
"type" => "menu",
|
||||
"url" => nil,
|
||||
}
|
||||
|
||||
template = <<~LIQUID
|
||||
{%- if link.links != blank -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}
|
||||
LIQUID
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => { **link, "links" => [] } })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => { **link, "links" => nil } })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => {} })
|
||||
assert_equal("true", act_output)
|
||||
end
|
||||
|
||||
def test_links_truthy_with_hash_returns_false_for_nil_and_empty_hash
|
||||
link = {
|
||||
"levels" => 0,
|
||||
"links" => [
|
||||
{
|
||||
"levels" => 1,
|
||||
"links" => [],
|
||||
"title" => { "text" => "About" },
|
||||
"type" => "page_link",
|
||||
"url" => "/pages/about",
|
||||
},
|
||||
{
|
||||
"levels" => 1,
|
||||
"links" => [],
|
||||
"title" => { "text" => "Contact" },
|
||||
"type" => "page_link",
|
||||
"url" => "/pages/contact",
|
||||
},
|
||||
],
|
||||
"title" => { "text" => "Main Menu" },
|
||||
"type" => "menu",
|
||||
"url" => nil,
|
||||
}
|
||||
|
||||
template = <<~LIQUID
|
||||
{%- if link.links -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}
|
||||
LIQUID
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => link })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => { **link, "links" => [] } })
|
||||
assert_equal("true", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => { **link, "links" => nil } })
|
||||
assert_equal("false", act_output)
|
||||
|
||||
act_output = Liquid::Template.parse(template).render({ "link" => {} })
|
||||
assert_equal("false", act_output)
|
||||
end
|
||||
|
||||
def test_conditions_with_boolean_operators_without_whitespace_around_operator
|
||||
template = <<~LIQUID
|
||||
<option variant_id="{{ variant.id }}" {% if current_variant.id==variant.id %}selected{%- endif -%}>{{ variant.title }}</option>
|
||||
LIQUID
|
||||
|
||||
context = {
|
||||
"variant" => {
|
||||
"id" => 420,
|
||||
"title" => "Default Title",
|
||||
},
|
||||
"current_variant" => {
|
||||
"id" => 420,
|
||||
},
|
||||
}
|
||||
|
||||
# Expected output
|
||||
# Note: Ideally we would like the whitespace around the boolean operator to be optional.
|
||||
# So the more correct expected output would be:
|
||||
#
|
||||
# <option variant_id="420" selected>Default Title</option>
|
||||
#
|
||||
# However, the existing behaviour in liquid-ruby is that the whitespace is required around the boolean operator.
|
||||
expected_lax_output = <<~HTML
|
||||
<option variant_id="420" >Default Title</option>
|
||||
HTML
|
||||
|
||||
expected_strict_output = <<~HTML
|
||||
<option variant_id="420" selected>Default Title</option>
|
||||
HTML
|
||||
|
||||
# This bugged output only happens in lax mode.
|
||||
assert_with_lax_parsing(template, expected_lax_output, context)
|
||||
|
||||
# Default test parsing mode (strict) works as properly expected
|
||||
assert_equal(expected_strict_output.delete("\n"), actual_strict_output.delete("\n"))
|
||||
end
|
||||
|
||||
# TESTING INCORRECT BEHAVIOUR OF LIQUID-RUBY
|
||||
# If liquid-vm fails this test, we should change it.
|
||||
def test_boolean_conditional_with_json_filter
|
||||
# Define the Liquid template to test
|
||||
template = <<~LIQUID
|
||||
{{ template.name == 'index' | json }}
|
||||
LIQUID
|
||||
|
||||
# Define the context for the template where the template name is 'index'
|
||||
context = {
|
||||
"template" => {
|
||||
"name" => "product",
|
||||
},
|
||||
}
|
||||
|
||||
# Expected output
|
||||
# Note: I dont know what is the correct output here but this is the liquid-ruby 'main' output.
|
||||
#
|
||||
# It feels incorrect but I dont know whats better
|
||||
expected_output = "product"
|
||||
|
||||
# Render the template with the context
|
||||
actual_parsed_template = Liquid::Template.parse(template)
|
||||
actual_output = actual_parsed_template.render(context)
|
||||
|
||||
# Assert that the actual output matches the expected output
|
||||
assert_equal(expected_output, actual_output.strip)
|
||||
end
|
||||
|
||||
# TESTING INCORRECT BEHAVIOUR OF LIQUID-RUBY
|
||||
# If liquid-vm fails this test, we should change it.
|
||||
def test_chained_conditional_with_object_contains
|
||||
# Define the Liquid template to test
|
||||
template = <<~LIQUID
|
||||
{{ settings.prefilter_status and template contains 'collection' }}
|
||||
LIQUID
|
||||
|
||||
# Test with context containing 'collection'
|
||||
context_with_collection = {
|
||||
"template" => {
|
||||
"name" => "collection",
|
||||
},
|
||||
"settings" => {
|
||||
"prefilter_status" => true,
|
||||
},
|
||||
}
|
||||
# NOTE: This is a bug that liquid-ruby `main` output returns the first value.
|
||||
assert_with_lax_parsing(template, "true", context_with_collection)
|
||||
|
||||
# Test with context not containing 'collection'
|
||||
context_without_collection = {
|
||||
"template" => {
|
||||
"name" => "not-collection",
|
||||
},
|
||||
"settings" => {
|
||||
"prefilter_status" => true,
|
||||
},
|
||||
}
|
||||
# NOTE: This is a bug that liquid-ruby `main` output returns the first value.
|
||||
assert_with_lax_parsing(template, "true", context_without_collection)
|
||||
end
|
||||
|
||||
# TESTING INCORRECT BEHAVIOUR OF LIQUID-RUBY
|
||||
# If liquid-vm fails this test, we should change it.
|
||||
def test_assign_boolean_expression_to_variable
|
||||
template = <<~LIQUID
|
||||
{%- liquid
|
||||
assign is_preview_mode = content_for_header contains "foo" or content_for_header contains "bar"
|
||||
echo is_preview_mode
|
||||
-%}
|
||||
LIQUID
|
||||
|
||||
context = { "content_for_header" => "Some content" }
|
||||
|
||||
# Expected output
|
||||
# This value should be "false" but it is the value of the variable from the failed expression.
|
||||
assert_template_result("Some content", template, context)
|
||||
|
||||
# This following validation should only be supported with our changes. It is the short-hand for the above template.
|
||||
# The validation for it is the expected correct output.
|
||||
template = Liquid::Template.parse("{% assign is_preview_mode = content_for_header contains 'foo' or content_for_header contains 'bar' %}{{ is_preview_mode }}")
|
||||
assert_equal("false", template.render(context))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_with_lax_parsing(template, expected_output, context = {})
|
||||
prev_error_mode = Liquid::Environment.default.error_mode
|
||||
Liquid::Environment.default.error_mode = :lax
|
||||
|
||||
begin
|
||||
actual_output = Liquid::Template.parse(template).render(context)
|
||||
rescue StandardError => e
|
||||
actual_output = e.message
|
||||
ensure
|
||||
Liquid::Environment.default.error_mode = prev_error_mode
|
||||
end
|
||||
|
||||
assert_equal(expected_output.strip, actual_output.strip)
|
||||
end
|
||||
|
||||
def assert_parity(liquid_expression, expected_result, args = {})
|
||||
assert_condition(liquid_expression, expected_result, args)
|
||||
assert_expression(liquid_expression, expected_result, args)
|
||||
end
|
||||
|
||||
def assert_expression(liquid_expression, expected_result, args = {})
|
||||
assert_parity_scenario(:expression, "{{ #{liquid_expression} }}", expected_result, args)
|
||||
end
|
||||
|
||||
def assert_condition(liquid_condition, expected_result, args = {})
|
||||
assert_parity_scenario(:condition, "{% if #{liquid_condition} %}true{% else %}false{% endif %}", 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
|
||||
|
||||
class LinkDrop < Liquid::Drop
|
||||
attr_accessor :levels, :links, :title, :type, :url
|
||||
|
||||
def initialize(levels: nil, links: nil, title: nil, type: nil, url: nil)
|
||||
super()
|
||||
|
||||
@levels = levels
|
||||
@links = links
|
||||
@title = title
|
||||
@type = type
|
||||
@url = url
|
||||
end
|
||||
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