This is my solution to the Incubyte String Calculator TDD Kata.
The goal of the kata is to implement a string calculator with the following capabilities:
- Return 0 for an empty string.
- Return the sum of numbers provided as a string.
- Support one or more numbers separated by commas or newlines.
- Support custom delimiters specified in the format:
//[delimiter]\n[numbers]
. - Throw an exception for negative numbers, listing all negatives.
This implementation was built following the TDD approach, with incremental tests and refactoring.
- Empty String
calculator.add("") # => 0
- Single Number
calculator.add("5") # => 5
- Multiple Numbers
calculator.add("1,2,3") # => 6
- Newline as Delimiter
calculator.add("1\n2,3") # => 6
- Custom Delimiter
calculator.add("//;\n1;2") # => 3
- Negative Number Handling
calculator.add("-1,2") # Raises: "negative numbers not allowed -1" calculator.add("2,-4,3,-5") # Raises: "negative numbers not allowed -4,-5"
-
Install dependencies (RSpec): gem install rspec
-
Run tests: rspec spec/string_calculator_spec.rb --format documentation