Skip to content
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
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches: [ master ]

jobs:
build:
name: Build
Expand All @@ -27,7 +27,11 @@ jobs:
os: [ macos-latest, ubuntu-latest, windows-latest ]
steps:
- uses: actions/checkout@master
- name: Setup dotnet
- name: Setup dotnet 2.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.1.811
- name: Setup dotnet 3.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.403
Expand All @@ -42,6 +46,14 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Setup dotnet 2.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.1.811
- name: Setup dotnet 3.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
- uses: actions/setup-java@v1
with:
java-version: '13' # The JDK version to make available on the path.
Expand Down
14 changes: 13 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ jobs:
os: [ macos-latest, ubuntu-latest, windows-latest ]
steps:
- uses: actions/checkout@master
- name: Setup dotnet
- name: Setup dotnet 2.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.1.811
- name: Setup dotnet 3.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.403
Expand All @@ -42,6 +46,14 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Setup dotnet 2.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.1.811
- name: Setup dotnet 3.1
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
- uses: actions/setup-java@v1
with:
java-version: '13' # The JDK version to make available on the path.
Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars.Benchmark/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Setup()
}

[Benchmark]
public Func<object, string> Template()
public HandlebarsTemplate<object, object> Template()
{
const string template = @"
childCount={{level1.Count}}
Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars.Benchmark/EndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace HandlebarsNet.Benchmark
public class EndToEnd
{
private object _data;
private Action<TextWriter, object> _default;
private HandlebarsTemplate<TextWriter, object, object> _default;

[Params(5)]
public int N { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars.Benchmark/Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace HandlebarsNet.Benchmark
{
public class Execution
{
private readonly List<Action<TextWriter, object>> _templates = new List<Action<TextWriter, object>>();
private readonly List<HandlebarsTemplate<TextWriter, object, object>> _templates = new List<HandlebarsTemplate<TextWriter, object, object>>();

[GlobalSetup]
public void Setup()
Expand Down
161 changes: 161 additions & 0 deletions source/Handlebars.Test/ArgumentsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System.Linq;
using Xunit;

namespace HandlebarsDotNet.Test
{
public class ArgumentsTests
{
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(10)]
public void ArgumentsCtor(int count)
{
var values = new object[count];
CreateValues(values);
var arguments = CreateArguments(values);

for (var index = 0; index < values.Length; index++)
{
Assert.Equal(values[index], arguments[index]);
}
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(10)]
public void ArgumentsEnumerator(int count)
{
var values = new object[count];
CreateValues(values);
var arguments = CreateArguments(values);

var valuesEnumerator = values.GetEnumerator();
var argumentsEnumerator = arguments.GetEnumerator();

for (int index = 0; index < values.Length; index++)
{
Assert.Equal(valuesEnumerator.MoveNext(), argumentsEnumerator.MoveNext());
Assert.Equal(valuesEnumerator.Current, argumentsEnumerator.Current);
}
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(10)]
public void ArgumentsEnumerable(int count)
{
var values = new object[count];
CreateValues(values);
var arguments = CreateArguments(values);

using var valuesEnumerator = values.AsEnumerable().GetEnumerator();
using var argumentsEnumerator = arguments.GetEnumerator();

for (int index = 0; index < values.Length; index++)
{
Assert.Equal(valuesEnumerator.MoveNext(), argumentsEnumerator.MoveNext());
Assert.Equal(valuesEnumerator.Current, argumentsEnumerator.Current);
}
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(10)]
public void ArgumentsAdd(int count)
{
var values = new object[count];
CreateValues(values);
var arguments = CreateArguments(values);

var value = new object();
var newArguments = arguments.Add(value);

Assert.Equal(arguments.Length + 1, newArguments.Length);
Assert.Equal(value, newArguments[newArguments.Length - 1]);
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(10)]
public void ArgumentsEquals(int count)
{
var values = new object[count];
CreateValues(values);
var arguments1 = CreateArguments(values);
var arguments2 = CreateArguments(values);

Assert.Equal(arguments1, arguments2);
}

private static void CreateValues(object[] values)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = new object();
}
}

private static Arguments CreateArguments(object[] values)
{
int count = values.Length;
var constructor =
typeof(Arguments)
.GetConstructors()
.SingleOrDefault(o =>
{
var parameterInfos = o.GetParameters();
return parameterInfos.Length == count && parameterInfos[0].ParameterType != typeof(object[]);
})
??
typeof(Arguments)
.GetConstructors()
.Single(o =>
{
var parameterInfos = o.GetParameters();
return parameterInfos.Length == 1 && parameterInfos[0].ParameterType == typeof(object[]);
});

Arguments arguments;
if (values.Length <= 6)
{
arguments = (Arguments) constructor.Invoke(values);
}
else
{
arguments = (Arguments) constructor.Invoke(new object[] {values});
}

return arguments;
}
}
}
23 changes: 22 additions & 1 deletion source/Handlebars.Test/BasicIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void BasicPropertyOnArray(IHandlebars handlebars)
public void AliasedPropertyOnArray(IHandlebars handlebars)
{
var source = "Array is {{ names.count }} item(s) long";
handlebars.Configuration.UseCollectionMemberAliasProvider();
var template = handlebars.Compile(source);
var data = new
{
Expand Down Expand Up @@ -345,6 +346,7 @@ public void CustomAliasedPropertyOnArray(IHandlebars handlebars)
public void AliasedPropertyOnList(IHandlebars handlebars)
{
var source = "Array is {{ names.Length }} item(s) long";
handlebars.Configuration.UseCollectionMemberAliasProvider();
var template = handlebars.Compile(source);
var data = new
{
Expand Down Expand Up @@ -432,6 +434,25 @@ public void BasicWith(IHandlebars handlebars)
Assert.Equal("Hello, my good friend Erik!", result);
}

[Theory, ClassData(typeof(HandlebarsEnvGenerator))]
public void GlobalDataPropagation(IHandlebars handlebars)
{
var source = "{{#with input}}{{first}} {{@global1}} {{#with second}}{{third}} {{@global2}}{{/with}}{{/with}}";
var template = handlebars.Compile(source);
var data = new
{
input = new
{
first = 1,
second = new {
third = 3
}
}
};
var result = template(data, new { global1 = 2, global2 = 4 });
Assert.Equal("1 2 3 4", result);
}

[Theory, ClassData(typeof(HandlebarsEnvGenerator))]
public void TestSingleLoopDictionary(IHandlebars handlebars)
{
Expand Down Expand Up @@ -1853,7 +1874,7 @@ public bool TryResolveHelper(string name, Type targetType, out HelperDescriptorB
return false;
}

object Helper(dynamic context, object[] arguments) => method.Invoke(arguments[0], arguments.Skip(1).ToArray());
object Helper(object context, Arguments arguments) => method.Invoke(arguments[0], arguments.AsEnumerable().Skip(1).ToArray());
helper = new DelegateReturnHelperDescriptor(name, Helper);
return true;
}
Expand Down
Loading