shopify-money 0.14.6 → 1.0.0.pre
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.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +34 -0
- data/README.md +8 -2
- data/UPGRADING.md +57 -0
- data/config/currency_historic.yml +1 -1
- data/lib/money.rb +1 -1
- data/lib/money/allocator.rb +3 -1
- data/lib/money/config.rb +26 -0
- data/lib/money/currency.rb +4 -0
- data/lib/money/helpers.rb +12 -4
- data/lib/money/money.rb +52 -35
- data/lib/money/money_parser.rb +11 -5
- data/lib/money/null_currency.rb +32 -1
- data/lib/money/version.rb +1 -1
- data/lib/money_column/active_record_hooks.rb +12 -7
- data/lib/rubocop/cop/money.rb +1 -0
- data/lib/rubocop/cop/money/missing_currency.rb +16 -8
- data/lib/rubocop/cop/money/zero_money.rb +65 -0
- data/money.gemspec +1 -1
- data/spec/accounting_money_parser_spec.rb +4 -2
- data/spec/allocator_spec.rb +3 -3
- data/spec/config_spec.rb +60 -0
- data/spec/core_extensions_spec.rb +5 -5
- data/spec/currency_spec.rb +10 -0
- data/spec/helpers_spec.rb +11 -5
- data/spec/money_column_spec.rb +55 -34
- data/spec/money_parser_spec.rb +20 -8
- data/spec/money_spec.rb +123 -90
- data/spec/rubocop/cop/money/missing_currency_spec.rb +43 -8
- data/spec/rubocop/cop/money/zero_money_spec.rb +78 -0
- data/spec/spec_helper.rb +15 -0
- metadata +14 -10
- data/.travis.yml +0 -13
- data/lib/money_accessor.rb +0 -33
- data/spec/money_accessor_spec.rb +0 -87
data/spec/money_parser_spec.rb
CHANGED
@@ -8,13 +8,15 @@ RSpec.describe MoneyParser do
|
|
8
8
|
|
9
9
|
describe "parsing of amounts with period decimal separator" do
|
10
10
|
it "parses an empty string to $0" do
|
11
|
-
expect(@parser.parse("")).to eq(Money.
|
11
|
+
expect(@parser.parse("")).to eq(Money.new(0, Money::NULL_CURRENCY))
|
12
12
|
end
|
13
13
|
|
14
14
|
it "parses an invalid string when not strict" do
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
configure(legacy_deprecations: true) do
|
16
|
+
expect(Money).to receive(:deprecate).twice
|
17
|
+
expect(@parser.parse("no money", 'USD')).to eq(Money.new(0, 'USD'))
|
18
|
+
expect(@parser.parse("1..", 'USD')).to eq(Money.new(1, 'USD'))
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
22
|
it "parses raise with an invalid string and strict option" do
|
@@ -22,6 +24,12 @@ RSpec.describe MoneyParser do
|
|
22
24
|
expect { @parser.parse("1..1", strict: true) }.to raise_error(MoneyParser::MoneyFormatError)
|
23
25
|
end
|
24
26
|
|
27
|
+
it "parses raise with an invalid when a currency is missing" do
|
28
|
+
configure do
|
29
|
+
expect { @parser.parse("1") }.to raise_error(Money::Currency::UnknownCurrency)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
25
33
|
it "parses a single digit integer string" do
|
26
34
|
expect(@parser.parse("1")).to eq(Money.new(1.00))
|
27
35
|
end
|
@@ -144,8 +152,10 @@ RSpec.describe MoneyParser do
|
|
144
152
|
end
|
145
153
|
|
146
154
|
it "parses amount with multiple inconsistent thousands delimiters" do
|
147
|
-
|
148
|
-
|
155
|
+
configure(legacy_deprecations: true) do
|
156
|
+
expect(Money).to receive(:deprecate).once
|
157
|
+
expect(@parser.parse("1.1.11.111", 'USD')).to eq(Money.new(1_111_111, 'USD'))
|
158
|
+
end
|
149
159
|
end
|
150
160
|
|
151
161
|
it "parses raises with multiple inconsistent thousands delimiters and strict option" do
|
@@ -216,8 +226,10 @@ RSpec.describe MoneyParser do
|
|
216
226
|
end
|
217
227
|
|
218
228
|
it "parses amount with multiple inconsistent thousands delimiters" do
|
219
|
-
|
220
|
-
|
229
|
+
configure(legacy_deprecations: true) do
|
230
|
+
expect(Money).to receive(:deprecate).once
|
231
|
+
expect(@parser.parse("1,1,11,111", 'USD')).to eq(Money.new(1_111_111, 'USD'))
|
232
|
+
end
|
221
233
|
end
|
222
234
|
|
223
235
|
it "parses raises with multiple inconsistent thousands delimiters and strict option" do
|
data/spec/money_spec.rb
CHANGED
@@ -3,40 +3,25 @@ require 'spec_helper'
|
|
3
3
|
require 'yaml'
|
4
4
|
|
5
5
|
RSpec.describe "Money" do
|
6
|
-
|
7
6
|
let (:money) { Money.new(1) }
|
8
7
|
let (:amount_money) { Money.new(1.23, 'USD') }
|
9
8
|
let (:non_fractional_money) { Money.new(1, 'JPY') }
|
10
9
|
let (:zero_money) { Money.new(0) }
|
11
10
|
|
12
|
-
it "is contructable with empty class method" do
|
13
|
-
expect(Money.empty).to eq(Money.new)
|
14
|
-
end
|
15
|
-
|
16
11
|
context "default currency not set" do
|
17
|
-
before(:each) do
|
18
|
-
@default_currency = Money.default_currency
|
19
|
-
Money.default_currency = nil
|
20
|
-
end
|
21
|
-
after(:each) do
|
22
|
-
Money.default_currency = @default_currency
|
23
|
-
end
|
24
|
-
|
25
12
|
it "raises an error" do
|
26
|
-
|
13
|
+
configure(default_currency: nil) do
|
14
|
+
expect { money }.to raise_error(ArgumentError)
|
15
|
+
end
|
27
16
|
end
|
28
17
|
end
|
29
18
|
|
30
19
|
it ".zero has no currency" do
|
31
|
-
expect(Money.
|
20
|
+
expect(Money.new(0, Money::NULL_CURRENCY).currency).to be_a(Money::NullCurrency)
|
32
21
|
end
|
33
22
|
|
34
23
|
it ".zero is a 0$ value" do
|
35
|
-
expect(Money.
|
36
|
-
end
|
37
|
-
|
38
|
-
it ".zero accepts an optional currency" do
|
39
|
-
expect(Money.zero('USD')).to eq(Money.new(0, 'USD'))
|
24
|
+
expect(Money.new(0, Money::NULL_CURRENCY)).to eq(Money.new(0))
|
40
25
|
end
|
41
26
|
|
42
27
|
it "returns itself with to_money" do
|
@@ -47,9 +32,15 @@ RSpec.describe "Money" do
|
|
47
32
|
expect(Money.new(1).to_money('CAD')).to eq(Money.new(1, 'CAD'))
|
48
33
|
end
|
49
34
|
|
50
|
-
it "#to_money doesn't overwrite the money object's currency" do
|
51
|
-
|
52
|
-
|
35
|
+
it "legacy_deprecations #to_money doesn't overwrite the money object's currency" do
|
36
|
+
configure(legacy_deprecations: true) do
|
37
|
+
expect(Money).to receive(:deprecate).once
|
38
|
+
expect(Money.new(1, 'USD').to_money('CAD')).to eq(Money.new(1, 'USD'))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "#to_money raises when changing currency" do
|
43
|
+
expect{ Money.new(1, 'USD').to_money('CAD') }.to raise_error(Money::IncompatibleCurrencyError)
|
53
44
|
end
|
54
45
|
|
55
46
|
it "defaults to 0 when constructed with no arguments" do
|
@@ -60,9 +51,15 @@ RSpec.describe "Money" do
|
|
60
51
|
expect(Money.new('')).to eq(Money.new(0))
|
61
52
|
end
|
62
53
|
|
63
|
-
it "defaults to 0 when constructed with an invalid string" do
|
64
|
-
|
65
|
-
|
54
|
+
it "legacy_deprecations defaults to 0 when constructed with an invalid string" do
|
55
|
+
configure(legacy_deprecations: true) do
|
56
|
+
expect(Money).to receive(:deprecate).once
|
57
|
+
expect(Money.new('invalid', 'USD')).to eq(Money.new(0.00, 'USD'))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "raises when constructed with an invalid string" do
|
62
|
+
expect{ Money.new('invalid') }.to raise_error(ArgumentError)
|
66
63
|
end
|
67
64
|
|
68
65
|
it "to_s correctly displays the right number of decimal places" do
|
@@ -107,8 +104,19 @@ RSpec.describe "Money" do
|
|
107
104
|
expect{ money.to_s(:some_weird_style) }.to raise_error(ArgumentError)
|
108
105
|
end
|
109
106
|
|
110
|
-
it "
|
111
|
-
|
107
|
+
it "legacy_json_format makes as_json return the legacy format" do
|
108
|
+
configure(legacy_json_format: true) do
|
109
|
+
expect(Money.new(1, 'CAD').as_json).to eq("1.00")
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
it "legacy_format correctly sets the json format" do
|
114
|
+
expect(Money.new(1, 'CAD').as_json(legacy_format: true)).to eq("1.00")
|
115
|
+
expect(Money.new(1, 'CAD').to_json(legacy_format: true)).to eq("1.00")
|
116
|
+
end
|
117
|
+
|
118
|
+
it "as_json as a json containing the value and currency" do
|
119
|
+
expect(money.as_json).to eq(value: "1.00", currency: "CAD")
|
112
120
|
end
|
113
121
|
|
114
122
|
it "is constructable with a BigDecimal" do
|
@@ -147,9 +155,15 @@ RSpec.describe "Money" do
|
|
147
155
|
expect((Money.new + Money.new)).to eq(Money.new)
|
148
156
|
end
|
149
157
|
|
150
|
-
it "adds inconsistent currencies" do
|
151
|
-
|
152
|
-
|
158
|
+
it "legacy_deprecations adds inconsistent currencies" do
|
159
|
+
configure(legacy_deprecations: true) do
|
160
|
+
expect(Money).to receive(:deprecate).once
|
161
|
+
expect(Money.new(5, 'USD') + Money.new(1, 'CAD')).to eq(Money.new(6, 'USD'))
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
it "raises when adding inconsistent currencies" do
|
166
|
+
expect{ Money.new(5, 'USD') + Money.new(1, 'CAD') }.to raise_error(Money::IncompatibleCurrencyError)
|
153
167
|
end
|
154
168
|
|
155
169
|
it "is subtractable" do
|
@@ -165,8 +179,10 @@ RSpec.describe "Money" do
|
|
165
179
|
end
|
166
180
|
|
167
181
|
it "logs a deprecation warning when adding across currencies" do
|
168
|
-
|
169
|
-
|
182
|
+
configure(legacy_deprecations: true) do
|
183
|
+
expect(Money).to receive(:deprecate)
|
184
|
+
expect(Money.new(10, 'USD') - Money.new(1, 'JPY')).to eq(Money.new(9, 'USD'))
|
185
|
+
end
|
170
186
|
end
|
171
187
|
|
172
188
|
it "keeps currency when doing a computation with a null currency" do
|
@@ -247,11 +263,6 @@ RSpec.describe "Money" do
|
|
247
263
|
expect((1.50 * Money.new(1.00))).to eq(Money.new(1.50))
|
248
264
|
end
|
249
265
|
|
250
|
-
it "is multipliable by a cents amount" do
|
251
|
-
expect((Money.new(1.00) * 0.50)).to eq(Money.new(0.50))
|
252
|
-
expect((0.50 * Money.new(1.00))).to eq(Money.new(0.50))
|
253
|
-
end
|
254
|
-
|
255
266
|
it "is multipliable by a rational" do
|
256
267
|
expect((Money.new(3.3) * Rational(1, 12))).to eq(Money.new(0.28))
|
257
268
|
expect((Rational(1, 12) * Money.new(3.3))).to eq(Money.new(0.28))
|
@@ -267,9 +278,15 @@ RSpec.describe "Money" do
|
|
267
278
|
expect(((1.0 / 12) * Money.new(3.3))).to eq(Money.new(0.28))
|
268
279
|
end
|
269
280
|
|
270
|
-
it "is multipliable by a money object" do
|
271
|
-
|
272
|
-
|
281
|
+
it "legacy_deprecations is multipliable by a money object" do
|
282
|
+
configure(legacy_deprecations: true) do
|
283
|
+
expect(Money).to receive(:deprecate).once
|
284
|
+
expect((Money.new(3.3, 'USD') * Money.new(1, 'USD'))).to eq(Money.new(3.3, 'USD'))
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
it "raises when multiplied by a money object" do
|
289
|
+
expect{ (Money.new(3.3) * Money.new(1)) }.to raise_error(ArgumentError)
|
273
290
|
end
|
274
291
|
|
275
292
|
it "rounds multiplication result with fractional penny of 5 or higher up" do
|
@@ -306,12 +323,14 @@ RSpec.describe "Money" do
|
|
306
323
|
expect { Money.new(55.00) / 55 }.to raise_error(RuntimeError)
|
307
324
|
end
|
308
325
|
|
309
|
-
it "returns cents in
|
310
|
-
|
326
|
+
it "returns cents in to_json" do
|
327
|
+
configure(legacy_json_format: true) do
|
328
|
+
expect(Money.new('1.23', 'USD').to_json).to eq('1.23')
|
329
|
+
end
|
311
330
|
end
|
312
331
|
|
313
|
-
it "returns
|
314
|
-
expect(Money.new(1.00).to_json).to eq("1.00")
|
332
|
+
it "returns value and currency in to_json" do
|
333
|
+
expect(Money.new(1.00).to_json).to eq('{"value":"1.00","currency":"CAD"}')
|
315
334
|
end
|
316
335
|
|
317
336
|
it "supports absolute value" do
|
@@ -330,24 +349,28 @@ RSpec.describe "Money" do
|
|
330
349
|
expect(Money.new(1.50).to_f.to_s).to eq("1.5")
|
331
350
|
end
|
332
351
|
|
333
|
-
|
334
|
-
|
335
|
-
|
352
|
+
describe '#from_subunits' do
|
353
|
+
it "creates Money object from an integer value in cents and currency" do
|
354
|
+
expect(Money.from_subunits(1950, 'CAD')).to eq(Money.new(19.50))
|
355
|
+
end
|
336
356
|
|
337
|
-
|
338
|
-
|
339
|
-
|
357
|
+
it "creates Money object from an integer value in dollars and currency with no cents" do
|
358
|
+
expect(Money.from_subunits(1950, 'JPY')).to eq(Money.new(1950, 'JPY'))
|
359
|
+
end
|
340
360
|
|
341
|
-
|
342
|
-
|
343
|
-
|
361
|
+
describe 'with format specified' do
|
362
|
+
it 'overrides the subunit_to_unit amount' do
|
363
|
+
expect(Money.from_subunits(100, 'ISK', format: :stripe)).to eq(Money.new(1, 'ISK'))
|
364
|
+
end
|
344
365
|
|
345
|
-
|
346
|
-
|
347
|
-
|
366
|
+
it 'fallbacks to the default subunit_to_unit amount if no override is specified' do
|
367
|
+
expect(Money.from_subunits(100, 'USD', format: :stripe)).to eq(Money.new(1, 'USD'))
|
368
|
+
end
|
348
369
|
|
349
|
-
|
350
|
-
|
370
|
+
it 'raises if the format is not found' do
|
371
|
+
expect { Money.from_subunits(100, 'ISK', format: :unknown) }.to(raise_error(ArgumentError))
|
372
|
+
end
|
373
|
+
end
|
351
374
|
end
|
352
375
|
|
353
376
|
it "raises when constructed with a NaN value" do
|
@@ -365,9 +388,12 @@ RSpec.describe "Money" do
|
|
365
388
|
end
|
366
389
|
|
367
390
|
it "generates a true rational" do
|
368
|
-
expect(Money.rational(Money.new(10.0), Money.new(15.0))).to eq(Rational(2,3))
|
369
|
-
|
370
|
-
|
391
|
+
expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'USD'))).to eq(Rational(2,3))
|
392
|
+
|
393
|
+
configure(legacy_deprecations: true) do
|
394
|
+
expect(Money).to receive(:deprecate).once
|
395
|
+
expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'JPY'))).to eq(Rational(2,3))
|
396
|
+
end
|
371
397
|
end
|
372
398
|
|
373
399
|
describe "frozen with amount of $1" do
|
@@ -426,9 +452,11 @@ RSpec.describe "Money" do
|
|
426
452
|
end
|
427
453
|
|
428
454
|
it "<=> issues deprecation warning when comparing incompatible currency" do
|
429
|
-
|
430
|
-
|
431
|
-
|
455
|
+
configure(legacy_deprecations: true) do
|
456
|
+
expect(Money).to receive(:deprecate).twice
|
457
|
+
expect(Money.new(1000, 'USD') <=> Money.new(2000, 'JPY')).to eq(-1)
|
458
|
+
expect(Money.new(2000, 'JPY') <=> Money.new(1000, 'USD')).to eq(1)
|
459
|
+
end
|
432
460
|
end
|
433
461
|
|
434
462
|
describe('same values') do
|
@@ -450,15 +478,22 @@ RSpec.describe "Money" do
|
|
450
478
|
it { expect(cad_10 < nil_10).to(eq(false)) }
|
451
479
|
end
|
452
480
|
|
453
|
-
describe('different currencies') do
|
481
|
+
describe('legacy different currencies') do
|
482
|
+
around(:each) do |test|
|
483
|
+
configure(legacy_deprecations: true) { test.run }
|
484
|
+
end
|
485
|
+
|
454
486
|
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <=> usd_10).to(eq(0)) }
|
455
487
|
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 > usd_10).to(eq(false)) }
|
456
488
|
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 >= usd_10).to(eq(true)) }
|
457
|
-
it { expect(cad_10 == usd_10).to(eq(false)) }
|
458
489
|
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <= usd_10).to(eq(true)) }
|
459
490
|
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 < usd_10).to(eq(false)) }
|
460
491
|
end
|
461
492
|
|
493
|
+
describe('different currencies') do
|
494
|
+
it { expect(cad_10 == usd_10).to(eq(false)) }
|
495
|
+
end
|
496
|
+
|
462
497
|
describe('to_money types') do
|
463
498
|
it { expect(cad_10 <=> 10.00).to(eq(0)) }
|
464
499
|
it { expect(cad_10 > 10.00).to(eq(false)) }
|
@@ -544,6 +579,20 @@ RSpec.describe "Money" do
|
|
544
579
|
expect(Money.new(1, 'IQD').subunits).to eq(1000)
|
545
580
|
expect(Money.new(1).subunits).to eq(100)
|
546
581
|
end
|
582
|
+
|
583
|
+
describe 'with format specified' do
|
584
|
+
it 'overrides the subunit_to_unit amount' do
|
585
|
+
expect(Money.new(1, 'ISK').subunits(format: :stripe)).to eq(100)
|
586
|
+
end
|
587
|
+
|
588
|
+
it 'fallbacks to the default subunit_to_unit amount if no override is specified' do
|
589
|
+
expect(Money.new(1, 'USD').subunits(format: :stripe)).to eq(100)
|
590
|
+
end
|
591
|
+
|
592
|
+
it 'raises if the format is not found' do
|
593
|
+
expect { Money.new(1, 'ISK').subunits(format: :unknown) }.to(raise_error(ArgumentError))
|
594
|
+
end
|
595
|
+
end
|
547
596
|
end
|
548
597
|
|
549
598
|
describe "value" do
|
@@ -589,10 +638,6 @@ RSpec.describe "Money" do
|
|
589
638
|
expect(money.value).to eq(BigDecimal("1.00"))
|
590
639
|
end
|
591
640
|
|
592
|
-
it "returns cents as 100 cents" do
|
593
|
-
expect(money.cents).to eq(100)
|
594
|
-
end
|
595
|
-
|
596
641
|
it "returns cents as 100 cents" do
|
597
642
|
expect(money.subunits).to eq(100)
|
598
643
|
end
|
@@ -713,8 +758,7 @@ RSpec.describe "Money" do
|
|
713
758
|
end
|
714
759
|
|
715
760
|
describe "parser dependency injection" do
|
716
|
-
|
717
|
-
after(:each) { Money.parser = MoneyParser }
|
761
|
+
around(:each) { |test| configure(parser: AccountingMoneyParser, default_currency: 'CAD') { test.run }}
|
718
762
|
|
719
763
|
it "keeps AccountingMoneyParser class on new money objects" do
|
720
764
|
expect(Money.new.class.parser).to eq(AccountingMoneyParser)
|
@@ -753,20 +797,6 @@ RSpec.describe "Money" do
|
|
753
797
|
end
|
754
798
|
|
755
799
|
describe "from_amount quacks like RubyMoney" do
|
756
|
-
it "accepts numeric values" do
|
757
|
-
expect(Money.from_amount(1)).to eq Money.from_cents(1_00)
|
758
|
-
expect(Money.from_amount(1.0)).to eq Money.from_cents(1_00)
|
759
|
-
expect(Money.from_amount(BigDecimal("1"))).to eq Money.from_cents(1_00)
|
760
|
-
end
|
761
|
-
|
762
|
-
it "accepts string values" do
|
763
|
-
expect(Money.from_amount("1")).to eq Money.from_cents(1_00)
|
764
|
-
end
|
765
|
-
|
766
|
-
it "accepts nil values" do
|
767
|
-
expect(Money.from_amount(nil)).to eq Money.from_cents(0)
|
768
|
-
end
|
769
|
-
|
770
800
|
it "accepts an optional currency parameter" do
|
771
801
|
expect { Money.from_amount(1, "CAD") }.to_not raise_error
|
772
802
|
end
|
@@ -786,10 +816,14 @@ RSpec.describe "Money" do
|
|
786
816
|
money = YAML.dump(Money.new(750, 'usd'))
|
787
817
|
expect(money).to eq("--- !ruby/object:Money\nvalue: '750.0'\ncurrency: USD\n")
|
788
818
|
end
|
819
|
+
|
820
|
+
it "does not change BigDecimal value to Integer while rounding for currencies without subunits" do
|
821
|
+
money = Money.new(100, 'JPY').to_yaml
|
822
|
+
expect(money).to eq("--- !ruby/object:Money\nvalue: '100.0'\ncurrency: JPY\n")
|
823
|
+
end
|
789
824
|
end
|
790
825
|
|
791
826
|
describe "YAML deserialization" do
|
792
|
-
|
793
827
|
it "accepts values with currencies" do
|
794
828
|
money = YAML.load("--- !ruby/object:Money\nvalue: '750.0'\ncurrency: USD\n")
|
795
829
|
expect(money).to eq(Money.new(750, 'usd'))
|
@@ -888,8 +922,7 @@ RSpec.describe "Money" do
|
|
888
922
|
end
|
889
923
|
|
890
924
|
context "with .default_currency set" do
|
891
|
-
|
892
|
-
after(:each) { Money.default_currency = Money::NULL_CURRENCY }
|
925
|
+
around(:each) { |test| configure(default_currency: Money::Currency.new('EUR')) { test.run }}
|
893
926
|
|
894
927
|
it "can be nested and falls back to default_currency outside of the blocks" do
|
895
928
|
money2, money3 = nil
|
@@ -8,12 +8,16 @@ RSpec.describe RuboCop::Cop::Money::MissingCurrency do
|
|
8
8
|
|
9
9
|
let(:config) { RuboCop::Config.new }
|
10
10
|
|
11
|
-
|
12
|
-
it 'registers an offense for Money.new without a currency argument' do
|
11
|
+
context 'with default configuration' do
|
12
|
+
it 'registers an offense and corrects for Money.new without a currency argument' do
|
13
13
|
expect_offense(<<~RUBY)
|
14
14
|
Money.new(1)
|
15
15
|
^^^^^^^^^^^^ Money is missing currency argument
|
16
16
|
RUBY
|
17
|
+
|
18
|
+
expect_correction(<<~RUBY)
|
19
|
+
Money.new(1, Money::NULL_CURRENCY)
|
20
|
+
RUBY
|
17
21
|
end
|
18
22
|
|
19
23
|
it 'does not register an offense for Money.new with currency argument' do
|
@@ -22,18 +26,26 @@ RSpec.describe RuboCop::Cop::Money::MissingCurrency do
|
|
22
26
|
RUBY
|
23
27
|
end
|
24
28
|
|
25
|
-
it 'registers an offense for Money.new without a currency argument' do
|
29
|
+
it 'registers an offense and corrects for Money.new without a currency argument' do
|
26
30
|
expect_offense(<<~RUBY)
|
27
31
|
Money.new
|
28
32
|
^^^^^^^^^ Money is missing currency argument
|
29
33
|
RUBY
|
34
|
+
|
35
|
+
expect_correction(<<~RUBY)
|
36
|
+
Money.new(0, Money::NULL_CURRENCY)
|
37
|
+
RUBY
|
30
38
|
end
|
31
39
|
|
32
|
-
it 'registers an offense for Money.from_amount without a currency argument' do
|
40
|
+
it 'registers an offense and corrects for Money.from_amount without a currency argument' do
|
33
41
|
expect_offense(<<~RUBY)
|
34
42
|
Money.from_amount(1)
|
35
43
|
^^^^^^^^^^^^^^^^^^^^ Money is missing currency argument
|
36
44
|
RUBY
|
45
|
+
|
46
|
+
expect_correction(<<~RUBY)
|
47
|
+
Money.from_amount(1, Money::NULL_CURRENCY)
|
48
|
+
RUBY
|
37
49
|
end
|
38
50
|
|
39
51
|
it 'does not register an offense for Money.from_amount with currency argument' do
|
@@ -42,11 +54,15 @@ RSpec.describe RuboCop::Cop::Money::MissingCurrency do
|
|
42
54
|
RUBY
|
43
55
|
end
|
44
56
|
|
45
|
-
it 'registers an offense for Money.from_cents without a currency argument' do
|
57
|
+
it 'registers an offense and corrects for Money.from_cents without a currency argument' do
|
46
58
|
expect_offense(<<~RUBY)
|
47
59
|
Money.from_cents(1)
|
48
60
|
^^^^^^^^^^^^^^^^^^^ Money is missing currency argument
|
49
61
|
RUBY
|
62
|
+
|
63
|
+
expect_correction(<<~RUBY)
|
64
|
+
Money.from_cents(1, Money::NULL_CURRENCY)
|
65
|
+
RUBY
|
50
66
|
end
|
51
67
|
|
52
68
|
it 'does not register an offense for Money.from_cents with currency argument' do
|
@@ -55,11 +71,26 @@ RSpec.describe RuboCop::Cop::Money::MissingCurrency do
|
|
55
71
|
RUBY
|
56
72
|
end
|
57
73
|
|
58
|
-
it 'registers an offense for to_money without a currency argument' do
|
74
|
+
it 'registers an offense and corrects for to_money without a currency argument' do
|
59
75
|
expect_offense(<<~RUBY)
|
60
76
|
'1'.to_money
|
61
77
|
^^^^^^^^^^^^ to_money is missing currency argument
|
62
78
|
RUBY
|
79
|
+
|
80
|
+
expect_correction(<<~RUBY)
|
81
|
+
'1'.to_money(Money::NULL_CURRENCY)
|
82
|
+
RUBY
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'registers an offense and corrects for safe navigation to_money without a currency argument' do
|
86
|
+
expect_offense(<<~RUBY)
|
87
|
+
item&.to_money
|
88
|
+
^^^^^^^^^^^^^^ to_money is missing currency argument
|
89
|
+
RUBY
|
90
|
+
|
91
|
+
expect_correction(<<~RUBY)
|
92
|
+
item&.to_money(Money::NULL_CURRENCY)
|
93
|
+
RUBY
|
63
94
|
end
|
64
95
|
|
65
96
|
it 'does not register an offense for to_money with currency argument' do
|
@@ -68,15 +99,19 @@ RSpec.describe RuboCop::Cop::Money::MissingCurrency do
|
|
68
99
|
RUBY
|
69
100
|
end
|
70
101
|
|
71
|
-
it 'registers an offense for to_money block pass form' do
|
102
|
+
it 'registers an offense and corrects for to_money block pass form' do
|
72
103
|
expect_offense(<<~RUBY)
|
73
104
|
['1'].map(&:to_money)
|
74
105
|
^^^^^^^^^^^^^^^^^^^^^ to_money is missing currency argument
|
75
106
|
RUBY
|
107
|
+
|
108
|
+
expect_correction(<<~RUBY)
|
109
|
+
['1'].map { |x| x.to_money(Money::NULL_CURRENCY) }
|
110
|
+
RUBY
|
76
111
|
end
|
77
112
|
end
|
78
113
|
|
79
|
-
|
114
|
+
context 'with ReplacementCurrency configuration' do
|
80
115
|
let(:config) do
|
81
116
|
RuboCop::Config.new(
|
82
117
|
'Money/MissingCurrency' => {
|