Let's practice TDD
Let's write some code using TDD. We will do that using a Code kata. Katas are exercises that allow us to practice coding practices to get better at applying them and using them.
The one we will use in this case is the String Calculator Kata.
Each section includes the answer to the exercise but only reveal if you are stuck. Practice makes perfect so give it a go.
The String Calculator Kata
1. Create a simple String calculator with the following method signature
int Add(string numbers)
The method can take up to two numbers, separated by commas, and will return their sum.
For example <empty-string>
or 1
or 1,2
as inputs. (For an empty string it should return 0)
2. Allow the Add
method to handle an unknown amount of numbers
3. Allow the Add
method to handle new lines between numbers (on top of commas)
- The following input is ok:
1\n2,3
(will equal 6) - The following input is NOT ok:
1,\n
(no need to prove it - just clarifying)
4. Support different delimiters
To change the default delimiter, the beginning of the string will contain a separate like that looks like this: //[delimiter]\n[numbers…]
.
For example //;\n1;2
should return 3
where the default delimiter is ;
.
The first line is optional and its absence should still default to ,
. All existing scenarios should still be supported.