money 3.6.1 → 3.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +335 -319
- data/LICENSE +21 -21
- data/README.md +214 -209
- data/Rakefile +49 -49
- data/lib/money.rb +27 -27
- data/lib/money/bank/base.rb +131 -131
- data/lib/money/bank/variable_exchange.rb +252 -251
- data/lib/money/core_extensions.rb +82 -63
- data/lib/money/currency.rb +422 -415
- data/lib/money/money.rb +387 -1210
- data/lib/money/money/arithmetic.rb +246 -0
- data/lib/money/money/formatting.rb +234 -0
- data/lib/money/money/parsing.rb +350 -0
- data/money.gemspec +34 -27
- data/spec/bank/base_spec.rb +72 -72
- data/spec/bank/variable_exchange_spec.rb +238 -238
- data/spec/core_extensions_spec.rb +158 -142
- data/spec/currency_spec.rb +133 -128
- data/spec/money/arithmetic_spec.rb +479 -0
- data/spec/money/formatting_spec.rb +352 -0
- data/spec/money/parsing_spec.rb +197 -0
- data/spec/money_spec.rb +271 -1268
- data/spec/spec_helper.rb +28 -17
- metadata +33 -23
- data/lib/money.rbc +0 -170
- data/lib/money/bank/base.rbc +0 -800
- data/lib/money/bank/variable_exchange.rbc +0 -2496
- data/lib/money/core_extensions.rbc +0 -474
- data/lib/money/currency.rbc +0 -22600
- data/lib/money/money.rbc +0 -10070
- data/spec/bank/base_spec.rbc +0 -2409
- data/spec/bank/variable_exchange_spec.rbc +0 -7389
- data/spec/core_extensions_spec.rbc +0 -5215
- data/spec/currency_spec.rbc +0 -4341
- data/spec/money_spec.rbc +0 -50121
- data/spec/spec_helper.rbc +0 -346
@@ -0,0 +1,352 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Money do
|
6
|
+
specify "Money.format brute force :subunit_to_unit = 1" do
|
7
|
+
("0".."9").each do |amt|
|
8
|
+
amt.to_money("VUV").format(:symbol => false).should == amt
|
9
|
+
end
|
10
|
+
("-1".."-9").each do |amt|
|
11
|
+
amt.to_money("VUV").format(:symbol => false).should == amt
|
12
|
+
end
|
13
|
+
"1000".to_money("VUV").format(:symbol => false).should == "1,000"
|
14
|
+
"-1000".to_money("VUV").format(:symbol => false).should == "-1,000"
|
15
|
+
end
|
16
|
+
|
17
|
+
specify "Money.format brute force :subunit_to_unit = 5" do
|
18
|
+
("0.0".."9.4").each do |amt|
|
19
|
+
next if amt[-1].to_i > 4
|
20
|
+
amt.to_money("MGA").format(:symbol => false).should == amt
|
21
|
+
end
|
22
|
+
("-0.1".."-9.4").each do |amt|
|
23
|
+
next if amt[-1].to_i > 4
|
24
|
+
amt.to_money("MGA").format(:symbol => false).should == amt
|
25
|
+
end
|
26
|
+
"1000.0".to_money("MGA").format(:symbol => false).should == "1,000.0"
|
27
|
+
"-1000.0".to_money("MGA").format(:symbol => false).should == "-1,000.0"
|
28
|
+
end
|
29
|
+
|
30
|
+
specify "Money.format brute force :subunit_to_unit = 10" do
|
31
|
+
("0.0".."9.9").each do |amt|
|
32
|
+
amt.to_money("VND").format(:symbol => false).should == amt.to_s.gsub(/\./, ",")
|
33
|
+
end
|
34
|
+
("-0.1".."-9.9").each do |amt|
|
35
|
+
amt.to_money("VND").format(:symbol => false).should == amt.to_s.gsub(/\./, ",")
|
36
|
+
end
|
37
|
+
"1000.0".to_money("VND").format(:symbol => false).should == "1.000,0"
|
38
|
+
"-1000.0".to_money("VND").format(:symbol => false).should == "-1.000,0"
|
39
|
+
end
|
40
|
+
|
41
|
+
specify "Money.format brute force :subunit_to_unit = 100" do
|
42
|
+
("0.00".."9.99").each do |amt|
|
43
|
+
amt.to_money("USD").format(:symbol => false).should == amt
|
44
|
+
end
|
45
|
+
("-0.01".."-9.99").each do |amt|
|
46
|
+
amt.to_money("USD").format(:symbol => false).should == amt
|
47
|
+
end
|
48
|
+
"1000.00".to_money("USD").format(:symbol => false).should == "1,000.00"
|
49
|
+
"-1000.00".to_money("USD").format(:symbol => false).should == "-1,000.00"
|
50
|
+
end
|
51
|
+
|
52
|
+
specify "Money.format brute force :subunit_to_unit = 1000" do
|
53
|
+
("0.000".."9.999").each do |amt|
|
54
|
+
amt.to_money("IQD").format(:symbol => false).should == amt
|
55
|
+
end
|
56
|
+
("-0.001".."-9.999").each do |amt|
|
57
|
+
amt.to_money("IQD").format(:symbol => false).should == amt
|
58
|
+
end
|
59
|
+
"1000.000".to_money("IQD").format(:symbol => false).should == "1,000.000"
|
60
|
+
"-1000.000".to_money("IQD").format(:symbol => false).should == "-1,000.000"
|
61
|
+
end
|
62
|
+
|
63
|
+
context "without i18n" do
|
64
|
+
subject{ Money.empty("USD") }
|
65
|
+
|
66
|
+
its(:thousands_separator){should == ","}
|
67
|
+
its(:decimal_mark){should == "."}
|
68
|
+
end
|
69
|
+
|
70
|
+
context "with i18n" do
|
71
|
+
after :each do
|
72
|
+
reset_i18n
|
73
|
+
I18n.locale = :en
|
74
|
+
end
|
75
|
+
|
76
|
+
context "with number.format.*" do
|
77
|
+
before :each do
|
78
|
+
reset_i18n
|
79
|
+
I18n.locale = :de
|
80
|
+
I18n.backend.store_translations(
|
81
|
+
:de, :number => {:format => {:delimiter => ".",
|
82
|
+
:separator => ","}}
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
subject{ Money.empty("USD") }
|
87
|
+
|
88
|
+
its(:thousands_separator){should == "."}
|
89
|
+
its(:decimal_mark){should == ","}
|
90
|
+
end
|
91
|
+
|
92
|
+
context "with number.currency.format.*" do
|
93
|
+
before :each do
|
94
|
+
reset_i18n
|
95
|
+
I18n.locale = :de
|
96
|
+
I18n.backend.store_translations(
|
97
|
+
:de, :number => {:currency => {:format => {:delimiter => ".",
|
98
|
+
:separator => ","}}}
|
99
|
+
)
|
100
|
+
end
|
101
|
+
|
102
|
+
subject{ Money.empty("USD") }
|
103
|
+
|
104
|
+
its(:thousands_separator){should == "."}
|
105
|
+
its(:decimal_mark){should == ","}
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#format" do
|
110
|
+
it "returns the monetary value as a string" do
|
111
|
+
Money.ca_dollar(100).format.should == "$1.00"
|
112
|
+
Money.new(40008).format.should == "$400.08"
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should respect :subunit_to_unit currency property" do
|
116
|
+
Money.new(10_00, "BHD").format.should == "ب.د1.000"
|
117
|
+
end
|
118
|
+
|
119
|
+
it "doesn't display a decimal when :subunit_to_unit is 1" do
|
120
|
+
Money.new(10_00, "CLP").format.should == "$1.000"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "respects the thousands_separator and decimal_mark defaults" do
|
124
|
+
one_thousand = Proc.new do |currency|
|
125
|
+
Money.new(1000_00, currency).format
|
126
|
+
end
|
127
|
+
|
128
|
+
# Pounds
|
129
|
+
one_thousand["GBP"].should == "£1,000.00"
|
130
|
+
|
131
|
+
# Dollars
|
132
|
+
one_thousand["USD"].should == "$1,000.00"
|
133
|
+
one_thousand["CAD"].should == "$1,000.00"
|
134
|
+
one_thousand["AUD"].should == "$1,000.00"
|
135
|
+
one_thousand["NZD"].should == "$1,000.00"
|
136
|
+
one_thousand["ZWD"].should == "$1,000.00"
|
137
|
+
|
138
|
+
# Yen
|
139
|
+
one_thousand["JPY"].should == "¥1,000.00"
|
140
|
+
one_thousand["CNY"].should == "¥1,000.00"
|
141
|
+
|
142
|
+
# Euro
|
143
|
+
one_thousand["EUR"].should == "1.000,00 €"
|
144
|
+
|
145
|
+
# Rupees
|
146
|
+
one_thousand["INR"].should == "₨1,000.00"
|
147
|
+
one_thousand["NPR"].should == "₨1,000.00"
|
148
|
+
one_thousand["SCR"].should == "1,000.00 ₨"
|
149
|
+
one_thousand["LKR"].should == "1,000.00 ₨"
|
150
|
+
|
151
|
+
# Brazilian Real
|
152
|
+
one_thousand["BRL"].should == "R$ 1.000,00"
|
153
|
+
|
154
|
+
# Other
|
155
|
+
one_thousand["SEK"].should == "kr1,000.00"
|
156
|
+
one_thousand["GHC"].should == "₵1,000.00"
|
157
|
+
end
|
158
|
+
|
159
|
+
describe "if the monetary value is 0" do
|
160
|
+
before :each do
|
161
|
+
@money = Money.us_dollar(0)
|
162
|
+
end
|
163
|
+
|
164
|
+
it "returns 'free' when :display_free is true" do
|
165
|
+
@money.format(:display_free => true).should == 'free'
|
166
|
+
end
|
167
|
+
|
168
|
+
it "returns '$0.00' when :display_free is false or not given" do
|
169
|
+
@money.format.should == '$0.00'
|
170
|
+
@money.format(:display_free => false).should == '$0.00'
|
171
|
+
@money.format(:display_free => nil).should == '$0.00'
|
172
|
+
end
|
173
|
+
|
174
|
+
it "returns the value specified by :display_free if it's a string-like object" do
|
175
|
+
@money.format(:display_free => 'gratis').should == 'gratis'
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
specify "#format(:with_currency => true) works as documented" do
|
180
|
+
Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
|
181
|
+
Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
|
182
|
+
end
|
183
|
+
|
184
|
+
specify "#format(:no_cents => true) works as documented" do
|
185
|
+
Money.ca_dollar(100).format(:no_cents => true).should == "$1"
|
186
|
+
Money.ca_dollar(599).format(:no_cents => true).should == "$5"
|
187
|
+
Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
|
188
|
+
Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
|
189
|
+
end
|
190
|
+
|
191
|
+
specify "#format(:no_cents => true) should respect :subunit_to_unit currency property" do
|
192
|
+
Money.new(10_00, "BHD").format(:no_cents => true).should == "ب.د1"
|
193
|
+
end
|
194
|
+
|
195
|
+
specify "#format(:no_cents_if_whole => true) works as documented" do
|
196
|
+
Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,000"
|
197
|
+
Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,034"
|
198
|
+
Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,000"
|
199
|
+
Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,006.4"
|
200
|
+
Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.000"
|
201
|
+
Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.003,4"
|
202
|
+
Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100"
|
203
|
+
Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100.34"
|
204
|
+
Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10"
|
205
|
+
Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10.034"
|
206
|
+
end
|
207
|
+
|
208
|
+
specify "#format(:no_cents_if_whole => false) works as documented" do
|
209
|
+
Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,000"
|
210
|
+
Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,034"
|
211
|
+
Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,000.0"
|
212
|
+
Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,006.4"
|
213
|
+
Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.000,0"
|
214
|
+
Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.003,4"
|
215
|
+
Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.00"
|
216
|
+
Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.34"
|
217
|
+
Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.000"
|
218
|
+
Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.034"
|
219
|
+
end
|
220
|
+
|
221
|
+
specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
|
222
|
+
Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
|
223
|
+
end
|
224
|
+
|
225
|
+
specify "#format(:symbol => true) returns symbol based on the given currency code" do
|
226
|
+
one = Proc.new do |currency|
|
227
|
+
Money.new(100, currency).format(:symbol => true)
|
228
|
+
end
|
229
|
+
|
230
|
+
# Pounds
|
231
|
+
one["GBP"].should == "£1.00"
|
232
|
+
|
233
|
+
# Dollars
|
234
|
+
one["USD"].should == "$1.00"
|
235
|
+
one["CAD"].should == "$1.00"
|
236
|
+
one["AUD"].should == "$1.00"
|
237
|
+
one["NZD"].should == "$1.00"
|
238
|
+
one["ZWD"].should == "$1.00"
|
239
|
+
|
240
|
+
# Yen
|
241
|
+
one["JPY"].should == "¥1.00"
|
242
|
+
one["CNY"].should == "¥1.00"
|
243
|
+
|
244
|
+
# Euro
|
245
|
+
one["EUR"].should == "1,00 €"
|
246
|
+
|
247
|
+
# Rupees
|
248
|
+
one["INR"].should == "₨1.00"
|
249
|
+
one["NPR"].should == "₨1.00"
|
250
|
+
one["SCR"].should == "1.00 ₨"
|
251
|
+
one["LKR"].should == "1.00 ₨"
|
252
|
+
|
253
|
+
# Brazilian Real
|
254
|
+
one["BRL"].should == "R$ 1,00"
|
255
|
+
|
256
|
+
# Other
|
257
|
+
one["SEK"].should == "kr1.00"
|
258
|
+
one["GHC"].should == "₵1.00"
|
259
|
+
end
|
260
|
+
|
261
|
+
specify "#format(:symbol => true) returns $ when currency code is not recognized" do
|
262
|
+
currency = Money::Currency.new("EUR")
|
263
|
+
currency.should_receive(:symbol).and_return(nil)
|
264
|
+
Money.new(100, currency).format(:symbol => true).should == "1,00 ¤"
|
265
|
+
end
|
266
|
+
|
267
|
+
specify "#format(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
|
268
|
+
Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
|
269
|
+
Money.new(100, "EUR").format(:symbol => true).should == "1,00 €"
|
270
|
+
Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
|
271
|
+
end
|
272
|
+
|
273
|
+
specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
|
274
|
+
money = Money.new(100, "GBP")
|
275
|
+
money.format(:symbol => "").should == "1.00"
|
276
|
+
money.format(:symbol => nil).should == "1.00"
|
277
|
+
money.format(:symbol => false).should == "1.00"
|
278
|
+
end
|
279
|
+
|
280
|
+
specify "#format without :symbol assumes that :symbol is set to true" do
|
281
|
+
money = Money.new(100)
|
282
|
+
money.format.should == "$1.00"
|
283
|
+
|
284
|
+
money = Money.new(100, "GBP")
|
285
|
+
money.format.should == "£1.00"
|
286
|
+
|
287
|
+
money = Money.new(100, "EUR")
|
288
|
+
money.format.should == "1,00 €"
|
289
|
+
end
|
290
|
+
|
291
|
+
specify "#format(:decimal_mark => a decimal_mark string) works as documented" do
|
292
|
+
Money.us_dollar(100).format(:decimal_mark => ",").should == "$1,00"
|
293
|
+
end
|
294
|
+
|
295
|
+
specify "#format will default decimal_mark to '.' if currency isn't recognized" do
|
296
|
+
Money.new(100, "ZWD").format.should == "$1.00"
|
297
|
+
end
|
298
|
+
|
299
|
+
specify "#format(:separator => a separator string) works as documented" do
|
300
|
+
Money.us_dollar(100).format(:separator => ",").should == "$1,00"
|
301
|
+
end
|
302
|
+
|
303
|
+
specify "#format(:thousands_separator => a thousands_separator string) works as documented" do
|
304
|
+
Money.us_dollar(100000).format(:thousands_separator => ".").should == "$1.000.00"
|
305
|
+
Money.us_dollar(200000).format(:thousands_separator => "").should == "$2000.00"
|
306
|
+
end
|
307
|
+
|
308
|
+
specify "#format(:thousands_separator => false or nil) works as documented" do
|
309
|
+
Money.us_dollar(100000).format(:thousands_separator => false).should == "$1000.00"
|
310
|
+
Money.us_dollar(200000).format(:thousands_separator => nil).should == "$2000.00"
|
311
|
+
end
|
312
|
+
|
313
|
+
specify "#format(:delimiter => a delimiter string) works as documented" do
|
314
|
+
Money.us_dollar(100000).format(:delimiter => ".").should == "$1.000.00"
|
315
|
+
Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
|
316
|
+
end
|
317
|
+
|
318
|
+
specify "#format(:delimiter => false or nil) works as documented" do
|
319
|
+
Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
|
320
|
+
Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
|
321
|
+
end
|
322
|
+
|
323
|
+
specify "#format will default thousands_separator to ',' if currency isn't recognized" do
|
324
|
+
Money.new(100000, "ZWD").format.should == "$1,000.00"
|
325
|
+
end
|
326
|
+
|
327
|
+
specify "#format(:html => true) works as documented" do
|
328
|
+
string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
|
329
|
+
string.should == "$5.70 <span class=\"currency\">CAD</span>"
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should insert commas into the result if the amount is sufficiently large" do
|
333
|
+
Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
|
334
|
+
Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
|
335
|
+
end
|
336
|
+
|
337
|
+
it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
|
338
|
+
Money.euro(1_234_567_12).format.should == "1.234.567,12 €"
|
339
|
+
Money.euro(1_234_567_12).format(:no_cents => true).should == "1.234.567 €"
|
340
|
+
end
|
341
|
+
|
342
|
+
it "inserts currency symbol before the amount when :symbol_position is set to :before" do
|
343
|
+
Money.euro(1_234_567_12).format(:symbol_position => :before).should == "€1.234.567,12"
|
344
|
+
end
|
345
|
+
|
346
|
+
it "inserts currency symbol after the amount when :symbol_position is set to :after" do
|
347
|
+
Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after).should == "1,000,000,000.12 $"
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
352
|
+
|
@@ -0,0 +1,197 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Money do
|
6
|
+
describe "Money.parse" do
|
7
|
+
|
8
|
+
it "should be able to parse european-formatted inputs under 10EUR" do
|
9
|
+
five_ninety_five = Money.new(595, 'EUR')
|
10
|
+
|
11
|
+
Money.parse('EUR 5,95').should == five_ninety_five
|
12
|
+
#TODO: try and handle these
|
13
|
+
#Money.parse('€5,95').should == five_ninety_five
|
14
|
+
#Money.parse('$5.95').should == five_ninety_five
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be able to parse european-formatted inputs with multiple thousands-seperators" do
|
18
|
+
Money.parse('EUR 1.234.567,89').should == Money.new(123456789, 'EUR')
|
19
|
+
Money.parse('EUR 1.111.234.567,89').should == Money.new(111123456789, 'EUR')
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be able to parse USD-formatted inputs under $10" do
|
23
|
+
five_ninety_five = Money.new(595, 'USD')
|
24
|
+
|
25
|
+
Money.parse(5.95).should == five_ninety_five
|
26
|
+
Money.parse('5.95').should == five_ninety_five
|
27
|
+
Money.parse('$5.95').should == five_ninety_five
|
28
|
+
Money.parse("\n $5.95 \n").should == five_ninety_five
|
29
|
+
Money.parse('$ 5.95').should == five_ninety_five
|
30
|
+
Money.parse('$5.95 ea.').should == five_ninety_five
|
31
|
+
Money.parse('$5.95, each').should == five_ninety_five
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be able to parse USD-formatted inputs with multiple thousands-seperators" do
|
35
|
+
Money.parse('1,234,567.89').should == Money.new(123456789, 'USD')
|
36
|
+
Money.parse('1,111,234,567.89').should == Money.new(111123456789, 'USD')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not return a price if there is a price range" do
|
40
|
+
lambda {Money.parse('$5.95-10.95')}.should raise_error ArgumentError
|
41
|
+
lambda {Money.parse('$5.95 - 10.95')}.should raise_error ArgumentError
|
42
|
+
lambda {Money.parse('$5.95 - $10.95')}.should raise_error ArgumentError
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not return a price for completely invalid input" do
|
46
|
+
# TODO: shouldn't these throw an error instead of being considered
|
47
|
+
# equal to $0.0?
|
48
|
+
empty_price = Money.new(0, 'USD')
|
49
|
+
|
50
|
+
Money.parse(nil).should == empty_price
|
51
|
+
Money.parse('hellothere').should == empty_price
|
52
|
+
Money.parse('').should == empty_price
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "Money.from_string" do
|
57
|
+
it "converts given amount to cents" do
|
58
|
+
Money.from_string("1").should == Money.new(1_00)
|
59
|
+
Money.from_string("1").should == Money.new(1_00, "USD")
|
60
|
+
Money.from_string("1", "EUR").should == Money.new(1_00, "EUR")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should respect :subunit_to_unit currency property" do
|
64
|
+
Money.from_string("1", "USD").should == Money.new(1_00, "USD")
|
65
|
+
Money.from_string("1", "TND").should == Money.new(1_000, "TND")
|
66
|
+
Money.from_string("1", "CLP").should == Money.new(1, "CLP")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "accepts a currency options" do
|
70
|
+
m = Money.from_string("1")
|
71
|
+
m.currency.should == Money.default_currency
|
72
|
+
|
73
|
+
m = Money.from_string("1", Money::Currency.wrap("EUR"))
|
74
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
75
|
+
|
76
|
+
m = Money.from_string("1", "EUR")
|
77
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "Money.from_fixnum" do
|
82
|
+
it "converts given amount to cents" do
|
83
|
+
Money.from_fixnum(1).should == Money.new(1_00)
|
84
|
+
Money.from_fixnum(1).should == Money.new(1_00, "USD")
|
85
|
+
Money.from_fixnum(1, "EUR").should == Money.new(1_00, "EUR")
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should respect :subunit_to_unit currency property" do
|
89
|
+
Money.from_fixnum(1, "USD").should == Money.new(1_00, "USD")
|
90
|
+
Money.from_fixnum(1, "TND").should == Money.new(1_000, "TND")
|
91
|
+
Money.from_fixnum(1, "CLP").should == Money.new(1, "CLP")
|
92
|
+
end
|
93
|
+
|
94
|
+
it "accepts a currency options" do
|
95
|
+
m = Money.from_fixnum(1)
|
96
|
+
m.currency.should == Money.default_currency
|
97
|
+
|
98
|
+
m = Money.from_fixnum(1, Money::Currency.wrap("EUR"))
|
99
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
100
|
+
|
101
|
+
m = Money.from_fixnum(1, "EUR")
|
102
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "Money.from_float" do
|
107
|
+
it "converts given amount to cents" do
|
108
|
+
Money.from_float(1.2).should == Money.new(1_20)
|
109
|
+
Money.from_float(1.2).should == Money.new(1_20, "USD")
|
110
|
+
Money.from_float(1.2, "EUR").should == Money.new(1_20, "EUR")
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should respect :subunit_to_unit currency property" do
|
114
|
+
Money.from_float(1.2, "USD").should == Money.new(1_20, "USD")
|
115
|
+
Money.from_float(1.2, "TND").should == Money.new(1_200, "TND")
|
116
|
+
Money.from_float(1.2, "CLP").should == Money.new(1, "CLP")
|
117
|
+
end
|
118
|
+
|
119
|
+
it "accepts a currency options" do
|
120
|
+
m = Money.from_float(1.2)
|
121
|
+
m.currency.should == Money.default_currency
|
122
|
+
|
123
|
+
m = Money.from_float(1.2, Money::Currency.wrap("EUR"))
|
124
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
125
|
+
|
126
|
+
m = Money.from_float(1.2, "EUR")
|
127
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "Money.from_bigdecimal" do
|
132
|
+
it "converts given amount to cents" do
|
133
|
+
Money.from_bigdecimal(BigDecimal.new("1")).should == Money.new(1_00)
|
134
|
+
Money.from_bigdecimal(BigDecimal.new("1")).should == Money.new(1_00, "USD")
|
135
|
+
Money.from_bigdecimal(BigDecimal.new("1"), "EUR").should == Money.new(1_00, "EUR")
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should respect :subunit_to_unit currency property" do
|
139
|
+
Money.from_bigdecimal(BigDecimal.new("1"), "USD").should == Money.new(1_00, "USD")
|
140
|
+
Money.from_bigdecimal(BigDecimal.new("1"), "TND").should == Money.new(1_000, "TND")
|
141
|
+
Money.from_bigdecimal(BigDecimal.new("1"), "CLP").should == Money.new(1, "CLP")
|
142
|
+
end
|
143
|
+
|
144
|
+
it "accepts a currency options" do
|
145
|
+
m = Money.from_bigdecimal(BigDecimal.new("1"))
|
146
|
+
m.currency.should == Money.default_currency
|
147
|
+
|
148
|
+
m = Money.from_bigdecimal(BigDecimal.new("1"), Money::Currency.wrap("EUR"))
|
149
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
150
|
+
|
151
|
+
m = Money.from_bigdecimal(BigDecimal.new("1"), "EUR")
|
152
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "Money.from_numeric" do
|
157
|
+
it "converts given amount to cents" do
|
158
|
+
Money.from_numeric(1).should == Money.new(1_00)
|
159
|
+
Money.from_numeric(1.0).should == Money.new(1_00)
|
160
|
+
Money.from_numeric(BigDecimal.new("1")).should == Money.new(1_00)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should raise ArgumentError with unsupported argument" do
|
164
|
+
lambda { Money.from_numeric("100") }.should raise_error(ArgumentError)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should optimize workload" do
|
168
|
+
Money.should_receive(:from_fixnum).with(1, "USD").and_return(Money.new(1_00, "USD"))
|
169
|
+
Money.from_numeric(1, "USD").should == Money.new(1_00, "USD")
|
170
|
+
Money.should_receive(:from_bigdecimal).with(BigDecimal.new("1.0"), "USD").and_return(Money.new(1_00, "USD"))
|
171
|
+
Money.from_numeric(1.0, "USD").should == Money.new(1_00, "USD")
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should respect :subunit_to_unit currency property" do
|
175
|
+
Money.from_numeric(1, "USD").should == Money.new(1_00, "USD")
|
176
|
+
Money.from_numeric(1, "TND").should == Money.new(1_000, "TND")
|
177
|
+
Money.from_numeric(1, "CLP").should == Money.new(1, "CLP")
|
178
|
+
end
|
179
|
+
|
180
|
+
it "accepts a bank option" do
|
181
|
+
Money.from_numeric(1).should == Money.new(1_00)
|
182
|
+
Money.from_numeric(1).should == Money.new(1_00, "USD")
|
183
|
+
Money.from_numeric(1, "EUR").should == Money.new(1_00, "EUR")
|
184
|
+
end
|
185
|
+
|
186
|
+
it "accepts a currency options" do
|
187
|
+
m = Money.from_numeric(1)
|
188
|
+
m.currency.should == Money.default_currency
|
189
|
+
|
190
|
+
m = Money.from_numeric(1, Money::Currency.wrap("EUR"))
|
191
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
192
|
+
|
193
|
+
m = Money.from_numeric(1, "EUR")
|
194
|
+
m.currency.should == Money::Currency.wrap("EUR")
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|