Sha256: 1c283ef2785a6a76aac8792c37e76ce569ed4ca48187fa650d573705e478ebf6
Contents?: true
Size: 1.34 KB
Versions: 1
Compression:
Stored size: 1.34 KB
Contents
require 'spec_helper' require 'deterministic/enum' Amount = Deterministic::enum { Due(:amount) Paid(:amount) Info(:amount) } class Amount def self.from_f(f) f >= 0 ? Amount::Due.new(f) : Amount::Paid.new(-1 * f) end end Deterministic::impl(Amount) { def to_s match { Due() {|a| "%0.2f" % [a] } Paid() {|a| "-%0.2f" % [a] } Info() {|a| "(%0.2f)" % [a] } } end def to_f match { Info() {|a| 0 } Due() {|a| a } Paid() {|a| -1 * a } } end def +(other) raise TypeError "Expected other to be an Amount, got #{other.class}" unless other.is_a? Amount Amount.from_f(to_f + other.to_f) end } describe Amount do def Due(a); Amount::Due.new(a); end def Paid(a); Amount::Paid.new(a); end def Info(a); Amount::Info.new(a); end it "due" do amount = Amount::Due.new(100.2) expect(amount.to_s).to eq "100.20" end it "paid" do amount = Amount::Paid.new(100.1) expect(amount.to_s).to eq "-100.10" end it "paid" do amount = Amount::Info.new(100.31) expect(amount.to_s).to eq "(100.31)" end it "+" do expect(Due(10) + Paid(20)).to eq Paid(10) expect(Due(10) + Paid(10)).to eq Due(0) expect(Due(10) + Due(10)).to eq Due(20) expect(Paid(10) + Paid(10)).to eq Paid(20) expect(Paid(10) + Due(1) + Info(99)).to eq Paid(9) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
deterministic-0.16.0 | spec/examples/amount_spec.rb |