From 762b7c64132d178c977f1ead11730b8538c015ab Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Aug 2013 14:41:05 -0400 Subject: [PATCH] rspec Value --- lib/gnucash/value.rb | 1 + spec/gnucash/value_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 spec/gnucash/value_spec.rb diff --git a/lib/gnucash/value.rb b/lib/gnucash/value.rb index 9c8e7a6..1abaf73 100644 --- a/lib/gnucash/value.rb +++ b/lib/gnucash/value.rb @@ -1,6 +1,7 @@ module Gnucash class Value attr_accessor :val + def initialize(val) if val.is_a?(String) if val =~ /^(-?\d+)\/100$/ diff --git a/spec/gnucash/value_spec.rb b/spec/gnucash/value_spec.rb new file mode 100644 index 0000000..c25ab89 --- /dev/null +++ b/spec/gnucash/value_spec.rb @@ -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