monetize 1.13.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,633 +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
- context 'with default currency' do
176
- before do
177
- Money.default_currency = Money::Currency.new('USD')
178
- end
179
-
180
- it 'parses currency given using default currency' do
181
- expect('20.00 OMG'.to_money).to eq Money.new(20_00, 'USD')
182
- end
183
- end
184
-
185
- context 'without default currency' do
186
- before do
187
- Money.default_currency = nil
188
- end
189
-
190
- after do
191
- Money.default_currency = Money::Currency.new('USD')
192
- end
193
-
194
- it 'raises an error if currency code is invalid' do
195
- expect { '20.00 OMG'.to_money }.to raise_error
196
- end
197
- end
198
- end
199
- end
200
-
201
- it 'parses USD-formatted inputs under $10' do
202
- five_ninety_five = Money.new(595, 'USD')
203
-
204
- expect(Monetize.parse(5.95)).to eq five_ninety_five
205
- expect(Monetize.parse('5.95')).to eq five_ninety_five
206
- expect(Monetize.parse('$5.95')).to eq five_ninety_five
207
- expect(Monetize.parse("\n $5.95 \n")).to eq five_ninety_five
208
- expect(Monetize.parse('$ 5.95')).to eq five_ninety_five
209
- expect(Monetize.parse('$5.95 ea.')).to eq five_ninety_five
210
- expect(Monetize.parse('$5.95, each')).to eq five_ninety_five
211
- end
212
-
213
- it 'parses USD-formatted inputs with multiple thousands-seperators' do
214
- expect(Monetize.parse('1,234,567.89')).to eq Money.new(1_234_567_89, 'USD')
215
- expect(Monetize.parse('1,111,234,567.89')).to eq Money.new(1_111_234_567_89, 'USD')
216
- end
217
-
218
- it 'parses DKK-formatted inputs' do
219
- expect(Monetize.parse('kr.123,45', 'DKK')).to eq Money.new(123_45, 'DKK')
220
- expect(Monetize.parse('kr.123.45', 'DKK')).to eq Money.new(123_45, 'DKK')
221
- expect(Monetize.parse('kr.45k', 'DKK')).to eq Money.new(45_000_00, 'DKK')
222
- end
223
-
224
- it 'returns nil if input is a price range' do
225
- expect(Monetize.parse('$5.95-10.95')).to be_nil
226
- expect(Monetize.parse('$5.95 - 10.95')).to be_nil
227
- expect(Monetize.parse('$5.95 - $10.95')).to be_nil
228
- end
229
-
230
- it 'does not return a price for completely invalid input' do
231
- expect(Monetize.parse(nil)).to eq Money.empty
232
- expect(Monetize.parse('hellothere')).to eq Money.empty
233
- expect(Monetize.parse('')).to eq Money.empty
234
- end
235
-
236
- it 'handles negative inputs' do
237
- five_ninety_five = Money.new(-595, 'USD')
238
-
239
- expect(Monetize.parse('$-5.95')).to eq five_ninety_five
240
- expect(Monetize.parse('-$5.95')).to eq five_ninety_five
241
- expect(Monetize.parse('$5.95-')).to eq five_ninety_five
242
- end
243
-
244
- it 'returns nil when unable to detect polarity' do
245
- expect(Monetize.parse('-$5.95-')).to be_nil
246
- end
247
-
248
- it 'returns nil when more than 2 digit separators are used' do
249
- expect(Monetize.parse("123.34,56'89 EUR")).to be_nil
250
- end
251
-
252
- it 'parses correctly strings with repeated digit separator' do
253
- expect(Monetize.parse('19.12.89', 'EUR')).to eq Money.new(191_289_00, 'EUR')
254
- end
255
-
256
- it 'parses correctly strings with exactly 3 decimal digits' do
257
- expect(Monetize.parse('6,534', 'EUR')).to eq Money.new(653, 'EUR')
258
- expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(653, 'EUR')
259
-
260
- Monetize.enforce_currency_delimiters = true
261
- expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(6_534_00, 'EUR')
262
- Monetize.enforce_currency_delimiters = false
263
- end
264
-
265
- context 'Money object attempting to be parsed' do
266
- let(:money) { Money.new(595, 'GBP') }
267
-
268
- it 'returns the original Money object' do
269
- expect(Monetize.parse(money)).to eq money
270
- expect(Monetize.parse(money).currency).to eq 'GBP'
271
- expect(Monetize.parse(money).cents).to eq 595
272
- end
273
- end
274
-
275
- context 'parsing an instance of Numeric class' do
276
- let(:integer) { 10 }
277
- let(:float) { 10.0 }
278
- let(:big_decimal) { BigDecimal('10') }
279
-
280
- [:integer, :float, :big_decimal].each do |type|
281
- it "returns a new Money object based on the #{type} input" do
282
- money = Monetize.parse(send(type), 'USD')
283
-
284
- expect(money).to be_instance_of(Money)
285
- expect(money.currency).to eq('USD')
286
- expect(money.cents).to eq(10_00)
287
- end
288
- end
289
- end
290
-
291
- context 'custom currencies with 4 decimal places' do
292
- before :each do
293
- Money::Currency.register(JSON.parse(bar, symbolize_names: true))
294
- Money::Currency.register(JSON.parse(eu4, symbolize_names: true))
295
- end
296
-
297
- after :each do
298
- Money::Currency.unregister(JSON.parse(bar, symbolize_names: true))
299
- Money::Currency.unregister(JSON.parse(eu4, symbolize_names: true))
300
- end
301
-
302
- # String#to_money(Currency) is equivalent to Monetize.parse(String, Currency)
303
- it 'parses strings respecting subunit to unit, decimal and thousands separator' do
304
- expect('$0.4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
305
- expect('€0,4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
306
-
307
- expect('$0.04'.to_money('BAR')).to eq Money.new(400, 'BAR')
308
- expect('€0,04'.to_money('EU4')).to eq Money.new(400, 'EU4')
309
-
310
- expect('$0.004'.to_money('BAR')).to eq Money.new(40, 'BAR')
311
- expect('€0,004'.to_money('EU4')).to eq Money.new(40, 'EU4')
312
-
313
- expect('$0.0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
314
- expect('€0,0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
315
-
316
- expect('$0.0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
317
- expect('€0,0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
318
-
319
- expect('$0.0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
320
- expect('€0,0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
321
-
322
- expect('$0.5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
323
- expect('€0,5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
324
-
325
- # Following currencies consider 4 decimal places
326
- # rubocop:disable Style/NumericLiterals
327
- expect('$6.5324'.to_money('BAR')).to eq Money.new(6_5324, 'BAR')
328
- expect('€6,5324'.to_money('EU4')).to eq Money.new(6_5324, 'EU4')
329
-
330
- expect('$86.5324'.to_money('BAR')).to eq Money.new(86_5324, 'BAR')
331
- expect('€86,5324'.to_money('EU4')).to eq Money.new(86_5324, 'EU4')
332
-
333
- expect('$186.5324'.to_money('BAR')).to eq Money.new(186_5324, 'BAR')
334
- expect('€186,5324'.to_money('EU4')).to eq Money.new(186_5324, 'EU4')
335
-
336
- expect('$3,331.0034'.to_money('BAR')).to eq Money.new(3_331_0034, 'BAR')
337
- expect('€3.331,0034'.to_money('EU4')).to eq Money.new(3_331_0034, 'EU4')
338
-
339
- expect('$8,883,331.0034'.to_money('BAR')).to eq Money.new(8_883_331_0034, 'BAR')
340
- expect('€8.883.331,0034'.to_money('EU4')).to eq Money.new(8_883_331_0034, 'EU4')
341
- # rubocop:enable Style/NumericLiterals
342
- end
343
-
344
- it 'parses strings deducing a decimal mark when a single delimiter is used and major is 0' do
345
- expect('$0,4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
346
- expect('€0.4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
347
-
348
- expect('$0,04'.to_money('BAR')).to eq Money.new(400, 'BAR')
349
- expect('€0.04'.to_money('EU4')).to eq Money.new(400, 'EU4')
350
-
351
- expect('$0,004'.to_money('BAR')).to eq Money.new(40, 'BAR')
352
- expect('€0.004'.to_money('EU4')).to eq Money.new(40, 'EU4')
353
-
354
- expect('$0,0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
355
- expect('€0.0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
356
-
357
- expect('$0,0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
358
- expect('€0.0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
359
-
360
- expect('$0,0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
361
- expect('€0.0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
362
-
363
- expect('$0,5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
364
- expect('€0.5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
365
- end
366
- end
367
-
368
- describe "expecting whole subunits" do
369
- before(:all) do
370
- Monetize.expect_whole_subunits = true
371
- Monetize.assume_from_symbol = true
372
- end
373
-
374
- after(:all) do
375
- Monetize.expect_whole_subunits = false
376
- Monetize.assume_from_symbol = false
377
- end
378
-
379
- it "handles euros" do
380
- expect(Monetize.parse('€10,000')).to eq Money.new(10_000_00, 'EUR')
381
- expect(Monetize.parse('€10,00')).to eq Money.new(10_00, 'EUR')
382
- expect(Monetize.parse('€10.00')).to eq Money.new(10_00, 'EUR')
383
- expect(Monetize.parse('EUR 10,000.00')).to eq Money.new(10_000_00, 'EUR')
384
- end
385
-
386
- it "handles GBP" do
387
- expect(Monetize.parse('£10,000')).to eq Money.new(10_000_00, 'GBP')
388
- expect(Monetize.parse('£10.000')).to eq Money.new(10_000_00, 'GBP')
389
- expect(Monetize.parse('£10,00')).to eq Money.new(10_00, 'GBP')
390
- expect(Monetize.parse('£10.00')).to eq Money.new(10_00, 'GBP')
391
- end
392
- end
393
- end
394
-
395
- describe '.parse!' do
396
- it 'does not return a price if there is a price range' do
397
- expect { Monetize.parse!('$5.95-10.95') }.to raise_error Monetize::ParseError
398
- expect { Monetize.parse!('$5.95 - 10.95') }.to raise_error Monetize::ParseError
399
- expect { Monetize.parse!('$5.95 - $10.95') }.to raise_error Monetize::ParseError
400
- end
401
-
402
- it 'raises ArgumentError when unable to detect polarity' do
403
- expect { Monetize.parse!('-$5.95-') }.to raise_error Monetize::ParseError
404
- end
405
-
406
- it 'raises ArgumentError with invalid format' do
407
- expect { Monetize.parse!('11..0') }.to raise_error Monetize::ParseError
408
- end
409
- end
410
-
411
- describe '.parse_collection' do
412
- it 'parses into a Money::Collection' do
413
- expect(Monetize.parse_collection('$7')).to be_a Monetize::Collection
414
- end
415
-
416
- it 'parses comma separated values' do
417
- collection = Monetize.parse_collection('$5, $7')
418
- expect(collection.first).to eq Monetize.parse('$5')
419
- expect(collection.last).to eq Monetize.parse('$7')
420
- end
421
-
422
- it 'parses slash separated values' do
423
- collection = Monetize.parse_collection('£4.50/€6')
424
- expect(collection.first).to eq Monetize.parse('£4.50')
425
- expect(collection.last).to eq Monetize.parse('€6')
426
- end
427
-
428
- it 'parses hyphens as ranges' do
429
- collection = Monetize.parse_collection('$4 - $10')
430
- expect(collection.first).to eq Monetize.parse('$4')
431
- expect(collection.last).to eq Monetize.parse('$10')
432
- end
433
-
434
- it 'raises an error if argument is invalid' do
435
- expect { Monetize.parse_collection(nil) }.to raise_error Monetize::ArgumentError
436
- end
437
- end
438
-
439
- describe '.from_string' do
440
- it 'converts given amount to cents' do
441
- expect(Monetize.from_string('1')).to eq Money.new(1_00)
442
- expect(Monetize.from_string('1')).to eq Money.new(1_00, 'USD')
443
- expect(Monetize.from_string('1', 'EUR')).to eq Money.new(1_00, 'EUR')
444
- end
445
-
446
- it 'respects :subunit_to_unit currency property' do
447
- expect(Monetize.from_string('1', 'USD')).to eq Money.new(1_00, 'USD')
448
- expect(Monetize.from_string('1', 'TND')).to eq Money.new(1_000, 'TND')
449
- expect(Monetize.from_string('1', 'JPY')).to eq Money.new(1, 'JPY')
450
- end
451
-
452
- it 'accepts a currency options' do
453
- m = Monetize.from_string('1')
454
- expect(m.currency).to eq Money.default_currency
455
-
456
- m = Monetize.from_string('1', Money::Currency.wrap('EUR'))
457
- expect(m.currency).to eq Money::Currency.wrap('EUR')
458
-
459
- m = Monetize.from_string('1', 'EUR')
460
- expect(m.currency).to eq Money::Currency.wrap('EUR')
461
- end
462
- end
463
-
464
- describe '.from_fixnum' do
465
- it 'converts given amount to cents' do
466
- expect(Monetize.from_fixnum(1)).to eq Money.new(1_00)
467
- expect(Monetize.from_fixnum(1)).to eq Money.new(1_00, 'USD')
468
- expect(Monetize.from_fixnum(1, 'EUR')).to eq Money.new(1_00, 'EUR')
469
- end
470
-
471
- it 'should respect :subunit_to_unit currency property' do
472
- expect(Monetize.from_fixnum(1, 'USD')).to eq Money.new(1_00, 'USD')
473
- expect(Monetize.from_fixnum(1, 'TND')).to eq Money.new(1_000, 'TND')
474
- expect(Monetize.from_fixnum(1, 'JPY')).to eq Money.new(1, 'JPY')
475
- end
476
-
477
- it 'accepts a currency options' do
478
- m = Monetize.from_fixnum(1)
479
- expect(m.currency).to eq Money.default_currency
480
-
481
- m = Monetize.from_fixnum(1, Money::Currency.wrap('EUR'))
482
- expect(m.currency).to eq Money::Currency.wrap('EUR')
483
-
484
- m = Monetize.from_fixnum(1, 'EUR')
485
- expect(m.currency).to eq Money::Currency.wrap('EUR')
486
- end
487
-
488
- it 'is aliased as from_integer' do
489
- expect(Monetize.from_integer(1)).to eq(Monetize.from_fixnum(1))
490
- end
491
- end
492
-
493
- describe '.from_float' do
494
- it 'converts given amount to cents' do
495
- expect(Monetize.from_float(1.2)).to eq Money.new(1_20)
496
- expect(Monetize.from_float(1.2)).to eq Money.new(1_20, 'USD')
497
- expect(Monetize.from_float(1.2, 'EUR')).to eq Money.new(1_20, 'EUR')
498
- end
499
-
500
- it 'respects :subunit_to_unit currency property' do
501
- expect(Monetize.from_float(1.2, 'USD')).to eq Money.new(1_20, 'USD')
502
- expect(Monetize.from_float(1.2, 'TND')).to eq Money.new(1_200, 'TND')
503
- expect(Monetize.from_float(1.2, 'JPY')).to eq Money.new(1, 'JPY')
504
- end
505
-
506
- it 'accepts a currency options' do
507
- m = Monetize.from_float(1.2)
508
- expect(m.currency).to eq Money.default_currency
509
-
510
- m = Monetize.from_float(1.2, Money::Currency.wrap('EUR'))
511
- expect(m.currency).to eq Money::Currency.wrap('EUR')
512
-
513
- m = Monetize.from_float(1.2, 'EUR')
514
- expect(m.currency).to eq Money::Currency.wrap('EUR')
515
- end
516
- end
517
-
518
- describe '.from_bigdecimal' do
519
- it 'converts given amount to cents' do
520
- expect(Monetize.from_bigdecimal(BigDecimal('1'))).to eq Money.new(1_00)
521
- expect(Monetize.from_bigdecimal(BigDecimal('1'))).to eq Money.new(1_00, 'USD')
522
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'EUR')).to eq Money.new(1_00, 'EUR')
523
- end
524
-
525
- it 'respects :subunit_to_unit currency property' do
526
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'USD')).to eq Money.new(1_00, 'USD')
527
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'TND')).to eq Money.new(1_000, 'TND')
528
- expect(Monetize.from_bigdecimal(BigDecimal('1'), 'JPY')).to eq Money.new(1, 'JPY')
529
- end
530
-
531
- it 'respects rounding mode when rounding amount to the nearest cent' do
532
- amount = BigDecimal('1.005')
533
-
534
- expect(Monetize.from_bigdecimal(amount, 'USD')).to eq Money.from_amount(amount, 'USD')
535
- end
536
-
537
- it 'accepts a currency options' do
538
- m = Monetize.from_bigdecimal(BigDecimal('1'))
539
- expect(m.currency).to eq Money.default_currency
540
-
541
- m = Monetize.from_bigdecimal(BigDecimal('1'), Money::Currency.wrap('EUR'))
542
- expect(m.currency).to eq Money::Currency.wrap('EUR')
543
-
544
- m = Monetize.from_bigdecimal(BigDecimal('1'), 'EUR')
545
- expect(m.currency).to eq Money::Currency.wrap('EUR')
546
- end
547
-
548
- context 'infinite_precision = true' do
549
- before do
550
- Money.infinite_precision = true
551
- end
552
-
553
- after do
554
- Money.infinite_precision = false
555
- end
556
-
557
- it 'keeps precision' do
558
- expect(Monetize.from_bigdecimal(BigDecimal('1'))).to eq Money.new(100)
559
- expect(Monetize.from_bigdecimal(BigDecimal('1.23456'))).to eq Money.new(123.456)
560
- expect(Monetize.from_bigdecimal(BigDecimal('-1.23456'))).to eq Money.new(-123.456)
561
- expect(Monetize.from_bigdecimal(BigDecimal('1.23456'))).to eq Money.new(123.456, 'USD')
562
- expect(Monetize.from_bigdecimal(BigDecimal('1.23456'), 'EUR')).to eq Money.new(123.456, 'EUR')
563
-
564
- expect('1'.to_money).to eq Money.new(100)
565
- expect('1.23456'.to_money).to eq Money.new(123.456)
566
- expect('-1.23456'.to_money).to eq Money.new(-123.456)
567
- expect('$1.23456'.to_money).to eq Money.new(123.456, 'USD')
568
- expect('1.23456 EUR'.to_money).to eq Money.new(123.456, 'EUR')
569
- end
570
- end
571
- end
572
-
573
- describe '.from_numeric' do
574
- it 'converts given amount to cents' do
575
- expect(Monetize.from_numeric(1)).to eq Money.new(1_00)
576
- expect(Monetize.from_numeric(1.0)).to eq Money.new(1_00)
577
- expect(Monetize.from_numeric(BigDecimal('1'))).to eq Money.new(1_00)
578
- end
579
-
580
- it 'raises ArgumentError with unsupported argument' do
581
- expect { Monetize.from_numeric('100') }.to raise_error(Monetize::ArgumentError)
582
- end
583
-
584
- it 'respects :subunit_to_unit currency property' do
585
- expect(Monetize.from_numeric(1, 'USD')).to eq Money.new(1_00, 'USD')
586
- expect(Monetize.from_numeric(1, 'TND')).to eq Money.new(1_000, 'TND')
587
- expect(Monetize.from_numeric(1, 'JPY')).to eq Money.new(1, 'JPY')
588
- end
589
-
590
- it 'accepts a bank option' do
591
- expect(Monetize.from_numeric(1)).to eq Money.new(1_00)
592
- expect(Monetize.from_numeric(1)).to eq Money.new(1_00, 'USD')
593
- expect(Monetize.from_numeric(1, 'EUR')).to eq Money.new(1_00, 'EUR')
594
- end
595
-
596
- it 'accepts a currency options' do
597
- m = Monetize.from_numeric(1)
598
- expect(m.currency).to eq Money.default_currency
599
-
600
- m = Monetize.from_numeric(1, Money::Currency.wrap('EUR'))
601
- expect(m.currency).to eq Money::Currency.wrap('EUR')
602
-
603
- m = Monetize.from_numeric(1, 'EUR')
604
- expect(m.currency).to eq Money::Currency.wrap('EUR')
605
- end
606
- end
607
-
608
- describe '.extract_cents' do
609
- it 'is deprecated' do
610
- allow(Monetize).to receive(:warn)
611
-
612
- Monetize.extract_cents('100')
613
-
614
- expect(Monetize)
615
- .to have_received(:warn)
616
- .with('[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents')
617
- end
618
-
619
- it 'extracts cents from a given string' do
620
- expect(Monetize.extract_cents('10.99')).to eq(1099)
621
- end
622
-
623
- it "correctly treats pipe marks '|' in input (regression test)" do
624
- expect(Monetize.extract_cents('100|0')).to eq Monetize.extract_cents('100!0')
625
- end
626
- end
627
-
628
- context 'given the same inputs to .parse and .from_*' do
629
- it 'gives the same results' do
630
- expect(4.635.to_money).to eq '4.635'.to_money
631
- end
632
- end
633
- 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