Skip to content

Commit 3804d8b

Browse files
Add more generators (#2375)
* parallel-letter-frequencies: add generator * beer-song: add generator * diffie-hellman: add generator * etl: add generator * palindrome-products: add generator * two-bucket: add generator * kindergarten-garden: add generator * robot-simulator: add generator * ledger: add generator * grep: add generator * Remove deps [no important files changed]
1 parent 0de1537 commit 3804d8b

File tree

30 files changed

+809
-777
lines changed

30 files changed

+809
-777
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Xunit;
2+
3+
public class {{ testClass }}
4+
{
5+
{{- for test in tests }}
6+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
7+
public void {{ test.testMethod }}()
8+
{
9+
var expected =
10+
{{- for line in test.expected }}
11+
{{ if for.last -}}
12+
{{ line | string.literal -}};
13+
{{- else -}}
14+
{{ line | string.append "\n" | string.literal }} +
15+
{{- end -}}
16+
{{- end }}
17+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.startBottles }}, {{ test.input.takeDown }}));
18+
}
19+
{{ end -}}
20+
}

exercises/practice/beer-song/BeerSongTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,54 @@
33
public class BeerSongTests
44
{
55
[Fact]
6-
public void First_generic_verse()
6+
public void Verse_single_verse_first_generic_verse()
77
{
8-
var expected =
8+
var expected =
99
"99 bottles of beer on the wall, 99 bottles of beer.\n" +
1010
"Take one down and pass it around, 98 bottles of beer on the wall.";
1111
Assert.Equal(expected, BeerSong.Recite(99, 1));
1212
}
1313

1414
[Fact(Skip = "Remove this Skip property to run this test")]
15-
public void Last_generic_verse()
15+
public void Verse_single_verse_last_generic_verse()
1616
{
17-
var expected =
17+
var expected =
1818
"3 bottles of beer on the wall, 3 bottles of beer.\n" +
1919
"Take one down and pass it around, 2 bottles of beer on the wall.";
2020
Assert.Equal(expected, BeerSong.Recite(3, 1));
2121
}
2222

2323
[Fact(Skip = "Remove this Skip property to run this test")]
24-
public void Verse_with_2_bottles()
24+
public void Verse_single_verse_verse_with_2_bottles()
2525
{
26-
var expected =
26+
var expected =
2727
"2 bottles of beer on the wall, 2 bottles of beer.\n" +
2828
"Take one down and pass it around, 1 bottle of beer on the wall.";
2929
Assert.Equal(expected, BeerSong.Recite(2, 1));
3030
}
3131

3232
[Fact(Skip = "Remove this Skip property to run this test")]
33-
public void Verse_with_1_bottle()
33+
public void Verse_single_verse_verse_with_1_bottle()
3434
{
35-
var expected =
35+
var expected =
3636
"1 bottle of beer on the wall, 1 bottle of beer.\n" +
3737
"Take it down and pass it around, no more bottles of beer on the wall.";
3838
Assert.Equal(expected, BeerSong.Recite(1, 1));
3939
}
4040

4141
[Fact(Skip = "Remove this Skip property to run this test")]
42-
public void Verse_with_0_bottles()
42+
public void Verse_single_verse_verse_with_0_bottles()
4343
{
44-
var expected =
44+
var expected =
4545
"No more bottles of beer on the wall, no more bottles of beer.\n" +
4646
"Go to the store and buy some more, 99 bottles of beer on the wall.";
4747
Assert.Equal(expected, BeerSong.Recite(0, 1));
4848
}
4949

5050
[Fact(Skip = "Remove this Skip property to run this test")]
51-
public void First_two_verses()
51+
public void Lyrics_multiple_verses_first_two_verses()
5252
{
53-
var expected =
53+
var expected =
5454
"99 bottles of beer on the wall, 99 bottles of beer.\n" +
5555
"Take one down and pass it around, 98 bottles of beer on the wall.\n" +
5656
"\n" +
@@ -60,9 +60,9 @@ public void First_two_verses()
6060
}
6161

6262
[Fact(Skip = "Remove this Skip property to run this test")]
63-
public void Last_three_verses()
63+
public void Lyrics_multiple_verses_last_three_verses()
6464
{
65-
var expected =
65+
var expected =
6666
"2 bottles of beer on the wall, 2 bottles of beer.\n" +
6767
"Take one down and pass it around, 1 bottle of beer on the wall.\n" +
6868
"\n" +
@@ -75,9 +75,9 @@ public void Last_three_verses()
7575
}
7676

7777
[Fact(Skip = "Remove this Skip property to run this test")]
78-
public void All_verses()
78+
public void Lyrics_multiple_verses_all_verses()
7979
{
80-
var expected =
80+
var expected =
8181
"99 bottles of beer on the wall, 99 bottles of beer.\n" +
8282
"Take one down and pass it around, 98 bottles of beer on the wall.\n" +
8383
"\n" +
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{{ func toarg(argument)
2+
if (object.typeof argument) == "number"
3+
ret $"new BigInteger({argument})"
4+
end
5+
6+
ret $"DiffieHellman.{string.capitalize argument}"
7+
end }}
8+
9+
using System.Linq;
10+
using System.Numerics;
11+
using Xunit;
12+
13+
public class {{ testClass }}
14+
{
15+
{{- for test in tests }}
16+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
17+
public void {{ test.testMethod }}()
18+
{
19+
{{- if test.property == "privateKeyIsInRange" }}
20+
var p = {{ 7919 | toarg }};
21+
var privateKeys = Enumerable.Range(0, 1000).Select(_ => {{ testedClass }}.PrivateKey(p));
22+
foreach (var privateKey in privateKeys)
23+
{
24+
Assert.InRange(privateKey, {{ 1 | toarg }}, p);
25+
}
26+
{{- else if test.property == "privateKeyIsRandom" }}
27+
var p = {{ 7919 | toarg }};
28+
var privateKeys = Enumerable.Range(0, 1000).Select(_ => DiffieHellman.PrivateKey(p)).ToArray();
29+
Assert.InRange(privateKeys.Distinct().Count(), privateKeys.Length - 100, privateKeys.Length);
30+
{{- else if test.property == "keyExchange" }}
31+
{{- for key in test.input | object.keys }}
32+
var {{key}} = {{ test.input[key] | toarg }};
33+
{{- end }}
34+
Assert.Equal(secretA, secretB);
35+
{{- else }}
36+
{{- for key in test.input | object.keys }}
37+
var {{key}} = {{ test.input[key] | toarg }};
38+
{{- end }}
39+
Assert.Equal({{ test.expected | toarg }}, {{ testedClass }}.{{ test.testedMethod }}({{ test.input | object.keys | array.join ", " }}));
40+
{{ end -}}
41+
}
42+
{{ end -}}
43+
}

exercises/practice/diffie-hellman/DiffieHellmanTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DiffieHellmanTests
88
public void Private_key_is_greater_than_1_and_less_than_p()
99
{
1010
var p = new BigInteger(7919);
11-
var privateKeys = Enumerable.Range(0, 1000).Select(_ => DiffieHellman.PrivateKey(p)).ToArray();
11+
var privateKeys = Enumerable.Range(0, 1000).Select(_ => DiffieHellman.PrivateKey(p));
1212
foreach (var privateKey in privateKeys)
1313
{
1414
Assert.InRange(privateKey, new BigInteger(1), p);
@@ -42,7 +42,7 @@ public void Can_calculate_public_key_when_given_a_different_private_key()
4242
}
4343

4444
[Fact(Skip = "Remove this Skip property to run this test")]
45-
public void Can_calculate_secret_using_other_partys_public_key()
45+
public void Can_calculate_secret_using_other_party_s_public_key()
4646
{
4747
var p = new BigInteger(23);
4848
var theirPublicKey = new BigInteger(19);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
public class {{ testClass }}
5+
{
6+
{{- for test in tests }}
7+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
8+
public void {{ test.testMethod }}()
9+
{
10+
var legacy = new Dictionary<int, string[]>
11+
{
12+
{{- for key in test.input.legacy | object.keys }}
13+
[{{ key }}] = {{ test.input.legacy[key] }}{{- if !for.last }},{{ end -}}
14+
{{ end -}}
15+
};
16+
var expected = new Dictionary<string, int>
17+
{
18+
{{- for key in test.expected | object.keys }}
19+
[{{ key | string.literal }}] = {{ test.expected[key] }}{{- if !for.last }},{{ end -}}
20+
{{ end -}}
21+
};
22+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(legacy));
23+
}
24+
{{ end -}}
25+
}

exercises/practice/etl/EtlTests.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ public class EtlTests
66
[Fact]
77
public void Single_letter()
88
{
9-
var input = new Dictionary<int, string[]>
9+
var legacy = new Dictionary<int, string[]>
1010
{
11-
[1] = new[] { "A" }
11+
[1] = ["A"]
1212
};
1313
var expected = new Dictionary<string, int>
1414
{
1515
["a"] = 1
1616
};
17-
Assert.Equal(expected, Etl.Transform(input));
17+
Assert.Equal(expected, Etl.Transform(legacy));
1818
}
1919

2020
[Fact(Skip = "Remove this Skip property to run this test")]
2121
public void Single_score_with_multiple_letters()
2222
{
23-
var input = new Dictionary<int, string[]>
23+
var legacy = new Dictionary<int, string[]>
2424
{
25-
[1] = new[] { "A", "E", "I", "O", "U" }
25+
[1] = ["A", "E", "I", "O", "U"]
2626
};
2727
var expected = new Dictionary<string, int>
2828
{
@@ -32,16 +32,16 @@ public void Single_score_with_multiple_letters()
3232
["o"] = 1,
3333
["u"] = 1
3434
};
35-
Assert.Equal(expected, Etl.Transform(input));
35+
Assert.Equal(expected, Etl.Transform(legacy));
3636
}
3737

3838
[Fact(Skip = "Remove this Skip property to run this test")]
3939
public void Multiple_scores_with_multiple_letters()
4040
{
41-
var input = new Dictionary<int, string[]>
41+
var legacy = new Dictionary<int, string[]>
4242
{
43-
[1] = new[] { "A", "E" },
44-
[2] = new[] { "D", "G" }
43+
[1] = ["A", "E"],
44+
[2] = ["D", "G"]
4545
};
4646
var expected = new Dictionary<string, int>
4747
{
@@ -50,21 +50,21 @@ public void Multiple_scores_with_multiple_letters()
5050
["e"] = 1,
5151
["g"] = 2
5252
};
53-
Assert.Equal(expected, Etl.Transform(input));
53+
Assert.Equal(expected, Etl.Transform(legacy));
5454
}
5555

5656
[Fact(Skip = "Remove this Skip property to run this test")]
5757
public void Multiple_scores_with_differing_numbers_of_letters()
5858
{
59-
var input = new Dictionary<int, string[]>
59+
var legacy = new Dictionary<int, string[]>
6060
{
61-
[1] = new[] { "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" },
62-
[2] = new[] { "D", "G" },
63-
[3] = new[] { "B", "C", "M", "P" },
64-
[4] = new[] { "F", "H", "V", "W", "Y" },
65-
[5] = new[] { "K" },
66-
[8] = new[] { "J", "X" },
67-
[10] = new[] { "Q", "Z" }
61+
[1] = ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
62+
[2] = ["D", "G"],
63+
[3] = ["B", "C", "M", "P"],
64+
[4] = ["F", "H", "V", "W", "Y"],
65+
[5] = ["K"],
66+
[8] = ["J", "X"],
67+
[10] = ["Q", "Z"]
6868
};
6969
var expected = new Dictionary<string, int>
7070
{
@@ -95,6 +95,6 @@ public void Multiple_scores_with_differing_numbers_of_letters()
9595
["y"] = 4,
9696
["z"] = 10
9797
};
98-
Assert.Equal(expected, Etl.Transform(input));
98+
Assert.Equal(expected, Etl.Transform(legacy));
9999
}
100100
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.IO;
3+
using Xunit;
4+
5+
public class {{ testClass }} : IDisposable
6+
{
7+
{{- for test in tests }}
8+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
9+
public void {{ test.shortTestMethod }}()
10+
{
11+
var pattern = {{ test.input.pattern | string.literal }};
12+
var flags = {{ test.input.flags | array.join " " | string.literal }};
13+
string[] files = {{ test.input.files }};
14+
{{- if test.expected.empty? }}
15+
var expected = "";
16+
{{- else }}
17+
var expected =
18+
{{- for line in test.expected }}
19+
{{ if for.last -}}
20+
{{ line | string.literal -}};
21+
{{- else -}}
22+
{{ line | string.append "\n" | string.literal }} +
23+
{{- end -}}
24+
{{- end -}}
25+
{{ end }}
26+
Assert.Equal(expected, {{ testedClass }}.Match(pattern, flags, files));
27+
}
28+
{{ end }}
29+
private const string IliadFileName = "iliad.txt";
30+
private const string IliadContents =
31+
"Achilles sing, O Goddess! Peleus' son;\n" +
32+
"His wrath pernicious, who ten thousand woes\n" +
33+
"Caused to Achaia's host, sent many a soul\n" +
34+
"Illustrious into Ades premature,\n" +
35+
"And Heroes gave (so stood the will of Jove)\n" +
36+
"To dogs and to all ravening fowls a prey,\n" +
37+
"When fierce dispute had separated once\n" +
38+
"The noble Chief Achilles from the son\n" +
39+
"Of Atreus, Agamemnon, King of men.\n";
40+
41+
private const string MidsummerNightFileName = "midsummer-night.txt";
42+
private const string MidsummerNightContents =
43+
"I do entreat your grace to pardon me.\n" +
44+
"I know not by what power I am made bold,\n" +
45+
"Nor how it may concern my modesty,\n" +
46+
"In such a presence here to plead my thoughts;\n" +
47+
"But I beseech your grace that I may know\n" +
48+
"The worst that may befall me in this case,\n" +
49+
"If I refuse to wed Demetrius.\n";
50+
51+
private const string ParadiseLostFileName = "paradise-lost.txt";
52+
private const string ParadiseLostContents =
53+
"Of Mans First Disobedience, and the Fruit\n" +
54+
"Of that Forbidden Tree, whose mortal tast\n" +
55+
"Brought Death into the World, and all our woe,\n" +
56+
"With loss of Eden, till one greater Man\n" +
57+
"Restore us, and regain the blissful Seat,\n" +
58+
"Sing Heav'nly Muse, that on the secret top\n" +
59+
"Of Oreb, or of Sinai, didst inspire\n" +
60+
"That Shepherd, who first taught the chosen Seed\n";
61+
62+
public {{ testClass }}()
63+
{
64+
Directory.SetCurrentDirectory(Path.GetTempPath());
65+
File.WriteAllText(IliadFileName, IliadContents);
66+
File.WriteAllText(MidsummerNightFileName, MidsummerNightContents);
67+
File.WriteAllText(ParadiseLostFileName, ParadiseLostContents);
68+
}
69+
70+
public void Dispose()
71+
{
72+
Directory.SetCurrentDirectory(Path.GetTempPath());
73+
File.Delete(IliadFileName);
74+
File.Delete(MidsummerNightFileName);
75+
File.Delete(ParadiseLostFileName);
76+
}
77+
}

0 commit comments

Comments
 (0)