Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LiteralConverter to support long #562

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
- uses: actions/setup-java@v1
- uses: actions/setup-java@v4
with:
java-version: '13' # The JDK version to make available on the path.
java-version: '17' # The JDK version to make available on the path.
- name: Clean package cache as a temporary workaround for https://github.com/actions/setup-dotnet/issues/155
working-directory: ./source
run: dotnet clean -c Release && dotnet nuget locals all --clear
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
dotnet-version: 3.1.x
- uses: actions/setup-java@v1
with:
java-version: '13' # The JDK version to make available on the path.
java-version: '17' # The JDK version to make available on the path.
- name: Clean package cache as a temporary workaround for https://github.com/actions/setup-dotnet/issues/155
working-directory: ./source
run: dotnet clean -c Release && dotnet nuget locals all --clear
Expand Down
56 changes: 40 additions & 16 deletions source/Handlebars.Test/NumericLiteralTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using HandlebarsDotNet.Compiler;
using System.Linq;
using Xunit;

namespace HandlebarsDotNet.Test
Expand All @@ -14,10 +12,37 @@ public NumericLiteralTests()
var arr = args.AsEnumerable().Select(a => (object)int.Parse(a.ToString()));
writer.Write(arr.Aggregate(0, (a, i) => a + (int)i));
});

Handlebars.RegisterHelper("longAdd", (writer, context, args) =>
{
var arr = args.AsEnumerable().Select(a => long.Parse(a.ToString()));
var sum = arr.Sum();
writer.Write(sum);
});
}

[Theory]
[InlineData("{{longAdd 1000000000 9999999999}}")]
[InlineData("{{longAdd 1000000000 9999999999}}")]
[InlineData("{{longAdd 1000000000 9999999999 }}")]
[InlineData("{{longAdd 1000000000 9999999999}}")]
[InlineData("{{longAdd 1000000000 9999999999}}")]
[InlineData("{{longAdd 1000000000 \"9999999999\"}}")]
[InlineData("{{longAdd 1000000000 \"9999999999\" }}")]
[InlineData("{{longAdd 1000000000 \"9999999999\"}}")]
[InlineData("{{longAdd 1000000000 \"9999999999\" }}")]
[InlineData("{{longAdd \"1000000000\" 9999999999}}")]
[InlineData("{{longAdd \"1000000000\" \"9999999999\"}}")]
public void NumericLiteralLongTests(string source)
{
var template = Handlebars.Compile(source);
var data = new { };
var result = template(data);
Assert.Equal("10999999999", result);
}

[Fact]
public void NumericLiteralTest1()
public void NumericLiteralIntegerTest1()
{
var source = "{{numericLiteralAdd 3 4}}";
var template = Handlebars.Compile(source);
Expand All @@ -27,7 +52,7 @@ public void NumericLiteralTest1()
}

[Fact]
public void NumericLiteralTest2()
public void NumericLiteralIntegerTest2()
{
var source = "{{numericLiteralAdd 3 4}}";
var template = Handlebars.Compile(source);
Expand All @@ -37,7 +62,7 @@ public void NumericLiteralTest2()
}

[Fact]
public void NumericLiteralTest3()
public void NumericLiteralIntegerTest3()
{
var source = "{{numericLiteralAdd 3 4 }}";
var template = Handlebars.Compile(source);
Expand All @@ -47,7 +72,7 @@ public void NumericLiteralTest3()
}

[Fact]
public void NumericLiteralTest4()
public void NumericLiteralIntegerTest4()
{
var source = "{{numericLiteralAdd 3 4 }}";
var template = Handlebars.Compile(source);
Expand All @@ -57,7 +82,7 @@ public void NumericLiteralTest4()
}

[Fact]
public void NumericLiteralTest5()
public void NumericLiteralIntegerTest5()
{
var source = "{{numericLiteralAdd 3 4 }}";
var template = Handlebars.Compile(source);
Expand All @@ -67,7 +92,7 @@ public void NumericLiteralTest5()
}

[Fact]
public void NumericLiteralTest6()
public void NumericLiteralIntegerTest6()
{
var source = "{{numericLiteralAdd 3 \"4\"}}";
var template = Handlebars.Compile(source);
Expand All @@ -77,7 +102,7 @@ public void NumericLiteralTest6()
}

[Fact]
public void NumericLiteralTest7()
public void NumericLiteralIntegerTest7()
{
var source = "{{numericLiteralAdd 3 \"4\" }}";
var template = Handlebars.Compile(source);
Expand All @@ -87,7 +112,7 @@ public void NumericLiteralTest7()
}

[Fact]
public void NumericLiteralTest8()
public void NumericLiteralIntegerTest8()
{
var source = "{{numericLiteralAdd 3 \"4\" }}";
var template = Handlebars.Compile(source);
Expand All @@ -97,7 +122,7 @@ public void NumericLiteralTest8()
}

[Fact]
public void NumericLiteralTest9()
public void NumericLiteralIntegerTest9()
{
var source = "{{numericLiteralAdd 3 \"4\" }}";
var template = Handlebars.Compile(source);
Expand All @@ -107,7 +132,7 @@ public void NumericLiteralTest9()
}

[Fact]
public void NumericLiteralTest10()
public void NumericLiteralIntegerTest10()
{
var source = "{{numericLiteralAdd \"3\" 4}}";
var template = Handlebars.Compile(source);
Expand All @@ -117,7 +142,7 @@ public void NumericLiteralTest10()
}

[Fact]
public void NumericLiteralTest11()
public void NumericLiteralIntegerTest11()
{
var source = "{{numericLiteralAdd \"3\" 4 }}";
var template = Handlebars.Compile(source);
Expand All @@ -126,5 +151,4 @@ public void NumericLiteralTest11()
Assert.Equal("7", result);
}
}
}

}
8 changes: 8 additions & 0 deletions source/Handlebars.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9AC0BCD-C060-4634-BBBB-636167C809B4}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
..\.github\workflows\pull_request.yml = ..\.github\workflows\pull_request.yml
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Benchmark", "Handlebars.Benchmark\Handlebars.Benchmark.csproj", "{417E2E51-2DD2-4045-84E5-BA66484E957B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Github Actions", "Github Actions", "{0683EE49-625C-473D-B600-079FDB9AF55B}"
ProjectSection(SolutionItems) = preProject
..\.github\workflows\ci.yml = ..\.github\workflows\ci.yml
..\.github\workflows\pull_request.yml = ..\.github\workflows\pull_request.yml
..\.github\workflows\release.yml = ..\.github\workflows\release.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
31 changes: 18 additions & 13 deletions source/Handlebars/Compiler/Lexer/Converter/LiteralConverter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using HandlebarsDotNet.Compiler.Lexer;
using System.Linq.Expressions;
using System.Linq;
using System.Linq.Expressions;
using HandlebarsDotNet.Compiler.Lexer;

namespace HandlebarsDotNet.Compiler
{
internal class LiteralConverter : TokenConverter
{
private static readonly LiteralConverter Converter = new LiteralConverter();

public static IEnumerable<object> Convert(IEnumerable<object> sequence)
{
return Converter.ConvertTokens(sequence).ToList();
Expand All @@ -28,15 +27,22 @@ public override IEnumerable<object> ConvertTokens(IEnumerable<object> sequence)
switch (item)
{
case LiteralExpressionToken literalExpression:
{
result = Expression.Convert(Expression.Constant(literalExpression.Value), typeof(object));
if (!literalExpression.IsDelimitedLiteral && int.TryParse(literalExpression.Value, out var intValue))
{
result = Expression.Convert(Expression.Constant(intValue), typeof(object));
result = Expression.Convert(Expression.Constant(literalExpression.Value), typeof(object));
if (!literalExpression.IsDelimitedLiteral)
{
if (int.TryParse(literalExpression.Value, out var intValue))
{
result = Expression.Convert(Expression.Constant(intValue), typeof(object));
}
else if (long.TryParse(literalExpression.Value, out var longValue))
{
result = Expression.Convert(Expression.Constant(longValue), typeof(object));
}
}

break;
}

break;
}

case WordExpressionToken wordExpression when bool.TryParse(wordExpression.Value, out var boolValue):
result = Expression.Convert(Expression.Constant(boolValue), typeof(object));
Expand All @@ -47,5 +53,4 @@ public override IEnumerable<object> ConvertTokens(IEnumerable<object> sequence)
}
}
}
}

}
Loading