Friday, August 13, 2010

Test Driven Development by Example, in Ruby

I have long been interested in the Ruby programming language. Luckily for me, I've just gotten a job which requires me to write code in Ruby, meaning that I finally have to learn Ruby properly. The hardest part of learning a new language for me is finding an interesting and somewhat challenging problem to work on with the new language. Little exercises like the Towers of Hanoi or the Fibonacci sequence are decent problems for programming interviews, but mainly test logic and basic language syntax.

I had just finished reading Programming Ruby 1.9, and was ready to sink my teeth into actually writing some Ruby. Being a long-time proponent of Test Driven Development, an interesting idea occurred to me. In Kent Beck's Test Driven Development: by Example book, he uses the example of adding different currencies together. This is a pretty easy problem to understand, and relatively easy to code up, but has just enough complexity in design to offer me some interesting practice with Ruby. In the book, Beck writes all of his unit tests and code in Java--I wrote the same tests and code in Ruby.

For readers who do not have their copy of Test Driven Development: by Example handy, the problem in a nutshell can be stated by one test:

  • $5 + 10 CHF = $10 if rate is 2:1

Or, in plain English: adding five US dollars to 10 Swiss francs should yield 10 US dollars if the exchange rate is 2 Swiss francs to 1 US dollar. Users of Test Driven Development (hereafter TDD) will not be surprised to learn that writing this test required writing many smaller tests first, such as adding two dollar amounts, converting a dollar amount into a franc amount, etc.

I was able to work through the 15 chapters of the example in two days (maybe six hours of coding time). Ruby's MiniTest unit testing framework is an xUnit variant, just like the JUnit framework that Beck uses in the book, so my Ruby tests are virtually the same as his Java tests. The class design is also the same, with the one exception that I did not need the Expression interface that his Java design requires, thanks to duck typing. It felt to me like duck typing simplified the Ruby code quite a bit, as compared to the Java code. There were certainly several times when Beck needed a page to present some refactorings to clean up the design that took me one line in Ruby. All told, my Ruby version was 62 lines of functional code and 78 lines of test code, as opposed to 91 and 89, respectively, in the Java version.

So, with no further ado, here is the example. I'll show you the tests first, then the functional code, then finally the log of all of my commits while working on this problem (my Git repository is available for anyone who wants it:

Test code: test_Money.rb

#!/usr/bin/env ruby1.9.1

require 'money'
require 'test/unit'

class TestMoney < MiniTest::Unit::TestCase
def test_multiplication
five = Money.dollar(5)
assert_equal(Money.dollar(10), five * 2)
assert_equal(Money.dollar(15), five * 3)

def test_equality
assert(Money.dollar(5) == Money.dollar(5))
refute(Money.dollar(5) == Money.dollar(6))
refute(Money.dollar(5) == Money.franc(5))

def test_currency
assert_equal(:USD, Money.dollar(1).currency)
assert_equal(:CHF, Money.franc(1).currency)

def test_simple_addition
five = Money.dollar(5)
sum = five + five
bank =
reduced = bank.reduce(sum, :USD)
assert_equal(Money.dollar(10), reduced)

def test_plus_returns_sum
five = Money.dollar(5)
sum = five + five
assert_equal(five, sum.augend)
assert_equal(five, sum.addend)

def test_reduce_sum
sum =, Money.dollar(4))
bank =
result = bank.reduce(sum, :USD)
assert_equal(Money.dollar(7), result)

def test_reduce_money
bank =
result = bank.reduce(Money.dollar(1), :USD)
assert_equal(Money.dollar(1), result)

def test_reduce_money_different_currency
bank =
bank.add_rate(:CHF, :USD, 2)
result = bank.reduce(Money.franc(2), :USD)
assert_equal(Money.dollar(1), result)

def test_identity_rate
assert_equal(1,, :USD))

def test_mixed_addition
five_bucks = Money.dollar(5)
ten_francs = Money.franc(10)
bank =
bank.add_rate(:CHF, :USD, 2)
result = bank.reduce(five_bucks + ten_francs, :USD)
assert_equal(Money.dollar(10), result)

def test_sum_plus_money
five_bucks = Money.dollar(5)
ten_francs = Money.franc(10)
bank =
bank.add_rate(:CHF, :USD, 2)
sum =, ten_francs) + five_bucks
result = bank.reduce(sum, :USD)
assert_equal(Money.dollar(15), result)

def test_sum_times
five_bucks = Money.dollar(5)
ten_francs = Money.franc(10)
bank =
bank.add_rate(:CHF, :USD, 2)
sum =, ten_francs) * 2
result = bank.reduce(sum, :USD)
assert_equal(Money.dollar(20), result)

Functional code: money.rb

class Money
attr_reader :amount, :currency

def initialize(amount, currency)
@amount = amount
@currency = currency

def self.dollar(amount), :USD)

def self.franc(amount), :CHF)

def ==(other)
@amount == other.amount &&
@currency == other.currency

def *(multiplier) * multiplier, @currency)

def +(addend), addend)

def reduce(bank, to_currency)
rate = bank.rate(@currency, to_currency) / rate, to_currency)

class Bank
def initialize
@rates = {}

def reduce(expression, to_currency)
expression.reduce(self, to_currency)

def add_rate(from, to, rate)
@rates[[from, to]] = rate

def rate(from, to)
return 1 if from == to
@rates[[from, to]]

class Expression

class Sum
attr_reader :augend, :addend

def initialize(augend, addend)
@augend = augend
@addend = addend

def reduce(bank, to_currency)
amount = @augend.reduce(bank, to_currency).amount +
@addend.reduce(bank, to_currency).amount, to_currency)

def +(addend), addend)

def *(multiplier) * multiplier, @addend * multiplier)

Commit log

  1. TestMoney#test_multiplication fails to compile (C1 : Multi-Currency Money)

  2. test_Dollar: TestDollar should subclass Test::Unit::Testcase instead of MiniTest::... (C1 - Multi-Currency Money)

  3. test_Dollar: should be (C1 - Multi-Currency Money)

  4. test_Dollar#test_multiplication: red bar! (C1 - Multi-Currency Money)

  5. TestDollar#test_multiplication: green bar! (C1 - Multi-Currency Money)

  6. Dollar#times sets @amount (green bar) (C1 - Multi-Currency Money)

  7. Dollar#times real implementation (green bar) (C1 - Multi-Currency Money)

  8. TestDollar#test_multiplication object modified (RB) (C2 - Degenerate Objects)

  9. TestDollar expects new obj from Dollar#times (ERR) (C2 - Degenerate Objects)

  10. Dollar#times returns new obj (GB) (C2 - Degenerate Objects)

  11. TestDollar#test_equality (RB) (C3 - Equality for All)

  12. Dollar#== return true (GB) (C3 - Equality for All)

  13. TestDollar#test_equality tests inequality (RB) (C3 - Equality for All)

  14. Dollar#== actual implementation (GB) (C3 - Equality for All)

  15. test_Dollar: switched to ruby1.9.1 and MiniTest

  16. TestDollar#test_multiplication asserts equality(GB) (C4 - Privacy)

  17. Dollar#amount is protected (GB) (C4 - Privacy)

  18. TestFranc#test_multiplication (ERR) (C5 - Franc-ly Speaking)

  19. Copied Dollar to Franc (GB) (C5 - Franc-ly Speaking)

  20. Added Money class (GB) (C6 - Equality for All, Redux)

  21. Dollar subclasses Money (GB) (C6 - Equality for All, Redux)

  22. Pushed Dollar#== up to Money (GB) (C6 - Equality for All, Redux) (Ruby eliminates the need to fool around with casts and instance variables!)

  23. Added TestFranc#test_equality (GB) (C6 - Equality for All, Redux)

  24. Franc subclasses Money (GB) (C6 - Equality for All, Redux)

  25. Removed Franc#== (GB) (C6 - Equality for All, Redux)

  26. TestMoney#test_equality exposes a bug (ERR): we should have moved the protected attr_reader :amount up to Money

  27. Pushed protected @amount up to Money (GB) (C7 - Apples and Oranges)

  28. Money#== compares classes (GB) (C7 - Apples and Oranges)

  29. TestDollar#test_multiplication use Money#dollar(GB) (C8 - Makin' Objects) (Ruby's duck typing eliminates the Money#times compiler error from the book!)

  30. Using Money#dollar in all tests (GB) (C8 - Makin' Objects)

  31. Added Money#franc factory (GB) (C8 - Makin' Objects)

  32. test_Money requiring 'money' breaks tests (ERR) (C8 - Makin' Objects) (This is a Ruby-only problem, since the Java tests probably do an 'import*')

  33. All classes declared in money.rb (GB) (C8 - Makin' Objects)

  34. Deleted dollar.rb and franc.rb (GB) (C8 - Makin' Objects)

  35. Money#currency and unit tests for same (GB) (C9 - Times We're Livin' In)

  36. Currency becomes an instance variable (GB) (C9 - Times We're Livin' In)

  37. Pulled up Money#currency (GB) (C9 - Times We're Livin' In)

  38. Added currency param to Franc#new (ERR) (C9 - Times We're Livin' In)

  39. Money#franc and Franc#times pass nil currency (RB) (C9 - Times We're Livin' In)

  40. Franc#times calls Money#franc which passes :CHF(GB) (C9 - The Times We're Livin' In)

  41. Money#dollar passes :USD (GB) (C9 - The Times We're Livin' In)

  42. Pushed up Dollar#new and Franc#new to Money (GB) (C9 - The Times We're Livin' In)

  43. Inlined factory in Dollar and Franc #times (GB) (C10 - Interesting Times)

  44. Dollar and Franc#times pass @currency to ctor (GB) (C10 - Interesting times)

  45. Dollar and Franc#times return a Money obj (RB) (C10 - Interesting Times)

  46. Backed out #times returning Money (GB) (C10 - Interesting Times)

  47. TestMoney#test_different_class_equality (RB) (C10 - Interesting Times)

  48. Money#== checks currency rather than class (GB) (C10 - Interesting Times) (This change feels more natural in Ruby, where duck typing means that testing class is rarely the best way forward)

  49. Franc#times returns a Money obj (GB) (C10 - Interesting Times)

  50. Dollar#times returns a Money obj (GB) (C10 - Interesting Times)

  51. Pulled up Money#times (GB) (C10 - Interesting Times)

  52. Removed explicit Money#currency (GB) (we already have attr_reader :currency, so this is unnecessary; the fact that I stuck it in there in the beginning means I still have some way to go to think natively in Ruby)

  53. Moved attr_reader :currency to top of money.rb (GB) (just to improve readability, in my opinion)

  54. Money#dollar and #franc return Money obj (GB) (C11 - The Root of All Evil)

  55. Removed Dollar class (GB) (C11 - The Root of All Evil)

  56. Removed Franc class (RB) (C11 - The Root of All Evil)

  57. Removed TestMoney#test_dft_class_inequality (GB) (and TestFranc#test_equality, which is equivalent to removing the third and forth assertions in testEquality() from TDDbE p.52) (C11 - The Root of All Evil)

  58. Removed TestFranc#test_multiplication (GB) (and test_Franc.rb, since there are no tests left in the file) (C11 - The Root of All Evil)

  59. Moved tests in test_Dollar.rb to test_Money.rb (GB) (tests are easier to keep track of all in one file, and since we don't have a Dollar subclass any more, the test naming convention is broken anyway)

  60. Added TestMoney#test_simple_addition (ERR) (C12 - Addition, Finally)

  61. Implemented Money#+ (GB) (C12 - Addition, Finally) (obvious implementation)

  62. TestMoney#t_smpl#add uses Expressions, Banks (ERR) (C12 - Addition, Finally)

  63. Fake implementation of Bank#reduce (GB) (C12 - Addition, Finally) (there should have been a red bar step before this one, but returning nil from Bank#reduce causes a compilation error in Ruby due to the lack of a formal return specification, i.e. def [Money] reduce...)

  64. TestMoney#t_smpl_add should add five + five (GB) (oversight on my part)

  65. TestMoney#test_plus_returns_sum (ERR) (C13 - Make It)

  66. Money#+ returns a Sum obj (GB) (C13 - Make It)

  67. Added TestMoney#test_reduce_sum (RB) (C13 - Make It)

  68. Bank#reduce actually adds (ERR) (C13 - Make It) (this seems like a bug in the TDDbE book to me; as we made the amount field protected in a previous chapter)

  69. Implemented Money#reduce (ERR) (C13 - Make It) (this should be a green bar, according to TDDbE, but the protected Money#amount accessor is called again)

  70. Made Money#amount public to stop errors (GB) (we want to be able to follow TDDbE closely, despite the seeming bug--I should do this exercise in Java as well to see if there really is a bug)

  71. Bank#reduce special case for Money (GB) (C13 - Make It)

  72. Implemented Money#reduce (GB) (C13 - Make It)

  73. Removed "cast" and class check from Bank#reduce(GB) (C13 - Make It)

  74. TestMoney#test_reduce_money_different_currency (RB) (C14 - Change)

  75. Hard-coded CHF -> USD in Money#reduce (GB) (C14 - Change)

  76. Added bank parameter to all #reduce methods (GB) (C14 - Change)

  77. Moved rate calculation to Bank#rate (GB) (C14 - Change)

  78. Bank stores rates in hashtable (ERR) (C14 - Change) (because Ruby can use an array--or any other object, for that matter--as a hash key, we don't need the Pair class from TDDbEx; we can simply use a two-element array ([from_currency, to_currency]) as our rate hash key)

  79. Bank#rate returns 1 when from and to are equal (GB) (C14 - Change)

  80. Added TestMoney#test_mixed_addition (RB) (C15 - Mixed Currencies) (this would throw "a host of compile errors" in Java, according to TDDbE, but duck typing means we compile just fine and simply fail a test)

  81. Sum#reduce reduces both arguments (GB) (C15 - Mixed Currencies)

  82. Stubbed out Sum#+ (GB) (C15 - Mixed Currencies) (we just did this to stay in sync with TDDbE--Java's type system required the stub, but Ruby's duck types care not)

  83. Added TestMoney#test_sum_plus_money (ERR) (C16 - Abstration, Finally)

  84. Real implementation of Sum#+ (GB) (C16 - Abstration, Finally)

  85. Added TestMoney#test_sum_times (ERR) (C16 - Abstration, Finally)

  86. Implemented Sum#times (GB) (C16 - Abstration, Finally)

  87. Redefined Money and Sum#times as #* (GB) (this seems more Ruby-native to me)

  88. TestMoney#test_plus_same_currency_returns_money(RB) (C16 - Abstraction, Finally)

  89. Removed TestMoney#t_plus_same_curr_ret_mny (GB) (C16 - Abstraction, Finally) (we removed this test because a clean way to have Money#+ return a Money object instead of a Sum was not obvious)