---
title: "TDD Kata 1 - String Calculator"
url: "https://osherove.com/tdd-kata-1.md"
canonical: "https://osherove.com/tdd-kata-1"
tags: []
author: "Roy Osherove"
description: "String Calculator The following is a TDD Kata- an exercise in coding, refactoring and test-first, that you should apply daily for at least 15 minutes . You"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# TDD Kata 1 - String Calculator

TDD Kata

## String Calculator

The following is a TDD Kata- an exercise in coding, refactoring and test-first, that you should apply daily for at least 15 minutes .

[You can download a more readable version of the Kata here.](https://osherove.com/s/String-Calculator-Kata-v1.pdf)

[Download as PDF](https://osherove.com/s/String-Calculator-Kata-v1.pdf)

---

## Before you start: 

- Try not to read ahead.

- Do one task at a time. The trick is to learn to work incrementally.

- Make sure you only test for **correct inputs**. there is no need to test for invalid inputs for this kata

**String Calculator**

1. ```
   Create a simple String calculator with a method signature:
   ```

2. ```
   Allow the Add method to handle an unknown amount of numbers
   ```

3. ```
   Allow the Add method to handle new lines between numbers (instead of commas).
   ```

    1. ```
       the following input is ok: “1\n2,3” (will equal 6)
       ```

    2. ```
       the following input is NOT ok: “1,\n” (not need to prove it - just clarifying)
       ```

4. ```
   Support different delimiters
   ```

    1. ```
       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 ‘;’ .
       ```

    2. ```
       the first line is optional. all existing scenarios should still be supported
       ```

5. ```
   Calling Add with a negative number will throw an exception “negatives not allowed” - and the negative that was passed. 
   ```

6. ```
   Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2
   ```

7. ```
   Delimiters can be of any length with the following format: “//[delimiter]\n” for example: “//[***]\n1***2***3” should return 6
   ```

8. ```
   Allow multiple delimiters like this: “//[delim1][delim2]\n” for example “//[*][%]\n1*2%3” should return 6.
   ```

9. ```
   make sure you can also handle multiple delimiters with length longer than one char
   ```

```
———————————————————————————————— 
```

### Here are several run throughs of the kata by different people in various languages:

| By | Language | Test framework | Video/post | Code repository | Site/blog/Twitter |
| --- | --- | --- | --- | --- | --- |
| Andrea Francia | Java | LamdaJ | [Vimeo](https://vimeo.com/8506325) |  |  |
| Corey Heines | Ruby | RSpec | [Vimeo](https://vimeo.com/7961506) |  |  |
| Dave Foley | Javascript |  | [anotherdave.wordpress.com](https://anotherdave.wordpress.com/2010/01/13/calculator-tdd-kata-in-javascript/) |  |  |
| Giordano Scalzo | Scala |  | [Vimeo](https://vimeo.com/8326880) |  |  |
| Rickard Nillson | C# | NUnit | [Vimeo](https://vimeo.com/7293092) |  |  |
| Bobby Johnson | C# | XUnit | [iamnotmyself.com](http://iamnotmyself.com/2009/10/23/tdd-kata-calculator-demonstration/) |  |  |
| Fredric Torres | C# | MSTest | [YouTube](https://www.youtube.com/watch?v=TgLfdsjJREI&) |  |  |
| Christian Scott | C# | LinqPad | [YouTube](https://www.youtube.com/playlist?p=PL3D3F4B7C71FF6AA0) |  |  |
| Ronnie Hegelund | Typescript |  | [ronniehegelund.blogspot.com](http://ronniehegelund.blogspot.com/2013/01/tdd-stringcalculator-kata-in-typescript.html) |  |  |
| Unknown | Lua |  | [Vimeo](https://vimeo.com/8027709) |  |  |
| Liam Devine | C++ | CPPUnit | [Vimeo](https://vimeo.com/8446999) |  |  |
| Andrew Woody | C# | NUnit | [Vimeo](https://vimeo.com/7014098) |  |  |
| Joakim Carlsson | C++ | Igloo | [Vimeo](https://vimeo.com/25207680) |  |  |
| Michael Feathers | Haskell |  | [Vimeo](https://vimeo.com/18423904) |  |  |
| Unknown | AutoHotKey |  | [YouTube](https://www.youtube.com/watch?v=Fm2Q-lJPgSg&hd=1) |  |  |
| Marian Schubert | Perl |  | [YouTube](https://www.youtube.com/watch?v=unzbQYmlPys) |  |  |

**Online Training by Roy Osherove**

.NET: [The Art of Unit Testing and TDD in .NET Online](http://tddcourse.osherove.com/)

.NET: [Legacy Code Hero (Advanced Refactoring Patterns)](http://legacyhero.osherove.com/)

Java: [Unit Testing and TDD in Java Online](http://javatdd.osherove.com/)

Ruby: [BDD and TDD in Ruby Online](http://rubytdd.osherove.com/)

## Lessons on this topic

- [Introducing TDD](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/introducing-tdd/) · The Art of Unit Testing and TDD in Java
- [Test Driving a String Calculator](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/test-driving-a-string-calculator/) · The Art of Unit Testing & TDD Master Class in .NET
- [Simplicity is Complicated](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/simplicity-is-complicated/) · The Art of Unit Testing and TDD in Java
- [Summary Day 1](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/summary-day-1/) · TDD in .NET in Hebrew
- [The Basics of RSpec](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/the-basics-of-rspec/) · TDD and BDD in Ruby


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
