Skip to content

Commit 2783eaf

Browse files
More-generators (#2358)
* triangle: simplify generator * Remove grouped test cases * yacht: add generator * word-count: add generator * variable-length-quantity: add generator * simple-cipher: add generator * series: add generator
1 parent d3bd5a3 commit 2783eaf

File tree

17 files changed

+250
-305
lines changed

17 files changed

+250
-305
lines changed

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

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

33
public class AllergiesTests
44
{
5-
{{for testCase in testCasesByProperty.allergicTo}}
5+
{{for testCase in testCases | property "allergicTo" }}
66
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
77
public void {{testCase.testMethodName}}()
88
{
@@ -11,7 +11,7 @@ public class AllergiesTests
1111
}
1212
{{end}}
1313

14-
{{for testCase in testCasesByProperty.list}}
14+
{{for testCase in testCases | property "list"}}
1515
[Fact(Skip = "Remove this Skip property to run this test")]
1616
public void {{testCase.testMethodName}}()
1717
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Xunit;
3+
4+
public class SeriesTests
5+
{
6+
{{for testCase in testCases}}
7+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
8+
public void {{testCase.testMethodName}}()
9+
{
10+
{{if testCase.expected.error}}
11+
Assert.Throws<ArgumentException>(() => Series.Slices({{testCase.input.series | string.literal}}, {{testCase.input.sliceLength}}));
12+
{{else}}
13+
string[] expected = {{testCase.expected}};
14+
Assert.Equal(expected, Series.Slices({{testCase.input.series | string.literal}}, {{testCase.input.sliceLength}}));
15+
{{end}}
16+
}
17+
{{end}}
18+
}
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
11
using System;
2-
using System.Linq;
32
using Xunit;
43

54
public class SeriesTests
65
{
76
[Fact]
87
public void Slices_of_one_from_one()
98
{
10-
var expected = new[] { "1" };
11-
Assert.Equal(expected, Series.Slices("1", 1).ToArray());
9+
string[] expected = ["1"];
10+
Assert.Equal(expected, Series.Slices("1", 1));
1211
}
1312

1413
[Fact(Skip = "Remove this Skip property to run this test")]
1514
public void Slices_of_one_from_two()
1615
{
17-
var expected = new[] { "1", "2" };
18-
Assert.Equal(expected, Series.Slices("12", 1).ToArray());
16+
string[] expected = ["1", "2"];
17+
Assert.Equal(expected, Series.Slices("12", 1));
1918
}
2019

2120
[Fact(Skip = "Remove this Skip property to run this test")]
2221
public void Slices_of_two()
2322
{
24-
var expected = new[] { "35" };
25-
Assert.Equal(expected, Series.Slices("35", 2).ToArray());
23+
string[] expected = ["35"];
24+
Assert.Equal(expected, Series.Slices("35", 2));
2625
}
2726

2827
[Fact(Skip = "Remove this Skip property to run this test")]
2928
public void Slices_of_two_overlap()
3029
{
31-
var expected = new[] { "91", "14", "42" };
32-
Assert.Equal(expected, Series.Slices("9142", 2).ToArray());
30+
string[] expected = ["91", "14", "42"];
31+
Assert.Equal(expected, Series.Slices("9142", 2));
3332
}
3433

3534
[Fact(Skip = "Remove this Skip property to run this test")]
3635
public void Slices_can_include_duplicates()
3736
{
38-
var expected = new[] { "777", "777", "777", "777" };
39-
Assert.Equal(expected, Series.Slices("777777", 3).ToArray());
37+
string[] expected = ["777", "777", "777", "777"];
38+
Assert.Equal(expected, Series.Slices("777777", 3));
4039
}
4140

4241
[Fact(Skip = "Remove this Skip property to run this test")]
4342
public void Slices_of_a_long_series()
4443
{
45-
var expected = new[] { "91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243" };
46-
Assert.Equal(expected, Series.Slices("918493904243", 5).ToArray());
44+
string[] expected = ["91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243"];
45+
Assert.Equal(expected, Series.Slices("918493904243", 5));
4746
}
4847

4948
[Fact(Skip = "Remove this Skip property to run this test")]
5049
public void Slice_length_is_too_large()
5150
{
52-
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 6).ToArray());
51+
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 6));
5352
}
5453

5554
[Fact(Skip = "Remove this Skip property to run this test")]
5655
public void Slice_length_is_way_too_large()
5756
{
58-
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 42).ToArray());
57+
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 42));
5958
}
6059

6160
[Fact(Skip = "Remove this Skip property to run this test")]
6261
public void Slice_length_cannot_be_zero()
6362
{
64-
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 0).ToArray());
63+
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 0));
6564
}
6665

6766
[Fact(Skip = "Remove this Skip property to run this test")]
6867
public void Slice_length_cannot_be_negative()
6968
{
70-
Assert.Throws<ArgumentException>(() => Series.Slices("123", -1).ToArray());
69+
Assert.Throws<ArgumentException>(() => Series.Slices("123", -1));
7170
}
7271

7372
[Fact(Skip = "Remove this Skip property to run this test")]
7473
public void Empty_series_is_invalid()
7574
{
76-
Assert.Throws<ArgumentException>(() => Series.Slices("", 1).ToArray());
75+
Assert.Throws<ArgumentException>(() => Series.Slices("", 1));
7776
}
7877
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Xunit;
2+
3+
{{func decode_arg}}
4+
{{if $0.input.ciphertext == "cipher.encode"}}
5+
sut.Encode({{$0.input.plaintext | string.literal}})
6+
{{else if $0.input.ciphertext == "cipher.key.substring(0, expected.length)"}}
7+
sut.Key.Substring(0, 10)
8+
{{else}}
9+
{{$0.input.ciphertext | string.literal}}
10+
{{end}}
11+
{{end}}
12+
13+
{{func expected_value}}
14+
{{if $0 == "cipher.key.substring(0, plaintext.length)"}}
15+
sut.Key.Substring(0, 10)
16+
{{else}}
17+
{{ $0 | string.literal }}
18+
{{end}}
19+
{{end}}
20+
21+
public class SimpleCipherTests
22+
{
23+
{{for testCase in testCases}}
24+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
25+
public void {{testCase.testMethodName}}()
26+
{
27+
var sut = new SimpleCipher({{if testCase.input.key}}{{testCase.input.key | string.literal}}{{end}});
28+
{{if testCase.property == "encode"}}
29+
Assert.Equal({{testCase.expected | expected_value}}, sut.Encode({{testCase.input.plaintext | string.literal}}));
30+
{{else if testCase.property == "decode"}}
31+
Assert.Equal({{testCase.expected | string.literal}}, sut.Decode({{testCase | decode_arg}}));
32+
{{else if testCase.property == "key"}}
33+
Assert.Matches({{testCase.expected.match | string.literal}}, sut.Key);
34+
{{end}}
35+
}
36+
{{end}}
37+
}

exercises/practice/simple-cipher/SimpleCipherTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using Xunit;
32

43
public class SimpleCipherTests
@@ -18,7 +17,7 @@ public void Random_key_cipher_can_decode()
1817
}
1918

2019
[Fact(Skip = "Remove this Skip property to run this test")]
21-
public void Random_key_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
20+
public void Random_key_cipher_is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
2221
{
2322
var sut = new SimpleCipher();
2423
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
@@ -46,7 +45,7 @@ public void Substitution_cipher_can_decode()
4645
}
4746

4847
[Fact(Skip = "Remove this Skip property to run this test")]
49-
public void Substitution_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
48+
public void Substitution_cipher_is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
5049
{
5150
var sut = new SimpleCipher("abcdefghij");
5251
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));

exercises/practice/triangle/.meta/Generator.tpl

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

33
public class TriangleTests
44
{
5-
{{for testCase in testCasesByProperty.equilateral}}
5+
{{for testCase in testCases}}
66
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
77
public void {{testCase.testMethodName}}()
88
{
9-
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.IsEquilateral({{testCase.input.sides | array.join ", "}}));
10-
}
11-
{{end}}
12-
13-
{{for testCase in testCasesByProperty.isosceles}}
14-
[Fact(Skip = "Remove this Skip property to run this test")]
15-
public void {{testCase.testMethodName}}()
16-
{
17-
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.IsIsosceles({{testCase.input.sides | array.join ", "}}));
18-
}
19-
{{end}}
20-
21-
{{for testCase in testCasesByProperty.scalene}}
22-
[Fact(Skip = "Remove this Skip property to run this test")]
23-
public void {{testCase.testMethodName}}()
24-
{
25-
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.IsScalene({{testCase.input.sides | array.join ", "}}));
9+
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.Is{{testCase.property | string.capitalize}}({{testCase.input.sides | array.join ", "}}));
2610
}
2711
{{end}}
2812
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using Xunit;
3+
4+
public class VariableLengthQuantityTests
5+
{
6+
{{for testCase in testCases}}
7+
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
8+
public void {{testCase.testMethodName}}()
9+
{
10+
uint[] integers = [
11+
{{for integer in testCase.input.integers}}
12+
{{integer}}{{if !for.last}},{{end}}
13+
{{end}}
14+
];
15+
{{if testCase.expected.error}}
16+
Assert.Throws<InvalidOperationException>(() => VariableLengthQuantity.{{testCase.property | string.capitalize}}(integers));
17+
{{else}}
18+
uint[] expected = [
19+
{{for expected in testCase.expected}}
20+
{{expected}}{{if !for.last}},{{end}}
21+
{{end}}
22+
];
23+
Assert.Equal(expected, VariableLengthQuantity.{{testCase.property | string.capitalize}}(integers));
24+
{{end}}
25+
}
26+
{{end}}
27+
}

0 commit comments

Comments
 (0)