String Calculator Kata

This kata is designed to help you practice and learn to use TDD in a methodical way. To that end, please follow these rules:

String Calculator Kata Steps (step-by-step)

  1. Create a StringCalculator object with a method int Add(string numbers), taking a string parameter and returning a number.
    1. The numbers parameter can include 0, 1, or 2 integers (e.g. “”, “1”, “1,2”).
    2. The return value should be the sum of the numbers given in the numbers string.
    3. Start with the simplest test case of an empty string, then 1 number, then 2.
    4. Solve things as simply as possible!
    5. Note: An empty string should return a sum of 0.
    6. Remember to refactor after each test.
  2. Allow the Add method to handle an unknown quantity of numbers (in the string).
  3. Allow the Add method to handle new lines between numbers (optionally instead of the commas).
    1. Example: “4\n5,6” returns 15.
  4. Implement support for different delimiters between the numbers in the string.
    1. To change the delimiter, the beginning of the string should be a separate line formatted like this: "//[delimiter]\n[numbers]"
    2. Example: "//;\n1;2" returns 3 (the delimiter is “;”).
    3. Note: This first line is optional; all existing scenarios should still work.
  5. 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.
    1. Example: "-1,2" throws "Negatives not allowed: -1".
  6. 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.
    1. Example: "1,-2,3,-4" throws "Negatives not allowed: -2,-4".
  7. Add a method to StringCalculator like int GetCalledCount() that will return how many times Add() has been called.
  8. Don’t forget to use TDD, there should be a failing test before you make this method work!
  9. Numbers greater than 1000 should be ignored.
    1. Example: "2,1001" returns 2.
  10. Allow multiple delimiters using this syntax: "//[delimiter1][delimiter2]\n". Example: "//[|||]\n1|||2|||3" returns 6.
  11. Allow multi-character delimiters. Example: "//[**]\n1,2**3" returns 6.

Original Source

This is a Roy Osherove kata