Skip to content

Commit dbbb872

Browse files
Converting templates to Scriban
1 parent 7dc6a88 commit dbbb872

File tree

33 files changed

+290
-287
lines changed

33 files changed

+290
-287
lines changed

docs/GENERATORS.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ To generate a practice exercise's tests, the test generator:
1515

1616
### Step 1: read `canonical-data.json` file
1717

18-
The test generator parses the test cases from the exercise's `canonical-data.json` using the [JSON.net library](https://www.newtonsoft.com/json).
18+
The test generator parses the test cases from the exercise's `canonical-data.json` using the [System.Text.Json namespace](https://learn.microsoft.com/en-us/dotnet/api/system.text.json).
1919

2020
Since some canonical data uses nesting, the parsed test case includes an additional `path` field that contains the `description` properties of any parent elements, as well as the test case's own `description` property.
2121

22-
Note: the JSON is parsed to an `ExpandoObject` instance, which makes dealing with dynamic data easier.
23-
2422
### Step 2: omit excluded tests from `tests.toml` file
2523

2624
Each exercise has a `tests.toml` file, in which individual tests can be excluded/disabled.
@@ -44,7 +42,7 @@ Finally, the output of the rendered template is written to the exercise's test f
4442

4543
## Templates
4644

47-
The templates are rendered using the [Handlebars.Net library](https://github.com/Handlebars-Net/Handlebars.Net), which supports [handlebars syntax](https://handlebarsjs.com/).
45+
The templates are rendered using the [Scriban library](https://github.com/scriban/scriban/).
4846

4947
## Command-line interface
5048

exercises/practice/acronym/.meta/Generator.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ using Xunit;
22

33
public class AcronymTests
44
{
5-
{{#test_cases}}
6-
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7-
public void {{test_method_name}}()
5+
{{for testCase in testCases}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{testCase.testMethodName}}()
88
{
9-
Assert.Equal({{lit expected}}, Acronym.Abbreviate({{lit input.phrase}}));
9+
Assert.Equal({{testCase.expected | string.literal}}, Acronym.Abbreviate({{testCase.input.phrase | string.literal}}));
1010
}
11-
{{/test_cases}}
11+
{{end}}
1212
}

exercises/practice/affine-cipher/.meta/Generator.tpl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ using Xunit;
33

44
public class AffineCipherTests
55
{
6-
{{#test_cases_by_property.encode}}
7-
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
8-
public void {{short_test_method_name}}()
6+
{{for testCase in testCasesByProperty.encode}}
7+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
8+
public void {{testCase.shortTestMethodName}}()
99
{
10-
{{#if expected.error}}
11-
Assert.Throws<ArgumentException>(() => AffineCipher.Encode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
10+
{{if testCase.expected.error}}
11+
Assert.Throws<ArgumentException>(() => AffineCipher.Encode({{testCase.input.phrase | string.literal}}, {{testCase.input.key.a}}, {{testCase.input.key.b}}));
1212
{{else}}
13-
Assert.Equal({{lit expected}}, AffineCipher.Encode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
14-
{{/if}}
13+
Assert.Equal({{testCase.expected | string.literal}}, AffineCipher.Encode({{testCase.input.phrase | string.literal}}, {{testCase.input.key.a}}, {{testCase.input.key.b}}));
14+
{{end}}
1515
}
16-
{{/test_cases_by_property.encode}}
16+
{{end}}
1717

18-
{{#test_cases_by_property.decode}}
18+
{{for testCase in testCasesByProperty.decode}}
1919
[Fact(Skip = "Remove this Skip property to run this test")]
20-
public void {{short_test_method_name}}()
20+
public void {{testCase.shortTestMethodName}}()
2121
{
22-
{{#if expected.error}}
23-
Assert.Throws<ArgumentException>(() => AffineCipher.Decode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
22+
{{if testCase.expected.error}}
23+
Assert.Throws<ArgumentException>(() => AffineCipher.Decode({{testCase.input.phrase | string.literal}}, {{testCase.input.key.a}}, {{testCase.input.key.b}}));
2424
{{else}}
25-
Assert.Equal({{lit expected}}, AffineCipher.Decode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
26-
{{/if}}
25+
Assert.Equal({{testCase.expected | string.literal}}, AffineCipher.Decode({{testCase.input.phrase}}, {{testCase.input.key.a}}, {{testCase.input.key.b}}));
26+
{{end}}
2727
}
28-
{{/test_cases_by_property.decode}}
28+
{{end}}
2929
}

exercises/practice/affine-cipher/AffineCipherTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,37 @@ public void Encode_with_a_not_coprime_to_m()
6060
[Fact(Skip = "Remove this Skip property to run this test")]
6161
public void Decode_exercism()
6262
{
63-
Assert.Equal("exercism", AffineCipher.Decode("tytgn fjr", 3, 7));
63+
Assert.Equal("exercism", AffineCipher.Decode(tytgn fjr, 3, 7));
6464
}
6565

6666
[Fact(Skip = "Remove this Skip property to run this test")]
6767
public void Decode_a_sentence()
6868
{
69-
Assert.Equal("anobstacleisoftenasteppingstone", AffineCipher.Decode("qdwju nqcro muwhn odqun oppmd aunwd o", 19, 16));
69+
Assert.Equal("anobstacleisoftenasteppingstone", AffineCipher.Decode(qdwju nqcro muwhn odqun oppmd aunwd o, 19, 16));
7070
}
7171

7272
[Fact(Skip = "Remove this Skip property to run this test")]
7373
public void Decode_numbers()
7474
{
75-
Assert.Equal("testing123testing", AffineCipher.Decode("odpoz ub123 odpoz ub", 25, 7));
75+
Assert.Equal("testing123testing", AffineCipher.Decode(odpoz ub123 odpoz ub, 25, 7));
7676
}
7777

7878
[Fact(Skip = "Remove this Skip property to run this test")]
7979
public void Decode_all_the_letters()
8080
{
81-
Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AffineCipher.Decode("swxtj npvyk lruol iejdc blaxk swxmh qzglf", 17, 33));
81+
Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AffineCipher.Decode(swxtj npvyk lruol iejdc blaxk swxmh qzglf, 17, 33));
8282
}
8383

8484
[Fact(Skip = "Remove this Skip property to run this test")]
8585
public void Decode_with_no_spaces_in_input()
8686
{
87-
Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AffineCipher.Decode("swxtjnpvyklruoliejdcblaxkswxmhqzglf", 17, 33));
87+
Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AffineCipher.Decode(swxtjnpvyklruoliejdcblaxkswxmhqzglf, 17, 33));
8888
}
8989

9090
[Fact(Skip = "Remove this Skip property to run this test")]
9191
public void Decode_with_too_many_spaces()
9292
{
93-
Assert.Equal("jollygreengiant", AffineCipher.Decode("vszzm cly yd cg qdp", 15, 16));
93+
Assert.Equal("jollygreengiant", AffineCipher.Decode(vszzm cly yd cg qdp, 15, 16));
9494
}
9595

9696
[Fact(Skip = "Remove this Skip property to run this test")]

exercises/practice/allergies/.meta/Generator.tpl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@ using Xunit;
22

33
public class AllergiesTests
44
{
5-
{{#test_cases_by_property.allergicTo}}
6-
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7-
public void {{test_method_name}}()
5+
{{for testCase in testCasesByProperty.allergicTo}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{testCase.testMethodName}}()
88
{
9-
var sut = new Allergies({{input.score}});
10-
Assert.{{expected}}(sut.IsAllergicTo(Allergen.{{Capitalize input.item}}));
9+
var sut = new Allergies({{testCase.input.score}});
10+
Assert.{{testCase.expected ? "True" : "False"}}(sut.IsAllergicTo({{testCase.input.item | enum type: "Allergen"}}));
1111
}
12-
{{/test_cases_by_property.allergicTo}}
12+
{{end}}
1313

14-
{{#test_cases_by_property.list}}
14+
{{for testCase in testCasesByProperty.list}}
1515
[Fact(Skip = "Remove this Skip property to run this test")]
16-
public void {{test_method_name}}()
16+
public void {{testCase.testMethodName}}()
1717
{
18-
var sut = new Allergies({{input.score}});
19-
{{#isempty expected}}
18+
var sut = new Allergies({{testCase.input.score}});
19+
{{if testCase.expected.empty?}}
2020
Assert.Empty(sut.List());
2121
{{else}}
22-
Allergen[] expected = [{{#each ../expected}}Allergen.{{Capitalize .}}{{#unless @last}},{{/unless}}{{/each}}];
22+
Allergen[] expected = [
23+
{{for expected in testCase.expected}}
24+
{{testCase.expected | enum type: "Allergen"}}{{if !for.last}},{{end}}
25+
{{end}}
26+
];
2327
Assert.Equal(expected, sut.List());
24-
{{/isempty}}
28+
{{end}}
2529
}
26-
{{/test_cases_by_property.list}}
30+
{{end}}
2731
}

exercises/practice/allergies/AllergiesTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,71 +293,71 @@ public void List_when_no_allergies()
293293
public void List_when_just_eggs()
294294
{
295295
var sut = new Allergies(1);
296-
Allergen[] expected = [Allergen.Eggs];
296+
Allergen[] expected = [Allergen.["eggs"]];
297297
Assert.Equal(expected, sut.List());
298298
}
299299

300300
[Fact(Skip = "Remove this Skip property to run this test")]
301301
public void List_when_just_peanuts()
302302
{
303303
var sut = new Allergies(2);
304-
Allergen[] expected = [Allergen.Peanuts];
304+
Allergen[] expected = [Allergen.["peanuts"]];
305305
Assert.Equal(expected, sut.List());
306306
}
307307

308308
[Fact(Skip = "Remove this Skip property to run this test")]
309309
public void List_when_just_strawberries()
310310
{
311311
var sut = new Allergies(8);
312-
Allergen[] expected = [Allergen.Strawberries];
312+
Allergen[] expected = [Allergen.["strawberries"]];
313313
Assert.Equal(expected, sut.List());
314314
}
315315

316316
[Fact(Skip = "Remove this Skip property to run this test")]
317317
public void List_when_eggs_and_peanuts()
318318
{
319319
var sut = new Allergies(3);
320-
Allergen[] expected = [Allergen.Eggs, Allergen.Peanuts];
320+
Allergen[] expected = [Allergen.["eggs", "peanuts"], Allergen.["eggs", "peanuts"]];
321321
Assert.Equal(expected, sut.List());
322322
}
323323

324324
[Fact(Skip = "Remove this Skip property to run this test")]
325325
public void List_when_more_than_eggs_but_not_peanuts()
326326
{
327327
var sut = new Allergies(5);
328-
Allergen[] expected = [Allergen.Eggs, Allergen.Shellfish];
328+
Allergen[] expected = [Allergen.["eggs", "shellfish"], Allergen.["eggs", "shellfish"]];
329329
Assert.Equal(expected, sut.List());
330330
}
331331

332332
[Fact(Skip = "Remove this Skip property to run this test")]
333333
public void List_when_lots_of_stuff()
334334
{
335335
var sut = new Allergies(248);
336-
Allergen[] expected = [Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats];
336+
Allergen[] expected = [Allergen.["strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["strawberries", "tomatoes", "chocolate", "pollen", "cats"]];
337337
Assert.Equal(expected, sut.List());
338338
}
339339

340340
[Fact(Skip = "Remove this Skip property to run this test")]
341341
public void List_when_everything()
342342
{
343343
var sut = new Allergies(255);
344-
Allergen[] expected = [Allergen.Eggs, Allergen.Peanuts, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats];
344+
Allergen[] expected = [Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"]];
345345
Assert.Equal(expected, sut.List());
346346
}
347347

348348
[Fact(Skip = "Remove this Skip property to run this test")]
349349
public void List_when_no_allergen_score_parts()
350350
{
351351
var sut = new Allergies(509);
352-
Allergen[] expected = [Allergen.Eggs, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats];
352+
Allergen[] expected = [Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"], Allergen.["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"]];
353353
Assert.Equal(expected, sut.List());
354354
}
355355

356356
[Fact(Skip = "Remove this Skip property to run this test")]
357357
public void List_when_no_allergen_score_parts_without_highest_valid_score()
358358
{
359359
var sut = new Allergies(257);
360-
Allergen[] expected = [Allergen.Eggs];
360+
Allergen[] expected = [Allergen.["eggs"]];
361361
Assert.Equal(expected, sut.List());
362362
}
363363
}

exercises/practice/darts/.meta/Generator.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ using Xunit;
22

33
public class DartsTests
44
{
5-
{{#test_cases}}
6-
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7-
public void {{test_method_name}}()
5+
{{for testCase in testCases}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{testCase.testMethodName}}()
88
{
9-
Assert.Equal({{expected}}, Darts.Score({{input.x}}, {{input.y}}));
9+
Assert.Equal({{testCase.expected}}, Darts.Score({{testCase.input.x}}, {{testCase.input.y}}));
1010
}
11-
{{/test_cases}}
11+
{{end}}
1212
}

exercises/practice/darts/DartsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void Just_outside_the_middle_circle()
6565
[Fact(Skip = "Remove this Skip property to run this test")]
6666
public void Just_within_the_outer_circle()
6767
{
68-
Assert.Equal(1, Darts.Score(-7, 7));
68+
Assert.Equal(1, Darts.Score(-7.0, 7.0));
6969
}
7070

7171
[Fact(Skip = "Remove this Skip property to run this test")]

exercises/practice/difference-of-squares/.meta/Generator.tpl

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,11 @@ using Xunit;
22

33
public class DifferenceOfSquaresTests
44
{
5-
{{#test_cases_by_property.squareOfSum}}
6-
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7-
public void {{short_test_method_name}}()
5+
{{for testCase in testCases}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{testCase.shortTestMethodName}}()
88
{
9-
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateSquareOfSum({{input.number}}));
9+
Assert.Equal({{testCase.expected}}, DifferenceOfSquares.Calculate{{testCase.property | string.capitalize}}({{testCase.input.number}}));
1010
}
11-
{{/test_cases_by_property.squareOfSum}}
12-
13-
{{#test_cases_by_property.sumOfSquares}}
14-
[Fact(Skip = "Remove this Skip property to run this test")]
15-
public void {{short_test_method_name}}()
16-
{
17-
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateSumOfSquares({{input.number}}));
18-
}
19-
{{/test_cases_by_property.sumOfSquares}}
20-
21-
{{#test_cases_by_property.differenceOfSquares}}
22-
[Fact(Skip = "Remove this Skip property to run this test")]
23-
public void {{short_test_method_name}}()
24-
{
25-
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateDifferenceOfSquares({{input.number}}));
26-
}
27-
{{/test_cases_by_property.differenceOfSquares}}
11+
{{end}}
2812
}

exercises/practice/eliuds-eggs/.meta/Generator.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ using Xunit;
22

33
public class EliudsEggsTests
44
{
5-
{{#test_cases}}
6-
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7-
public void {{test_method_name}}()
5+
{{for testCase in testCases}}
6+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
7+
public void {{testCase.testMethodName}}()
88
{
9-
Assert.Equal({{expected}}, EliudsEggs.EggCount({{input.number}}));
9+
Assert.Equal({{testCase.expected}}, EliudsEggs.EggCount({{testCase.input.number}}));
1010
}
11-
{{/test_cases}}
11+
{{end}}
1212
}

0 commit comments

Comments
 (0)