String Calculator TDD Kata Created by Roy Osherove http://osherove.com/tdd-kata-1 Ruby Solution based on performance by Corey Haines http://katas.softwarecraftsmanship.org/?p=80
Basic Requirements Create a simple string calculator with a method Add(numbers) that takes a string The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example or 1 or 1,2
Rules Start with the simplest test case of an empty string and move to one and two numbers Remember to solve things as simply as possible so that you force yourself to write tests you did not think about Remember to refactor after each passing test
Getting Start from project shell on Github git clone git@github.com:calebphillips/string_calculator.git cd string_calculator gem install bundler bundle install git checkout shell autotest
The Empty String What s a StringCalculator? `const_missing': uninitialized constant Object::StringCalculator (NameError)
The Empty String That s nicer.
The Empty String This is what we are looking for.
The Empty String Remember to solve things as simply as possible
Single numbers Duplication Saw that one coming.
Single numbers Duplication Remember to solve things as simply as possible
Single numbers What if we could say it like this?
Single numbers Duplication removed. Don t get distracted by RSpec syntax.
Single numbers Expected that, but why don t I see the actual value returned?
Single numbers Still red, but the message is much better.
Single numbers Getting smarter.
Two numbers How did it come up with 2?
Two numbers Remember to solve things as simply as possible
Two numbers Two digit number throws a monkey wrench.
Two numbers Getting a little more general. How many times can you say to_i?
Two numbers Still green. Now there are a lot of details here.
Two numbers Still green and add is a little cleaner now
New Requirement Allow the Add method to handle an unknown amount of numbers
Three numbers Red
Three numbers Why is cheating so much fun?
Three numbers Busted.
Three numbers More general. Are there really 3 different cases here?
Three numbers Ah, that s nicer.
Many numbers Passes as-is.
New Requirement Allow the Add method to handle new lines between numbers (instead of commas). the following input is ok: 1\n2,3 (will equal 6) the following input is NOT ok: 1,\n (not need to prove it - just clarifying)
New lines as delimiters Red.
New lines as delimiters Green., is duplicated.
New lines as delimiters Hide that literal away in a descriptively named method
New lines as delimiters Passes as-is.
New Requirement Support different delimiters To change a delimiter, the beginning of the string will contain a separate line that looks like this: //[delimiter]\n[numbers ] for example //;\n1;2 should return three where the default delimiter is ;. The first line is optional - all existing scenarios should still be supported
Custom delimiters Bonus Points: Why did it return 1?
Custom delimiters Delimiter Hard coding abstraction comes in handy - we only changed one method.
Custom delimiters Red
Custom delimiters Are the concepts clear here? Replace constant with variable
Custom delimiters No comments needed.
Custom delimiters A little less noise.
Custom delimiters You can memoize the delimiter if you only want to calculate it once.
New Requirement Calling Add with a negative number will throw an exception negatives not allowed - and the negative that was passed If there are multiple negatives, show all of them in the exception message
Negative Numbers Red.
Negative Numbers Remember to solve things as simply as possible
Negative Numbers add reads like the requirements
Negative Numbers Red.
Negative Numbers Refactor in the green.
Negative Numbers Extract a method A little memoization
Negative Numbers Back to Red.
Negative Numbers Done.
Checking your answers git diff master Starting fresh git reset --hard HEAD