Skip to content

Commit 44185a1

Browse files
Switch to handlebars
1 parent 311cc6d commit 44185a1

File tree

6 files changed

+50
-58
lines changed

6 files changed

+50
-58
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using Xunit;
22

3-
public class {{exercise.name}}Tests
3+
public class AcronymTests
44
{
5-
{{for test_case in test_cases}}
6-
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7-
public void {{test_case.path | method_name}}()
5+
{{#test_cases}}
6+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7+
public void {{method_name path}}()
88
{
9-
Assert.Equal("{{test_case.expected}}", {{exercise.name}}.Abbreviate("{{test_case.input.phrase}}"));
9+
Assert.Equal("{{expected}}", Acronym.Abbreviate("{{input.phrase}}"));
1010
}
11-
{{end}}
11+
{{/test_cases}}
1212
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
using Xunit;
22

3-
public class {{exercise.name}}Tests
3+
public class DifferenceOfSquaresTests
44
{
5-
{{for test_case in test_cases_by_property.squareOfSum}}
6-
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7-
public void {{test_case.description | method_name}}()
5+
{{#test_cases_by_property.squareOfSum}}
6+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7+
public void {{method_name description}}()
88
{
9-
Assert.Equal({{test_case.expected}}, {{exercise.name}}.CalculateSquareOfSum({{test_case.input.number}}));
9+
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateSquareOfSum({{input.number}}));
1010
}
11-
{{end}}
11+
{{/test_cases_by_property.squareOfSum}}
1212

13-
{{for test_case in test_cases_by_property.sumOfSquares}}
14-
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
15-
public void {{test_case.description | method_name}}()
13+
{{#test_cases_by_property.sumOfSquares}}
14+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
15+
public void {{method_name description}}()
1616
{
17-
Assert.Equal({{test_case.expected}}, {{exercise.name}}.CalculateSumOfSquares({{test_case.input.number}}));
17+
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateSumOfSquares({{input.number}}));
1818
}
19-
{{end}}
19+
{{/test_cases_by_property.sumOfSquares}}
2020

21-
{{for test_case in test_cases_by_property.differenceOfSquares}}
22-
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
23-
public void {{test_case.description | method_name}}()
21+
{{#test_cases_by_property.differenceOfSquares}}
22+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
23+
public void {{method_name description}}()
2424
{
25-
Assert.Equal({{test_case.expected}}, {{exercise.name}}.CalculateDifferenceOfSquares({{test_case.input.number}}));
25+
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateDifferenceOfSquares({{input.number}}));
2626
}
27-
{{end}}
27+
{{/test_cases_by_property.differenceOfSquares}}
2828
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using Xunit;
22

3-
public class {{exercise.name}}Tests
3+
public class IsogramTests
44
{
5-
{{for test_case in test_cases}}
6-
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7-
public void {{test_case.path | method_name}}()
5+
{{#test_cases}}
6+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7+
public void {{method_name path}}()
88
{
9-
Assert.{{test_case.expected}}({{exercise.name}}.IsIsogram("{{test_case.input.phrase}}"));
9+
Assert.{{expected}}(Isogram.IsIsogram("{{input.phrase}}"));
1010
}
11-
{{end}}
11+
{{/test_cases}}
1212
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using Xunit;
22

3-
public class {{exercise.name}}Tests
3+
public class LeapTests
44
{
5-
{{for test_case in test_cases}}
6-
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7-
public void {{test_case.path | method_name}}()
5+
{{#test_cases}}
6+
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7+
public void {{method_name path}}()
88
{
9-
Assert.{{test_case.expected}}({{exercise.name}}.IsLeapYear({{test_case.input.year}}));
9+
Assert.{{expected}}(Leap.IsLeapYear({{input.year}}));
1010
}
11-
{{end}}
11+
{{/test_cases}}
1212
}

generators.new/Generators.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="CommandLineParser" Version="2.9.1" />
12+
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
1213
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
1314
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
14-
<PackageReference Include="Scriban" Version="5.12.1" />
1515
<PackageReference Include="Tomlyn" Version="0.18.0" />
1616
</ItemGroup>
1717

generators.new/TemplateRenderer.cs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
1-
using Humanizer;
1+
using HandlebarsDotNet;
22

3-
using Scriban;
4-
using Scriban.Runtime;
3+
using Humanizer;
54

65
namespace Generators;
76

87
internal static class TemplateRenderer
98
{
9+
static TemplateRenderer() =>
10+
Handlebars.RegisterHelper("method_name", (writer, context, parameters) =>
11+
{
12+
var path = parameters.SelectMany(parameter => parameter as IEnumerable<string> ?? [parameter.ToString()!]);
13+
writer.WriteSafeString(string.Join(" ", path).Dehumanize());
14+
});
15+
1016
public static string RenderTests(Exercise exercise, TestCase[] testCases) =>
11-
Template.Parse(File.ReadAllText(Paths.TemplateFile(exercise)))
12-
.Render(CreateTemplateContext(exercise, testCases))!;
17+
CompileTemplate(exercise)(ToTemplateData(exercise, testCases));
18+
19+
private static HandlebarsTemplate<object, object> CompileTemplate(Exercise exercise) =>
20+
Handlebars.Compile(File.ReadAllText(Paths.TemplateFile(exercise)));
1321

14-
private static TemplateContext CreateTemplateContext(Exercise exercise, TestCase[] testCases)
15-
{
16-
var customFunctions = new CustomFunctions
22+
private static Dictionary<string, object> ToTemplateData(Exercise exercise, TestCase[] testCases) =>
23+
new()
1724
{
1825
{ "exercise", exercise },
1926
{ "test_cases", testCases },
2027
{ "test_cases_by_property", GroupTestCasesByProperty(testCases)}
2128
};
2229

23-
var context = new TemplateContext();
24-
context.PushGlobal(customFunctions);
25-
26-
return context;
27-
}
28-
2930
private static Dictionary<string, TestCase[]> GroupTestCasesByProperty(TestCase[] testCases) =>
3031
testCases
3132
.GroupBy(testCase => testCase.Property)
3233
.ToDictionary(kv => kv.Key, kv => kv.ToArray());
33-
34-
private class CustomFunctions : ScriptObject
35-
{
36-
public static string MethodName(params object[] path)
37-
{
38-
var flattenedPath = path.SelectMany(x => x as IEnumerable<string> ?? [x.ToString()!]);
39-
return string.Join(" ", flattenedPath).Dehumanize();
40-
}
41-
}
4234
}

0 commit comments

Comments
 (0)