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

Wednesday, April 27, 2011

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”

Wednesday, April 27, 2011

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

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

The Empty String

What’s a StringCalculator?

`const_missing': uninitialized constant Object::StringCalculator (NameError) 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

Duplication

Wednesday, April 27, 2011

Saw that one coming.

Single numbers

Duplication

Wednesday, April 27, 2011

“Remember to solve things as simply as possible”

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 numbers Getting a little more general.

How many times can you say ‘to_i’?

Wednesday, April 27, 2011

Two numbers Still 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 numbers Why is cheating so much fun?

Wednesday, April 27, 2011

Three numbers

Busted.

Wednesday, April 27, 2011

Three numbers More general.

Are there really 3 different cases here?

Wednesday, April 27, 2011

Three numbers Ah, 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.

Wednesday, April 27, 2011

‘,’ is duplicated.

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 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

Wednesday, April 27, 2011

Custom delimiters

Bonus Points: Why did it return 1?

Wednesday, April 27, 2011

Custom delimiters

Hard coding

Wednesday, April 27, 2011

Delimiter abstraction comes in handy - we only changed one method.

Custom delimiters

Red

Wednesday, April 27, 2011

Custom delimiters

Are the concepts clear here?

Wednesday, April 27, 2011

Replace constant with variable

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

Wednesday, April 27, 2011

A little memoization

Negative Numbers

Back to Red.

Wednesday, April 27, 2011

Negative Numbers

Done.

Wednesday, April 27, 2011

Checking your answers git diff master

Starting fresh git reset --hard HEAD

Wednesday, April 27, 2011

Created by Roy Osherove http://osherove.com/tdd-kata-1 Ruby ...

Apr 27, 2011 - Support different delimiters. To change a delimiter, the beginning of the string will contain a separate line that looks like this:.

3MB Sizes 0 Downloads 131 Views

Recommend Documents

harichandan roy
stakeholders or publicly for data mining and any other analysis purposes. ... Moreover, developed in-house projects and complaints management tool to increase ... Ranking and Visualization using open source java packages: Crawler4j, jsoup, ... Statis

created by-hiren patel “shikshak group” on whatsapp ... - Gujinfoupdate
CREATED BY-HIREN PATEL. “SHIKSHAK GROUP” ON WHATSAPP…… Page 2. CREATED BY-HIREN PATEL. “SHIKSHAK GROUP” ON WHATSAPP…

Advance Inking and Coloring for Adobe Illustrator Created by www ...
Advance Inking and Coloring for Adobe Illustrator. Created by www.claytowne.com. Drawing Copyright 2013 by Clay Butler. This means you are not allowed to ...

Created Co-Creator as Science and Symbol, By ...
creating." If you would like to offer your own critical reflections to Hefner or others, please subscribe to ... of our nature as creators - we create the meanings of our experience, almost instinctively. ..... See also website: www.lynnrandolph.com.

created by-hiren patel “shikshak group” - Gujinfoupdate
Apr 1, 2017 - CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 2. CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 3. CREATED ...

created by-hiren patel “shikshak group” - Gujinfoupdate
Apr 1, 2017 - CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 2. CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 3. CREATED ...

around & around & around by roy starkey
Mar 19, 2015 - By Erling Vikanes. An extraordinary tale of a extraordinary life of an extraordinary person. Roy Starkey shows us that nothing is impossible for the one who dares. I wish that this book will be an inspiration for todays younger generat

reliability evaluation of power systems by roy billinton -
The book Reliability Evaluation Of Power Systems By Roy Billinton will certainly still offer you positive worth if you do it well. Finishing guide Reliability ...

pdf-1444\the-sara-poems-by-roy-cameron.pdf
pdf-1444\the-sara-poems-by-roy-cameron.pdf. pdf-1444\the-sara-poems-by-roy-cameron.pdf. Open. Extract. Open with. Sign In. Main menu.

Linear Integrated Circuit 2nd Edition - D. Roy Choudhary- By ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Linear Integrated Circuit 2nd Edition - D. Roy Choudhary- By EasyEngineering.net.pdf. Linear Integrated Circ

Draft Copy created by σΚ for IPZois
For Dr. I.P. Zois. Introduction to ... which does not exist; according to the path-integral formulation of quantum mechanics, even unrealized .... That didn't mean the branes were voids: Quantum theory asserts that even the total vacuum of empty.

created by-hiren patel “shikshak group” - Gujinfoupdate
Apr 6, 2017 - CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 2. CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 3. CREATED ...

created by-hiren patel “shikshak group” - Gujinfoupdate
Apr 6, 2017 - Page 8. CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 9. CREATED BY-HIREN PATEL. “SHIKSHAK GROUP”. Page 10 ...

Sougata Roy Choudhury -
“Pre Employment Test" at your campus at no cost and allocate required infrastructure for this test. The outcome of the test will help your institution to identify.

Sougata Roy Choudhury -
We are glad to inform you that Confederation of Indian Industry is partnering with. Wheebox to build & publish the “India Skill Report 2013". This is India's first ...

Ruby Writeup.pdf
Whoops! There was a problem loading more pages. Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps.