Skip to content

Commit f3432d5

Browse files
Add test generator for wordy exercise (#268)
Allow more flexible specifying of exception throwing values
1 parent fd763f5 commit f3432d5

File tree

7 files changed

+74
-49
lines changed

7 files changed

+74
-49
lines changed

exercises/wordy/Example.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text.RegularExpressions;
44

5-
public static class WordProblem
5+
public static class Wordy
66
{
77
private static readonly Regex VariableAndOperatorGroupsRegex =
88
new Regex(string.Format(@"{0} {1} {0}\s*{1}*\s*{0}*", @"(-?\d+)", "(plus|minus|multiplied by|divided by)"));
@@ -14,9 +14,9 @@ public static class WordProblem
1414
{ "divided by", (x, y) => x / y },
1515
};
1616

17-
public static int Solve(string problem)
17+
public static int Answer(string question)
1818
{
19-
var parsedProblem = ParseProblem(problem);
19+
var parsedProblem = ParseProblem(question);
2020
var firstPass = Operations[parsedProblem.Operation1](parsedProblem.X, parsedProblem.Y);
2121
if (parsedProblem.Operation2.Length == 0)
2222
return firstPass;

exercises/wordy/Wordy.cs

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

3-
public static class WordProblem
3+
public static class Wordy
44
{
5-
public static int Solve(string problem)
5+
public static int Answer(string question)
66
{
77
throw new NotImplementedException("You need to implement this function.");
88
}

exercises/wordy/WordyTest.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,101 @@
1-
using System;
21
using Xunit;
2+
using System;
33

4-
public class WordProblemTest
4+
public class WordyTest
55
{
66
[Fact]
7-
public void Can_parse_and_solve_addition_problems()
7+
public void Addition()
88
{
9-
Assert.Equal(2, WordProblem.Solve("What is 1 plus 1?"));
9+
Assert.Equal(2, Wordy.Answer("What is 1 plus 1?"));
1010
}
1111

1212
[Fact(Skip = "Remove to run test")]
13-
public void Can_add_double_digit_numbers()
13+
public void More_addition()
1414
{
15-
Assert.Equal(55, WordProblem.Solve("What is 53 plus 2?"));
15+
Assert.Equal(55, Wordy.Answer("What is 53 plus 2?"));
1616
}
1717

1818
[Fact(Skip = "Remove to run test")]
19-
public void Can_add_negative_numbers()
19+
public void Addition_with_negative_numbers()
2020
{
21-
Assert.Equal(-11, WordProblem.Solve("What is -1 plus -10?"));
21+
Assert.Equal(-11, Wordy.Answer("What is -1 plus -10?"));
2222
}
2323

2424
[Fact(Skip = "Remove to run test")]
25-
public void Can_add_large_numbers()
25+
public void Large_addition()
2626
{
27-
Assert.Equal(45801, WordProblem.Solve("What is 123 plus 45678?"));
27+
Assert.Equal(45801, Wordy.Answer("What is 123 plus 45678?"));
2828
}
2929

3030
[Fact(Skip = "Remove to run test")]
31-
public void Can_parse_and_solve_subtraction_problems()
31+
public void Subtraction()
3232
{
33-
Assert.Equal(16, WordProblem.Solve("What is 4 minus -12"));
33+
Assert.Equal(16, Wordy.Answer("What is 4 minus -12?"));
3434
}
3535

3636
[Fact(Skip = "Remove to run test")]
37-
public void Can_parse_and_solve_multiplication_problems()
37+
public void Multiplication()
3838
{
39-
Assert.Equal(-75, WordProblem.Solve("What is -3 multiplied by 25?"));
39+
Assert.Equal(-75, Wordy.Answer("What is -3 multiplied by 25?"));
4040
}
4141

4242
[Fact(Skip = "Remove to run test")]
43-
public void Can_parse_and_solve_division_problems()
43+
public void Division()
4444
{
45-
Assert.Equal(-11, WordProblem.Solve("What is 33 divided by -3?"));
45+
Assert.Equal(-11, Wordy.Answer("What is 33 divided by -3?"));
4646
}
4747

4848
[Fact(Skip = "Remove to run test")]
49-
public void Can_add_twice()
49+
public void Multiple_additions()
5050
{
51-
Assert.Equal(3, WordProblem.Solve("What is 1 plus 1 plus 1?"));
51+
Assert.Equal(3, Wordy.Answer("What is 1 plus 1 plus 1?"));
5252
}
5353

5454
[Fact(Skip = "Remove to run test")]
55-
public void Can_add_then_subtract()
55+
public void Addition_and_subtraction()
5656
{
57-
Assert.Equal(8, WordProblem.Solve("What is 1 plus 5 minus -2?"));
57+
Assert.Equal(8, Wordy.Answer("What is 1 plus 5 minus -2?"));
5858
}
5959

6060
[Fact(Skip = "Remove to run test")]
61-
public void Can_subtract_twice()
61+
public void Multiple_subtraction()
6262
{
63-
Assert.Equal(3, WordProblem.Solve("What is 20 minus 4 minus 13?"));
63+
Assert.Equal(3, Wordy.Answer("What is 20 minus 4 minus 13?"));
6464
}
6565

6666
[Fact(Skip = "Remove to run test")]
67-
public void Can_subtract_then_add()
67+
public void Subtraction_then_addition()
6868
{
69-
Assert.Equal(14, WordProblem.Solve("What is 17 minus 6 plus 3?"));
69+
Assert.Equal(14, Wordy.Answer("What is 17 minus 6 plus 3?"));
7070
}
7171

7272
[Fact(Skip = "Remove to run test")]
73-
public void Can_multiply_twice()
73+
public void Multiple_multiplication()
7474
{
75-
Assert.Equal(-12, WordProblem.Solve("What is 2 multiplied by -2 multiplied by 3?"));
75+
Assert.Equal(-12, Wordy.Answer("What is 2 multiplied by -2 multiplied by 3?"));
7676
}
7777

7878
[Fact(Skip = "Remove to run test")]
79-
public void Can_add_then_multiply()
79+
public void Addition_and_multiplication()
8080
{
81-
Assert.Equal(-8, WordProblem.Solve("What is -3 plus 7 multiplied by -2?"));
81+
Assert.Equal(-8, Wordy.Answer("What is -3 plus 7 multiplied by -2?"));
8282
}
8383

8484
[Fact(Skip = "Remove to run test")]
85-
public void Can_divide_twice()
85+
public void Multiple_division()
8686
{
87-
Assert.Equal(2, WordProblem.Solve("What is -12 divided by 2 divided by -3?"));
87+
Assert.Equal(2, Wordy.Answer("What is -12 divided by 2 divided by -3?"));
8888
}
8989

9090
[Fact(Skip = "Remove to run test")]
91-
public void Cubed_is_too_advanced()
91+
public void Unknown_operation()
9292
{
93-
Assert.Throws<ArgumentException>(() => WordProblem.Solve("What is 53 cubed?"));
93+
Assert.Throws<ArgumentException>(() => Wordy.Answer("What is 52 cubed?"));
9494
}
9595

9696
[Fact(Skip = "Remove to run test")]
97-
public void Irrelevant_problems_are_not_valid()
97+
public void Non_math_question()
9898
{
99-
Assert.Throws<ArgumentException>(() => WordProblem.Solve("Who is the president of the United States?"));
99+
Assert.Throws<ArgumentException>(() => Wordy.Answer("Who is the President of the United States?"));
100100
}
101-
}
101+
}

generators/Exercises/EqualityExercise.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ protected EqualityExercise(string name) : base(name)
99
}
1010

1111
protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
12-
=> CreateEqualityTestMethod(testMethodData);
12+
{
13+
if (testMethodData.Options.ThrowExceptionWhenExpectedValueEquals(testMethodData.CanonicalDataCase.Expected))
14+
return CreateExceptionTestMethod(testMethodData);
15+
16+
return CreateEqualityTestMethod(testMethodData);
17+
}
1318
}
1419
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Generators.Data;
23
using Generators.Methods;
34

45
namespace Generators.Exercises
@@ -9,15 +10,13 @@ public NthPrimeExercise() : base("nth-prime")
910
{
1011
}
1112

12-
protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
13+
protected override TestMethodOptions CreateTestMethodOptions(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
1314
{
14-
if (testMethodData.CanonicalDataCase.Expected is bool b && !b)
15-
{
16-
testMethodData.Options.ExceptionType = typeof(ArgumentOutOfRangeException);
17-
return CreateExceptionTestMethod(testMethodData);
18-
}
15+
var testMethodOptions = base.CreateTestMethodOptions(canonicalData, canonicalDataCase, index);
16+
testMethodOptions.ExceptionType = typeof(ArgumentOutOfRangeException);
17+
testMethodOptions.ThrowExceptionWhenExpectedValueEquals = x => x is bool;
1918

20-
return CreateEqualityTestMethod(testMethodData);
21-
}
19+
return testMethodOptions;
20+
}
2221
}
2322
}

generators/Exercises/WordyExercise.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Generators.Data;
2+
using Generators.Methods;
3+
4+
namespace Generators.Exercises
5+
{
6+
public class WordyExercise : EqualityExercise
7+
{
8+
public WordyExercise() : base("wordy")
9+
{
10+
}
11+
12+
protected override TestMethodOptions CreateTestMethodOptions(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
13+
{
14+
var testMethodOptions = base.CreateTestMethodOptions(canonicalData, canonicalDataCase, index);
15+
testMethodOptions.ThrowExceptionWhenExpectedValueEquals = x => x is bool;
16+
17+
return testMethodOptions;
18+
}
19+
}
20+
}

generators/Methods/TestMethodOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class TestMethodOptions
1010
public bool UseVariableForExpected { get; set; } = false;
1111
public Type ExceptionType { get; set; } = typeof(ArgumentException);
1212
public TestedMethodType TestedMethodType { get; set; } = TestedMethodType.Static;
13+
public Func<object, bool> ThrowExceptionWhenExpectedValueEquals { get; set; } = (x) => false;
1314
}
1415
}

0 commit comments

Comments
 (0)