mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 09:20:42 -07:00
Add support for logical expressions
This commit is contained in:
@@ -2,6 +2,25 @@
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class ExecutionSpy
|
||||
attr_reader :called
|
||||
attr_accessor :value
|
||||
|
||||
def initialize(value)
|
||||
@called = false
|
||||
@value = value
|
||||
end
|
||||
|
||||
def to_liquid_value
|
||||
@called = true
|
||||
@value
|
||||
end
|
||||
|
||||
def reset
|
||||
@called = false
|
||||
end
|
||||
end
|
||||
|
||||
class BinaryExpressionTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
@@ -13,6 +32,32 @@ class BinaryExpressionTest < Minitest::Test
|
||||
assert_eval(true, BinaryExpression.new("abcd", "contains", "a"))
|
||||
end
|
||||
|
||||
def test_logical_expression_short_circuiting
|
||||
spy = ExecutionSpy.new(true)
|
||||
|
||||
# false or spy should try spy
|
||||
assert_eval(true, BinaryExpression.new(false, 'or', spy))
|
||||
assert_equal(true, spy.called)
|
||||
|
||||
spy.reset
|
||||
|
||||
# true or spy should not call spy
|
||||
assert_eval(true, BinaryExpression.new(true, 'or', spy))
|
||||
assert_equal(false, spy.called)
|
||||
|
||||
spy.reset
|
||||
|
||||
# true and spy should try spy
|
||||
assert_eval(true, BinaryExpression.new(true, 'and', spy))
|
||||
assert_equal(true, spy.called)
|
||||
|
||||
spy.reset
|
||||
|
||||
# false and spy should not try spy
|
||||
assert_eval(false, BinaryExpression.new(false, 'and', spy))
|
||||
assert_equal(false, spy.called)
|
||||
end
|
||||
|
||||
def test_complex_evaluation
|
||||
# 1 > 2 == 2 > 3
|
||||
assert_eval(true, BinaryExpression.new(
|
||||
|
||||
Reference in New Issue
Block a user