Avoid an exception from checking if a string contains a non-string.

This commit is contained in:
Dylan Thacker-Smith
2015-06-03 02:21:51 -04:00
parent ea29f8b4b8
commit 8a2947865b
2 changed files with 11 additions and 1 deletions

View File

@@ -16,7 +16,12 @@ module Liquid
'>='.freeze => :>=,
'<='.freeze => :<=,
'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
}

View File

@@ -85,6 +85,11 @@ class ConditionUnitTest < Minitest::Test
assert_evalutes_false 1, 'contains', 0
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
condition = Condition.new(1, '==', 2)