mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Don't raise if no variable found when using context.key?
Previously if you set `strict_variables` to `true` on the context using
`key?('key_name')` would raise a `Liquid::UndefinedVariable` error.
Raising this error makes sense if you're trying to access the variable
directly with something like `context['key_name']` but by using `key?`
you're safely checking if it exists first.
You should be able to enable `strict_variables` and use `key?` in
combination with each other to ensure code safety.
This commit is contained in:
@@ -184,7 +184,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def key?(key)
|
def key?(key)
|
||||||
self[key] != nil
|
find_variable(key, raise_on_not_found: false) != nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluate(object)
|
def evaluate(object)
|
||||||
|
|||||||
@@ -639,6 +639,21 @@ class ContextTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_key_lookup_will_raise_for_missing_keys_when_strict_variables_is_enabled
|
||||||
|
context = Context.new
|
||||||
|
context.strict_variables = true
|
||||||
|
assert_raises(Liquid::UndefinedVariable) do
|
||||||
|
context['unknown']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_has_key_will_not_raise_for_missing_keys_when_strict_variables_is_enabled
|
||||||
|
context = Context.new
|
||||||
|
context.strict_variables = true
|
||||||
|
refute(context.key?('unknown'))
|
||||||
|
assert_empty(context.errors)
|
||||||
|
end
|
||||||
|
|
||||||
def test_context_always_uses_static_registers
|
def test_context_always_uses_static_registers
|
||||||
registers = {
|
registers = {
|
||||||
my_register: :my_value,
|
my_register: :my_value,
|
||||||
|
|||||||
Reference in New Issue
Block a user