monetize 1.12.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,587 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
- require 'monetize'
5
-
6
- describe Monetize do
7
- bar = <<-JSON
8
- {
9
- "priority": 1,
10
- "iso_code": "BAR",
11
- "iso_numeric": "840",
12
- "name": "Dollar with 4 decimal places",
13
- "symbol": "$",
14
- "subunit": "Cent",
15
- "subunit_to_unit": 10000,
16
- "symbol_first": true,
17
- "html_entity": "$",
18
- "decimal_mark": ".",
19
- "thousands_separator": ","
20
- }
21
- JSON
22
-
23
- eu4 = <<-JSON
24
- {
25
- "priority": 1,
26
- "iso_code": "EU4",
27
- "iso_numeric": "841",
28
- "name": "Euro with 4 decimal places",
29
- "symbol": "€",
30
- "subunit": "Cent",
31
- "subunit_to_unit": 10000,
32
- "symbol_first": true,
33
- "html_entity": "€",
34
- "decimal_mark": ",",
35
- "thousands_separator": "."
36
- }
37
- JSON
38
-
39
- describe '.parse' do
40
- it 'parses european-formatted inputs under 10EUR' do
41
- expect(Monetize.parse('EUR 5,95')).to eq Money.new(595, 'EUR')
42
- end
43
-
44
- it 'parses european-formatted inputs with multiple thousands-seperators' do
45
- expect(Monetize.parse('EUR 1.234.567,89')).to eq Money.new(1_234_567_89, 'EUR')
46
- expect(Monetize.parse('EUR 1.111.234.567,89')).to eq Money.new(1_111_234_567_89, 'EUR')
47
- end
48
-
49
- describe 'currency detection' do
50
- context 'opted in' do
51
- before :all do
52
- Monetize.assume_from_symbol = true
53
- end
54
-
55
- after :all do
56
- Monetize.assume_from_symbol = false
57
- end
58
-
59
- Monetize::Parser::CURRENCY_SYMBOLS.each_pair do |symbol, iso_code|
60
- context iso_code do
61
- let(:currency) { Money::Currency.find(iso_code) }
62
- let(:amount) { 5_95 }
63
- let(:amount_in_units) { amount.to_f / currency.subunit_to_unit }
64
-
65
- it 'ensures correct amount calculations for test' do
66
- expect(amount_in_units * currency.subunit_to_unit).to eq(amount)
67
- end
68
-
69
- it "parses formatted inputs with #{iso_code} passed as a symbol" do
70
- expect(Monetize.parse("#{symbol}#{amount_in_units}")).to eq Money.new(amount, iso_code)
71
- end
72
-
73
- it "parses formatted inputs with #{iso_code} symbol is after the amount" do
74
- expect(Monetize.parse("#{amount_in_units}#{symbol}")).to eq Money.new(amount, iso_code)
75
- end
76
-
77
- context 'prefix' do
78
- it 'parses formatted inputs with plus sign and currency as a symbol' do
79
- expect(Monetize.parse("+#{symbol}#{amount_in_units}")).to eq Money.new(amount, iso_code)
80
- end
81
-
82
- it 'parses formatted inputs with minus sign and currency as a symbol' do
83
- expect(Monetize.parse("-#{symbol}#{amount_in_units}")).to eq Money.new(-amount, iso_code)
84
- end
85
- end
86
-
87
- context 'postfix' do
88
- it 'parses formatted inputs with currency symbol and postfix minus sign' do
89
- expect(Monetize.parse("#{symbol}#{amount_in_units}-")).to eq Money.new(-amount, iso_code)
90
- end
91
-
92
- it 'parses formatted inputs with currency symbol and postfix plus sign' do
93
- expect(Monetize.parse("#{symbol}#{amount_in_units}+")).to eq Money.new(amount, iso_code)
94
- end
95
- end
96
-
97
- context 'amount suffixes' do
98
- it 'parses formatted inputs with amounts given with suffixes' do
99
- expect(Monetize.parse("#{symbol}1.26K")).to eq Money.new(1_260 * currency.subunit_to_unit, iso_code)
100
- expect(Monetize.parse("#{symbol}126.36M")).to eq Money.new(126_360_000 * currency.subunit_to_unit, iso_code)
101
- expect(Monetize.parse("#{symbol}.45B")).to eq Money.new(450_000_000 * currency.subunit_to_unit, iso_code)
102
- expect(Monetize.parse("-#{symbol}2.45B")).to eq Money.new(-2_450_000_000 * currency.subunit_to_unit, iso_code)
103
- expect(Monetize.parse("#{symbol}1.65T")).to eq Money.new(1_650_000_000_000 * currency.subunit_to_unit, iso_code)
104
- end
105
- end
106
-
107
- it 'parses formatted inputs with symbol and surrounding spaces' do
108
- expect(Monetize.parse(" #{symbol}#{amount_in_units} ")).to eq Money.new(amount, iso_code)
109
- end
110
-
111
- it 'parses formatted inputs without currency detection when overridden' do
112
- expect(Monetize.parse("#{symbol}5.95", nil, assume_from_symbol: false)).to eq Money.new(amount, 'USD')
113
- end
114
- end
115
- end
116
-
117
- it 'should assume default currency if not a recognised symbol' do
118
- expect(Monetize.parse('L9.99')).to eq Money.new(999, 'USD')
119
- end
120
-
121
- it 'should use provided currency over symbol' do
122
- expect(Monetize.parse('$1.05 CAD')).to eq Money.new(105, 'CAD')
123
- end
124
-
125
- it 'ignores ZAR symbols that is part of a text' do
126
- expect(Monetize.parse('EUR 9.99')).to eq Money.new(999, 'EUR')
127
- expect(Monetize.parse('9.99 EUR')).to eq Money.new(999, 'EUR')
128
- end
129
-
130
- context 'negatives' do
131
- it 'ignores the ambiguous kr symbol' do
132
- # Could mean either of DKK, EEK, ISK, NOK, SEK
133
- expect(Monetize.parse('kr9.99')).to eq Money.new(999, 'USD')
134
- end
135
- end
136
- end
137
-
138
- context 'opted out' do
139
- before do
140
- Monetize.assume_from_symbol = false
141
- end
142
-
143
- it 'ignores the Euro symbol' do
144
- expect(Monetize.parse('€5.95')).to eq Money.new(595, 'USD')
145
- end
146
-
147
- it 'ignores the South African Rand symbol' do
148
- expect(Monetize.parse('R5.95')).to eq Money.new(595, 'USD')
149
- end
150
-
151
- it 'ignores the Euro symbol with surrounding spaces' do
152
- expect(Monetize.parse(' €5.95 ')).to eq Money.new(595, 'USD')
153
- end
154
-
155
- it 'ignores the British Pounds Sterling symbol' do
156
- expect(Monetize.parse('£9.99')).to eq Money.new(999, 'USD')
157
- end
158
-
159
- it 'parses formatted inputs with currency detection when overridden' do
160
- expect(Monetize.parse('£9.99', nil, assume_from_symbol: true)).to eq Money.new(999, 'GBP')
161
- end
162
- end
163
-
164
- it 'should opt out by default' do
165
- expect(Monetize.assume_from_symbol).to be_falsy
166
- end
167
-
168
- context 'ISO code' do
169
- it 'parses currency given as ISO code' do
170
- expect('20.00 USD'.to_money).to eq Money.new(20_00, 'USD')
171
- expect('20.00 EUR'.to_money).to eq Money.new(20_00, 'EUR')
172
- expect('20.00 GBP'.to_money).to eq Money.new(20_00, 'GBP')
173
- end
174
-
175
- it 'raises an error if currency code is invalid' do
176
- expect { '20.00 OMG'.to_money }.to raise_error Monetize::ParseError
177
- end
178
- end
179
- end
180
-
181
- it 'parses USD-formatted inputs under $10' do
182
- five_ninety_five = Money.new(595, 'USD')
183
-
184
- expect(Monetize.parse(5.95)).to eq five_ninety_five
185
- expect(Monetize.parse('5.95')).to eq five_ninety_five
186
- expect(Monetize.parse('$5.95')).to eq five_ninety_five
187
- expect(Monetize.parse("\n $5.95 \n")).to eq five_ninety_five
188
- expect(Monetize.parse('$ 5.95')).to eq five_ninety_five
189
- expect(Monetize.parse('$5.95 ea.')).to eq five_ninety_five
190
- expect(Monetize.parse('$5.95, each')).to eq five_ninety_five
191
- end
192
-
193
- it 'parses USD-formatted inputs with multiple thousands-seperators' do
194
- expect(Monetize.parse('1,234,567.89')).to eq Money.new(1_234_567_89, 'USD')
195
- expect(Monetize.parse('1,111,234,567.89')).to eq Money.new(1_111_234_567_89, 'USD')
196
- end
197
-
198
- it 'parses DKK-formatted inputs' do
199
- expect(Monetize.parse('kr.123,45', 'DKK')).to eq Money.new(123_45, 'DKK')
200
- expect(Monetize.parse('kr.123.45', 'DKK')).to eq Money.new(123_45, 'DKK')
201
- expect(Monetize.parse('kr.45k', 'DKK')).to eq Money.new(45_000_00, 'DKK')
202
- end
203
-
204
- it 'returns nil if input is a price range' do
205
- expect(Monetize.parse('$5.95-10.95')).to be_nil
206
- expect(Monetize.parse('$5.95 - 10.95')).to be_nil
207
- expect(Monetize.parse('$5.95 - $10.95')).to be_nil
208
- end
209
-
210
- it 'does not return a price for completely invalid input' do
211
- expect(Monetize.parse(nil)).to eq Money.empty
212
- expect(Monetize.parse('hellothere')).to eq Money.empty
213
- expect(Monetize.parse('')).to eq Money.empty
214
- end
215
-
216
- it 'handles negative inputs' do
217
- five_ninety_five = Money.new(-595, 'USD')
218
-
219
- expect(Monetize.parse('$-5.95')).to eq five_ninety_five
220
- expect(Monetize.parse('-$5.95')).to eq five_ninety_five
221
- expect(Monetize.parse('$5.95-')).to eq five_ninety_five
222
- end
223
-
224
- it 'returns nil when unable to detect polarity' do
225
- expect(Monetize.parse('-$5.95-')).to be_nil
226
- end
227
-
228
- it 'returns nil when more than 2 digit separators are used' do
229
- expect(Monetize.parse("123.34,56'89 EUR")).to be_nil
230
- end
231
-
232
- it 'parses correctly strings with repeated digit separator' do
233
- expect(Monetize.parse('19.12.89', 'EUR')).to eq Money.new(191_289_00, 'EUR')
234
- end
235
-
236
- it 'parses correctly strings with exactly 3 decimal digits' do
237
- expect(Monetize.parse('6,534', 'EUR')).to eq Money.new(653, 'EUR')
238
- expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(653, 'EUR')
239
-
240
- Monetize.enforce_currency_delimiters = true
241
- expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(6_534_00, 'EUR')
242
- Monetize.enforce_currency_delimiters = false
243
- end
244
-
245
- context 'Money object attempting to be parsed' do
246
- let(:money) { Money.new(595, 'GBP') }
247
-
248
- it 'returns the original Money object' do
249
- expect(Monetize.parse(money)).to eq money
250
- expect(Monetize.parse(money).currency).to eq 'GBP'
251
- expect(Monetize.parse(money).cents).to eq 595
252
- end
253
- end
254
-
255
- context 'parsing an instance of Numeric class' do
256
- let(:integer) { 10 }
257
- let(:float) { 10.0 }
258
- let(:big_decimal) { BigDecimal('10') }
259
-
260
- [:integer, :float, :big_decimal].each do |type|
261
- it "returns a new Money object based on the #{type} input" do
262
- money = Monetize.parse(send(type), 'USD')
263
-
264
- expect(money).to be_instance_of(Money)
265
- expect(money.currency).to eq('USD')
266
- expect(money.cents).to eq(10_00)
267
- end
268
- end
269
- end
270
-
271
- context 'custom currencies with 4 decimal places' do
272
- before :each do
273
- Money::Currency.register(JSON.parse(bar, symbolize_names: true))
274
- Money::Currency.register(JSON.parse(eu4, symbolize_names: true))
275
- end
276
-
277
- after :each do
278
- Money::Currency.unregister(JSON.parse(bar, symbolize_names: true))
279
- Money::Currency.unregister(JSON.parse(eu4, symbolize_names: true))
280
- end
281
-
282
- # String#to_money(Currency) is equivalent to Monetize.parse(String, Currency)
283
- it 'parses strings respecting subunit to unit, decimal and thousands separator' do
284
- expect('$0.4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
285
- expect('€0,4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
286
-
287
- expect('$0.04'.to_money('BAR')).to eq Money.new(400, 'BAR')
288
- expect('€0,04'.to_money('EU4')).to eq Money.new(400, 'EU4')
289
-
290
- expect('$0.004'.to_money('BAR')).to eq Money.new(40, 'BAR')
291
- expect('€0,004'.to_money('EU4')).to eq Money.new(40, 'EU4')
292
-
293
- expect('$0.0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
294
- expect('€0,0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
295
-
296
- expect('$0.0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
297
- expect('€0,0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
298
-
299
- expect('$0.0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
300
- expect('€0,0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
301
-
302
- expect('$0.5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
303
- expect('€0,5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
304
-
305
- # Following currencies consider 4 decimal places
306
- # rubocop:disable Style/NumericLiterals
307
- expect('$6.5324'.to_money('BAR')).to eq Money.new(6_5324, 'BAR')
308
- expect('€6,5324'.to_money('EU4')).to eq Money.new(6_5324, 'EU4')
309
-
310
- expect('$86.5324'.to_money('BAR')).to eq Money.new(86_5324, 'BAR')
311
- expect('€86,5324'.to_money('EU4')).to eq Money.new(86_5324, 'EU4')
312
-
313
- expect('$186.5324'.to_money('BAR')).to eq Money.new(186_5324, 'BAR')
314
- expect('€186,5324'.to_money('EU4')).to eq Money.new(186_5324, 'EU4')
315
-
316
- expect('$3,331.0034'.to_money('BAR')).to eq Money.new(3_331_0034, 'BAR')
317
- expect('€3.331,0034'.to_money('EU4')).to eq Money.new(3_331_0034, 'EU4')
318
-
319
- expect('$8,883,331.0034'.to_money('BAR')).to eq Money.new(8_883_331_0034, 'BAR')
320
- expect('€8.883.331,0034'.to_money('EU4')).to eq Money.new(8_883_331_0034, 'EU4')
321
- # rubocop:enable Style/NumericLiterals
322
- end
323
-
324
- it 'parses strings deducing a decimal mark when a single delimiter is used and major is 0' do
325
- expect('$0,4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
326
- expect('€0.4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
327
-
328
- expect('$0,04'.to_money('BAR')).to eq Money.new(400, 'BAR')
329
- expect('€0.04'.to_money('EU4')).to eq Money.new(400, 'EU4')
330
-
331
- expect('$0,004'.to_money('BAR')).to eq Money.new(40, 'BAR')
332
- expect('€0.004'.to_money('EU4')).to eq Money.new(40, 'EU4')
333
-
334
- expect('$0,0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
335
- expect('€0.0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
336
-
337
- expect('$0,0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
338
- expect('€0.0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
339
-
340
- expect('$0,0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
341
- expect('€0.0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
342
-
343
- expect('$0,5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
344
- expect('€0.5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
345
- end
346
- end
347
- end
348
-
349
- describe '.parse!' do
350
- it 'does not return a price if there is a price range' do
351
- expect { Monetize.parse!('$5.95-10.95') }.to raise_error Monetize::ParseError
352
- expect { Monetize.parse!('$5.95 - 10.95') }.to raise_error Monetize::ParseError
353
- expect { Monetize.parse!('$5.95 - $10.95') }.to raise_error Monetize::ParseError
354
- end
355
-
356
- it 'raises ArgumentError when unable to detect polarity' do
357
- expect { Monetize.parse!('-$5.95-') }.to raise_error Monetize::ParseError
358
- end
359
-
360
- it 'raises ArgumentError with invalid format' do
361
- expect { Monetize.parse!('11..0') }.to raise_error Monetize::ParseError
362
- end
363
- end
364
-
365
- describe '.parse_collection' do
366
- it 'parses into a Money::Collection' do
367
- expect(Monetize.parse_collection('$7')).to be_a Monetize::Collection
368
- end
369
-
370
- it 'parses comma separated values' do
371
- collection = Monetize.parse_collection('$5, $7')
372
- expect(collection.first).to eq Monetize.parse('$5')
373
- expect(collection.last).to eq Monetize.parse('$7')
374
- end
375
-
376
- it 'parses slash separated values' do
377
- collection = Monetize.parse_collection('£4.50/€6')
378
- expect(collection.first).to eq Monetize.parse('£4.50')
379
- expect(collection.last).to eq Monetize.parse('€6')
380
- end
381
-
382
- it 'parses hyphens as ranges' do
383
- collection = Monetize.parse_collection('$4 - $10')
384
- expect(collection.first).to eq Monetize.parse('$4')
385
- expect(collection.last).to eq Monetize.parse('$10')
386
- end
387
-
388
- it 'raises an error if argument is invalid' do
389
- expect { Monetize.parse_collection(nil) }.to raise_error Monetize::ArgumentError
390
- end
391
- end
392
-
393
- describe '.from_string' do
394
- it 'converts given amount to cents' do
395
- expect(Monetize.from_string('1')).to eq Money.new(1_00)
396
- expect(Monetize.from_string('1')).to eq Money.new(1_00, 'USD')
397
- expect(Monetize.from_string('1', 'EUR')).to eq Money.new(1_00, 'EUR')
398
- end
399
-
400
- it 'respects :subunit_to_unit currency property' do
401
- expect(Monetize.from_string('1', 'USD')).to eq Money.new(1_00, 'USD')
402
- expect(Monetize.from_string('1', 'TND')).to eq Money.new(1_000, 'TND')
403
- expect(Monetize.from_string('1', 'JPY')).to eq Money.new(1, 'JPY')
404
- end
405
-
406
- it 'accepts a currency options' do
407
- m = Monetize.from_string('1')
408
- expect(m.currency).to eq Money.default_currency
409
-
410
- m = Monetize.from_string('1', Money::Currency.wrap('EUR'))
411
- expect(m.currency).to eq Money::Currency.wrap('EUR')
412
-
413
- m = Monetize.from_string('1', 'EUR')
414
- expect(m.currency).to eq Money::Currency.wrap('EUR')
415
- end
416
- end
417
-
418
- describe '.from_fixnum' do
419
- it 'converts given amount to cents' do
420
- expect(Monetize.from_fixnum(1)).to eq Money.new(1_00)
421
- expect(Monetize.from_fixnum(1)).to eq Money.new(1_00, 'USD')
422
- expect(Monetize.from_fixnum(1, 'EUR')).to eq Money.new(1_00, 'EUR')
423
- end
424
-
425
- it 'should respect :subunit_to_unit currency property' do
426
- expect(Monetize.from_fixnum(1, 'USD')).to eq Money.new(1_00, 'USD')
427
- expect(Monetize.from_fixnum(1, 'TND')).to eq Money.new(1_000, 'TND')
428
- expect(Monetize.from_fixnum(1, 'JPY')).to eq Money.new(1, 'JPY')
429
- end
430
-
431
- it 'accepts a currency options' do
432
- m = Monetize.from_fixnum(1)
433
- expect(m.currency).to eq Money.default_currency
434
-
435
- m = Monetize.from_fixnum(1, Money::Currency.wrap('EUR'))
436
- expect(m.currency).to eq Money::Currency.wrap('EUR')
437
-
438
- m = Monetize.from_fixnum(1, 'EUR')
439
- expect(m.currency).to eq Money::Currency.wrap('EUR')
440
- end
441
-
442
- it 'is aliased as from_integer' do
443
- expect(Monetize.from_integer(1)).to eq(Monetize.from_fixnum(1))
444
- end
445
- end
446
-
447
- describe '.from_float' do
448
- it 'converts given amount to cents' do
449
- expect(Monetize.from_float(1.2)).to eq Money.new(1_20)
450
- expect(Monetize.from_float(1.2)).to eq Money.new(1_20, 'USD')
451
- expect(Monetize.from_float(1.2, 'EUR')).to eq Money.new(1_20, 'EUR')
452
- end
453
-
454
- it 'respects :subunit_to_unit currency property' do
455
- expect(Monetize.from_float(1.2, 'USD')).to eq Money.new(1_20, 'USD')
456
- expect(Monetize.from_float(1.2, 'TND')).to eq Money.new(1_200, 'TND')
457
- expect(Monetize.from_float(1.2, 'JPY')).to eq Money.new(1, 'JPY')
458
- end
459
-
460
- it 'accepts a currency options' do
461
- m = Monetize.from_float(1.2)
462
- expect(m.currency).to eq Money.default_currency
463
-
464
- m = Monetize.from_float(1.2, Money::Currency.wrap('EUR'))
465
- expect(m.currency).to eq Money::Currency.wrap('EUR')
466
-
467
- m = Monetize.from_float(1.2, 'EUR')
468
- expect(m.currency).to eq Money::Currency.wrap('EUR')
469
- end
470
- end
471
-
472
- describe '.from_bigdecimal' do
473
- it 'converts given amount to cents' do
474
- expect(Monetize.from_bigdecimal(BigDecimal('1'))).to eq Money.new(1_00)
475
- expect(Monetize.from_bigdecimal(BigDecimal('1'))).to eq Money.new(1_00, 'USD')
476
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'EUR')).to eq Money.new(1_00, 'EUR')
477
- end
478
-
479
- it 'respects :subunit_to_unit currency property' do
480
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'USD')).to eq Money.new(1_00, 'USD')
481
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'TND')).to eq Money.new(1_000, 'TND')
482
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'JPY')).to eq Money.new(1, 'JPY')
483
- end
484
-
485
- it 'respects rounding mode when rounding amount to the nearest cent' do
486
- amount = BigDecimal('1.005')
487
-
488
- expect(Monetize.from_bigdecimal(amount, 'USD')).to eq Money.from_amount(amount, 'USD')
489
- end
490
-
491
- it 'accepts a currency options' do
492
- m = Monetize.from_bigdecimal(BigDecimal('1'))
493
- expect(m.currency).to eq Money.default_currency
494
-
495
- m = Monetize.from_bigdecimal(BigDecimal('1'), Money::Currency.wrap('EUR'))
496
- expect(m.currency).to eq Money::Currency.wrap('EUR')
497
-
498
- m = Monetize.from_bigdecimal(BigDecimal('1'), 'EUR')
499
- expect(m.currency).to eq Money::Currency.wrap('EUR')
500
- end
501
-
502
- context 'infinite_precision = true' do
503
- before do
504
- Money.infinite_precision = true
505
- end
506
-
507
- after do
508
- Money.infinite_precision = false
509
- end
510
-
511
- it 'keeps precision' do
512
- expect(Monetize.from_bigdecimal(BigDecimal('1'))).to eq Money.new(100)
513
- expect(Monetize.from_bigdecimal(BigDecimal('1.23456'))).to eq Money.new(123.456)
514
- expect(Monetize.from_bigdecimal(BigDecimal('-1.23456'))).to eq Money.new(-123.456)
515
- expect(Monetize.from_bigdecimal(BigDecimal('1.23456'))).to eq Money.new(123.456, 'USD')
516
- expect(Monetize.from_bigdecimal(BigDecimal('1.23456'), 'EUR')).to eq Money.new(123.456, 'EUR')
517
-
518
- expect('1'.to_money).to eq Money.new(100)
519
- expect('1.23456'.to_money).to eq Money.new(123.456)
520
- expect('-1.23456'.to_money).to eq Money.new(-123.456)
521
- expect('$1.23456'.to_money).to eq Money.new(123.456, 'USD')
522
- expect('1.23456 EUR'.to_money).to eq Money.new(123.456, 'EUR')
523
- end
524
- end
525
- end
526
-
527
- describe '.from_numeric' do
528
- it 'converts given amount to cents' do
529
- expect(Monetize.from_numeric(1)).to eq Money.new(1_00)
530
- expect(Monetize.from_numeric(1.0)).to eq Money.new(1_00)
531
- expect(Monetize.from_numeric(BigDecimal('1'))).to eq Money.new(1_00)
532
- end
533
-
534
- it 'raises ArgumentError with unsupported argument' do
535
- expect { Monetize.from_numeric('100') }.to raise_error(Monetize::ArgumentError)
536
- end
537
-
538
- it 'respects :subunit_to_unit currency property' do
539
- expect(Monetize.from_numeric(1, 'USD')).to eq Money.new(1_00, 'USD')
540
- expect(Monetize.from_numeric(1, 'TND')).to eq Money.new(1_000, 'TND')
541
- expect(Monetize.from_numeric(1, 'JPY')).to eq Money.new(1, 'JPY')
542
- end
543
-
544
- it 'accepts a bank option' do
545
- expect(Monetize.from_numeric(1)).to eq Money.new(1_00)
546
- expect(Monetize.from_numeric(1)).to eq Money.new(1_00, 'USD')
547
- expect(Monetize.from_numeric(1, 'EUR')).to eq Money.new(1_00, 'EUR')
548
- end
549
-
550
- it 'accepts a currency options' do
551
- m = Monetize.from_numeric(1)
552
- expect(m.currency).to eq Money.default_currency
553
-
554
- m = Monetize.from_numeric(1, Money::Currency.wrap('EUR'))
555
- expect(m.currency).to eq Money::Currency.wrap('EUR')
556
-
557
- m = Monetize.from_numeric(1, 'EUR')
558
- expect(m.currency).to eq Money::Currency.wrap('EUR')
559
- end
560
- end
561
-
562
- describe '.extract_cents' do
563
- it 'is deprecated' do
564
- allow(Monetize).to receive(:warn)
565
-
566
- Monetize.extract_cents('100')
567
-
568
- expect(Monetize)
569
- .to have_received(:warn)
570
- .with('[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents')
571
- end
572
-
573
- it 'extracts cents from a given string' do
574
- expect(Monetize.extract_cents('10.99')).to eq(1099)
575
- end
576
-
577
- it "correctly treats pipe marks '|' in input (regression test)" do
578
- expect(Monetize.extract_cents('100|0')).to eq Monetize.extract_cents('100!0')
579
- end
580
- end
581
-
582
- context 'given the same inputs to .parse and .from_*' do
583
- it 'gives the same results' do
584
- expect(4.635.to_money).to eq '4.635'.to_money
585
- end
586
- end
587
- end
data/spec/spec_helper.rb DELETED
@@ -1,7 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'money'
4
-
5
- RSpec.configure do |config|
6
- config.order = 'random'
7
- end