spec/money/money_spec.rb in nofxx-money-2.2.2 vs spec/money/money_spec.rb in nofxx-money-2.3.1

- old
+ new

@@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- require File.dirname(__FILE__) + '/../spec_helper.rb' describe Money do it "is associated to the singleton instance of VariableExchangeBank by default" do Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id @@ -73,12 +74,12 @@ it "shuld sum array" do Money.new(10_00).split_in_installments(3).sum.cents.should eql(1000) end it "should calculate tax" do - Money.new(100).with_tax(20).cents.should eql(120) - Money.new(100).with_tax(-20).cents.should eql(80) + Money.new(100).add_tax(20).cents.should eql(120) + Money.new(100).add_tax(-20).cents.should eql(80) end it "should calculate compound tax" do @ma = Money.new(1000_00) @ma.compound_interest(12.99,12).to_s.should eql("137.92") @@ -93,21 +94,17 @@ @ma = Money.new(2500_00) @ma.compound_interest(12.99,3).to_s.should eql("82.07") end it "shuld sum array" do - @money = Money.new(10_00).with_tax(10) + @money = Money.new(10_00).add_tax(10) @money.split_in_installments(3).sum.cents.should eql(1100) end - it "should output as float" do - Money.new(10_00).to_f.should eql(10.0) - end + it { Money.new(10_00).to_f.should eql(10.0) } + it { Money.new(10_00).to_s.should eql("10.00") } - it "should output as string" do - Money.new(10_00).to_s.should eql("10.00") - end it "should create a new Money object of 0 cents if empty" do Money.empty.should == Money.new(0) end @@ -129,52 +126,91 @@ it "Money.add_rate works" do Money.add_rate("EUR", "USD", 10) Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD") end -end -describe "Actions involving two Money objects" do - describe "if the other Money object has the same currency" do - it "#<=> compares the two objects' amounts" do - (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0 - (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0 - (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0 - end + it "Money method missing exchange" do + Money.add_rate("EUR", "BRL", 10) + Money.new(10_00, "EUR").as_brl.should == Money.new(100_00, "BRL") + end - it "#+ adds the other object's amount to the current object's amount while retaining the currency" do - (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD") - end + describe "Format out " do - it "#- substracts the other object's amount from the current object's amount while retaining the currency" do - (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD") + describe "Options" do + before(:each) do + @cash = Money.new(200) + end + + it { @cash.format.should eql("$2.00") } + it { @cash.format(:symbol => "R$ ").should eql("R$ 2.00") } + it { @cash.format(:no_cents => true).should eql("$2") } + it { @cash.format(:no_cents => true, :symbol => "R$ ").should eql("R$ 2") } end + + it { Money.new(0).format.should eql("$0.00") } + it { Money.new(0).format(:display_free => true).should eql("free") } + it { Money.new(0).format(:display_free => "GRATIS").should eql("GRATIS") } + + it { Money.new(9).format.should eql("$0.09") } + it { Money.new(99).format.should eql("$0.99") } + it { Money.new(800).format.should eql("$8.00") } + it { Money.new(-8000).format(:no_cents => true).should eql("$-80") } + it { Money.new(80000).format.should eql("$800.00") } + it { Money.new(800000).format.should eql("$8,000.00") } + it { Money.new(-8000000, "JPY").format(:no_cents => true).should eql("¥-80.000") } + it { Money.new(87654321, "BRL").format.should eql("R$876.543,21") } + it { Money.new(800000000, "BRL").format.should eql("R$8.000.000,00") } + it { Money.new(8000000000, "BRL").format.should eql("R$80.000.000,00") } + it { Money.new(80000000000, "CAD").format.should eql("$800,000,000.00") } + it { Money.new(880000000000, "GBP").format(:no_cents => true).should eql("£8,800,000,000") } + it { Money.new(8800000000088, "EUR").format.should eql("€88,000,000,000.88") } + end - describe "if the other Money object has a different currency" do - it "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do - target = Money.new(200_00, "EUR") - target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD")) - (Money.new(100_00, "USD") <=> target).should < 0 - target = Money.new(200_00, "EUR") - target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD")) - (Money.new(100_00, "USD") <=> target).should == 0 + describe "Actions involving two Money objects" do + describe "if the other Money object has the same currency" do + it "#<=> compares the two objects' amounts" do + (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0 + (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0 + (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0 + end - target = Money.new(200_00, "EUR") - target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD")) - (Money.new(100_00, "USD") <=> target).should > 0 - end + it "#+ adds the other object's amount to the current object's amount while retaining the currency" do + (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD") + end - it "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do - other = Money.new(90, "EUR") - other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD")) - (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD") + it "#- substracts the other object's amount from the current object's amount while retaining the currency" do + (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD") + end end - it "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do - other = Money.new(90, "EUR") - other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD")) - (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD") + describe "if the other Money object has a different currency" do + it "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do + target = Money.new(200_00, "EUR") + target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD")) + (Money.new(100_00, "USD") <=> target).should < 0 + + target = Money.new(200_00, "EUR") + target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD")) + (Money.new(100_00, "USD") <=> target).should == 0 + + target = Money.new(200_00, "EUR") + target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD")) + (Money.new(100_00, "USD") <=> target).should > 0 + end + + it "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do + other = Money.new(90, "EUR") + other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD")) + (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD") + end + + it "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do + other = Money.new(90, "EUR") + other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD")) + (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD") + end end end end