mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 07:05:12 -07:00
Merge pull request #586 from Shopify/string-contains-non-string
Avoid an exception from checking if a string contains a non-string.
This commit is contained in:
@@ -16,7 +16,12 @@ module Liquid
|
|||||||
'>='.freeze => :>=,
|
'>='.freeze => :>=,
|
||||||
'<='.freeze => :<=,
|
'<='.freeze => :<=,
|
||||||
'contains'.freeze => lambda do |cond, left, right|
|
'contains'.freeze => lambda do |cond, left, right|
|
||||||
left && right && left.respond_to?(:include?) ? left.include?(right) : false
|
if left && right && left.respond_to?(:include?)
|
||||||
|
right = right.to_s if left.is_a?(String)
|
||||||
|
left.include?(right)
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,11 @@ class ConditionUnitTest < Minitest::Test
|
|||||||
assert_evalutes_false 1, 'contains', 0
|
assert_evalutes_false 1, 'contains', 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_contains_with_string_left_operand_coerces_right_operand_to_string
|
||||||
|
assert_evalutes_true ' 1 ', 'contains', 1
|
||||||
|
assert_evalutes_false ' 1 ', 'contains', 2
|
||||||
|
end
|
||||||
|
|
||||||
def test_or_condition
|
def test_or_condition
|
||||||
condition = Condition.new(1, '==', 2)
|
condition = Condition.new(1, '==', 2)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user