Skip to content

Commit fd763f5

Browse files
Add food-chain generator (#267)
Add support for formatting multi-line strings Add support for putting expected value in variable
1 parent a581922 commit fd763f5

12 files changed

Lines changed: 256 additions & 100 deletions

exercises/food-chain/Example.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public static class FoodChain
3636
"I don't know why she swallowed the fly. Perhaps she'll die."
3737
};
3838

39-
public static string Song() => string.Join("\n\n", Enumerable.Range(1, Verses).Select(Verse));
40-
4139
public static string Verse(int number) => $"{VerseBegin(number)}\n{VerseEnd(number)}";
4240

41+
public static string Verse(int begin, int end) => string.Join("\n\n", Enumerable.Range(begin, end - begin + 1).Select(i => Verse(i)));
42+
4343
private static string VerseBegin(int number)
4444
{
4545
if (number == 1)

exercises/food-chain/FoodChain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
public static class FoodChain
44
{
5-
public static string Song()
5+
public static string Verse(int number)
66
{
77
throw new NotImplementedException("You need to implement this function.");
88
}
99

10-
public static string Verse(int number)
10+
public static string Verse(int begin, int end)
1111
{
1212
throw new NotImplementedException("You need to implement this function.");
1313
}
Lines changed: 150 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,180 @@
1-
using Xunit;
1+
using Xunit;
22

33
public class FoodChainTest
44
{
55
[Fact]
6-
public void Verse_one()
6+
public void Fly()
77
{
8-
const string expected = "I know an old lady who swallowed a fly.\n" +
9-
"I don't know why she swallowed the fly. Perhaps she'll die.";
10-
8+
var expected =
9+
"I know an old lady who swallowed a fly.\n"+
10+
"I don't know why she swallowed the fly. Perhaps she'll die.";
1111
Assert.Equal(expected, FoodChain.Verse(1));
1212
}
1313

1414
[Fact(Skip = "Remove to run test")]
15-
public void Verse_two()
15+
public void Spider()
1616
{
17-
const string expected = "I know an old lady who swallowed a spider.\n" +
18-
"It wriggled and jiggled and tickled inside her.\n" +
19-
"She swallowed the spider to catch the fly.\n" +
20-
"I don't know why she swallowed the fly. Perhaps she'll die.";
21-
17+
var expected =
18+
"I know an old lady who swallowed a spider.\n"+
19+
"It wriggled and jiggled and tickled inside her.\n"+
20+
"She swallowed the spider to catch the fly.\n"+
21+
"I don't know why she swallowed the fly. Perhaps she'll die.";
2222
Assert.Equal(expected, FoodChain.Verse(2));
2323
}
2424

2525
[Fact(Skip = "Remove to run test")]
26-
public void Verse_four()
26+
public void Bird()
2727
{
28-
const string expected = "I know an old lady who swallowed a cat.\n" +
29-
"Imagine that, to swallow a cat!\n" +
30-
"She swallowed the cat to catch the bird.\n" +
31-
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
32-
"She swallowed the spider to catch the fly.\n" +
33-
"I don't know why she swallowed the fly. Perhaps she'll die.";
28+
var expected =
29+
"I know an old lady who swallowed a bird.\n"+
30+
"How absurd to swallow a bird!\n"+
31+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
32+
"She swallowed the spider to catch the fly.\n"+
33+
"I don't know why she swallowed the fly. Perhaps she'll die.";
34+
Assert.Equal(expected, FoodChain.Verse(3));
35+
}
3436

37+
[Fact(Skip = "Remove to run test")]
38+
public void Cat()
39+
{
40+
var expected =
41+
"I know an old lady who swallowed a cat.\n"+
42+
"Imagine that, to swallow a cat!\n"+
43+
"She swallowed the cat to catch the bird.\n"+
44+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
45+
"She swallowed the spider to catch the fly.\n"+
46+
"I don't know why she swallowed the fly. Perhaps she'll die.";
3547
Assert.Equal(expected, FoodChain.Verse(4));
3648
}
3749

3850
[Fact(Skip = "Remove to run test")]
39-
public void Verse_eight()
51+
public void Dog()
4052
{
41-
const string expected = "I know an old lady who swallowed a horse.\n" +
42-
"She's dead, of course!";
53+
var expected =
54+
"I know an old lady who swallowed a dog.\n"+
55+
"What a hog, to swallow a dog!\n"+
56+
"She swallowed the dog to catch the cat.\n"+
57+
"She swallowed the cat to catch the bird.\n"+
58+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
59+
"She swallowed the spider to catch the fly.\n"+
60+
"I don't know why she swallowed the fly. Perhaps she'll die.";
61+
Assert.Equal(expected, FoodChain.Verse(5));
62+
}
63+
64+
[Fact(Skip = "Remove to run test")]
65+
public void Goat()
66+
{
67+
var expected =
68+
"I know an old lady who swallowed a goat.\n"+
69+
"Just opened her throat and swallowed a goat!\n"+
70+
"She swallowed the goat to catch the dog.\n"+
71+
"She swallowed the dog to catch the cat.\n"+
72+
"She swallowed the cat to catch the bird.\n"+
73+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
74+
"She swallowed the spider to catch the fly.\n"+
75+
"I don't know why she swallowed the fly. Perhaps she'll die.";
76+
Assert.Equal(expected, FoodChain.Verse(6));
77+
}
4378

79+
[Fact(Skip = "Remove to run test")]
80+
public void Cow()
81+
{
82+
var expected =
83+
"I know an old lady who swallowed a cow.\n"+
84+
"I don't know how she swallowed a cow!\n"+
85+
"She swallowed the cow to catch the goat.\n"+
86+
"She swallowed the goat to catch the dog.\n"+
87+
"She swallowed the dog to catch the cat.\n"+
88+
"She swallowed the cat to catch the bird.\n"+
89+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
90+
"She swallowed the spider to catch the fly.\n"+
91+
"I don't know why she swallowed the fly. Perhaps she'll die.";
92+
Assert.Equal(expected, FoodChain.Verse(7));
93+
}
94+
95+
[Fact(Skip = "Remove to run test")]
96+
public void Horse()
97+
{
98+
var expected =
99+
"I know an old lady who swallowed a horse.\n"+
100+
"She's dead, of course!";
44101
Assert.Equal(expected, FoodChain.Verse(8));
45102
}
46103

47104
[Fact(Skip = "Remove to run test")]
48-
public void Complete_song()
105+
public void Multiple_verses()
49106
{
50-
const string expected = "I know an old lady who swallowed a fly.\n" +
51-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
52-
"\n" +
53-
"I know an old lady who swallowed a spider.\n" +
54-
"It wriggled and jiggled and tickled inside her.\n" +
55-
"She swallowed the spider to catch the fly.\n" +
56-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
57-
"\n" +
58-
"I know an old lady who swallowed a bird.\n" +
59-
"How absurd to swallow a bird!\n" +
60-
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
61-
"She swallowed the spider to catch the fly.\n" +
62-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
63-
"\n" +
64-
"I know an old lady who swallowed a cat.\n" +
65-
"Imagine that, to swallow a cat!\n" +
66-
"She swallowed the cat to catch the bird.\n" +
67-
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
68-
"She swallowed the spider to catch the fly.\n" +
69-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
70-
"\n" +
71-
"I know an old lady who swallowed a dog.\n" +
72-
"What a hog, to swallow a dog!\n" +
73-
"She swallowed the dog to catch the cat.\n" +
74-
"She swallowed the cat to catch the bird.\n" +
75-
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
76-
"She swallowed the spider to catch the fly.\n" +
77-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
78-
"\n" +
79-
"I know an old lady who swallowed a goat.\n" +
80-
"Just opened her throat and swallowed a goat!\n" +
81-
"She swallowed the goat to catch the dog.\n" +
82-
"She swallowed the dog to catch the cat.\n" +
83-
"She swallowed the cat to catch the bird.\n" +
84-
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
85-
"She swallowed the spider to catch the fly.\n" +
86-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
87-
"\n" +
88-
"I know an old lady who swallowed a cow.\n" +
89-
"I don't know how she swallowed a cow!\n" +
90-
"She swallowed the cow to catch the goat.\n" +
91-
"She swallowed the goat to catch the dog.\n" +
92-
"She swallowed the dog to catch the cat.\n" +
93-
"She swallowed the cat to catch the bird.\n" +
94-
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
95-
"She swallowed the spider to catch the fly.\n" +
96-
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
97-
"\n" +
98-
"I know an old lady who swallowed a horse.\n" +
99-
"She's dead, of course!";
107+
var expected =
108+
"I know an old lady who swallowed a fly.\n"+
109+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
110+
"\n"+
111+
"I know an old lady who swallowed a spider.\n"+
112+
"It wriggled and jiggled and tickled inside her.\n"+
113+
"She swallowed the spider to catch the fly.\n"+
114+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
115+
"\n"+
116+
"I know an old lady who swallowed a bird.\n"+
117+
"How absurd to swallow a bird!\n"+
118+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
119+
"She swallowed the spider to catch the fly.\n"+
120+
"I don't know why she swallowed the fly. Perhaps she'll die.";
121+
Assert.Equal(expected, FoodChain.Verse(1, 3));
122+
}
100123

101-
Assert.Equal(expected, FoodChain.Song());
124+
[Fact(Skip = "Remove to run test")]
125+
public void Full_song()
126+
{
127+
var expected =
128+
"I know an old lady who swallowed a fly.\n"+
129+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
130+
"\n"+
131+
"I know an old lady who swallowed a spider.\n"+
132+
"It wriggled and jiggled and tickled inside her.\n"+
133+
"She swallowed the spider to catch the fly.\n"+
134+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
135+
"\n"+
136+
"I know an old lady who swallowed a bird.\n"+
137+
"How absurd to swallow a bird!\n"+
138+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
139+
"She swallowed the spider to catch the fly.\n"+
140+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
141+
"\n"+
142+
"I know an old lady who swallowed a cat.\n"+
143+
"Imagine that, to swallow a cat!\n"+
144+
"She swallowed the cat to catch the bird.\n"+
145+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
146+
"She swallowed the spider to catch the fly.\n"+
147+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
148+
"\n"+
149+
"I know an old lady who swallowed a dog.\n"+
150+
"What a hog, to swallow a dog!\n"+
151+
"She swallowed the dog to catch the cat.\n"+
152+
"She swallowed the cat to catch the bird.\n"+
153+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
154+
"She swallowed the spider to catch the fly.\n"+
155+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
156+
"\n"+
157+
"I know an old lady who swallowed a goat.\n"+
158+
"Just opened her throat and swallowed a goat!\n"+
159+
"She swallowed the goat to catch the dog.\n"+
160+
"She swallowed the dog to catch the cat.\n"+
161+
"She swallowed the cat to catch the bird.\n"+
162+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
163+
"She swallowed the spider to catch the fly.\n"+
164+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
165+
"\n"+
166+
"I know an old lady who swallowed a cow.\n"+
167+
"I don't know how she swallowed a cow!\n"+
168+
"She swallowed the cow to catch the goat.\n"+
169+
"She swallowed the goat to catch the dog.\n"+
170+
"She swallowed the dog to catch the cat.\n"+
171+
"She swallowed the cat to catch the bird.\n"+
172+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n"+
173+
"She swallowed the spider to catch the fly.\n"+
174+
"I don't know why she swallowed the fly. Perhaps she'll die.\n"+
175+
"\n"+
176+
"I know an old lady who swallowed a horse.\n"+
177+
"She's dead, of course!";
178+
Assert.Equal(expected, FoodChain.Verse(1, 8));
102179
}
103180
}

exercises/luhn/LuhnTest.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,30 @@ public void A_single_zero_is_invalid()
1414
Assert.False(Luhn.IsValid("0"));
1515
}
1616

17-
1817
[Fact(Skip = "Remove to run test")]
19-
public void A_simple_valid_SIN_that_remains_valid_if_reversed()
18+
public void A_simple_valid_sin_that_remains_valid_if_reversed()
2019
{
2120
Assert.True(Luhn.IsValid("059"));
2221
}
2322

2423
[Fact(Skip = "Remove to run test")]
25-
public void A_simple_valid_SIN_that_becomes_invalid_if_reversed()
24+
public void A_simple_valid_sin_that_becomes_invalid_if_reversed()
2625
{
2726
Assert.True(Luhn.IsValid("59"));
2827
}
2928

3029
[Fact(Skip = "Remove to run test")]
31-
public void A_valid_Canadian_SIN()
30+
public void A_valid_canadian_sin()
3231
{
3332
Assert.True(Luhn.IsValid("055 444 285"));
3433
}
3534

3635
[Fact(Skip = "Remove to run test")]
37-
public void Invalid_Canadian_SIN()
36+
public void Invalid_canadian_sin()
3837
{
3938
Assert.False(Luhn.IsValid("055 444 286"));
4039
}
4140

42-
4341
[Fact(Skip = "Remove to run test")]
4442
public void Invalid_credit_card()
4543
{
@@ -81,5 +79,4 @@ public void Input_digit_9_is_correctly_converted_to_output_digit_9()
8179
{
8280
Assert.True(Luhn.IsValid("091"));
8381
}
84-
85-
}
82+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace Generators.Data
4+
{
5+
public static class CanonicalDataValue
6+
{
7+
public static string StringArrayToString(object expected)
8+
=> string.Join("\\n\"+\n\"", ((JArray) expected).Values<string>());
9+
}
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Generators.Data;
2+
using Generators.Methods;
3+
4+
namespace Generators.Exercises
5+
{
6+
public class FoodChainExercise : EqualityExercise
7+
{
8+
public FoodChainExercise() : base("food-chain")
9+
{
10+
}
11+
12+
protected override TestMethodData CreateTestMethodData(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
13+
{
14+
var testMethodData = base.CreateTestMethodData(canonicalData, canonicalDataCase, index);
15+
16+
testMethodData.Options.UseVariableForExpected = true;
17+
testMethodData.CanonicalDataCase.Expected = CanonicalDataValue.StringArrayToString(canonicalDataCase.Expected);
18+
19+
if (testMethodData.CanonicalDataCase.Data.ContainsKey("end verse"))
20+
testMethodData.CanonicalDataCase.Input = new[] { testMethodData.CanonicalDataCase.Data["start verse"], testMethodData.CanonicalDataCase.Data["end verse"] };
21+
else
22+
testMethodData.Options.InputProperty = "start verse";
23+
24+
return testMethodData;
25+
}
26+
}
27+
}

generators/Exercises/NthPrimeExercise.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using Generators.Data;
32
using Generators.Methods;
4-
using Humanizer;
53

64
namespace Generators.Exercises
75
{
@@ -13,7 +11,7 @@ public NthPrimeExercise() : base("nth-prime")
1311

1412
protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
1513
{
16-
if (testMethodData.CanonicalDataCase.Expected is bool b)
14+
if (testMethodData.CanonicalDataCase.Expected is bool b && !b)
1715
{
1816
testMethodData.Options.ExceptionType = typeof(ArgumentOutOfRangeException);
1917
return CreateExceptionTestMethod(testMethodData);

0 commit comments

Comments
 (0)