Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'matchers.rb'
describe CheckOut do
let(:checkout) { CheckOut.new(RULES) }
context "calculating totals" do
subject { checkout }
it { should price("").at(0) }
it { should price("A").at(50) }
it { should price("AB").at(80) }
it { should price("CDBA").at(115) }
it { should price("AA").at(100) }
it { should price("AAA").at(130) }
it { should price("AAAA").at(180) }
it { should price("AAAAA").at(230) }
it { should price("AAAAAA").at(260) }
it { should price("AAAB").at(160) }
it { should price("AAABB").at(175) }
it { should price("AAABBD").at(190) }
it { should price("DABABA").at(190) }
end
it "calculates incremental totals" do
checkout.total.should eq(0)
checkout.scan("A"); checkout.total.should eq(50)
checkout.scan("B"); checkout.total.should eq(80)
checkout.scan("A"); checkout.total.should eq(130)
checkout.scan("A"); checkout.total.should eq(160)
checkout.scan("B"); checkout.total.should eq(175)
end
end
RSpec::Matchers.define :price do |goods|
match do |checkout|
goods.split(//).each { |item| checkout.scan(item) }
(@actual_total = checkout.total) == @price
end
chain :at do |price|
@price = price
end
failure_message_for_should do |checkout|
"expected checkout to price #{goods.inspect} at #{@price}, got #{@actual_total}"
end
description do
"price #{goods.inspect} at #{@price}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment