mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Raise Liquid::ArugmentError when condition has wrong usage
Condition now raises ::ArgumentError when built wrongly. This patch make it raise Liquid::ArgumentError instead to indicate a liquid markup error instead of ruby error.
This commit is contained in:
@@ -94,12 +94,16 @@ module Liquid
|
|||||||
|
|
||||||
left, right = context[left], context[right]
|
left, right = context[left], context[right]
|
||||||
|
|
||||||
operation = self.class.operators[op] || raise(ArgumentError.new("Unknown operator #{op}"))
|
operation = self.class.operators[op] || raise(Liquid::ArgumentError.new("Unknown operator #{op}"))
|
||||||
|
|
||||||
if operation.respond_to?(:call)
|
if operation.respond_to?(:call)
|
||||||
operation.call(self, left, right)
|
operation.call(self, left, right)
|
||||||
elsif left.respond_to?(operation) and right.respond_to?(operation)
|
elsif left.respond_to?(operation) and right.respond_to?(operation)
|
||||||
left.send(operation, right)
|
begin
|
||||||
|
left.send(operation, right)
|
||||||
|
rescue ::ArgumentError => e
|
||||||
|
raise Liquid::ArgumentError.new(e.message)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -49,6 +49,17 @@ class ConditionUnitTest < Test::Unit::TestCase
|
|||||||
assert_evalutes_false "'bob'", 'contains', "'---'"
|
assert_evalutes_false "'bob'", 'contains', "'---'"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_invalid_comparation_operator
|
||||||
|
assert_evaluates_argument_error "1", '~~', '0'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_comparation_of_int_and_str
|
||||||
|
assert_evaluates_argument_error "'1'", '>', '0'
|
||||||
|
assert_evaluates_argument_error "'1'", '<', '0'
|
||||||
|
assert_evaluates_argument_error "'1'", '>=', '0'
|
||||||
|
assert_evaluates_argument_error "'1'", '<=', '0'
|
||||||
|
end
|
||||||
|
|
||||||
def test_contains_works_on_arrays
|
def test_contains_works_on_arrays
|
||||||
@context = Liquid::Context.new
|
@context = Liquid::Context.new
|
||||||
@context['array'] = [1,2,3,4,5]
|
@context['array'] = [1,2,3,4,5]
|
||||||
@@ -124,4 +135,11 @@ class ConditionUnitTest < Test::Unit::TestCase
|
|||||||
assert !Condition.new(left, op, right).evaluate(@context || Liquid::Context.new),
|
assert !Condition.new(left, op, right).evaluate(@context || Liquid::Context.new),
|
||||||
"Evaluated true: #{left} #{op} #{right}"
|
"Evaluated true: #{left} #{op} #{right}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def assert_evaluates_argument_error(left, op, right)
|
||||||
|
assert_raises(Liquid::ArgumentError) do
|
||||||
|
Condition.new(left, op, right).evaluate(@context || Liquid::Context.new)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end # ConditionTest
|
end # ConditionTest
|
||||||
|
|||||||
Reference in New Issue
Block a user