Note: This is adapted from Roy Osherove's TDD Kata Here
To practice programming, yes.
But also to practice Test-Driven Development.
In the project StringCalculator
you have two source code files.
One is StringCalculatorTests
and the other is StringCalculator
The StringCalculator
may only have one public method on it, and it already does.
public int Add(string numbers)
{
return -42;
}
Your task is, by writing tests, to add the following functionality:
Right now, that is failing. The test
[Fact]
public void EmptyStringReturnsZero()
{
var calculator = new StringCalculator();
var result = calculator.Add("");
Assert.Equal(0, result);
}
Modify the Add
method to make this test pass. Don't worry about the next test.
That 'Don't worry about the next test` is the secret sauce of this Kata. Play dumb. Even if you've done it a million times. Write just enough to get this test to pass.
The next one is when you pass a single digit to StringCalculator#Add
, it returns that digit as a number.
For example:
int answer = calculator.Add("2");
return 2
in the inswer.
int answer = calculator.Add("212");
Returns 212
in the answer.
The next test should allow you to pass two numbers in.
int answer = calculator.Add("1,2");
Should yield 3
And
int answer = calculator.Add("212,10");
Should yield 222
Safety Check - if when you are done with this step you can pass in multiple numbers (not just two) you have failed the kata!
For example, this would be "bad" (no need to write a test for it)
int answer = calculator.Add("1,2,3");
Yielding 6
.
That's the next part.
You saw this coming (but you didn't already write the code, right?... RIGHT?)
int answer = calculator.Add("1,2,3");
Yields 6
int answer = calculator.Add("1,2,3,4,5,6,7,8,9");
Yields 45
int answer = calculator.Add("1\n2\n3");
Yields 6
Note: the
\n
is a literal for a "new line"
Make sure all the previous tests still pass.
Also, mixing delimeters is allowed.
int answer = calculator.Add("1\n2,3");
Also yields 6
.