String Calculator TDD Kata
Created by Roy Osherovehttp://osherove.com/tdd-kata-1
Ruby Solution based on performance by Corey Haineshttp://katas.softwarecraftsmanship.org/?p=80
Wednesday, April 27, 2011
• 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”
Basic Requirements
Wednesday, April 27, 2011
• 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
Rules
Wednesday, April 27, 2011
Getting Start from project shell on Github
git clone [email protected]:calebphillips/string_calculator.git
cd string_calculator
gem install bundler
bundle install
git checkout shell
autotest
Wednesday, April 27, 2011
`const_missing': uninitialized constant Object::StringCalculator (NameError)
The Empty String
What’s a StringCalculator?
Wednesday, April 27, 2011
The Empty String
That’s nicer.
Wednesday, April 27, 2011
The Empty String
This is what we are looking for.
Wednesday, April 27, 2011
The Empty String
“Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
Single numbers
Saw that one coming.
Duplication
Wednesday, April 27, 2011
Single numbers
Duplication “Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
Single numbers
What if we could say it like this?
Wednesday, April 27, 2011
Single numbers
Duplication removed. Don’t get distracted by RSpec
syntax.
Wednesday, April 27, 2011
Single numbers
Expected that, but why don’t I see the
actual value returned?
Wednesday, April 27, 2011
Single numbers
Still red, but the message is much
better.
Wednesday, April 27, 2011
Single numbers
Getting smarter.
Wednesday, April 27, 2011
Two numbers
How did it come up with 2?
Wednesday, April 27, 2011
Two numbers
“Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
Two numbers
Two digit number throws a monkey
wrench.
Wednesday, April 27, 2011
Two numbersGetting a little more general.
How many times can you say ‘to_i’?
Wednesday, April 27, 2011
Two numbersStill green.
Now there are a lot of details here.
Wednesday, April 27, 2011
Two numbers
Still green and add is a little cleaner now
Wednesday, April 27, 2011
New Requirement
Allow the Add method to handle an unknown amount of numbers
Wednesday, April 27, 2011
Three numbers
Red
Wednesday, April 27, 2011
Three numbersWhy is cheating
so much fun?
Wednesday, April 27, 2011
Three numbers
Busted.
Wednesday, April 27, 2011
Three numbersMore general.
Are there really 3 different cases
here?
Wednesday, April 27, 2011
Three numbersAh, that’s
nicer.
Wednesday, April 27, 2011
Many numbers
Passes as-is.
Wednesday, April 27, 2011
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)
Wednesday, April 27, 2011
New lines as delimiters
Red.
Wednesday, April 27, 2011
New lines as delimiters
Green. ‘,’ is duplicated.
Wednesday, April 27, 2011
New lines as delimiters
Hide that literal away in a descriptively named
method
Wednesday, April 27, 2011
New lines as delimiters
Passes as-is.
Wednesday, April 27, 2011
New RequirementSupport 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
Wednesday, April 27, 2011
Custom delimiters
Bonus Points: Why did it return 1?
Wednesday, April 27, 2011
Custom delimiters
Delimiter abstraction comes in handy - we only
changed one method.
Hard coding
Wednesday, April 27, 2011
Custom delimiters
Red
Wednesday, April 27, 2011
Custom delimiters
Replace constant with variable
Are the concepts clear
here?
Wednesday, April 27, 2011
Custom delimiters
No comments needed.
Wednesday, April 27, 2011
Custom delimiters
A little less noise.
Wednesday, April 27, 2011
Custom delimiters
You can memoize the delimiter if you only
want to calculate it once.
Wednesday, April 27, 2011
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
Wednesday, April 27, 2011
Negative Numbers
Red.
Wednesday, April 27, 2011
Negative Numbers
“Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
Negative Numbers
add reads like the requirements
Wednesday, April 27, 2011
Negative Numbers
Red.
Wednesday, April 27, 2011
Negative Numbers
Refactor in the green.
Wednesday, April 27, 2011
Negative Numbers
Extract a method A little
memoization
Wednesday, April 27, 2011
Negative Numbers
Back to Red.
Wednesday, April 27, 2011
Negative Numbers
Done.
Wednesday, April 27, 2011
Checking your answersgit diff master
Starting fresh
git reset --hard HEAD
Wednesday, April 27, 2011