add mathematical operators for Value to operate with Numeric objects
This commit is contained in:
1 parent
31d89371ff
commit
d7475fb09e
2 files changed
+84
-4
No files matched your search
@@ -30,6 +30,13 @@ module Gnucash
|
||||
c.to_s.should == "35.79"
|
||||
end
|
||||
|
||||
it "allows adding a Value object to a Numeric" do
|
||||
a = Value.new("1234/100")
|
||||
b = 15
|
||||
c = a + b
|
||||
c.should == 27.34
|
||||
end
|
||||
|
||||
it "allows subtracting two value objects" do
|
||||
a = Value.new("-12300/100")
|
||||
b = Value.new("99/100")
|
||||
@@ -37,6 +44,27 @@ module Gnucash
|
||||
c.to_s.should == "-123.99"
|
||||
end
|
||||
|
||||
it "allows subtracting a Numeric from a Value object" do
|
||||
a = Value.new("890/100")
|
||||
b = 7.8
|
||||
c = a - b
|
||||
c.should == 1.1
|
||||
end
|
||||
|
||||
it "allows multiplying a Value by a Numeric" do
|
||||
a = Value.new("100/100")
|
||||
b = 12
|
||||
c = a * b
|
||||
c.should == 12.00
|
||||
end
|
||||
|
||||
it "allows dividing a Value by a Numeric" do
|
||||
a = Value.new("150000/100")
|
||||
b = 150
|
||||
c = a / b
|
||||
c.should == 10.0
|
||||
end
|
||||
|
||||
it "formats the number with two decimal places" do
|
||||
Value.new("1400/100").to_s.should == "14.00"
|
||||
end
|
||||
@@ -46,5 +74,23 @@ module Gnucash
|
||||
(Value.new("89/100") < Value.new("100/100")).should be_true
|
||||
(Value.new("1234/100") > Value.new("222/100")).should be_true
|
||||
end
|
||||
|
||||
context "errors" do
|
||||
it "raises an error when attempting to add an unknown object type" do
|
||||
expect { Value.new(33) + nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
|
||||
it "raises an error when attempting to subtract an unknown object type" do
|
||||
expect { Value.new(33) - nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
|
||||
it "raises an error when attempting to multiply by an unknown object type" do
|
||||
expect { Value.new(33) * nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
|
||||
it "raises an error when attempting to divide by an unknown object type" do
|
||||
expect { Value.new(33) / nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user