Skip to content

Add test generator for wordy exercise #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exercises/wordy/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;

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

public static int Solve(string problem)
public static int Answer(string question)
{
var parsedProblem = ParseProblem(problem);
var parsedProblem = ParseProblem(question);
var firstPass = Operations[parsedProblem.Operation1](parsedProblem.X, parsedProblem.Y);
if (parsedProblem.Operation2.Length == 0)
return firstPass;
Expand Down
4 changes: 2 additions & 2 deletions exercises/wordy/Wordy.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

public static class WordProblem
public static class Wordy
{
public static int Solve(string problem)
public static int Answer(string question)
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
70 changes: 35 additions & 35 deletions exercises/wordy/WordyTest.cs
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
using System;
using Xunit;
using System;

public class WordProblemTest
public class WordyTest
{
[Fact]
public void Can_parse_and_solve_addition_problems()
public void Addition()
{
Assert.Equal(2, WordProblem.Solve("What is 1 plus 1?"));
Assert.Equal(2, Wordy.Answer("What is 1 plus 1?"));
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[Fact(Skip = "Remove to run test")]
public void Irrelevant_problems_are_not_valid()
public void Non_math_question()
{
Assert.Throws<ArgumentException>(() => WordProblem.Solve("Who is the president of the United States?"));
Assert.Throws<ArgumentException>(() => Wordy.Answer("Who is the President of the United States?"));
}
}
}
7 changes: 6 additions & 1 deletion generators/Exercises/EqualityExercise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ protected EqualityExercise(string name) : base(name)
}

protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
=> CreateEqualityTestMethod(testMethodData);
{
if (testMethodData.Options.ThrowExceptionWhenExpectedValueEquals(testMethodData.CanonicalDataCase.Expected))
return CreateExceptionTestMethod(testMethodData);

return CreateEqualityTestMethod(testMethodData);
}
}
}
15 changes: 7 additions & 8 deletions generators/Exercises/NthPrimeExercise.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Generators.Data;
using Generators.Methods;

namespace Generators.Exercises
Expand All @@ -9,15 +10,13 @@ public NthPrimeExercise() : base("nth-prime")
{
}

protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
protected override TestMethodOptions CreateTestMethodOptions(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
{
if (testMethodData.CanonicalDataCase.Expected is bool b && !b)
{
testMethodData.Options.ExceptionType = typeof(ArgumentOutOfRangeException);
return CreateExceptionTestMethod(testMethodData);
}
var testMethodOptions = base.CreateTestMethodOptions(canonicalData, canonicalDataCase, index);
testMethodOptions.ExceptionType = typeof(ArgumentOutOfRangeException);
testMethodOptions.ThrowExceptionWhenExpectedValueEquals = x => x is bool;

return CreateEqualityTestMethod(testMethodData);
}
return testMethodOptions;
}
}
}
20 changes: 20 additions & 0 deletions generators/Exercises/WordyExercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Generators.Data;
using Generators.Methods;

namespace Generators.Exercises
{
public class WordyExercise : EqualityExercise
{
public WordyExercise() : base("wordy")
{
}

protected override TestMethodOptions CreateTestMethodOptions(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
{
var testMethodOptions = base.CreateTestMethodOptions(canonicalData, canonicalDataCase, index);
testMethodOptions.ThrowExceptionWhenExpectedValueEquals = x => x is bool;

return testMethodOptions;
}
}
}
1 change: 1 addition & 0 deletions generators/Methods/TestMethodOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class TestMethodOptions
public bool UseVariableForExpected { get; set; } = false;
public Type ExceptionType { get; set; } = typeof(ArgumentException);
public TestedMethodType TestedMethodType { get; set; } = TestedMethodType.Static;
public Func<object, bool> ThrowExceptionWhenExpectedValueEquals { get; set; } = (x) => false;
}
}