Do not raise when variable is defined but nil when using strict_variables

This commit is contained in:
Pascal Betz
2017-08-18 18:09:57 +02:00
committed by Justin Li
parent 59162f7a0e
commit a979b3ec95
2 changed files with 12 additions and 1 deletions

View File

@@ -171,7 +171,9 @@ module Liquid
if scope.nil?
@environments.each do |e|
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
unless variable.nil?
# When lookup returned a value OR there is no value but the lookup also did not raise
# then it is the value we are looking for.
if !variable.nil? || @strict_variables && raise_on_not_found
scope = e
break
end

View File

@@ -261,6 +261,15 @@ class TemplateTest < Minitest::Test
assert_equal 'Liquid error: undefined variable d', t.errors[2].message
end
def test_nil_value_does_not_raise
Liquid::Template.error_mode = :strict
t = Template.parse("some{{x}}thing")
result = t.render!({ 'x' => nil }, strict_variables: true)
assert_equal 0, t.errors.count
assert_equal 'something', result
end
def test_undefined_variables_raise
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")