diff --git a/Fluid.Tests/CycleStatementTests.cs b/Fluid.Tests/CycleStatementTests.cs index a9b27d0f..e07ca818 100644 --- a/Fluid.Tests/CycleStatementTests.cs +++ b/Fluid.Tests/CycleStatementTests.cs @@ -9,11 +9,6 @@ namespace Fluid.Tests { public class CycleStatementTests { - private Statement[] TEXT(string text) - { - return new Statement[] { new TextSpanStatement(text) }; - } - private LiteralExpression LIT(string text) { return new LiteralExpression(new StringValue(text)); @@ -74,6 +69,35 @@ public async Task CycleEvaluatesGroupsValue() Assert.Equal("aabbccaabb", sw.ToString()); } + [Fact] + public async Task CycleShouldGroupByStringRepresentation() + { + var group1 = new CycleStatement( + LIT(2), + new[] { + LIT("a"), LIT("b"), LIT("c") + } + ); + + var group2 = new CycleStatement( + LIT("2"), + new[] { + LIT("a"), LIT("b"), LIT("c") + } + ); + + var context = new TemplateContext(); + + var sw = new StringWriter(); + for (var i = 1; i <= 5; i++) + { + await group1.WriteToAsync(sw, HtmlEncoder.Default, context); + await group2.WriteToAsync(sw, HtmlEncoder.Default, context); + } + + Assert.Equal("abcabcabca", sw.ToString()); + } + [Fact] public async Task CycleShouldSupportNumbers() { diff --git a/Fluid.Tests/TemplateTests.cs b/Fluid.Tests/TemplateTests.cs index 5e791a20..a767e20d 100644 --- a/Fluid.Tests/TemplateTests.cs +++ b/Fluid.Tests/TemplateTests.cs @@ -371,6 +371,8 @@ public Task ShouldEvaluateCaseStatement(int x, string expected) [Theory] [InlineData(@"{%cycle 'a', 'b'%}{%cycle 'a', 'b'%}{%cycle 'a', 'b'%}", "aba")] [InlineData(@"{%cycle x:'a', 'b'%}{%cycle 'a', 'b'%}{%cycle x:'a', 'b'%}", "aab")] + [InlineData(@"{%cycle 2:'a', 'b'%}{%cycle '2': 'a', 'b'%}", "ab")] + [InlineData(@"{%cycle 'a', 'b'%}{%cycle foo: 'a', 'b'%}", "ab")] public Task ShouldEvaluateCycleStatement(string source, string expected) { return CheckAsync(source, expected, ctx => { ctx.SetValue("x", 3); }); diff --git a/Fluid/Ast/CycleStatement.cs b/Fluid/Ast/CycleStatement.cs index 0c469fcc..626c6f07 100644 --- a/Fluid/Ast/CycleStatement.cs +++ b/Fluid/Ast/CycleStatement.cs @@ -30,7 +30,7 @@ public override async ValueTask WriteToAsync(TextWriter writer, Text { context.IncrementSteps(); - var groupValue = Group == null ? "$defautGroup" : "$" + (await Group.EvaluateAsync(context)).ToStringValue(); + var groupValue = Group == null ? "$cycle_" : "$cycle_" + (await Group.EvaluateAsync(context)).ToStringValue(); var currentValue = context.GetValue(groupValue); diff --git a/Fluid/FluidParser.cs b/Fluid/FluidParser.cs index 752e23c9..a2add549 100644 --- a/Fluid/FluidParser.cs +++ b/Fluid/FluidParser.cs @@ -226,17 +226,10 @@ public FluidParser() .Then(x => new CaptureStatement(x.Item1, x.Item2)) .ElseError("Invalid 'capture' tag") ; - var CycleTag = ZeroOrOne(Identifier.AndSkip(Colon)) + var CycleTag = ZeroOrOne(Primary.AndSkip(Colon)) .And(Separated(Comma, Primary)) .AndSkip(TagEnd) - .Then(x => - { - var group = string.IsNullOrEmpty(x.Item1) - ? null - : new LiteralExpression(StringValue.Create(x.Item1)); - - return new CycleStatement(group, x.Item2); - }) + .Then(x => new CycleStatement(x.Item1, x.Item2)) .ElseError("Invalid 'cycle' tag") ; var DecrementTag = Identifier.AndSkip(TagEnd)