Created
July 24, 2013 15:49
-
-
Save pjc/6071824 to your computer and use it in GitHub Desktop.
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' | |
class Dollar | |
def initialize(amount) | |
@amount = amount | |
end | |
def times(multiplier) | |
Dollar.new(@amount * multiplier) | |
end | |
def equals?(object) | |
@amount == object.send(:amount) | |
end | |
private | |
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 Dollar.new(10).equals?(five.times(2)) | |
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