Created
July 24, 2013 14:59
-
-
Save pjc/6071377 to your computer and use it in GitHub Desktop.
TDD by example chapter 5/6 equality / privacy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
# WE NEED TO BE ABLE TO ... AND ... GIVEN ... | |
# WE NEED TO BE ABLE TO add amounts in different currencies AND convert the result GIVEN a set of exchange rates | |
# $5 + 10 CHF = $10 if CHF:USD rate is 2:1 | |
# WE NEED TO BE ABLE TO multiply a number (of shares) with an amount (price per share) AND receive an amount | |
# $5 * 2 = $10 | |
# money rounding, make amount private, equals(), hashCode(), equal null, equal object? | |
class Dollar | |
def initialize(amount) | |
@amount = amount | |
end | |
def times(multiplier) | |
Dollar.new(@amount * multiplier) | |
end | |
def equals?(object) | |
@amount == object.amount | |
end | |
def amount | |
@amount | |
end | |
end | |
describe "test multiplication" do | |
it "must multiply an amount (price per share) with a number (of shares) and return an amount (total price)" do | |
five = Dollar.new(5) | |
# assert_equal 10, five.times(2).amount | |
assert Dollar.new(10).equals?(five.times(2)) | |
# assert_equal 15, five.times(3).amount | |
assert Dollar.new(15).equals?(five.times(3)) # Triangulation + prevent dollar side effect of remembering previous calculations | |
end | |
end | |
describe "test equality" do # equals? implementation needed because it is value object | |
it "must be equal every time you create $5" do | |
assert Dollar.new(5).equals?(Dollar.new(5)) | |
refute Dollar.new(5).equals?(Dollar.new(6)) # triangulation | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment