目の前に僕らの道がある

勉強会とか、技術的にはまったことのメモ

テスト駆動開発入門をRubyで写経してみた。5

5章で実施したこと

  • Francクラスの追加

ただし一番汚い形で。

money.rb

#!/usr/bin/ruby
#coding: utf-8
# テスト駆動開発入門5章 「フランク」に話す
class Dollar
  attr_accessor :amount
  protected :amount
  def initialize(amount)
    @amount = amount
  end

  def *(multiplier)
    return Dollar.new(@amount * multiplier)
  end

  def ==(other)
    return @amount == other.amount
  end
end

class Franc
  attr_accessor :amount
  protected :amount
  def initialize(amount)
    @amount = amount
  end

  def *(multiplier)
    return Franc.new(@amount * multiplier)
  end

  def ==(other)
    return @amount == other.amount
  end
end

money_test.rb

#!/usr/bin/ruby
#coding: utf-8
# テスト駆動開発入門5章 「フランク」に話す
require 'test/unit'
require 'money'

class TestMoney < Test::Unit::TestCase
  def test_multiplication
    five = Dollar.new(5)
    assert_equal(Dollar.new(10), five * 2)
    assert_equal(Dollar.new(15), five * 3)
  end

  def test_equality
    assert(Dollar.new(5) == Dollar.new(5))
    assert(Dollar.new(5) != Dollar.new(6))
  end

  def test_Franc_multiplication
    five = Franc.new(5)
    assert_equal(Franc.new(10), five * 2)
    assert_equal(Franc.new(15), five * 3)
  end
end