File tree Expand file tree Collapse file tree 2 files changed +55
-1
lines changed Expand file tree Collapse file tree 2 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -68,7 +68,13 @@ def render_to_output_buffer(context, output)
68
68
def variables_from_string ( markup )
69
69
markup . split ( ',' ) . collect do |var |
70
70
var =~ /\s *(#{ QuotedFragment } )\s */o
71
- Regexp . last_match ( 1 ) ? parse_expression ( Regexp . last_match ( 1 ) ) : nil
71
+ next unless Regexp . last_match ( 1 )
72
+
73
+ # Expression Parser returns cached objects, and we need to dup them to
74
+ # start the cycle over for each new cycle call.
75
+ # Liquid-C does not have a cache, so we don't need to dup the object.
76
+ var = parse_expression ( Regexp . last_match ( 1 ) )
77
+ var . is_a? ( VariableLookup ) ? var . dup : var
72
78
end . compact
73
79
end
74
80
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class CycleTagTest < Minitest ::Test
6
+ def test_simple_cycle
7
+ template = <<~LIQUID
8
+ {%- cycle '1', '2', '3' -%}
9
+ {%- cycle '1', '2', '3' -%}
10
+ {%- cycle '1', '2', '3' -%}
11
+ LIQUID
12
+
13
+ assert_template_result ( "123" , template )
14
+ end
15
+
16
+ def test_simple_cycle_inside_for_loop
17
+ template = <<~LIQUID
18
+ {%- for i in (1..3) -%}
19
+ {% cycle '1', '2', '3' %}
20
+ {%- endfor -%}
21
+ LIQUID
22
+
23
+ assert_template_result ( "123" , template )
24
+ end
25
+
26
+ def test_cycle_with_variables_inside_for_loop
27
+ template = <<~LIQUID
28
+ {%- assign a = 1 -%}
29
+ {%- assign b = 2 -%}
30
+ {%- assign c = 3 -%}
31
+ {%- for i in (1..3) -%}
32
+ {% cycle a, b, c %}
33
+ {%- endfor -%}
34
+ LIQUID
35
+
36
+ assert_template_result ( "123" , template )
37
+ end
38
+
39
+ def test_cycle_tag_always_resets_cycle
40
+ template = <<~LIQUID
41
+ {%- assign a = "1" -%}
42
+ {%- cycle a, "2" -%}
43
+ {%- cycle a, "2" -%}
44
+ LIQUID
45
+
46
+ assert_template_result ( "11" , template )
47
+ end
48
+ end
You can’t perform that action at this time.
0 commit comments