Skip to content

Instantly share code, notes, and snippets.

@pjc
Created July 24, 2013 15:49
Show Gist options
  • Save pjc/6071824 to your computer and use it in GitHub Desktop.
Save pjc/6071824 to your computer and use it in GitHub Desktop.
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