File: variable_unit_test.rb

package info (click to toggle)
ruby-liquid 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,444 kB
  • sloc: ruby: 14,571; makefile: 6
file content (216 lines) | stat: -rw-r--r-- 8,001 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# frozen_string_literal: true

require 'test_helper'

class VariableUnitTest < Minitest::Test
  include Liquid

  def test_variable
    var = create_variable('hello')
    assert_equal(VariableLookup.new('hello'), var.name)
  end

  def test_filters
    var = create_variable('hello | textileze')
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['textileze', []]], var.filters)

    var = create_variable('hello | textileze | paragraph')
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['textileze', []], ['paragraph', []]], var.filters)

    var = create_variable(%( hello | strftime: '%Y'))
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['strftime', ['%Y']]], var.filters)

    var = create_variable(%( 'typo' | link_to: 'Typo', true ))
    assert_equal('typo', var.name)
    assert_equal([['link_to', ['Typo', true]]], var.filters)

    var = create_variable(%( 'typo' | link_to: 'Typo', false ))
    assert_equal('typo', var.name)
    assert_equal([['link_to', ['Typo', false]]], var.filters)

    var = create_variable(%( 'foo' | repeat: 3 ))
    assert_equal('foo', var.name)
    assert_equal([['repeat', [3]]], var.filters)

    var = create_variable(%( 'foo' | repeat: 3, 3 ))
    assert_equal('foo', var.name)
    assert_equal([['repeat', [3, 3]]], var.filters)

    var = create_variable(%( 'foo' | repeat: 3, 3, 3 ))
    assert_equal('foo', var.name)
    assert_equal([['repeat', [3, 3, 3]]], var.filters)

    var = create_variable(%( hello | strftime: '%Y, okay?'))
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['strftime', ['%Y, okay?']]], var.filters)

    var = create_variable(%( hello | things: "%Y, okay?", 'the other one'))
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['things', ['%Y, okay?', 'the other one']]], var.filters)
  end

  def test_filter_with_date_parameter
    var = create_variable(%( '2006-06-06' | date: "%m/%d/%Y"))
    assert_equal('2006-06-06', var.name)
    assert_equal([['date', ['%m/%d/%Y']]], var.filters)
  end

  def test_filters_without_whitespace
    var = create_variable('hello | textileze | paragraph')
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['textileze', []], ['paragraph', []]], var.filters)

    var = create_variable('hello|textileze|paragraph')
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['textileze', []], ['paragraph', []]], var.filters)

    var = create_variable("hello|replace:'foo','bar'|textileze")
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['replace', ['foo', 'bar']], ['textileze', []]], var.filters)
  end

  def test_symbol
    var = create_variable("http://disney.com/logo.gif | image: 'med' ", error_mode: :lax)
    assert_equal(VariableLookup.new('http://disney.com/logo.gif'), var.name)
    assert_equal([['image', ['med']]], var.filters)
  end

  def test_string_to_filter
    var = create_variable("'http://disney.com/logo.gif' | image: 'med' ")
    assert_equal('http://disney.com/logo.gif', var.name)
    assert_equal([['image', ['med']]], var.filters)
  end

  def test_string_single_quoted
    var = create_variable(%( "hello" ))
    assert_equal('hello', var.name)
  end

  def test_string_double_quoted
    var = create_variable(%( 'hello' ))
    assert_equal('hello', var.name)
  end

  def test_integer
    var = create_variable(%( 1000 ))
    assert_equal(1000, var.name)
  end

  def test_float
    var = create_variable(%( 1000.01 ))
    assert_equal(1000.01, var.name)
  end

  def test_dashes
    assert_equal(VariableLookup.new('foo-bar'), create_variable('foo-bar').name)
    assert_equal(VariableLookup.new('foo-bar-2'), create_variable('foo-bar-2').name)

    with_error_modes(:strict) do
      assert_raises(Liquid::SyntaxError) { create_variable('foo - bar') }
      assert_raises(Liquid::SyntaxError) { create_variable('-foo') }
      assert_raises(Liquid::SyntaxError) { create_variable('2foo') }
    end
  end

  def test_string_with_special_chars
    var = create_variable(%( 'hello! $!@.;"ddasd" ' ))
    assert_equal('hello! $!@.;"ddasd" ', var.name)
  end

  def test_string_dot
    var = create_variable(%( test.test ))
    assert_equal(VariableLookup.new('test.test'), var.name)
  end

  def test_filter_with_keyword_arguments
    var = create_variable(%( hello | things: greeting: "world", farewell: 'goodbye'))
    assert_equal(VariableLookup.new('hello'), var.name)
    assert_equal([['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters)
  end

  def test_lax_filter_argument_parsing
    var = create_variable(%( number_of_comments | pluralize: 'comment': 'comments' ), error_mode: :lax)
    assert_equal(VariableLookup.new('number_of_comments'), var.name)
    assert_equal([['pluralize', ['comment', 'comments']]], var.filters)

    # missing does not throws error
    create_variable(%(n | f1: ,), error_mode: :lax)
    create_variable(%(n | f1: ,| f2), error_mode: :lax)

    # arg does not require colon, but ignores args :O, also ignores first kwarg since it splits on ':'
    var = create_variable(%(n | f1 1 | f2 k1: v1), error_mode: :lax)
    assert_equal([['f1', []], ['f2', [VariableLookup.new('v1')]]], var.filters)

    # positional and kwargs parsing
    var = create_variable(%(n | filter: 1, 2, 3 | filter2: k1: 1, k2: 2), error_mode: :lax)
    assert_equal([['filter', [1, 2, 3]], ['filter2', [], { "k1" => 1, "k2" => 2 }]], var.filters)

    # positional and kwargs intermixed (pos1, key1: val1, pos2)
    var = create_variable(%(n | link_to: class: "black", "https://example.com", title: "title"), error_mode: :lax)
    assert_equal([['link_to', ["https://example.com"], { "class" => "black", "title" => "title" }]], var.filters)
  end

  def test_strict_filter_argument_parsing
    with_error_modes(:strict) do
      assert_raises(SyntaxError) do
        create_variable(%( number_of_comments | pluralize: 'comment': 'comments' ))
      end
    end
  end

  def test_strict2_filter_argument_parsing
    with_error_modes(:strict2) do
      # optional colon
      var = create_variable(%(n | f1 | f2:))
      assert_equal([['f1', []], ['f2', []]], var.filters)

      # missing argument throws error
      assert_raises(SyntaxError) { create_variable(%(n | f1: ,)) }
      assert_raises(SyntaxError) { create_variable(%(n | f1: ,| f2)) }

      # arg requires colon
      assert_raises(SyntaxError) { create_variable(%(n | f1 1)) }

      # trailing comma doesn't throw
      create_variable(%(n | f1: 1, 2, 3, | f2:))

      # missing comma throws error
      assert_raises(SyntaxError) { create_variable(%(n | filter: 1 2, 3)) }

      # positional and kwargs parsing
      var = create_variable(%(n | filter: 1, 2, 3 | filter2: k1: 1, k2: 2))
      assert_equal([['filter', [1, 2, 3]], ['filter2', [], { "k1" => 1, "k2" => 2 }]], var.filters)

      # positional and kwargs mixed
      var = create_variable(%(n | filter: 'a', 'b', key1: 1, key2: 2, 'c'))
      assert_equal([["filter", ["a", "b", "c"], { "key1" => 1, "key2" => 2 }]], var.filters)

      # positional and kwargs intermixed (pos1, key1: val1, pos2)
      var = create_variable(%(n | link_to: class: "black", "https://example.com", title: "title"))
      assert_equal([['link_to', ["https://example.com"], { "class" => "black", "title" => "title" }]], var.filters)

      # string key throws
      assert_raises(SyntaxError) { create_variable(%(n | pluralize: 'comment': 'comments')) }
    end
  end

  def test_output_raw_source_of_variable
    var = create_variable(%( name_of_variable | upcase ))
    assert_equal(" name_of_variable | upcase ", var.raw)
  end

  def test_variable_lookup_interface
    lookup = VariableLookup.new('a.b.c')
    assert_equal('a', lookup.name)
    assert_equal(['b', 'c'], lookup.lookups)
  end

  private

  def create_variable(markup, options = {})
    Variable.new(markup, ParseContext.new(options))
  end
end