TDD Kata - String Calculator [http://osherove.com/kata]
- TRY NOT TO READ AHEAD
- DO ONE TASK AT A TIME
- THE TRICK IS TO LEARN TO WORK INCREMENTALLY
- TEST FIRST
- REMEMBER TO REFACTOR AFTER EACH TEST PASSES
Create a class with a single method, add, which receives a string and returns an integer. This is the class we will be testing.
For example
"" == 0
For example
"1" == 1
For example
"1,2" == 3
The following input is ok:
"1\n2,3" == 6
The following is INVALID input, so do not expect it:
"1,\n"
To change a delimiter, the beginning of the string will contain a separate line that looks like this:
"//[delimiter]\n[numbers...]"
For example, since the default delimiter is ‘;’:
"//;\n1;2" == 3
Note: All existing scenarios and tests should still be supported
7. Throw an exception if a negative number is included, the exception message should contain the negative number that was included
For example:
"1001,2" == 2
A multi-character delimiter will be surrounded by square brackets, formatted as:
"//[delimiter]\n"
For example:
"//[***]\n1***2***3" == 6
Multiple delimiters will be surrounded by square brackets one after another, formatted as:
"//[delim1][delim2]\n"
For example:
"//[*][%]\n1*2%3" == 6
You reached the end of the exercise. Hopefully you have been refactoring as you go; but consider taking some time to reflect on how you might improve the maintainability or understandability of your code.