This kata is designed to help you practice and learn to use TDD in a methodical way. To that end, please follow these rules:
- Try not to read ahead.
- Do one task at a time.
- Make sure you only test for correct inputs. there is no need to test for invalid inputs for this kata.
- Always write tests before making any changes.
String Calculator Kata Steps (step-by-step)
- Create a
StringCalculator
object with a methodint Add(string numbers)
, taking a string parameter and returning a number.- The numbers parameter can include 0, 1, or 2 integers (e.g. “”, “1”, “1,2”).
- The return value should be the sum of the numbers given in the numbers string.
- Start with the simplest test case of an empty string, then 1 number, then 2.
- Solve things as simply as possible!
- Note: An empty string should return a sum of 0.
- Remember to refactor after each test.
- Allow the
Add
method to handle an unknown quantity of numbers (in the string). - Allow the
Add
method to handle new lines between numbers (optionally instead of the commas).- Example: “4\n5,6” returns 15.
- Implement support for different delimiters between the numbers in the string.
- To change the delimiter, the beginning of the string should be a separate line formatted like this:
"//[delimiter]\n[numbers]"
- Example:
"//;\n1;2"
returns 3 (the delimiter is “;”). - Note: This first line is optional; all existing scenarios should still work.
- To change the delimiter, the beginning of the string should be a separate line formatted like this:
- Calling
Add
with a negative number will throw an exception message"Negatives not allowed: "
followed by the negative number that was in the list of nubmers.- Example:
"-1,2"
throws"Negatives not allowed: -1"
.
- Example:
- Calling
Add
with multiple negative numbers will throw an exception message"Negatives not allowed: "
followed by a list all negative numbers that were in the list of numbers.- Example:
"1,-2,3,-4"
throws"Negatives not allowed: -2,-4"
.
- Example:
- Add a method to
StringCalculator
likeint GetCalledCount()
that will return how many timesAdd()
has been called. - Don’t forget to use TDD, there should be a failing test before you make this method work!
- Numbers greater than 1000 should be ignored.
- Example:
"2,1001"
returns2
.
- Example:
- Allow multiple delimiters using this syntax:
"//[delimiter1][delimiter2]\n"
. Example:"//[|||]\n1|||2|||3"
returns6
. - Allow multi-character delimiters. Example:
"//[**]\n1,2**3"
returns6
.
Original Source
This is a Roy Osherove kata https://osherove.com/tdd-kata-1/