/
core_extensions_spec.rb
158 lines (136 loc) · 6.27 KB
/
core_extensions_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require "spec_helper"
describe "Money core extensions" do
describe Numeric do
describe "#to_money" do
it "should work" do
money = 1234.to_money
money.cents.should == 1234_00
money.currency.should == Money.default_currency
money = 100.37.to_money
money.cents.should == 100_37
money.currency.should == Money.default_currency
money = BigDecimal.new('1234').to_money
money.cents.should == 1234_00
money.currency.should == Money.default_currency
end
it "should accept optional currency" do
1234.to_money('USD').should == Money.new(123400, 'USD')
1234.to_money('EUR').should == Money.new(123400, 'EUR')
end
it "should respect :subunit_to_unit currency property" do
10.to_money('USD').should == Money.new(10_00, 'USD')
10.to_money('TND').should == Money.new(10_000, 'TND')
10.to_money('CLP').should == Money.new(10, 'CLP')
end
specify "#issue/15" do
amount = 555.55.to_money
amount.should == Money.new(55555)
end
end
end
describe String do
describe "#to_money" do
StringToMoney = {
"20.15" => Money.new(20_15) ,
"100" => Money.new(100_00) ,
"100.37" => Money.new(100_37) ,
"100,37" => Money.new(100_37) ,
"100 000" => Money.new(100_000_00) ,
"100,000.00" => Money.new(100_000_00) ,
"1,000" => Money.new(1_000_00) ,
"-1,000" => Money.new(-1_000_00) ,
"1,000.5" => Money.new(1_000_50) ,
"1,000.51" => Money.new(1_000_51) ,
"1,000.505" => Money.new(1_000_51) ,
"1,000.504" => Money.new(1_000_50) ,
"1,000.0000" => Money.new(1_000_00) ,
"1,000.5000" => Money.new(1_000_50) ,
"1,000.5099" => Money.new(1_000_51) ,
"1.550" => Money.new(1_55) ,
"25." => Money.new(25_00) ,
".75" => Money.new(75) ,
"100 USD" => Money.new(100_00, "USD") ,
"-100 USD" => Money.new(-100_00, "USD") ,
"100 EUR" => Money.new(100_00, "EUR") ,
"100.37 EUR" => Money.new(100_37, "EUR") ,
"100,37 EUR" => Money.new(100_37, "EUR") ,
"100,000.00 USD" => Money.new(100_000_00, "USD") ,
"100.000,00 EUR" => Money.new(100_000_00, "EUR") ,
"1,000 USD" => Money.new(1_000_00, "USD") ,
"-1,000 USD" => Money.new(-1_000_00, "USD") ,
"1,000.5500 USD" => Money.new(1_000_55, "USD") ,
"-1,000.6500 USD" => Money.new(-1_000_65, "USD") ,
"1.550 USD" => Money.new(1_55, "USD") ,
"USD 100" => Money.new(100_00, "USD") ,
"EUR 100" => Money.new(100_00, "EUR") ,
"EUR 100.37" => Money.new(100_37, "EUR") ,
"CAD -100.37" => Money.new(-100_37, "CAD") ,
"EUR 100,37" => Money.new(100_37, "EUR") ,
"EUR -100,37" => Money.new(-100_37, "EUR") ,
"USD 100,000.00" => Money.new(100_000_00, "USD") ,
"EUR 100.000,00" => Money.new(100_000_00, "EUR") ,
"USD 1,000" => Money.new(1_000_00, "USD") ,
"USD -1,000" => Money.new(-1_000_00, "USD") ,
"USD 1,000.9000" => Money.new(1_000_90, "USD") ,
"USD -1,000.090" => Money.new(-1_000_09, "USD") ,
"USD 1.5500" => Money.new(1_55, "USD") ,
"$100 USD" => Money.new(100_00, "USD") ,
"$1,194.59 USD" => Money.new(1_194_59, "USD") ,
"$-1,955 USD" => Money.new(-1_955_00, "USD") ,
"$1,194.5900 USD" => Money.new(1_194_59, "USD") ,
"$-1,955.000 USD" => Money.new(-1_955_00, "USD") ,
"$1.99000 USD" => Money.new(1_99, "USD") ,
}
specify "it should work" do
StringToMoney.each do |string, money|
string.to_money.should == money
end
end
it "should coerce input to string" do
Money.parse(20, "USD").should == Money.new(20_00, "USD")
end
it "should accept optional currency" do
"10.10".to_money('USD').should == Money.new(1010, 'USD')
"10.10".to_money('EUR').should == Money.new(1010, 'EUR')
"10.10 USD".to_money('USD').should == Money.new(1010, 'USD')
end
it "should raise error if optional currency doesn't match string currency" do
lambda{ "10.10 USD".to_money('EUR') }.should raise_error(/Mismatching Currencies/)
end
it "should ignore unrecognized data" do
"hello 2000 world".to_money.should == Money.new(2000_00)
end
it "should respect :subunit_to_unit currency property" do
"1".to_money("USD").should == Money.new(1_00, "USD")
"1".to_money("TND").should == Money.new(1_000, "TND")
"1".to_money("CLP").should == Money.new(1, "CLP")
"1.5".to_money("KWD").cents.should == 1500
end
end
describe "#to_currency" do
it "should convert string to Currency" do
"USD".to_currency.should == Money::Currency.new(:usd)
"EUR".to_currency.should == Money::Currency.new(:eur)
end
it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
lambda { "XXX".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
lambda { " ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
end
end
end
describe Symbol do
describe "#to_currency" do
it "should convert symbol to Currency" do
:usd.to_currency.should == Money::Currency.new(:usd)
:ars.to_currency.should == Money::Currency.new(:ars)
end
it "should work case-insensitive" do
:EUR.to_currency.should == Money::Currency.new(:eur)
end
it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
lambda { :XXX.to_currency }.should raise_error(Money::Currency::UnknownCurrency)
lambda { :" ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
end
end
end
end