money 6.1.1 → 6.2.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.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/AUTHORS +6 -0
- data/CHANGELOG.md +16 -0
- data/README.md +30 -1
- data/config/currency_iso.json +6 -6
- data/lib/money/currency.rb +16 -5
- data/lib/money/money/arithmetic.rb +16 -10
- data/lib/money/money/formatting.rb +19 -6
- data/lib/money/money.rb +18 -13
- data/lib/money/version.rb +1 -1
- data/money.gemspec +2 -2
- data/spec/bank/base_spec.rb +11 -11
- data/spec/bank/variable_exchange_spec.rb +37 -37
- data/spec/currency/heuristics_spec.rb +25 -25
- data/spec/currency_spec.rb +56 -57
- data/spec/money/arithmetic_spec.rb +107 -99
- data/spec/money/formatting_spec.rb +230 -181
- data/spec/money_spec.rb +161 -128
- metadata +12 -6
data/spec/money_spec.rb
CHANGED
@@ -7,7 +7,9 @@ describe Money do
|
|
7
7
|
let(:initializing_value) { 1 }
|
8
8
|
subject(:money) { Money.new(initializing_value) }
|
9
9
|
|
10
|
-
|
10
|
+
it "should be an instance of `Money::Bank::VariableExchange`" do
|
11
|
+
expect(money.bank).to be Money::Bank::VariableExchange.instance
|
12
|
+
end
|
11
13
|
|
12
14
|
context 'given the initializing value is an integer' do
|
13
15
|
let(:initializing_value) { Integer(1) }
|
@@ -19,38 +21,41 @@ describe Money do
|
|
19
21
|
context 'given the initializing value is a float' do
|
20
22
|
context 'and the value is 1.00' do
|
21
23
|
let(:initializing_value) { 1.00 }
|
22
|
-
it {
|
24
|
+
it { is_expected.to eq Money.new(1) }
|
23
25
|
end
|
24
26
|
|
25
27
|
context 'and the value is 1.01' do
|
26
28
|
let(:initializing_value) { 1.01 }
|
27
|
-
it {
|
29
|
+
it { is_expected.to eq Money.new(1) }
|
28
30
|
end
|
29
31
|
|
30
32
|
context 'and the value is 1.50' do
|
31
33
|
let(:initializing_value) { 1.50 }
|
32
|
-
it {
|
34
|
+
it { is_expected.to eq Money.new(2) }
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
38
|
context 'given the initializing value is a rational' do
|
37
39
|
let(:initializing_value) { Rational(1) }
|
38
|
-
it {
|
40
|
+
it { is_expected.to eq Money.new(1) }
|
39
41
|
end
|
40
42
|
|
41
43
|
context 'given the initializing value is money' do
|
42
44
|
let(:initializing_value) { Money.new(1_00, Money::Currency.new('NZD')) }
|
43
|
-
it {
|
45
|
+
it { is_expected.to eq initializing_value }
|
44
46
|
end
|
45
47
|
|
46
48
|
context "given the initializing value doesn't respond to .to_d" do
|
47
49
|
let(:initializing_value) { :"1" }
|
48
|
-
it {
|
50
|
+
it { is_expected.to eq Money.new(1) }
|
49
51
|
end
|
50
52
|
|
51
53
|
context 'given a currency is not provided' do
|
52
54
|
subject(:money) { Money.new(initializing_value) }
|
53
|
-
|
55
|
+
|
56
|
+
it "should have the default currency" do
|
57
|
+
expect(money.currency).to eq Money.default_currency
|
58
|
+
end
|
54
59
|
end
|
55
60
|
|
56
61
|
context 'given a currency is provided' do
|
@@ -58,53 +63,71 @@ describe Money do
|
|
58
63
|
|
59
64
|
context 'and the currency is NZD' do
|
60
65
|
let(:currency) { Money::Currency.new('NZD') }
|
61
|
-
|
66
|
+
|
67
|
+
it "should have NZD currency" do
|
68
|
+
expect(money.currency).to eq Money::Currency.new('NZD')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'and the currency is nil' do
|
73
|
+
let(:currency) { nil }
|
74
|
+
|
75
|
+
it "should have the default currency" do
|
76
|
+
expect(money.currency).to eq Money.default_currency
|
77
|
+
end
|
62
78
|
end
|
63
79
|
end
|
64
80
|
|
65
81
|
context "infinite_precision = true" do
|
66
|
-
before { Money.
|
82
|
+
before { expect(Money).to receive(:infinite_precision).and_return(true) }
|
67
83
|
context 'given the initializing value is 1.50' do
|
68
84
|
let(:initializing_value) { 1.50 }
|
69
|
-
|
85
|
+
|
86
|
+
it "should have the correct cents" do
|
87
|
+
expect(money.cents).to eq BigDecimal('1.50')
|
88
|
+
end
|
70
89
|
end
|
71
90
|
end
|
72
91
|
end
|
73
92
|
|
74
93
|
describe ".empty" do
|
75
94
|
it "creates a new Money object of 0 cents" do
|
76
|
-
Money.empty.
|
95
|
+
expect(Money.empty).to eq Money.new(0)
|
77
96
|
end
|
78
97
|
|
79
98
|
it "memoizes the result" do
|
80
|
-
Money.empty.object_id.
|
99
|
+
expect(Money.empty.object_id).to eq Money.empty.object_id
|
81
100
|
end
|
82
101
|
|
83
102
|
it "memoizes a result for each currency" do
|
84
|
-
Money.empty(:cad).object_id.
|
103
|
+
expect(Money.empty(:cad).object_id).to eq Money.empty(:cad).object_id
|
104
|
+
end
|
105
|
+
|
106
|
+
it "doesn't allow money to be modified for a currency" do
|
107
|
+
expect(Money.empty).to be_frozen
|
85
108
|
end
|
86
109
|
end
|
87
110
|
|
88
111
|
describe ".zero" do
|
89
112
|
subject { Money.zero }
|
90
|
-
it {
|
113
|
+
it { is_expected.to eq Money.empty }
|
91
114
|
end
|
92
115
|
|
93
116
|
describe ".ca_dollar" do
|
94
117
|
it "creates a new Money object of the given value in CAD" do
|
95
|
-
Money.ca_dollar(50).
|
118
|
+
expect(Money.ca_dollar(50)).to eq Money.new(50, "CAD")
|
96
119
|
end
|
97
120
|
end
|
98
121
|
|
99
122
|
describe ".us_dollar" do
|
100
123
|
it "creates a new Money object of the given value in USD" do
|
101
|
-
Money.us_dollar(50).
|
124
|
+
expect(Money.us_dollar(50)).to eq Money.new(50, "USD")
|
102
125
|
end
|
103
126
|
end
|
104
127
|
|
105
128
|
describe ".euro" do
|
106
129
|
it "creates a new Money object of the given value in EUR" do
|
107
|
-
Money.euro(50).
|
130
|
+
expect(Money.euro(50)).to eq Money.new(50, "EUR")
|
108
131
|
end
|
109
132
|
end
|
110
133
|
|
@@ -120,7 +143,7 @@ describe Money do
|
|
120
143
|
|
121
144
|
it "saves rate into current bank" do
|
122
145
|
Money.add_rate("EUR", "USD", 10)
|
123
|
-
Money.new(10_00, "EUR").exchange_to("USD").
|
146
|
+
expect(Money.new(10_00, "EUR").exchange_to("USD")).to eq Money.new(100_00, "USD")
|
124
147
|
end
|
125
148
|
end
|
126
149
|
|
@@ -145,19 +168,19 @@ describe Money do
|
|
145
168
|
def expectation.fractional
|
146
169
|
"expectation"
|
147
170
|
end
|
148
|
-
expectation.cents.
|
171
|
+
expect(expectation.cents).to eq "expectation"
|
149
172
|
end
|
150
173
|
end
|
151
174
|
|
152
175
|
describe "#fractional" do
|
153
176
|
it "returns the amount in fractional unit" do
|
154
|
-
Money.new(1_00).fractional.
|
177
|
+
expect(Money.new(1_00).fractional).to eq 1_00
|
155
178
|
end
|
156
179
|
|
157
180
|
it "stores fractional as an integer regardless of what is passed into the constructor" do
|
158
181
|
m = Money.new(100)
|
159
|
-
m.fractional.
|
160
|
-
m.fractional.
|
182
|
+
expect(m.fractional).to eq 100
|
183
|
+
expect(m.fractional).to be_a(Fixnum)
|
161
184
|
end
|
162
185
|
|
163
186
|
context "loading a serialized Money via YAML" do
|
@@ -186,10 +209,10 @@ YAML
|
|
186
209
|
|
187
210
|
it "uses BigDecimal when rounding" do
|
188
211
|
m = YAML::load serialized
|
189
|
-
m.
|
190
|
-
m.class.infinite_precision.
|
191
|
-
m.fractional.
|
192
|
-
m.fractional.
|
212
|
+
expect(m).to be_a(Money)
|
213
|
+
expect(m.class.infinite_precision).to be false
|
214
|
+
expect(m.fractional).to eq 250 # 249.5 rounded up
|
215
|
+
expect(m.fractional).to be_a(Integer)
|
193
216
|
end
|
194
217
|
|
195
218
|
context "with infinite_precision" do
|
@@ -203,7 +226,7 @@ YAML
|
|
203
226
|
|
204
227
|
it "is a BigDecimal" do
|
205
228
|
money = YAML::load serialized
|
206
|
-
money.fractional.
|
229
|
+
expect(money.fractional).to be_a BigDecimal
|
207
230
|
end
|
208
231
|
end
|
209
232
|
end
|
@@ -216,36 +239,36 @@ YAML
|
|
216
239
|
context "with the setter" do
|
217
240
|
it "respects the rounding_mode" do
|
218
241
|
Money.rounding_mode = BigDecimal::ROUND_DOWN
|
219
|
-
Money.new(1.9).fractional.
|
242
|
+
expect(Money.new(1.9).fractional).to eq 1
|
220
243
|
|
221
244
|
Money.rounding_mode = BigDecimal::ROUND_UP
|
222
|
-
Money.new(1.1).fractional.
|
245
|
+
expect(Money.new(1.1).fractional).to eq 2
|
223
246
|
end
|
224
247
|
end
|
225
248
|
|
226
249
|
context "with a block" do
|
227
250
|
it "respects the rounding_mode" do
|
228
|
-
Money.rounding_mode(BigDecimal::ROUND_DOWN) do
|
251
|
+
expect(Money.rounding_mode(BigDecimal::ROUND_DOWN) do
|
229
252
|
Money.new(1.9).fractional
|
230
|
-
end.
|
253
|
+
end).to eq 1
|
231
254
|
|
232
|
-
Money.rounding_mode(BigDecimal::ROUND_UP) do
|
255
|
+
expect(Money.rounding_mode(BigDecimal::ROUND_UP) do
|
233
256
|
Money.new(1.1).fractional
|
234
|
-
end.
|
257
|
+
end).to eq 2
|
235
258
|
|
236
|
-
Money.rounding_mode.
|
259
|
+
expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_EVEN
|
237
260
|
end
|
238
261
|
|
239
262
|
it "works for multiplication within a block" do
|
240
263
|
Money.rounding_mode(BigDecimal::ROUND_DOWN) do
|
241
|
-
(Money.new(1_00) * "0.019".to_d).fractional.
|
264
|
+
expect((Money.new(1_00) * "0.019".to_d).fractional).to eq 1
|
242
265
|
end
|
243
266
|
|
244
267
|
Money.rounding_mode(BigDecimal::ROUND_UP) do
|
245
|
-
(Money.new(1_00) * "0.011".to_d).fractional.
|
268
|
+
expect((Money.new(1_00) * "0.011".to_d).fractional).to eq 2
|
246
269
|
end
|
247
270
|
|
248
|
-
Money.rounding_mode.
|
271
|
+
expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_EVEN
|
249
272
|
end
|
250
273
|
end
|
251
274
|
end
|
@@ -260,34 +283,34 @@ YAML
|
|
260
283
|
end
|
261
284
|
|
262
285
|
it "returns the amount in fractional unit" do
|
263
|
-
Money.new(1_00).fractional.
|
286
|
+
expect(Money.new(1_00).fractional).to eq BigDecimal("100")
|
264
287
|
end
|
265
288
|
|
266
289
|
it "stores in fractional unit as an integer regardless of what is passed into the constructor" do
|
267
290
|
m = Money.new(100)
|
268
|
-
m.fractional.
|
269
|
-
m.fractional.
|
291
|
+
expect(m.fractional).to eq BigDecimal("100")
|
292
|
+
expect(m.fractional).to be_a(BigDecimal)
|
270
293
|
end
|
271
294
|
end
|
272
295
|
end
|
273
296
|
|
274
297
|
describe "#amount" do
|
275
298
|
it "returns the amount of cents as dollars" do
|
276
|
-
Money.new(1_00).amount.
|
299
|
+
expect(Money.new(1_00).amount).to eq 1
|
277
300
|
end
|
278
301
|
|
279
302
|
it "respects :subunit_to_unit currency property" do
|
280
|
-
Money.new(1_00, "USD").amount.
|
281
|
-
Money.new(1_000, "TND").amount.
|
282
|
-
Money.new(1, "CLP").amount.
|
303
|
+
expect(Money.new(1_00, "USD").amount).to eq 1
|
304
|
+
expect(Money.new(1_000, "TND").amount).to eq 1
|
305
|
+
expect(Money.new(1, "CLP").amount).to eq 1
|
283
306
|
end
|
284
307
|
|
285
308
|
it "does not loose precision" do
|
286
|
-
Money.new(100_37).amount.
|
309
|
+
expect(Money.new(100_37).amount).to eq 100.37
|
287
310
|
end
|
288
311
|
|
289
312
|
it 'produces a BigDecimal' do
|
290
|
-
Money.new(1_00).amount.
|
313
|
+
expect(Money.new(1_00).amount).to be_a BigDecimal
|
291
314
|
end
|
292
315
|
end
|
293
316
|
|
@@ -300,20 +323,20 @@ YAML
|
|
300
323
|
5
|
301
324
|
end
|
302
325
|
|
303
|
-
m.dollars.
|
326
|
+
expect(m.dollars).to eq 5
|
304
327
|
end
|
305
328
|
end
|
306
329
|
|
307
330
|
describe "#currency" do
|
308
331
|
it "returns the currency object" do
|
309
|
-
Money.new(1_00, "USD").currency.
|
332
|
+
expect(Money.new(1_00, "USD").currency).to eq Money::Currency.new("USD")
|
310
333
|
end
|
311
334
|
end
|
312
335
|
|
313
336
|
describe "#currency_as_string" do
|
314
337
|
it "returns the iso_code of the currency object" do
|
315
|
-
Money.new(1_00, "USD").currency_as_string.
|
316
|
-
Money.new(1_00, "EUR").currency_as_string.
|
338
|
+
expect(Money.new(1_00, "USD").currency_as_string).to eq "USD"
|
339
|
+
expect(Money.new(1_00, "EUR").currency_as_string).to eq "EUR"
|
317
340
|
end
|
318
341
|
end
|
319
342
|
|
@@ -321,61 +344,61 @@ YAML
|
|
321
344
|
it "sets the currency object using the provided string" do
|
322
345
|
money = Money.new(100_00, "USD")
|
323
346
|
money.currency_as_string = "EUR"
|
324
|
-
money.currency.
|
347
|
+
expect(money.currency).to eq Money::Currency.new("EUR")
|
325
348
|
money.currency_as_string = "YEN"
|
326
|
-
money.currency.
|
349
|
+
expect(money.currency).to eq Money::Currency.new("YEN")
|
327
350
|
end
|
328
351
|
end
|
329
352
|
|
330
353
|
describe "#hash=" do
|
331
354
|
it "returns the same value for equal objects" do
|
332
|
-
Money.new(1_00, "EUR").hash.
|
333
|
-
Money.new(2_00, "USD").hash.
|
334
|
-
Money.new(1_00, "EUR").hash.
|
335
|
-
Money.new(1_00, "EUR").hash.
|
336
|
-
Money.new(1_00, "EUR").hash.
|
355
|
+
expect(Money.new(1_00, "EUR").hash).to eq Money.new(1_00, "EUR").hash
|
356
|
+
expect(Money.new(2_00, "USD").hash).to eq Money.new(2_00, "USD").hash
|
357
|
+
expect(Money.new(1_00, "EUR").hash).not_to eq Money.new(2_00, "EUR").hash
|
358
|
+
expect(Money.new(1_00, "EUR").hash).not_to eq Money.new(1_00, "USD").hash
|
359
|
+
expect(Money.new(1_00, "EUR").hash).not_to eq Money.new(2_00, "USD").hash
|
337
360
|
end
|
338
361
|
|
339
362
|
it "can be used to return the intersection of Money object arrays" do
|
340
363
|
intersection = [Money.new(1_00, "EUR"), Money.new(1_00, "USD")] & [Money.new(1_00, "EUR")]
|
341
|
-
intersection.
|
364
|
+
expect(intersection).to eq [Money.new(1_00, "EUR")]
|
342
365
|
end
|
343
366
|
end
|
344
367
|
|
345
368
|
describe "#symbol" do
|
346
369
|
it "works as documented" do
|
347
370
|
currency = Money::Currency.new("EUR")
|
348
|
-
currency.
|
349
|
-
Money.new(0, currency).symbol.
|
371
|
+
expect(currency).to receive(:symbol).and_return("€")
|
372
|
+
expect(Money.new(0, currency).symbol).to eq "€"
|
350
373
|
|
351
374
|
currency = Money::Currency.new("EUR")
|
352
|
-
currency.
|
353
|
-
Money.new(0, currency).symbol.
|
375
|
+
expect(currency).to receive(:symbol).and_return(nil)
|
376
|
+
expect(Money.new(0, currency).symbol).to eq "¤"
|
354
377
|
end
|
355
378
|
end
|
356
379
|
|
357
380
|
describe "#to_s" do
|
358
381
|
it "works as documented" do
|
359
|
-
Money.new(10_00).to_s.
|
360
|
-
Money.new(400_08).to_s.
|
361
|
-
Money.new(-237_43).to_s.
|
382
|
+
expect(Money.new(10_00).to_s).to eq "10.00"
|
383
|
+
expect(Money.new(400_08).to_s).to eq "400.08"
|
384
|
+
expect(Money.new(-237_43).to_s).to eq "-237.43"
|
362
385
|
end
|
363
386
|
|
364
387
|
it "respects :subunit_to_unit currency property" do
|
365
|
-
Money.new(10_00, "BHD").to_s.
|
366
|
-
Money.new(10_00, "CNY").to_s.
|
388
|
+
expect(Money.new(10_00, "BHD").to_s).to eq "1.000"
|
389
|
+
expect(Money.new(10_00, "CNY").to_s).to eq "10.00"
|
367
390
|
end
|
368
391
|
|
369
392
|
it "does not have decimal when :subunit_to_unit == 1" do
|
370
|
-
Money.new(10_00, "CLP").to_s.
|
393
|
+
expect(Money.new(10_00, "CLP").to_s).to eq "1000"
|
371
394
|
end
|
372
395
|
|
373
396
|
it "does not work when :subunit_to_unit == 5" do
|
374
|
-
Money.new(10_00, "MGA").to_s.
|
397
|
+
expect(Money.new(10_00, "MGA").to_s).to eq "200.0"
|
375
398
|
end
|
376
399
|
|
377
400
|
it "respects :decimal_mark" do
|
378
|
-
Money.new(10_00, "BRL").to_s.
|
401
|
+
expect(Money.new(10_00, "BRL").to_s).to eq "10,00"
|
379
402
|
end
|
380
403
|
|
381
404
|
context "infinite_precision = true" do
|
@@ -388,15 +411,15 @@ YAML
|
|
388
411
|
end
|
389
412
|
|
390
413
|
it "shows fractional cents" do
|
391
|
-
Money.new(1.05, "USD").to_s.
|
414
|
+
expect(Money.new(1.05, "USD").to_s).to eq "0.0105"
|
392
415
|
end
|
393
416
|
|
394
417
|
it "suppresses fractional cents when there is none" do
|
395
|
-
Money.new(1.0, "USD").to_s.
|
418
|
+
expect(Money.new(1.0, "USD").to_s).to eq "0.01"
|
396
419
|
end
|
397
420
|
|
398
421
|
it "shows fractional if needed when :subunut_to_unit == 1" do
|
399
|
-
Money.new(10_00.1, "CLP").to_s.
|
422
|
+
expect(Money.new(10_00.1, "CLP").to_s).to eq "1000,1"
|
400
423
|
end
|
401
424
|
end
|
402
425
|
end
|
@@ -404,92 +427,102 @@ YAML
|
|
404
427
|
describe "#to_d" do
|
405
428
|
it "works as documented" do
|
406
429
|
decimal = Money.new(10_00).to_d
|
407
|
-
decimal.
|
408
|
-
decimal.
|
430
|
+
expect(decimal).to be_a(BigDecimal)
|
431
|
+
expect(decimal).to eq 10.0
|
409
432
|
end
|
410
433
|
|
411
434
|
it "respects :subunit_to_unit currency property" do
|
412
435
|
decimal = Money.new(10_00, "BHD").to_d
|
413
|
-
decimal.
|
414
|
-
decimal.
|
436
|
+
expect(decimal).to be_a(BigDecimal)
|
437
|
+
expect(decimal).to eq 1.0
|
415
438
|
end
|
416
439
|
|
417
440
|
it "works with float :subunit_to_unit currency property" do
|
418
441
|
money = Money.new(10_00, "BHD")
|
419
|
-
money.currency.
|
442
|
+
allow(money.currency).to receive(:subunit_to_unit).and_return(1000.0)
|
420
443
|
|
421
444
|
decimal = money.to_d
|
422
|
-
decimal.
|
423
|
-
decimal.
|
445
|
+
expect(decimal).to be_a(BigDecimal)
|
446
|
+
expect(decimal).to eq 1.0
|
424
447
|
end
|
425
448
|
end
|
426
449
|
|
427
450
|
describe "#to_f" do
|
428
451
|
it "works as documented" do
|
429
|
-
Money.new(10_00).to_f.
|
452
|
+
expect(Money.new(10_00).to_f).to eq 10.0
|
430
453
|
end
|
431
454
|
|
432
455
|
it "respects :subunit_to_unit currency property" do
|
433
|
-
Money.new(10_00, "BHD").to_f.
|
456
|
+
expect(Money.new(10_00, "BHD").to_f).to eq 1.0
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
describe "#to_i" do
|
461
|
+
it "works as documented" do
|
462
|
+
expect(Money.new(10_00).to_i).to eq 10
|
463
|
+
end
|
464
|
+
|
465
|
+
it "respects :subunit_to_unit currency property" do
|
466
|
+
expect(Money.new(10_00, "BHD").to_i).to eq 1
|
434
467
|
end
|
435
468
|
end
|
436
469
|
|
437
470
|
describe "#to_money" do
|
438
471
|
it "works as documented" do
|
439
472
|
money = Money.new(10_00, "DKK")
|
440
|
-
money.
|
441
|
-
money.
|
442
|
-
money.bank.
|
443
|
-
money.to_money("EUR").
|
473
|
+
expect(money).to eq money.to_money
|
474
|
+
expect(money).to eq money.to_money("DKK")
|
475
|
+
expect(money.bank).to receive(:exchange_with).with(Money.new(10_00, Money::Currency.new("DKK")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
|
476
|
+
expect(money.to_money("EUR")).to eq Money.new(200_00, "EUR")
|
444
477
|
end
|
445
478
|
end
|
446
479
|
|
447
480
|
describe "#exchange_to" do
|
448
481
|
it "exchanges the amount via its exchange bank" do
|
449
482
|
money = Money.new(100_00, "USD")
|
450
|
-
money.bank.
|
483
|
+
expect(money.bank).to receive(:exchange_with).with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
|
451
484
|
money.exchange_to("EUR")
|
452
485
|
end
|
453
486
|
|
454
487
|
it "exchanges the amount properly" do
|
455
488
|
money = Money.new(100_00, "USD")
|
456
|
-
money.bank.
|
457
|
-
money.exchange_to("EUR").
|
489
|
+
expect(money.bank).to receive(:exchange_with).with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
|
490
|
+
expect(money.exchange_to("EUR")).to eq Money.new(200_00, "EUR")
|
458
491
|
end
|
459
492
|
|
460
493
|
it 'uses the block given as rounding method' do
|
461
494
|
money = Money.new(100_00, 'USD')
|
462
|
-
money.bank.
|
495
|
+
expect(money.bank).to receive(:exchange_with).and_yield(300_00)
|
463
496
|
expect { |block| money.exchange_to(Money::Currency.new('EUR'), &block) }.to yield_successive_args(300_00)
|
464
497
|
end
|
465
498
|
|
466
499
|
it "does no exchange when the currencies are the same" do
|
467
500
|
money = Money.new(100_00, "USD")
|
468
|
-
money.bank.
|
469
|
-
money.exchange_to("USD").
|
501
|
+
expect(money.bank).to_not receive(:exchange_with)
|
502
|
+
expect(money.exchange_to("USD")).to eq money
|
470
503
|
end
|
471
504
|
end
|
472
505
|
|
473
506
|
describe "#allocate" do
|
474
507
|
it "takes no action when one gets all" do
|
475
|
-
Money.us_dollar(005).allocate([1.0]).
|
508
|
+
expect(Money.us_dollar(005).allocate([1.0])).to eq [Money.us_dollar(5)]
|
476
509
|
end
|
477
510
|
|
478
511
|
it "keeps currencies intact" do
|
479
|
-
Money.ca_dollar(005).allocate([1]).
|
512
|
+
expect(Money.ca_dollar(005).allocate([1])).to eq [Money.ca_dollar(5)]
|
480
513
|
end
|
481
514
|
|
482
515
|
it "does not loose pennies" do
|
483
516
|
moneys = Money.us_dollar(5).allocate([0.3, 0.7])
|
484
|
-
moneys[0].
|
485
|
-
moneys[1].
|
517
|
+
expect(moneys[0]).to eq Money.us_dollar(2)
|
518
|
+
expect(moneys[1]).to eq Money.us_dollar(3)
|
486
519
|
end
|
487
520
|
|
488
521
|
it "does not loose pennies" do
|
489
522
|
moneys = Money.us_dollar(100).allocate([0.333, 0.333, 0.333])
|
490
|
-
moneys[0].cents.
|
491
|
-
moneys[1].cents.
|
492
|
-
moneys[2].cents.
|
523
|
+
expect(moneys[0].cents).to eq 34
|
524
|
+
expect(moneys[1].cents).to eq 33
|
525
|
+
expect(moneys[2].cents).to eq 33
|
493
526
|
end
|
494
527
|
|
495
528
|
it "requires total to be less then 1" do
|
@@ -509,9 +542,9 @@ YAML
|
|
509
542
|
one_third = BigDecimal("1") / BigDecimal("3")
|
510
543
|
|
511
544
|
moneys = Money.new(100).allocate([one_third, one_third, one_third])
|
512
|
-
moneys[0].cents.
|
513
|
-
moneys[1].cents.
|
514
|
-
moneys[2].cents.
|
545
|
+
expect(moneys[0].cents).to eq one_third * BigDecimal("100")
|
546
|
+
expect(moneys[1].cents).to eq one_third * BigDecimal("100")
|
547
|
+
expect(moneys[2].cents).to eq one_third * BigDecimal("100")
|
515
548
|
end
|
516
549
|
end
|
517
550
|
end
|
@@ -523,22 +556,22 @@ YAML
|
|
523
556
|
end
|
524
557
|
|
525
558
|
it "gives 1 cent to both people if we start with 2" do
|
526
|
-
Money.us_dollar(2).split(2).
|
559
|
+
expect(Money.us_dollar(2).split(2)).to eq [Money.us_dollar(1), Money.us_dollar(1)]
|
527
560
|
end
|
528
561
|
|
529
562
|
it "may distribute no money to some parties if there isnt enough to go around" do
|
530
|
-
Money.us_dollar(2).split(3).
|
563
|
+
expect(Money.us_dollar(2).split(3)).to eq [Money.us_dollar(1), Money.us_dollar(1), Money.us_dollar(0)]
|
531
564
|
end
|
532
565
|
|
533
566
|
it "does not lose pennies" do
|
534
|
-
Money.us_dollar(5).split(2).
|
567
|
+
expect(Money.us_dollar(5).split(2)).to eq [Money.us_dollar(3), Money.us_dollar(2)]
|
535
568
|
end
|
536
569
|
|
537
570
|
it "splits a dollar" do
|
538
571
|
moneys = Money.us_dollar(100).split(3)
|
539
|
-
moneys[0].cents.
|
540
|
-
moneys[1].cents.
|
541
|
-
moneys[2].cents.
|
572
|
+
expect(moneys[0].cents).to eq 34
|
573
|
+
expect(moneys[1].cents).to eq 33
|
574
|
+
expect(moneys[2].cents).to eq 33
|
542
575
|
end
|
543
576
|
|
544
577
|
context "infinite_precision = true" do
|
@@ -554,9 +587,9 @@ YAML
|
|
554
587
|
thirty_three_and_one_third = BigDecimal("100") / BigDecimal("3")
|
555
588
|
|
556
589
|
moneys = Money.new(100).split(3)
|
557
|
-
moneys[0].cents.
|
558
|
-
moneys[1].cents.
|
559
|
-
moneys[2].cents.
|
590
|
+
expect(moneys[0].cents).to eq thirty_three_and_one_third
|
591
|
+
expect(moneys[1].cents).to eq thirty_three_and_one_third
|
592
|
+
expect(moneys[2].cents).to eq thirty_three_and_one_third
|
560
593
|
end
|
561
594
|
end
|
562
595
|
end
|
@@ -573,8 +606,8 @@ YAML
|
|
573
606
|
|
574
607
|
it "returns self (as it is already rounded)" do
|
575
608
|
rounded = money.round
|
576
|
-
rounded.
|
577
|
-
rounded.cents.
|
609
|
+
expect(rounded).to be money
|
610
|
+
expect(rounded.cents).to eq 16
|
578
611
|
end
|
579
612
|
end
|
580
613
|
|
@@ -588,20 +621,20 @@ YAML
|
|
588
621
|
end
|
589
622
|
|
590
623
|
it "returns a different money" do
|
591
|
-
rounded.
|
624
|
+
expect(rounded).not_to be money
|
592
625
|
end
|
593
626
|
|
594
627
|
it "rounds the cents" do
|
595
|
-
rounded.cents.
|
628
|
+
expect(rounded.cents).to eq 16
|
596
629
|
end
|
597
630
|
|
598
631
|
it "maintains the currency" do
|
599
|
-
rounded.currency.
|
632
|
+
expect(rounded.currency).to eq Money::Currency.new('NZD')
|
600
633
|
end
|
601
634
|
|
602
635
|
it "uses a provided rounding strategy" do
|
603
636
|
rounded = money.round(BigDecimal::ROUND_DOWN)
|
604
|
-
rounded.cents.
|
637
|
+
expect(rounded.cents).to eq 15
|
605
638
|
end
|
606
639
|
end
|
607
640
|
end
|
@@ -614,7 +647,7 @@ YAML
|
|
614
647
|
# ./lib/money/money.rb:63:in `fractional'
|
615
648
|
# ./lib/money/money/arithmetic.rb:115:in `-'
|
616
649
|
MoneyChild = Class.new(Money)
|
617
|
-
(MoneyChild.new(1000) - Money.new(500)).
|
650
|
+
expect(MoneyChild.new(1000) - Money.new(500)).to eq Money.new(500)
|
618
651
|
end
|
619
652
|
end
|
620
653
|
|
@@ -632,17 +665,17 @@ YAML
|
|
632
665
|
|
633
666
|
specify "as_us_dollar converts Money object to USD" do
|
634
667
|
obj = Money.new(1, "EUR")
|
635
|
-
obj.as_us_dollar.
|
668
|
+
expect(obj.as_us_dollar).to eq Money.new(1, "USD")
|
636
669
|
end
|
637
670
|
|
638
671
|
specify "as_ca_dollar converts Money object to CAD" do
|
639
672
|
obj = Money.new(1, "EUR")
|
640
|
-
obj.as_ca_dollar.
|
673
|
+
expect(obj.as_ca_dollar).to eq Money.new(1, "CAD")
|
641
674
|
end
|
642
675
|
|
643
676
|
specify "as_euro converts Money object to EUR" do
|
644
677
|
obj = Money.new(1, "USD")
|
645
|
-
obj.as_euro.
|
678
|
+
expect(obj.as_euro).to eq Money.new(1, "EUR")
|
646
679
|
end
|
647
680
|
end
|
648
681
|
|
@@ -651,18 +684,18 @@ YAML
|
|
651
684
|
@default_currency = Money.default_currency
|
652
685
|
end
|
653
686
|
|
687
|
+
after do
|
688
|
+
Money.default_currency = @default_currency
|
689
|
+
end
|
690
|
+
|
654
691
|
it "accepts a lambda" do
|
655
692
|
Money.default_currency = lambda { :eur }
|
656
|
-
Money.default_currency.
|
693
|
+
expect(Money.default_currency).to eq Money::Currency.new(:eur)
|
657
694
|
end
|
658
695
|
|
659
696
|
it "accepts a symbol" do
|
660
697
|
Money.default_currency = :eur
|
661
|
-
Money.default_currency.
|
662
|
-
end
|
663
|
-
|
664
|
-
after do
|
665
|
-
Money.default_currency = @default_currency
|
698
|
+
expect(Money.default_currency).to eq Money::Currency.new(:eur)
|
666
699
|
end
|
667
700
|
end
|
668
701
|
end
|