TDD Kata 02: String Calculator

This is my weekly Kata post. Read the first one to learn what it is all about.

Last week: The Bowling Game

See last post: TDD Kata 01: The Bowling Game

My personal goals this week

  • Try it out in Ruby
  • Also do it in PHP, using Knapsack collections

I started well within my comfort zone, in PHP with PHPUnit. I followed the sections (first test, second test, …) as described. I used Knapsack collections, so my test data provider looked like this in the end:

    public static function dataGame()
        return [
            'gutter game' => [0, repeat(0, 20)],
            'all ones' => [20, repeat(1, 20)],
            'one spare' => [16, values([5, 5, 3])->concat(repeat(0, 17))],
            'one strike' => [24, values([10, 3, 4])->concat(repeat(0, 16))],

In the implementation I tried to stick with collection functions as well, but at some point it got impractical because scores depend on previous rolls. The bowling game turns out to be not an ideal use case for collection pipelines.

Then I did it in Ruby, which I just started to learn a while ago. In the implementation I followed the suggested steps quite closely.

But I learned one thing: When looking for colored test output facilities, I found out about -r minitest/pride and loved it!

fabulous minitest

Second Kata: String Calculator

The kata is described here: String Calculator. Requirements are added step by step and you are supposed to implement them one after another without thinking ahead. This is a good practice to do just as much as necessary to make the tests pass.

My personal goals this week

  • Again, do it in PHP and Ruby
  • Timebox to 15 minutes, complete or not