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
@@ -5,26 +5,26 @@ require "spec_helper"
|
|
5
5
|
describe Money do
|
6
6
|
describe "-@" do
|
7
7
|
it "changes the sign of a number" do
|
8
|
-
(- Money.new(0)).
|
9
|
-
(- Money.new(1)).
|
10
|
-
(- Money.new(-1)).
|
8
|
+
expect((- Money.new(0))).to eq Money.new(0)
|
9
|
+
expect((- Money.new(1))).to eq Money.new(-1)
|
10
|
+
expect((- Money.new(-1))).to eq Money.new(1)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
describe "#==" do
|
15
15
|
it "returns true if and only if their amount and currency are equal" do
|
16
|
-
Money.new(1_00, "USD").
|
17
|
-
Money.new(1_00, "USD").
|
18
|
-
Money.new(1_00, "USD").
|
19
|
-
Money.new(1_00, "USD").
|
16
|
+
expect(Money.new(1_00, "USD")).to eq Money.new(1_00, "USD")
|
17
|
+
expect(Money.new(1_00, "USD")).not_to eq Money.new(1_00, "EUR")
|
18
|
+
expect(Money.new(1_00, "USD")).not_to eq Money.new(2_00, "USD")
|
19
|
+
expect(Money.new(1_00, "USD")).not_to eq Money.new(99_00, "EUR")
|
20
20
|
end
|
21
21
|
|
22
22
|
it "returns false if used to compare with an object that doesn't respond to #to_money" do
|
23
|
-
Money.new(1_00, "USD").
|
24
|
-
Money.new(1_00, "USD").
|
25
|
-
Money.new(1_00, "USD").
|
26
|
-
Money.new(1_00, "USD").
|
27
|
-
Money.new(1_00, "USD").
|
23
|
+
expect(Money.new(1_00, "USD")).not_to eq Object.new
|
24
|
+
expect(Money.new(1_00, "USD")).not_to eq Class
|
25
|
+
expect(Money.new(1_00, "USD")).not_to eq Kernel
|
26
|
+
expect(Money.new(1_00, "USD")).not_to eq /foo/
|
27
|
+
expect(Money.new(1_00, "USD")).not_to eq nil
|
28
28
|
end
|
29
29
|
|
30
30
|
it "can be used to compare with an object that responds to #to_money" do
|
@@ -38,27 +38,27 @@ describe Money do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
Money.new(1_00, "USD").
|
42
|
-
Money.new(2_50, "USD").
|
43
|
-
Money.new(2_50, "USD").
|
44
|
-
Money.new(1_00, "GBP").
|
41
|
+
expect(Money.new(1_00, "USD")).to eq klass.new(Money.new(1_00, "USD"))
|
42
|
+
expect(Money.new(2_50, "USD")).to eq klass.new(Money.new(2_50, "USD"))
|
43
|
+
expect(Money.new(2_50, "USD")).not_to eq klass.new(Money.new(3_00, "USD"))
|
44
|
+
expect(Money.new(1_00, "GBP")).not_to eq klass.new(Money.new(1_00, "USD"))
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "#eql?" do
|
49
49
|
it "returns true if and only if their amount and currency are equal" do
|
50
|
-
Money.new(1_00, "USD").eql?(Money.new(1_00, "USD")).
|
51
|
-
Money.new(1_00, "USD").eql?(Money.new(1_00, "EUR")).
|
52
|
-
Money.new(1_00, "USD").eql?(Money.new(2_00, "USD")).
|
53
|
-
Money.new(1_00, "USD").eql?(Money.new(99_00, "EUR")).
|
50
|
+
expect(Money.new(1_00, "USD").eql?(Money.new(1_00, "USD"))).to be true
|
51
|
+
expect(Money.new(1_00, "USD").eql?(Money.new(1_00, "EUR"))).to be false
|
52
|
+
expect(Money.new(1_00, "USD").eql?(Money.new(2_00, "USD"))).to be false
|
53
|
+
expect(Money.new(1_00, "USD").eql?(Money.new(99_00, "EUR"))).to be false
|
54
54
|
end
|
55
55
|
|
56
56
|
it "returns false if used to compare with an object that doesn't respond to #to_money" do
|
57
|
-
Money.new(1_00, "USD").eql?(Object.new).
|
58
|
-
Money.new(1_00, "USD").eql?(Class).
|
59
|
-
Money.new(1_00, "USD").eql?(Kernel).
|
60
|
-
Money.new(1_00, "USD").eql?(/foo/).
|
61
|
-
Money.new(1_00, "USD").eql?(nil).
|
57
|
+
expect(Money.new(1_00, "USD").eql?(Object.new)).to be false
|
58
|
+
expect(Money.new(1_00, "USD").eql?(Class)).to be false
|
59
|
+
expect(Money.new(1_00, "USD").eql?(Kernel)).to be false
|
60
|
+
expect(Money.new(1_00, "USD").eql?(/foo/)).to be false
|
61
|
+
expect(Money.new(1_00, "USD").eql?(nil)).to be false
|
62
62
|
end
|
63
63
|
|
64
64
|
it "can be used to compare with an object that responds to #to_money" do
|
@@ -72,32 +72,32 @@ describe Money do
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
-
Money.new(1_00, "USD").eql?(klass.new(Money.new(1_00, "USD"))).
|
76
|
-
Money.new(2_50, "USD").eql?(klass.new(Money.new(2_50, "USD"))).
|
77
|
-
Money.new(2_50, "USD").eql?(klass.new(Money.new(3_00, "USD"))).
|
78
|
-
Money.new(1_00, "GBP").eql?(klass.new(Money.new(1_00, "USD"))).
|
75
|
+
expect(Money.new(1_00, "USD").eql?(klass.new(Money.new(1_00, "USD")))).to be true
|
76
|
+
expect(Money.new(2_50, "USD").eql?(klass.new(Money.new(2_50, "USD")))).to be true
|
77
|
+
expect(Money.new(2_50, "USD").eql?(klass.new(Money.new(3_00, "USD")))).to be false
|
78
|
+
expect(Money.new(1_00, "GBP").eql?(klass.new(Money.new(1_00, "USD")))).to be false
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
82
|
describe "#<=>" do
|
83
83
|
it "compares the two object amounts (same currency)" do
|
84
|
-
(Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).
|
85
|
-
(Money.new(1_00, "USD") <=> Money.new(99, "USD")).
|
86
|
-
(Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).
|
84
|
+
expect((Money.new(1_00, "USD") <=> Money.new(1_00, "USD"))).to eq 0
|
85
|
+
expect((Money.new(1_00, "USD") <=> Money.new(99, "USD"))).to be > 0
|
86
|
+
expect((Money.new(1_00, "USD") <=> Money.new(2_00, "USD"))).to be < 0
|
87
87
|
end
|
88
88
|
|
89
89
|
it "converts other object amount to current currency, then compares the two object amounts (different currency)" do
|
90
90
|
target = Money.new(200_00, "EUR")
|
91
|
-
target.
|
92
|
-
(Money.new(100_00, "USD") <=> target).
|
91
|
+
expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(300_00, "USD"))
|
92
|
+
expect(Money.new(100_00, "USD") <=> target).to be < 0
|
93
93
|
|
94
94
|
target = Money.new(200_00, "EUR")
|
95
|
-
target.
|
96
|
-
(Money.new(100_00, "USD") <=> target).
|
95
|
+
expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(100_00, "USD"))
|
96
|
+
expect(Money.new(100_00, "USD") <=> target).to eq 0
|
97
97
|
|
98
98
|
target = Money.new(200_00, "EUR")
|
99
|
-
target.
|
100
|
-
(Money.new(100_00, "USD") <=> target).
|
99
|
+
expect(target).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(99_00, "USD"))
|
100
|
+
expect(Money.new(100_00, "USD") <=> target).to be > 0
|
101
101
|
end
|
102
102
|
|
103
103
|
it "can be used to compare with an object that responds to #to_money" do
|
@@ -111,9 +111,9 @@ describe Money do
|
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
(Money.new(1_00) <=> klass.new(Money.new(1_00))).
|
115
|
-
(Money.new(1_00) <=> klass.new(Money.new(99))).
|
116
|
-
(Money.new(1_00) <=> klass.new(Money.new(2_00))).
|
114
|
+
expect(Money.new(1_00) <=> klass.new(Money.new(1_00))).to eq 0
|
115
|
+
expect(Money.new(1_00) <=> klass.new(Money.new(99))).to be > 0
|
116
|
+
expect(Money.new(1_00) <=> klass.new(Money.new(2_00))).to be < 0
|
117
117
|
end
|
118
118
|
|
119
119
|
it "raises ArgumentError when used to compare with an object that doesn't respond to #to_money" do
|
@@ -127,53 +127,61 @@ describe Money do
|
|
127
127
|
|
128
128
|
describe "#positive?" do
|
129
129
|
it "returns true if the amount is greater than 0" do
|
130
|
-
Money.new(1).
|
130
|
+
expect(Money.new(1)).to be_positive
|
131
131
|
end
|
132
132
|
|
133
133
|
it "returns false if the amount is 0" do
|
134
|
-
Money.new(0).
|
134
|
+
expect(Money.new(0)).not_to be_positive
|
135
135
|
end
|
136
136
|
|
137
137
|
it "returns false if the amount is negative" do
|
138
|
-
Money.new(-1).
|
138
|
+
expect(Money.new(-1)).not_to be_positive
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
142
142
|
describe "#negative?" do
|
143
143
|
it "returns true if the amount is less than 0" do
|
144
|
-
Money.new(-1).
|
144
|
+
expect(Money.new(-1)).to be_negative
|
145
145
|
end
|
146
146
|
|
147
147
|
it "returns false if the amount is 0" do
|
148
|
-
Money.new(0).
|
148
|
+
expect(Money.new(0)).not_to be_negative
|
149
149
|
end
|
150
150
|
|
151
151
|
it "returns false if the amount is greater than 0" do
|
152
|
-
Money.new(1).
|
152
|
+
expect(Money.new(1)).not_to be_negative
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
156
156
|
describe "#+" do
|
157
157
|
it "adds other amount to current amount (same currency)" do
|
158
|
-
(Money.new(10_00, "USD") + Money.new(90, "USD")).
|
158
|
+
expect(Money.new(10_00, "USD") + Money.new(90, "USD")).to eq Money.new(10_90, "USD")
|
159
159
|
end
|
160
160
|
|
161
161
|
it "converts other object amount to current currency and adds other amount to current amount (different currency)" do
|
162
162
|
other = Money.new(90, "EUR")
|
163
|
-
other.
|
164
|
-
(Money.new(10_00, "USD") + other).
|
163
|
+
expect(other).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(9_00, "USD"))
|
164
|
+
expect(Money.new(10_00, "USD") + other).to eq Money.new(19_00, "USD")
|
165
|
+
end
|
166
|
+
|
167
|
+
it "adds Fixnum 0 to money and returns the same ammount" do
|
168
|
+
expect(Money.new(10_00) + 0).to eq Money.new(10_00)
|
165
169
|
end
|
166
170
|
end
|
167
171
|
|
168
172
|
describe "#-" do
|
169
173
|
it "subtracts other amount from current amount (same currency)" do
|
170
|
-
(Money.new(10_00, "USD") - Money.new(90, "USD")).
|
174
|
+
expect(Money.new(10_00, "USD") - Money.new(90, "USD")).to eq Money.new(9_10, "USD")
|
171
175
|
end
|
172
176
|
|
173
177
|
it "converts other object amount to current currency and subtracts other amount from current amount (different currency)" do
|
174
178
|
other = Money.new(90, "EUR")
|
175
|
-
other.
|
176
|
-
(Money.new(10_00, "USD") - other).
|
179
|
+
expect(other).to receive(:exchange_to).with(Money::Currency.new("USD")).and_return(Money.new(9_00, "USD"))
|
180
|
+
expect(Money.new(10_00, "USD") - other).to eq Money.new(1_00, "USD")
|
181
|
+
end
|
182
|
+
|
183
|
+
it "subtract Fixnum 0 to money and returns the same ammount" do
|
184
|
+
expect(Money.new(10_00) - 0).to eq Money.new(10_00)
|
177
185
|
end
|
178
186
|
end
|
179
187
|
|
@@ -186,7 +194,7 @@ describe Money do
|
|
186
194
|
{:a => Money.new(-10, :USD), :b => -4, :c => Money.new( 40, :USD)},
|
187
195
|
]
|
188
196
|
ts.each do |t|
|
189
|
-
(t[:a] * t[:b]).
|
197
|
+
expect(t[:a] * t[:b]).to eq t[:c]
|
190
198
|
end
|
191
199
|
end
|
192
200
|
|
@@ -212,44 +220,44 @@ describe Money do
|
|
212
220
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new( 3, :USD)},
|
213
221
|
]
|
214
222
|
ts.each do |t|
|
215
|
-
(t[:a] / t[:b]).
|
223
|
+
expect(t[:a] / t[:b]).to eq t[:c]
|
216
224
|
end
|
217
225
|
end
|
218
226
|
|
219
227
|
context 'rounding preference' do
|
220
228
|
before do
|
221
|
-
Money.
|
229
|
+
allow(Money).to receive(:rounding_mode).and_return(rounding_mode)
|
222
230
|
end
|
223
231
|
|
224
232
|
after do
|
225
|
-
Money.
|
233
|
+
allow(Money).to receive(:rounding_mode).and_call_original
|
226
234
|
end
|
227
235
|
|
228
236
|
context 'ceiling rounding' do
|
229
237
|
let(:rounding_mode) { BigDecimal::ROUND_CEILING }
|
230
238
|
it "obeys the rounding preference" do
|
231
|
-
(Money.new(10) / 3).
|
239
|
+
expect(Money.new(10) / 3).to eq Money.new(4)
|
232
240
|
end
|
233
241
|
end
|
234
242
|
|
235
243
|
context 'floor rounding' do
|
236
244
|
let(:rounding_mode) { BigDecimal::ROUND_FLOOR }
|
237
245
|
it "obeys the rounding preference" do
|
238
|
-
(Money.new(10) / 6).
|
246
|
+
expect(Money.new(10) / 6).to eq Money.new(1)
|
239
247
|
end
|
240
248
|
end
|
241
249
|
|
242
250
|
context 'half up rounding' do
|
243
251
|
let(:rounding_mode) { BigDecimal::ROUND_HALF_UP }
|
244
252
|
it "obeys the rounding preference" do
|
245
|
-
(Money.new(10) / 4).
|
253
|
+
expect(Money.new(10) / 4).to eq Money.new(3)
|
246
254
|
end
|
247
255
|
end
|
248
256
|
|
249
257
|
context 'half down rounding' do
|
250
258
|
let(:rounding_mode) { BigDecimal::ROUND_HALF_DOWN }
|
251
259
|
it "obeys the rounding preference" do
|
252
|
-
(Money.new(10) / 4).
|
260
|
+
expect(Money.new(10) / 4).to eq Money.new(2)
|
253
261
|
end
|
254
262
|
end
|
255
263
|
end
|
@@ -262,7 +270,7 @@ describe Money do
|
|
262
270
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => 3.25},
|
263
271
|
]
|
264
272
|
ts.each do |t|
|
265
|
-
(t[:a] / t[:b]).
|
273
|
+
expect(t[:a] / t[:b]).to eq t[:c]
|
266
274
|
end
|
267
275
|
end
|
268
276
|
|
@@ -274,8 +282,8 @@ describe Money do
|
|
274
282
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => 1.625},
|
275
283
|
]
|
276
284
|
ts.each do |t|
|
277
|
-
t[:b].
|
278
|
-
(t[:a] / t[:b]).
|
285
|
+
expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
|
286
|
+
expect(t[:a] / t[:b]).to eq t[:c]
|
279
287
|
end
|
280
288
|
end
|
281
289
|
|
@@ -296,7 +304,7 @@ describe Money do
|
|
296
304
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new( 3.25, :USD)},
|
297
305
|
]
|
298
306
|
ts.each do |t|
|
299
|
-
(t[:a] / t[:b]).
|
307
|
+
expect(t[:a] / t[:b]).to eq t[:c]
|
300
308
|
end
|
301
309
|
end
|
302
310
|
end
|
@@ -311,7 +319,7 @@ describe Money do
|
|
311
319
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new( 3, :USD)},
|
312
320
|
]
|
313
321
|
ts.each do |t|
|
314
|
-
t[:a].div(t[:b]).
|
322
|
+
expect(t[:a].div(t[:b])).to eq t[:c]
|
315
323
|
end
|
316
324
|
end
|
317
325
|
|
@@ -323,7 +331,7 @@ describe Money do
|
|
323
331
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => 3.25},
|
324
332
|
]
|
325
333
|
ts.each do |t|
|
326
|
-
t[:a].div(t[:b]).
|
334
|
+
expect(t[:a].div(t[:b])).to eq t[:c]
|
327
335
|
end
|
328
336
|
end
|
329
337
|
|
@@ -335,8 +343,8 @@ describe Money do
|
|
335
343
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => 1.625},
|
336
344
|
]
|
337
345
|
ts.each do |t|
|
338
|
-
t[:b].
|
339
|
-
t[:a].div(t[:b]).
|
346
|
+
expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
|
347
|
+
expect(t[:a].div(t[:b])).to eq t[:c]
|
340
348
|
end
|
341
349
|
end
|
342
350
|
|
@@ -357,7 +365,7 @@ describe Money do
|
|
357
365
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new( 3.25, :USD)},
|
358
366
|
]
|
359
367
|
ts.each do |t|
|
360
|
-
t[:a].div(t[:b]).
|
368
|
+
expect(t[:a].div(t[:b])).to eq t[:c]
|
361
369
|
end
|
362
370
|
end
|
363
371
|
end
|
@@ -372,7 +380,7 @@ describe Money do
|
|
372
380
|
{:a => Money.new(-13, :USD), :b => -4, :c => [Money.new( 3, :USD), Money.new(-1, :USD)]},
|
373
381
|
]
|
374
382
|
ts.each do |t|
|
375
|
-
t[:a].divmod(t[:b]).
|
383
|
+
expect(t[:a].divmod(t[:b])).to eq t[:c]
|
376
384
|
end
|
377
385
|
end
|
378
386
|
|
@@ -384,7 +392,7 @@ describe Money do
|
|
384
392
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => [ 3, Money.new(-1, :USD)]},
|
385
393
|
]
|
386
394
|
ts.each do |t|
|
387
|
-
t[:a].divmod(t[:b]).
|
395
|
+
expect(t[:a].divmod(t[:b])).to eq t[:c]
|
388
396
|
end
|
389
397
|
end
|
390
398
|
|
@@ -396,8 +404,8 @@ describe Money do
|
|
396
404
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => [ 1, Money.new(-5, :USD)]},
|
397
405
|
]
|
398
406
|
ts.each do |t|
|
399
|
-
t[:b].
|
400
|
-
t[:a].divmod(t[:b]).
|
407
|
+
expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
|
408
|
+
expect(t[:a].divmod(t[:b])).to eq t[:c]
|
401
409
|
end
|
402
410
|
end
|
403
411
|
|
@@ -418,7 +426,7 @@ describe Money do
|
|
418
426
|
{:a => Money.new(-13, :USD), :b => -4, :c => [Money.new( 3, :USD), Money.new(-1, :USD)]},
|
419
427
|
]
|
420
428
|
ts.each do |t|
|
421
|
-
t[:a].divmod(t[:b]).
|
429
|
+
expect(t[:a].divmod(t[:b])).to eq t[:c]
|
422
430
|
end
|
423
431
|
end
|
424
432
|
end
|
@@ -433,7 +441,7 @@ describe Money do
|
|
433
441
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new(-1, :USD)},
|
434
442
|
]
|
435
443
|
ts.each do |t|
|
436
|
-
t[:a].modulo(t[:b]).
|
444
|
+
expect(t[:a].modulo(t[:b])).to eq t[:c]
|
437
445
|
end
|
438
446
|
end
|
439
447
|
|
@@ -445,7 +453,7 @@ describe Money do
|
|
445
453
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-1, :USD)},
|
446
454
|
]
|
447
455
|
ts.each do |t|
|
448
|
-
t[:a].modulo(t[:b]).
|
456
|
+
expect(t[:a].modulo(t[:b])).to eq t[:c]
|
449
457
|
end
|
450
458
|
end
|
451
459
|
|
@@ -457,8 +465,8 @@ describe Money do
|
|
457
465
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-5, :USD)},
|
458
466
|
]
|
459
467
|
ts.each do |t|
|
460
|
-
t[:b].
|
461
|
-
t[:a].modulo(t[:b]).
|
468
|
+
expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
|
469
|
+
expect(t[:a].modulo(t[:b])).to eq t[:c]
|
462
470
|
end
|
463
471
|
end
|
464
472
|
end
|
@@ -472,7 +480,7 @@ describe Money do
|
|
472
480
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new(-1, :USD)},
|
473
481
|
]
|
474
482
|
ts.each do |t|
|
475
|
-
(t[:a] % t[:b]).
|
483
|
+
expect(t[:a] % t[:b]).to eq t[:c]
|
476
484
|
end
|
477
485
|
end
|
478
486
|
|
@@ -484,7 +492,7 @@ describe Money do
|
|
484
492
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :USD), :c => Money.new(-1, :USD)},
|
485
493
|
]
|
486
494
|
ts.each do |t|
|
487
|
-
(t[:a] % t[:b]).
|
495
|
+
expect(t[:a] % t[:b]).to eq t[:c]
|
488
496
|
end
|
489
497
|
end
|
490
498
|
|
@@ -496,8 +504,8 @@ describe Money do
|
|
496
504
|
{:a => Money.new(-13, :USD), :b => Money.new(-4, :EUR), :c => Money.new(-5, :USD)},
|
497
505
|
]
|
498
506
|
ts.each do |t|
|
499
|
-
t[:b].
|
500
|
-
(t[:a] % t[:b]).
|
507
|
+
expect(t[:b]).to receive(:exchange_to).once.with(t[:a].currency).and_return(Money.new(t[:b].cents * 2, :USD))
|
508
|
+
expect(t[:a] % t[:b]).to eq t[:c]
|
501
509
|
end
|
502
510
|
end
|
503
511
|
end
|
@@ -511,7 +519,7 @@ describe Money do
|
|
511
519
|
{:a => Money.new(-13, :USD), :b => -4, :c => Money.new(-1, :USD)},
|
512
520
|
]
|
513
521
|
ts.each do |t|
|
514
|
-
t[:a].remainder(t[:b]).
|
522
|
+
expect(t[:a].remainder(t[:b])).to eq t[:c]
|
515
523
|
end
|
516
524
|
end
|
517
525
|
end
|
@@ -519,42 +527,42 @@ describe Money do
|
|
519
527
|
describe "#abs" do
|
520
528
|
it "returns the absolute value as a new Money object" do
|
521
529
|
n = Money.new(-1, :USD)
|
522
|
-
n.abs.
|
523
|
-
n.
|
530
|
+
expect(n.abs).to eq Money.new( 1, :USD)
|
531
|
+
expect(n).to eq Money.new(-1, :USD)
|
524
532
|
end
|
525
533
|
end
|
526
534
|
|
527
535
|
describe "#zero?" do
|
528
536
|
it "returns whether the amount is 0" do
|
529
|
-
Money.new(0, "USD").
|
530
|
-
Money.new(0, "EUR").
|
531
|
-
Money.new(1, "USD").
|
532
|
-
Money.new(10, "YEN").
|
533
|
-
Money.new(-1, "EUR").
|
537
|
+
expect(Money.new(0, "USD")).to be_zero
|
538
|
+
expect(Money.new(0, "EUR")).to be_zero
|
539
|
+
expect(Money.new(1, "USD")).not_to be_zero
|
540
|
+
expect(Money.new(10, "YEN")).not_to be_zero
|
541
|
+
expect(Money.new(-1, "EUR")).not_to be_zero
|
534
542
|
end
|
535
543
|
end
|
536
544
|
|
537
545
|
describe "#nonzero?" do
|
538
546
|
it "returns whether the amount is not 0" do
|
539
|
-
Money.new(0, "USD").
|
540
|
-
Money.new(0, "EUR").
|
541
|
-
Money.new(1, "USD").
|
542
|
-
Money.new(10, "YEN").
|
543
|
-
Money.new(-1, "EUR").
|
547
|
+
expect(Money.new(0, "USD")).not_to be_nonzero
|
548
|
+
expect(Money.new(0, "EUR")).not_to be_nonzero
|
549
|
+
expect(Money.new(1, "USD")).to be_nonzero
|
550
|
+
expect(Money.new(10, "YEN")).to be_nonzero
|
551
|
+
expect(Money.new(-1, "EUR")).to be_nonzero
|
544
552
|
end
|
545
553
|
|
546
554
|
it "has the same return-value semantics as Numeric#nonzero?" do
|
547
|
-
Money.new(0, "USD").nonzero
|
555
|
+
expect(Money.new(0, "USD").nonzero?).to be_nil
|
548
556
|
|
549
557
|
money = Money.new(1, "USD")
|
550
|
-
money.nonzero
|
558
|
+
expect(money.nonzero?).to be_equal(money)
|
551
559
|
end
|
552
560
|
end
|
553
561
|
|
554
562
|
describe "#coerce" do
|
555
563
|
it "allows mathematical operations by coercing arguments" do
|
556
564
|
result = 2 * Money.new(4, 'USD')
|
557
|
-
result.
|
565
|
+
expect(result).to eq Money.new(8, 'USD')
|
558
566
|
end
|
559
567
|
end
|
560
568
|
end
|