Skip to content

Commit

Permalink
Cycle tag should accept primary value for group (#338)
Browse files Browse the repository at this point in the history
Fixes #337
  • Loading branch information
sebastienros authored May 8, 2021
1 parent 584a2f8 commit c9b0176
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
34 changes: 29 additions & 5 deletions Fluid.Tests/CycleStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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()
{
Expand Down
2 changes: 2 additions & 0 deletions Fluid.Tests/TemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
Expand Down
2 changes: 1 addition & 1 deletion Fluid/Ast/CycleStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async ValueTask<Completion> 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);

Expand Down
11 changes: 2 additions & 9 deletions Fluid/FluidParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,10 @@ public FluidParser()
.Then<Statement>(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<Statement>(x =>
{
var group = string.IsNullOrEmpty(x.Item1)
? null
: new LiteralExpression(StringValue.Create(x.Item1));

return new CycleStatement(group, x.Item2);
})
.Then<Statement>(x => new CycleStatement(x.Item1, x.Item2))
.ElseError("Invalid 'cycle' tag")
;
var DecrementTag = Identifier.AndSkip(TagEnd)
Expand Down

0 comments on commit c9b0176

Please sign in to comment.