-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbb64b3
commit ad788d3
Showing
128 changed files
with
1,569 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[*.cs] | ||
|
||
# IDE0022: Use block body for method | ||
csharp_style_expression_bodied_methods = when_on_single_line | ||
|
||
# IDE0022: Use expression body for method | ||
dotnet_diagnostic.IDE0022.severity = silent | ||
|
||
# IDE0008: Use explicit type | ||
csharp_style_var_for_built_in_types = true | ||
|
||
# IDE0008: Use explicit type | ||
csharp_style_var_elsewhere = true | ||
|
||
# IDE0008: Use explicit type | ||
csharp_style_var_when_type_is_apparent = true | ||
|
||
# IDE0290: Use primary constructor | ||
csharp_style_prefer_primary_constructors = false | ||
|
||
# IDE0046: Convert to conditional expression | ||
dotnet_diagnostic.IDE0046.severity = silent | ||
|
||
# IDE0305: Simplify collection initialization | ||
dotnet_diagnostic.IDE0305.severity = suggestion | ||
|
||
# IDE0028: Simplify collection initialization | ||
dotnet_diagnostic.IDE0028.severity = suggestion | ||
|
||
# IDE0090: Use 'new(...)' | ||
dotnet_diagnostic.IDE0090.severity = suggestion | ||
|
||
# IDE0045: Convert to conditional expression | ||
dotnet_diagnostic.IDE0045.severity = suggestion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Fluid.Tests.Visitors; | ||
using Fluid.Values; | ||
using Xunit; | ||
|
||
namespace Fluid.Tests | ||
{ | ||
public class VisitorTest | ||
{ | ||
[Fact] | ||
public void ShouldReplaceTwos() | ||
{ | ||
var template = new FluidParser().Parse("{{ 1 | plus: 2 }}"); | ||
var visitor = new ReplaceTwosVisitor(NumberValue.Create(4)); | ||
var changed = visitor.VisitTemplate(template); | ||
|
||
var result = changed.Render(); | ||
|
||
Assert.Equal("5", result); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSubtract() | ||
{ | ||
var template = new FluidParser().Parse("{{ 1 | plus: 2 }}"); | ||
var visitor = new ReplacePlusFiltersVisitor(); | ||
var changed = visitor.VisitTemplate(template); | ||
|
||
var result = changed.Render(); | ||
|
||
Assert.Equal("-1", result); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ShouldDoNothing() | ||
{ | ||
var template = new FluidParser().Parse("{{ 1 | plus: 2 }}"); | ||
var visitor = new RemovePlusFiltersVisitor(); | ||
var changed = visitor.VisitTemplate(template); | ||
|
||
var result = changed.Render(); | ||
|
||
Assert.Equal("1", result); | ||
} | ||
|
||
[Fact] | ||
public void ShouldDetectForLoopUsage() | ||
{ | ||
var template1 = new FluidParser().Parse(@" | ||
{% for page in pages -%} | ||
{%- if forloop.length > 0 -%} | ||
{{ page.title }}{% unless forloop.last %}, {% endunless -%} | ||
{%- endif -%} | ||
{% endfor %}" | ||
); | ||
|
||
var template2 = new FluidParser().Parse(@" | ||
{% for page in pages -%} | ||
{%- if pages.length > 0 -%} | ||
{{ page.title }}{% unless pages.last %}, {% endunless -%} | ||
{%- endif -%} | ||
{% endfor %}" | ||
); | ||
|
||
var visitor = new IdentifierIsAccessedVisitor("forloop"); | ||
|
||
visitor.VisitTemplate(template1); | ||
var result1 = visitor.IsAccessed; | ||
|
||
visitor.VisitTemplate(template2); | ||
var result2 = visitor.IsAccessed; | ||
|
||
Assert.True(result1); | ||
Assert.False(result2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Fluid.Ast; | ||
using System.Linq; | ||
|
||
namespace Fluid.Tests.Visitors | ||
{ | ||
internal class IdentifierIsAccessedVisitor : AstVisitor | ||
{ | ||
private readonly string _identifier; | ||
|
||
public IdentifierIsAccessedVisitor(string identifier) | ||
{ | ||
_identifier = identifier; | ||
} | ||
|
||
public bool IsAccessed { get; private set; } | ||
|
||
public override IFluidTemplate VisitTemplate(IFluidTemplate template) | ||
{ | ||
// Initialize the result each time a template is visited with the same visitor instance | ||
|
||
IsAccessed = false; | ||
return base.VisitTemplate(template); | ||
} | ||
|
||
protected override Expression VisitMemberExpression(MemberExpression memberExpression) | ||
{ | ||
var firstSegment = memberExpression.Segments.FirstOrDefault() as IdentifierSegment; | ||
|
||
if (firstSegment != null) | ||
{ | ||
IsAccessed |= firstSegment.Identifier == _identifier; | ||
} | ||
|
||
return base.VisitMemberExpression(memberExpression); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Fluid.Ast; | ||
|
||
namespace Fluid.Tests.Visitors | ||
{ | ||
internal class RemovePlusFiltersVisitor : AstRewriter | ||
{ | ||
protected override Expression VisitFilterExpression(FilterExpression filterExpression) | ||
{ | ||
if (filterExpression.Name == "plus") | ||
{ | ||
return filterExpression.Input; | ||
} | ||
|
||
return filterExpression; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Fluid.Ast; | ||
|
||
namespace Fluid.Tests.Visitors | ||
{ | ||
internal class ReplacePlusFiltersVisitor : AstRewriter | ||
{ | ||
protected override Expression VisitFilterExpression(FilterExpression filterExpression) | ||
{ | ||
if (filterExpression.Name == "plus") | ||
{ | ||
return new FilterExpression(filterExpression.Input, "minus", filterExpression.Parameters); | ||
} | ||
|
||
return filterExpression; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Fluid.Ast; | ||
using Fluid.Values; | ||
|
||
namespace Fluid.Tests.Visitors | ||
{ | ||
internal class ReplaceTwosVisitor : AstRewriter | ||
{ | ||
private readonly FluidValue _replacement; | ||
|
||
public ReplaceTwosVisitor(FluidValue replacement) | ||
{ | ||
_replacement = replacement; | ||
} | ||
|
||
protected override Expression VisitLiteralExpression(LiteralExpression literalExpression) | ||
{ | ||
if (literalExpression.Value is NumberValue n && n.ToNumberValue() == 2) | ||
{ | ||
return new LiteralExpression(_replacement); | ||
} | ||
|
||
return literalExpression; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Reflection; | ||
|
||
namespace Fluid.Accessors | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.