papercavalier-money 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,375 @@
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 but use_i18n = false" do
71
+ before :each do
72
+ reset_i18n
73
+ I18n.locale = :de
74
+ I18n.backend.store_translations(
75
+ :de, :number => {:currency => {:format => {:delimiter => ".",
76
+ :separator => ","}}}
77
+ )
78
+
79
+ Money.use_i18n = false
80
+ end
81
+
82
+ after :each do
83
+ reset_i18n
84
+ I18n.locale = :en
85
+ Money.use_i18n = true
86
+ end
87
+
88
+ subject{ Money.new(1000_00, :usd) }
89
+
90
+ its(:format){should == "$1,000.00"}
91
+ end
92
+
93
+ context "with i18n" do
94
+ after :each do
95
+ reset_i18n
96
+ I18n.locale = :en
97
+ end
98
+
99
+ context "with number.format.*" do
100
+ before :each do
101
+ reset_i18n
102
+ I18n.locale = :de
103
+ I18n.backend.store_translations(
104
+ :de, :number => {:format => {:delimiter => ".",
105
+ :separator => ","}}
106
+ )
107
+ end
108
+
109
+ subject{ Money.empty("USD") }
110
+
111
+ its(:thousands_separator){should == "."}
112
+ its(:decimal_mark){should == ","}
113
+ end
114
+
115
+ context "with number.currency.format.*" do
116
+ before :each do
117
+ reset_i18n
118
+ I18n.locale = :de
119
+ I18n.backend.store_translations(
120
+ :de, :number => {:currency => {:format => {:delimiter => ".",
121
+ :separator => ","}}}
122
+ )
123
+ end
124
+
125
+ subject{ Money.empty("USD") }
126
+
127
+ its(:thousands_separator){should == "."}
128
+ its(:decimal_mark){should == ","}
129
+ end
130
+ end
131
+
132
+ describe "#format" do
133
+ it "returns the monetary value as a string" do
134
+ Money.ca_dollar(100).format.should == "$1.00"
135
+ Money.new(40008).format.should == "$400.08"
136
+ end
137
+
138
+ it "should respect :subunit_to_unit currency property" do
139
+ Money.new(10_00, "BHD").format.should == "ب.د1.000"
140
+ end
141
+
142
+ it "doesn't display a decimal when :subunit_to_unit is 1" do
143
+ Money.new(10_00, "CLP").format.should == "$1.000"
144
+ end
145
+
146
+ it "respects the thousands_separator and decimal_mark defaults" do
147
+ one_thousand = Proc.new do |currency|
148
+ Money.new(1000_00, currency).format
149
+ end
150
+
151
+ # Pounds
152
+ one_thousand["GBP"].should == "£1,000.00"
153
+
154
+ # Dollars
155
+ one_thousand["USD"].should == "$1,000.00"
156
+ one_thousand["CAD"].should == "$1,000.00"
157
+ one_thousand["AUD"].should == "$1,000.00"
158
+ one_thousand["NZD"].should == "$1,000.00"
159
+ one_thousand["ZWD"].should == "$1,000.00"
160
+
161
+ # Yen
162
+ one_thousand["JPY"].should == "¥1,000.00"
163
+ one_thousand["CNY"].should == "¥1,000.00"
164
+
165
+ # Euro
166
+ one_thousand["EUR"].should == "1.000,00 €"
167
+
168
+ # Rupees
169
+ one_thousand["INR"].should == "₨1,000.00"
170
+ one_thousand["NPR"].should == "₨1,000.00"
171
+ one_thousand["SCR"].should == "1,000.00 ₨"
172
+ one_thousand["LKR"].should == "1,000.00 ₨"
173
+
174
+ # Brazilian Real
175
+ one_thousand["BRL"].should == "R$ 1.000,00"
176
+
177
+ # Other
178
+ one_thousand["SEK"].should == "kr1,000.00"
179
+ one_thousand["GHC"].should == "₵1,000.00"
180
+ end
181
+
182
+ describe "if the monetary value is 0" do
183
+ before :each do
184
+ @money = Money.us_dollar(0)
185
+ end
186
+
187
+ it "returns 'free' when :display_free is true" do
188
+ @money.format(:display_free => true).should == 'free'
189
+ end
190
+
191
+ it "returns '$0.00' when :display_free is false or not given" do
192
+ @money.format.should == '$0.00'
193
+ @money.format(:display_free => false).should == '$0.00'
194
+ @money.format(:display_free => nil).should == '$0.00'
195
+ end
196
+
197
+ it "returns the value specified by :display_free if it's a string-like object" do
198
+ @money.format(:display_free => 'gratis').should == 'gratis'
199
+ end
200
+ end
201
+
202
+ specify "#format(:with_currency => true) works as documented" do
203
+ Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
204
+ Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
205
+ end
206
+
207
+ specify "#format(:no_cents => true) works as documented" do
208
+ Money.ca_dollar(100).format(:no_cents => true).should == "$1"
209
+ Money.ca_dollar(599).format(:no_cents => true).should == "$5"
210
+ Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
211
+ Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
212
+ end
213
+
214
+ specify "#format(:no_cents => true) should respect :subunit_to_unit currency property" do
215
+ Money.new(10_00, "BHD").format(:no_cents => true).should == "ب.د1"
216
+ end
217
+
218
+ specify "#format(:no_cents_if_whole => true) works as documented" do
219
+ Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,000"
220
+ Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,034"
221
+ Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,000"
222
+ Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,006.4"
223
+ Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.000"
224
+ Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.003,4"
225
+ Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100"
226
+ Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100.34"
227
+ Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10"
228
+ Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10.034"
229
+ end
230
+
231
+ specify "#format(:no_cents_if_whole => false) works as documented" do
232
+ Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,000"
233
+ Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,034"
234
+ Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,000.0"
235
+ Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,006.4"
236
+ Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.000,0"
237
+ Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.003,4"
238
+ Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.00"
239
+ Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.34"
240
+ Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.000"
241
+ Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.034"
242
+ end
243
+
244
+ specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
245
+ Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
246
+ end
247
+
248
+ specify "#format(:symbol => true) returns symbol based on the given currency code" do
249
+ one = Proc.new do |currency|
250
+ Money.new(100, currency).format(:symbol => true)
251
+ end
252
+
253
+ # Pounds
254
+ one["GBP"].should == "£1.00"
255
+
256
+ # Dollars
257
+ one["USD"].should == "$1.00"
258
+ one["CAD"].should == "$1.00"
259
+ one["AUD"].should == "$1.00"
260
+ one["NZD"].should == "$1.00"
261
+ one["ZWD"].should == "$1.00"
262
+
263
+ # Yen
264
+ one["JPY"].should == "¥1.00"
265
+ one["CNY"].should == "¥1.00"
266
+
267
+ # Euro
268
+ one["EUR"].should == "1,00 €"
269
+
270
+ # Rupees
271
+ one["INR"].should == "₨1.00"
272
+ one["NPR"].should == "₨1.00"
273
+ one["SCR"].should == "1.00 ₨"
274
+ one["LKR"].should == "1.00 ₨"
275
+
276
+ # Brazilian Real
277
+ one["BRL"].should == "R$ 1,00"
278
+
279
+ # Other
280
+ one["SEK"].should == "kr1.00"
281
+ one["GHC"].should == "₵1.00"
282
+ end
283
+
284
+ specify "#format(:symbol => true) returns $ when currency code is not recognized" do
285
+ currency = Money::Currency.new("EUR")
286
+ currency.should_receive(:symbol).and_return(nil)
287
+ Money.new(100, currency).format(:symbol => true).should == "1,00 ¤"
288
+ end
289
+
290
+ specify "#format(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
291
+ Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
292
+ Money.new(100, "EUR").format(:symbol => true).should == "1,00 €"
293
+ Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
294
+ end
295
+
296
+ specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
297
+ money = Money.new(100, "GBP")
298
+ money.format(:symbol => "").should == "1.00"
299
+ money.format(:symbol => nil).should == "1.00"
300
+ money.format(:symbol => false).should == "1.00"
301
+ end
302
+
303
+ specify "#format without :symbol assumes that :symbol is set to true" do
304
+ money = Money.new(100)
305
+ money.format.should == "$1.00"
306
+
307
+ money = Money.new(100, "GBP")
308
+ money.format.should == "£1.00"
309
+
310
+ money = Money.new(100, "EUR")
311
+ money.format.should == "1,00 €"
312
+ end
313
+
314
+ specify "#format(:decimal_mark => a decimal_mark string) works as documented" do
315
+ Money.us_dollar(100).format(:decimal_mark => ",").should == "$1,00"
316
+ end
317
+
318
+ specify "#format will default decimal_mark to '.' if currency isn't recognized" do
319
+ Money.new(100, "ZWD").format.should == "$1.00"
320
+ end
321
+
322
+ specify "#format(:separator => a separator string) works as documented" do
323
+ Money.us_dollar(100).format(:separator => ",").should == "$1,00"
324
+ end
325
+
326
+ specify "#format(:thousands_separator => a thousands_separator string) works as documented" do
327
+ Money.us_dollar(100000).format(:thousands_separator => ".").should == "$1.000.00"
328
+ Money.us_dollar(200000).format(:thousands_separator => "").should == "$2000.00"
329
+ end
330
+
331
+ specify "#format(:thousands_separator => false or nil) works as documented" do
332
+ Money.us_dollar(100000).format(:thousands_separator => false).should == "$1000.00"
333
+ Money.us_dollar(200000).format(:thousands_separator => nil).should == "$2000.00"
334
+ end
335
+
336
+ specify "#format(:delimiter => a delimiter string) works as documented" do
337
+ Money.us_dollar(100000).format(:delimiter => ".").should == "$1.000.00"
338
+ Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
339
+ end
340
+
341
+ specify "#format(:delimiter => false or nil) works as documented" do
342
+ Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
343
+ Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
344
+ end
345
+
346
+ specify "#format will default thousands_separator to ',' if currency isn't recognized" do
347
+ Money.new(100000, "ZWD").format.should == "$1,000.00"
348
+ end
349
+
350
+ specify "#format(:html => true) works as documented" do
351
+ string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
352
+ string.should == "$5.70 <span class=\"currency\">CAD</span>"
353
+ end
354
+
355
+ it "should insert commas into the result if the amount is sufficiently large" do
356
+ Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
357
+ Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
358
+ end
359
+
360
+ it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
361
+ Money.euro(1_234_567_12).format.should == "1.234.567,12 €"
362
+ Money.euro(1_234_567_12).format(:no_cents => true).should == "1.234.567 €"
363
+ end
364
+
365
+ it "inserts currency symbol before the amount when :symbol_position is set to :before" do
366
+ Money.euro(1_234_567_12).format(:symbol_position => :before).should == "€1.234.567,12"
367
+ end
368
+
369
+ it "inserts currency symbol after the amount when :symbol_position is set to :after" do
370
+ Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after).should == "1,000,000,000.12 $"
371
+ end
372
+ end
373
+
374
+ end
375
+
@@ -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('&#036;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