rspec Value
This commit is contained in:
parent
febf4a9f6d
commit
762b7c6413
@ -1,6 +1,7 @@
|
|||||||
module Gnucash
|
module Gnucash
|
||||||
class Value
|
class Value
|
||||||
attr_accessor :val
|
attr_accessor :val
|
||||||
|
|
||||||
def initialize(val)
|
def initialize(val)
|
||||||
if val.is_a?(String)
|
if val.is_a?(String)
|
||||||
if val =~ /^(-?\d+)\/100$/
|
if val =~ /^(-?\d+)\/100$/
|
||||||
|
34
spec/gnucash/value_spec.rb
Normal file
34
spec/gnucash/value_spec.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
module Gnucash
|
||||||
|
describe Value do
|
||||||
|
it "raises an error when an unexpected string is given" do
|
||||||
|
expect { Value.new("1234") }.to raise_error /Unexpected value string/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an error when an unexpected type is given" do
|
||||||
|
expect { Value.new(Object.new) }.to raise_error /Unexpected value type/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows construction from a string" do
|
||||||
|
Value.new("5678/100").val.should == 5678
|
||||||
|
end
|
||||||
|
|
||||||
|
it "converts the value to the expected string representation" do
|
||||||
|
Value.new("5678/100").to_s.should == "56.78"
|
||||||
|
Value.new("1000231455/100").to_s.should == "10002314.55"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows adding two value objects" do
|
||||||
|
a = Value.new("1234/100")
|
||||||
|
b = Value.new("2345/100")
|
||||||
|
c = a + b
|
||||||
|
c.to_s.should == "35.79"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows subtracting two value objects" do
|
||||||
|
a = Value.new("-12300/100")
|
||||||
|
b = Value.new("99/100")
|
||||||
|
c = a - b
|
||||||
|
c.to_s.should == "-123.99"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user