Skip to content

Commit

Permalink
Fix swapped rindex and rindex0 in 'for', 'include' and 'render'
Browse files Browse the repository at this point in the history
  • Loading branch information
nairdo committed Aug 31, 2024
1 parent de1fa85 commit 240fa2a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Fluid.Tests/ForStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task ForShouldProvideHelperVariables()
context.SetValue("items", new[] { 1, 2, 3 });
await e.WriteToAsync(sw, HtmlEncoder.Default, context);

Assert.Equal("31023truefalse32112falsefalse33201falsetrue", sw.ToString());
Assert.Equal("31032truefalse32121falsefalse33210falsetrue", sw.ToString());
}

[Fact]
Expand Down
8 changes: 4 additions & 4 deletions Fluid.Tests/IncludeStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void RenderTagCantUseDynamicName()
public void IncludeTag_For_Loop()
{
var fileProvider = new MockFileProvider();
fileProvider.Add("product.liquid", "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} ");
fileProvider.Add("product.liquid", "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} rindex:{{ forloop.rindex }} rindex0:{{ forloop.rindex0 }} " );

var options = new TemplateOptions() { FileProvider = fileProvider, MemberAccessStrategy = UnsafeMemberAccessStrategy.Instance };
var context = new TemplateContext(options);
Expand All @@ -291,14 +291,14 @@ public void IncludeTag_For_Loop()

var result = template.Render(context);

Assert.Equal("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ", result);
Assert.Equal("Product: Draft 151cm first index:1 rindex:2 rindex0:1 Product: Element 155cm last index:2 rindex:1 rindex0:0 ", result);
}

[Fact]
public void RenderTag_For_Loop()
{
var fileProvider = new MockFileProvider();
fileProvider.Add("product.liquid", "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} ");
fileProvider.Add("product.liquid", "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} rindex:{{ forloop.rindex }} rindex0:{{ forloop.rindex0 }} " );

var options = new TemplateOptions() { FileProvider = fileProvider, MemberAccessStrategy = UnsafeMemberAccessStrategy.Instance };
var context = new TemplateContext(options);
Expand All @@ -307,7 +307,7 @@ public void RenderTag_For_Loop()

var result = template.Render(context);

Assert.Equal("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ", result);
Assert.Equal("Product: Draft 151cm first index:1 rindex:2 rindex0:1 Product: Element 155cm last index:2 rindex:1 rindex0:0 ", result);
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions Fluid/Ast/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public override async ValueTask<Completion> WriteToAsync(TextWriter writer, Text
// Set helper variables
forloop.Index = i + 1;
forloop.Index0 = i;
forloop.RIndex = length - i - 1;
forloop.RIndex0 = length - i;
forloop.RIndex = length - i;
forloop.RIndex0 = length - i - 1;
forloop.First = i == 0;
forloop.Last = i == length - 1;

Expand Down
4 changes: 2 additions & 2 deletions Fluid/Ast/IncludeStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public override async ValueTask<Completion> WriteToAsync(TextWriter writer, Text
// Set helper variables
forloop.Index = i + 1;
forloop.Index0 = i;
forloop.RIndex = length - i - 1;
forloop.RIndex0 = length - i;
forloop.RIndex = length - i;
forloop.RIndex0 = length - i - 1;
forloop.First = i == 0;
forloop.Last = i == length - 1;

Expand Down
4 changes: 2 additions & 2 deletions Fluid/Ast/RenderStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ public override async ValueTask<Completion> WriteToAsync(TextWriter writer, Text
// Set helper variables
forloop.Index = i + 1;
forloop.Index0 = i;
forloop.RIndex = length - i - 1;
forloop.RIndex0 = length - i;
forloop.RIndex = length - i;
forloop.RIndex0 = length - i - 1;
forloop.First = i == 0;
forloop.Last = i == length - 1;

Expand Down

0 comments on commit 240fa2a

Please sign in to comment.