summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/liquid-4.0.4/test/unit/condition_unit_test.rb
blob: b3b90e8835a198dc5b8d052b808532a439868ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'test_helper'

class ConditionUnitTest < Minitest::Test
  include Liquid

  def setup
    @context = Liquid::Context.new
  end

  def test_basic_condition
    assert_equal false, Condition.new(1, '==', 2).evaluate
    assert_equal true,  Condition.new(1, '==', 1).evaluate
  end

  def test_default_operators_evalute_true
    assert_evaluates_true 1, '==', 1
    assert_evaluates_true 1, '!=', 2
    assert_evaluates_true 1, '<>', 2
    assert_evaluates_true 1, '<', 2
    assert_evaluates_true 2, '>', 1
    assert_evaluates_true 1, '>=', 1
    assert_evaluates_true 2, '>=', 1
    assert_evaluates_true 1, '<=', 2
    assert_evaluates_true 1, '<=', 1
    # negative numbers
    assert_evaluates_true 1, '>', -1
    assert_evaluates_true -1, '<', 1
    assert_evaluates_true 1.0, '>', -1.0
    assert_evaluates_true -1.0, '<', 1.0
  end

  def test_default_operators_evalute_false
    assert_evaluates_false 1, '==', 2
    assert_evaluates_false 1, '!=', 1
    assert_evaluates_false 1, '<>', 1
    assert_evaluates_false 1, '<', 0
    assert_evaluates_false 2, '>', 4
    assert_evaluates_false 1, '>=', 3
    assert_evaluates_false 2, '>=', 4
    assert_evaluates_false 1, '<=', 0
    assert_evaluates_false 1, '<=', 0
  end

  def test_contains_works_on_strings
    assert_evaluates_true 'bob', 'contains', 'o'
    assert_evaluates_true 'bob', 'contains', 'b'
    assert_evaluates_true 'bob', 'contains', 'bo'
    assert_evaluates_true 'bob', 'contains', 'ob'
    assert_evaluates_true 'bob', 'contains', 'bob'

    assert_evaluates_false 'bob', 'contains', 'bob2'
    assert_evaluates_false 'bob', 'contains', 'a'
    assert_evaluates_false 'bob', 'contains', '---'
  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_hash_compare_backwards_compatibility
    assert_nil Condition.new({}, '>', 2).evaluate
    assert_nil Condition.new(2, '>', {}).evaluate
    assert_equal false, Condition.new({}, '==', 2).evaluate
    assert_equal true, Condition.new({ 'a' => 1 }, '==', { 'a' => 1 }).evaluate
    assert_equal true, Condition.new({ 'a' => 2 }, 'contains', 'a').evaluate
  end

  def test_contains_works_on_arrays
    @context = Liquid::Context.new
    @context['array'] = [1, 2, 3, 4, 5]
    array_expr = VariableLookup.new("array")

    assert_evaluates_false array_expr, 'contains', 0
    assert_evaluates_true array_expr, 'contains', 1
    assert_evaluates_true array_expr, 'contains', 2
    assert_evaluates_true array_expr, 'contains', 3
    assert_evaluates_true array_expr, 'contains', 4
    assert_evaluates_true array_expr, 'contains', 5
    assert_evaluates_false array_expr, 'contains', 6
    assert_evaluates_false array_expr, 'contains', "1"
  end

  def test_contains_returns_false_for_nil_operands
    @context = Liquid::Context.new
    assert_evaluates_false VariableLookup.new('not_assigned'), 'contains', '0'
    assert_evaluates_false 0, 'contains', VariableLookup.new('not_assigned')
  end

  def test_contains_return_false_on_wrong_data_type
    assert_evaluates_false 1, 'contains', 0
  end

  def test_contains_with_string_left_operand_coerces_right_operand_to_string
    assert_evaluates_true ' 1 ', 'contains', 1
    assert_evaluates_false ' 1 ', 'contains', 2
  end

  def test_or_condition
    condition = Condition.new(1, '==', 2)

    assert_equal false, condition.evaluate

    condition.or Condition.new(2, '==', 1)

    assert_equal false, condition.evaluate

    condition.or Condition.new(1, '==', 1)

    assert_equal true, condition.evaluate
  end

  def test_and_condition
    condition = Condition.new(1, '==', 1)

    assert_equal true, condition.evaluate

    condition.and Condition.new(2, '==', 2)

    assert_equal true, condition.evaluate

    condition.and Condition.new(2, '==', 1)

    assert_equal false, condition.evaluate
  end

  def test_should_allow_custom_proc_operator
    Condition.operators['starts_with'] = proc { |cond, left, right| left =~ %r{^#{right}} }

    assert_evaluates_true 'bob', 'starts_with', 'b'
    assert_evaluates_false 'bob', 'starts_with', 'o'
  ensure
    Condition.operators.delete 'starts_with'
  end

  def test_left_or_right_may_contain_operators
    @context = Liquid::Context.new
    @context['one'] = @context['another'] = "gnomeslab-and-or-liquid"

    assert_evaluates_true VariableLookup.new("one"), '==', VariableLookup.new("another")
  end

  private

  def assert_evaluates_true(left, op, right)
    assert Condition.new(left, op, right).evaluate(@context),
      "Evaluated false: #{left} #{op} #{right}"
  end

  def assert_evaluates_false(left, op, right)
    assert !Condition.new(left, op, right).evaluate(@context),
      "Evaluated true: #{left} #{op} #{right}"
  end

  def assert_evaluates_argument_error(left, op, right)
    assert_raises(Liquid::ArgumentError) do
      Condition.new(left, op, right).evaluate(@context)
    end
  end
end # ConditionTest