money 6.7.0 → 6.13.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/.rspec +2 -1
- data/.travis.yml +22 -5
- data/AUTHORS +5 -0
- data/CHANGELOG.md +109 -3
- data/Gemfile +13 -4
- data/LICENSE +2 -0
- data/README.md +69 -49
- data/config/currency_backwards_compatible.json +30 -0
- data/config/currency_iso.json +139 -62
- data/config/currency_non_iso.json +66 -2
- data/lib/money.rb +0 -13
- data/lib/money/bank/variable_exchange.rb +9 -22
- data/lib/money/currency.rb +35 -38
- data/lib/money/currency/heuristics.rb +1 -144
- data/lib/money/currency/loader.rb +1 -1
- data/lib/money/locale_backend/base.rb +7 -0
- data/lib/money/locale_backend/errors.rb +6 -0
- data/lib/money/locale_backend/i18n.rb +24 -0
- data/lib/money/locale_backend/legacy.rb +28 -0
- data/lib/money/money.rb +120 -151
- data/lib/money/money/allocation.rb +37 -0
- data/lib/money/money/arithmetic.rb +57 -52
- data/lib/money/money/constructors.rb +1 -2
- data/lib/money/money/formatter.rb +397 -0
- data/lib/money/money/formatting_rules.rb +120 -0
- data/lib/money/money/locale_backend.rb +20 -0
- data/lib/money/rates_store/memory.rb +1 -2
- data/lib/money/version.rb +1 -1
- data/money.gemspec +10 -16
- data/spec/bank/variable_exchange_spec.rb +7 -3
- data/spec/currency/heuristics_spec.rb +2 -153
- data/spec/currency_spec.rb +45 -4
- data/spec/locale_backend/i18n_spec.rb +62 -0
- data/spec/locale_backend/legacy_spec.rb +74 -0
- data/spec/money/allocation_spec.rb +130 -0
- data/spec/money/arithmetic_spec.rb +217 -104
- data/spec/money/constructors_spec.rb +0 -12
- data/spec/money/formatting_spec.rb +320 -179
- data/spec/money/locale_backend_spec.rb +14 -0
- data/spec/money_spec.rb +159 -26
- data/spec/rates_store/memory_spec.rb +13 -2
- data/spec/spec_helper.rb +2 -0
- data/spec/support/shared_examples/money_examples.rb +14 -0
- metadata +32 -41
- data/lib/money/money/formatting.rb +0 -417
@@ -7,18 +7,6 @@ describe Money::Constructors do
|
|
7
7
|
expect(Money.empty).to eq Money.new(0)
|
8
8
|
end
|
9
9
|
|
10
|
-
it "memoizes the result" do
|
11
|
-
expect(Money.empty.object_id).to eq Money.empty.object_id
|
12
|
-
end
|
13
|
-
|
14
|
-
it "memoizes a result for each currency" do
|
15
|
-
expect(Money.empty(:cad).object_id).to eq Money.empty(:cad).object_id
|
16
|
-
end
|
17
|
-
|
18
|
-
it "doesn't allow money to be modified for a currency" do
|
19
|
-
expect(Money.empty).to be_frozen
|
20
|
-
end
|
21
|
-
|
22
10
|
it "instantiates a subclass when inheritance is used" do
|
23
11
|
special_money_class = Class.new(Money)
|
24
12
|
expect(special_money_class.empty).to be_a special_money_class
|
@@ -18,13 +18,26 @@ describe Money, "formatting" do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
context 'without locale_backend' do
|
22
|
+
before { Money.locale_backend = nil }
|
23
|
+
after { Money.locale_backend = :legacy }
|
24
|
+
|
25
|
+
subject(:money) { Money.new(1099_99, 'USD') }
|
26
|
+
|
27
|
+
it 'falls back to using defaults' do
|
28
|
+
expect(money.thousands_separator).to eq('')
|
29
|
+
expect(money.decimal_mark).to eq('.')
|
30
|
+
expect(money.format).to eq('$1099.99')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
21
34
|
context "with i18n but use_i18n = false" do
|
22
35
|
before :each do
|
23
36
|
reset_i18n
|
24
37
|
I18n.locale = :de
|
25
38
|
I18n.backend.store_translations(
|
26
39
|
:de,
|
27
|
-
:
|
40
|
+
number: { currency: { format: { delimiter: ".", separator: "," } } }
|
28
41
|
)
|
29
42
|
Money.use_i18n = false
|
30
43
|
end
|
@@ -58,7 +71,7 @@ describe Money, "formatting" do
|
|
58
71
|
I18n.locale = :de
|
59
72
|
I18n.backend.store_translations(
|
60
73
|
:de,
|
61
|
-
:
|
74
|
+
number: { format: { delimiter: ".", separator: "," } }
|
62
75
|
)
|
63
76
|
end
|
64
77
|
|
@@ -79,7 +92,7 @@ describe Money, "formatting" do
|
|
79
92
|
I18n.locale = :de
|
80
93
|
I18n.backend.store_translations(
|
81
94
|
:de,
|
82
|
-
:
|
95
|
+
number: { currency: { format: { delimiter: ".", separator: "," } } }
|
83
96
|
)
|
84
97
|
end
|
85
98
|
|
@@ -100,14 +113,24 @@ describe Money, "formatting" do
|
|
100
113
|
I18n.locale = :de
|
101
114
|
I18n.backend.store_translations(
|
102
115
|
:de,
|
103
|
-
:
|
116
|
+
number: { currency: { symbol: { CAD: "CAD$" } } }
|
104
117
|
)
|
105
118
|
end
|
106
119
|
|
107
120
|
subject(:money) { Money.empty("CAD") }
|
108
121
|
|
109
122
|
it "should use 'CAD$' as the currency symbol" do
|
110
|
-
expect(money.format(:
|
123
|
+
expect(money.format(translate: true)).to eq("CAD$0.00")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "with overridden i18n settings" do
|
128
|
+
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies that have no subunit" do
|
129
|
+
expect(Money.new(300_000, 'ISK').format(thousands_separator: ".", decimal_mark: ',')).to eq "kr300.000"
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies with subunits that drop_trailing_zeros" do
|
133
|
+
expect(Money.new(300_000, 'USD').format(thousands_separator: ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000"
|
111
134
|
end
|
112
135
|
end
|
113
136
|
end
|
@@ -119,7 +142,7 @@ describe Money, "formatting" do
|
|
119
142
|
it "formats Japanese currency in Japanese properly" do
|
120
143
|
money = Money.new(1000, "JPY")
|
121
144
|
expect(money.format).to eq "1,000円"
|
122
|
-
expect(money.format(:
|
145
|
+
expect(money.format(symbol: false)).to eq "1,000"
|
123
146
|
end
|
124
147
|
|
125
148
|
after { I18n.locale = @_locale }
|
@@ -134,8 +157,18 @@ describe Money, "formatting" do
|
|
134
157
|
expect(Money.new(10_00, "BHD").format).to eq "ب.د1.000"
|
135
158
|
end
|
136
159
|
|
137
|
-
|
138
|
-
|
160
|
+
context "when :subunit_to_unit is 1" do
|
161
|
+
it "does not display a decimal part" do
|
162
|
+
expect(Money.new(10_00, "VUV").format).to eq "Vt1,000"
|
163
|
+
end
|
164
|
+
|
165
|
+
it "does not displays a decimal part when infinite_precision is false" do
|
166
|
+
expect(Money.new(10_00.1, "VUV").format).to eq "Vt1,000"
|
167
|
+
end
|
168
|
+
|
169
|
+
it "displays a decimal part when infinite_precision is true", :infinite_precision do
|
170
|
+
expect(Money.new(10_00.1, "VUV").format).to eq "Vt1,000.1"
|
171
|
+
end
|
139
172
|
end
|
140
173
|
|
141
174
|
it "respects the thousands_separator and decimal_mark defaults" do
|
@@ -176,17 +209,17 @@ describe Money, "formatting" do
|
|
176
209
|
|
177
210
|
it "inserts commas into the result if the amount is sufficiently large" do
|
178
211
|
expect(Money.us_dollar(1_000_000_000_12).format).to eq "$1,000,000,000.12"
|
179
|
-
expect(Money.us_dollar(1_000_000_000_12).format(:
|
212
|
+
expect(Money.us_dollar(1_000_000_000_12).format(no_cents: true)).to eq "$1,000,000,000"
|
180
213
|
end
|
181
214
|
|
182
215
|
it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
|
183
216
|
expect(Money.euro(1_234_567_12).format).to eq "€1.234.567,12"
|
184
|
-
expect(Money.euro(1_234_567_12).format(:
|
217
|
+
expect(Money.euro(1_234_567_12).format(no_cents: true)).to eq "€1.234.567"
|
185
218
|
end
|
186
219
|
|
187
220
|
context 'when default_formatting_rules defines (display_free: true)' do
|
188
221
|
before do
|
189
|
-
Money.default_formatting_rules = { :
|
222
|
+
Money.default_formatting_rules = { display_free: "you won't pay a thing" }
|
190
223
|
end
|
191
224
|
|
192
225
|
after do
|
@@ -201,7 +234,7 @@ describe Money, "formatting" do
|
|
201
234
|
|
202
235
|
context 'with rule (display_free: false) provided' do
|
203
236
|
it 'acknowledges provided rule' do
|
204
|
-
expect(Money.new(0, 'USD').format(:
|
237
|
+
expect(Money.new(0, 'USD').format(display_free: false)).to eq '$0.00'
|
205
238
|
end
|
206
239
|
end
|
207
240
|
end
|
@@ -213,79 +246,85 @@ describe Money, "formatting" do
|
|
213
246
|
|
214
247
|
context 'acknowledges provided rule' do
|
215
248
|
it 'acknowledges provided rule' do
|
216
|
-
expect(Money.new(100, 'USD').format(:
|
249
|
+
expect(Money.new(100, 'USD').format(with_currency: true)).to eq '$1.00 USD'
|
217
250
|
end
|
218
251
|
end
|
219
252
|
end
|
220
253
|
|
221
254
|
describe ":with_currency option" do
|
222
255
|
specify "(:with_currency option => true) works as documented" do
|
223
|
-
expect(Money.ca_dollar(100).format(:
|
224
|
-
expect(Money.us_dollar(85).format(:
|
256
|
+
expect(Money.ca_dollar(100).format(with_currency: true)).to eq "$1.00 CAD"
|
257
|
+
expect(Money.us_dollar(85).format(with_currency: true)).to eq "$0.85 USD"
|
225
258
|
end
|
226
259
|
end
|
227
260
|
|
228
261
|
describe ":no_cents option" do
|
229
262
|
specify "(:with_currency option => true) works as documented" do
|
230
|
-
expect(Money.ca_dollar(100).format(:
|
231
|
-
expect(Money.ca_dollar(599).format(:
|
232
|
-
expect(Money.ca_dollar(570).format(:
|
233
|
-
expect(Money.ca_dollar(39000).format(:
|
263
|
+
expect(Money.ca_dollar(100).format(no_cents: true)).to eq "$1"
|
264
|
+
expect(Money.ca_dollar(599).format(no_cents: true)).to eq "$5"
|
265
|
+
expect(Money.ca_dollar(570).format(no_cents: true, with_currency: true)).to eq "$5 CAD"
|
266
|
+
expect(Money.ca_dollar(39000).format(no_cents: true)).to eq "$390"
|
234
267
|
end
|
235
268
|
|
236
269
|
it "respects :subunit_to_unit currency property" do
|
237
|
-
expect(Money.new(10_00, "BHD").format(:
|
270
|
+
expect(Money.new(10_00, "BHD").format(no_cents: true)).to eq "ب.د1"
|
238
271
|
end
|
239
272
|
|
240
273
|
it "inserts thousand separators if symbol contains decimal mark and no_cents is true" do
|
241
|
-
expect(Money.new(100000000, "AMD").format(:
|
242
|
-
expect(Money.new(100000000, "USD").format(:
|
243
|
-
expect(Money.new(100000000, "RUB").format(:
|
274
|
+
expect(Money.new(100000000, "AMD").format(no_cents: true)).to eq "1,000,000 դր."
|
275
|
+
expect(Money.new(100000000, "USD").format(no_cents: true)).to eq "$1,000,000"
|
276
|
+
expect(Money.new(100000000, "RUB").format(no_cents: true)).to eq "1.000.000 ₽"
|
244
277
|
end
|
245
278
|
|
246
279
|
it "doesn't incorrectly format HTML" do
|
247
280
|
money = ::Money.new(1999, "RUB")
|
248
|
-
output = money.format(:
|
281
|
+
output = money.format(html: true, no_cents: true)
|
249
282
|
expect(output).to eq "19 ₽"
|
250
283
|
end
|
284
|
+
|
285
|
+
it "doesn't incorrectly format HTML (html_wrap)" do
|
286
|
+
money = ::Money.new(1999, "RUB")
|
287
|
+
output = money.format(html_wrap: true, no_cents: true)
|
288
|
+
expect(output).to eq "<span class=\"money-whole\">19</span> <span class=\"money-currency-symbol\">₽</span>"
|
289
|
+
end
|
251
290
|
end
|
252
291
|
|
253
292
|
describe ":no_cents_if_whole option" do
|
254
|
-
specify "(:
|
255
|
-
expect(Money.new(10000, "VUV").format(:
|
256
|
-
expect(Money.new(10034, "VUV").format(:
|
257
|
-
expect(Money.new(10000, "MGA").format(:
|
258
|
-
expect(Money.new(10034, "MGA").format(:
|
259
|
-
expect(Money.new(10000, "VND").format(:
|
260
|
-
expect(Money.new(10034, "VND").format(:
|
261
|
-
expect(Money.new(10000, "USD").format(:
|
262
|
-
expect(Money.new(10034, "USD").format(:
|
263
|
-
expect(Money.new(10000, "IQD").format(:
|
264
|
-
expect(Money.new(10034, "IQD").format(:
|
265
|
-
end
|
266
|
-
|
267
|
-
specify "(:
|
268
|
-
expect(Money.new(10000, "VUV").format(:
|
269
|
-
expect(Money.new(10034, "VUV").format(:
|
270
|
-
expect(Money.new(10000, "MGA").format(:
|
271
|
-
expect(Money.new(10034, "MGA").format(:
|
272
|
-
expect(Money.new(10000, "VND").format(:
|
273
|
-
expect(Money.new(10034, "VND").format(:
|
274
|
-
expect(Money.new(10000, "USD").format(:
|
275
|
-
expect(Money.new(10034, "USD").format(:
|
276
|
-
expect(Money.new(10000, "IQD").format(:
|
277
|
-
expect(Money.new(10034, "IQD").format(:
|
293
|
+
specify "(no_cents_if_whole: true) works as documented" do
|
294
|
+
expect(Money.new(10000, "VUV").format(no_cents_if_whole: true, symbol: false)).to eq "10,000"
|
295
|
+
expect(Money.new(10034, "VUV").format(no_cents_if_whole: true, symbol: false)).to eq "10,034"
|
296
|
+
expect(Money.new(10000, "MGA").format(no_cents_if_whole: true, symbol: false)).to eq "2,000"
|
297
|
+
expect(Money.new(10034, "MGA").format(no_cents_if_whole: true, symbol: false)).to eq "2,006.8"
|
298
|
+
expect(Money.new(10000, "VND").format(no_cents_if_whole: true, symbol: false)).to eq "10.000"
|
299
|
+
expect(Money.new(10034, "VND").format(no_cents_if_whole: true, symbol: false)).to eq "10.034"
|
300
|
+
expect(Money.new(10000, "USD").format(no_cents_if_whole: true, symbol: false)).to eq "100"
|
301
|
+
expect(Money.new(10034, "USD").format(no_cents_if_whole: true, symbol: false)).to eq "100.34"
|
302
|
+
expect(Money.new(10000, "IQD").format(no_cents_if_whole: true, symbol: false)).to eq "10"
|
303
|
+
expect(Money.new(10034, "IQD").format(no_cents_if_whole: true, symbol: false)).to eq "10.034"
|
304
|
+
end
|
305
|
+
|
306
|
+
specify "(no_cents_if_whole: false) works as documented" do
|
307
|
+
expect(Money.new(10000, "VUV").format(no_cents_if_whole: false, symbol: false)).to eq "10,000"
|
308
|
+
expect(Money.new(10034, "VUV").format(no_cents_if_whole: false, symbol: false)).to eq "10,034"
|
309
|
+
expect(Money.new(10000, "MGA").format(no_cents_if_whole: false, symbol: false)).to eq "2,000.0"
|
310
|
+
expect(Money.new(10034, "MGA").format(no_cents_if_whole: false, symbol: false)).to eq "2,006.8"
|
311
|
+
expect(Money.new(10000, "VND").format(no_cents_if_whole: false, symbol: false)).to eq "10.000"
|
312
|
+
expect(Money.new(10034, "VND").format(no_cents_if_whole: false, symbol: false)).to eq "10.034"
|
313
|
+
expect(Money.new(10000, "USD").format(no_cents_if_whole: false, symbol: false)).to eq "100.00"
|
314
|
+
expect(Money.new(10034, "USD").format(no_cents_if_whole: false, symbol: false)).to eq "100.34"
|
315
|
+
expect(Money.new(10000, "IQD").format(no_cents_if_whole: false, symbol: false)).to eq "10.000"
|
316
|
+
expect(Money.new(10034, "IQD").format(no_cents_if_whole: false, symbol: false)).to eq "10.034"
|
278
317
|
end
|
279
318
|
end
|
280
319
|
|
281
320
|
describe ":symbol option" do
|
282
|
-
specify "(:
|
283
|
-
expect(Money.new(100, "GBP").format(:
|
321
|
+
specify "(symbol: a symbol string) uses the given value as the money symbol" do
|
322
|
+
expect(Money.new(100, "GBP").format(symbol: "£")).to eq "£1.00"
|
284
323
|
end
|
285
324
|
|
286
|
-
specify "(:
|
325
|
+
specify "(symbol: true) returns symbol based on the given currency code" do
|
287
326
|
one = Proc.new do |currency|
|
288
|
-
Money.new(100, currency).format(:
|
327
|
+
Money.new(100, currency).format(symbol: true)
|
289
328
|
end
|
290
329
|
|
291
330
|
# Pounds
|
@@ -314,31 +353,34 @@ describe Money, "formatting" do
|
|
314
353
|
# Brazilian Real
|
315
354
|
expect(one["BRL"]).to eq "R$1,00"
|
316
355
|
|
356
|
+
# Vietnamese Dong
|
357
|
+
expect(one["VND"]).to eq "100 ₫"
|
358
|
+
|
317
359
|
# Other
|
318
360
|
expect(one["SEK"]).to eq "1,00 kr"
|
319
361
|
expect(one["GHC"]).to eq "₵1.00"
|
320
362
|
end
|
321
363
|
|
322
|
-
specify "(:
|
364
|
+
specify "(symbol: true) returns $ when currency code is not recognized" do
|
323
365
|
currency = Money::Currency.new("EUR")
|
324
366
|
expect(currency).to receive(:symbol).and_return(nil)
|
325
|
-
expect(Money.new(100, currency).format(:
|
367
|
+
expect(Money.new(100, currency).format(symbol: true)).to eq "¤1,00"
|
326
368
|
end
|
327
369
|
|
328
|
-
specify "(:
|
329
|
-
expect(Money.new(100, "GBP").format(:
|
330
|
-
expect(Money.new(100, "EUR").format(:
|
331
|
-
expect(Money.new(100, "SEK").format(:
|
370
|
+
specify "(symbol: some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
|
371
|
+
expect(Money.new(100, "GBP").format(symbol: true)).to eq "£1.00"
|
372
|
+
expect(Money.new(100, "EUR").format(symbol: true)).to eq "€1,00"
|
373
|
+
expect(Money.new(100, "SEK").format(symbol: true)).to eq "1,00 kr"
|
332
374
|
end
|
333
375
|
|
334
|
-
specify "(:
|
376
|
+
specify "(symbol: "", nil or false) returns the amount without a symbol" do
|
335
377
|
money = Money.new(100, "GBP")
|
336
|
-
expect(money.format(:
|
337
|
-
expect(money.format(:
|
338
|
-
expect(money.format(:
|
378
|
+
expect(money.format(symbol: "")).to eq "1.00"
|
379
|
+
expect(money.format(symbol: nil)).to eq "1.00"
|
380
|
+
expect(money.format(symbol: false)).to eq "1.00"
|
339
381
|
|
340
382
|
money = Money.new(100, "JPY")
|
341
|
-
expect(money.format(:
|
383
|
+
expect(money.format(symbol: false)).to eq "100"
|
342
384
|
end
|
343
385
|
|
344
386
|
it "defaults :symbol to true" do
|
@@ -352,19 +394,19 @@ describe Money, "formatting" do
|
|
352
394
|
expect(money.format).to eq "€1,00"
|
353
395
|
end
|
354
396
|
|
355
|
-
specify "(:
|
397
|
+
specify "(symbol: false) returns a signed amount without a symbol" do
|
356
398
|
money = Money.new(-100, "EUR")
|
357
|
-
expect(money.format(:
|
399
|
+
expect(money.format(symbol: false)).to eq "-1,00"
|
358
400
|
|
359
401
|
money = Money.new(100, "EUR")
|
360
|
-
expect(money.format(:
|
361
|
-
:
|
402
|
+
expect(money.format(symbol: false,
|
403
|
+
sign_positive: true)).to eq "+1,00"
|
362
404
|
end
|
363
405
|
end
|
364
406
|
|
365
407
|
describe ":decimal_mark option" do
|
366
|
-
specify "(:
|
367
|
-
expect(Money.us_dollar(100).format(:
|
408
|
+
specify "(decimal_mark: a decimal_mark string) works as documented" do
|
409
|
+
expect(Money.us_dollar(100).format(decimal_mark: ",")).to eq "$1,00"
|
368
410
|
end
|
369
411
|
|
370
412
|
it "defaults to '.' if currency isn't recognized" do
|
@@ -373,179 +415,215 @@ describe Money, "formatting" do
|
|
373
415
|
end
|
374
416
|
|
375
417
|
describe ":separator option" do
|
376
|
-
specify "(:
|
377
|
-
expect(Money.us_dollar(100).format(:
|
418
|
+
specify "(separator: a separator string) works as documented" do
|
419
|
+
expect(Money.us_dollar(100).format(separator: ",")).to eq "$1,00"
|
378
420
|
end
|
379
421
|
end
|
380
422
|
|
381
423
|
describe ":south_asian_number_formatting delimiter" do
|
382
424
|
before(:each) do
|
383
|
-
Money::Currency.register(JSON.parse(INDIAN_BAR, :
|
425
|
+
Money::Currency.register(JSON.parse(INDIAN_BAR, symbolize_names: true))
|
384
426
|
end
|
385
427
|
|
386
428
|
after(:each) do
|
387
|
-
Money::Currency.unregister(JSON.parse(INDIAN_BAR, :
|
429
|
+
Money::Currency.unregister(JSON.parse(INDIAN_BAR, symbolize_names: true))
|
430
|
+
end
|
431
|
+
|
432
|
+
specify "(south_asian_number_formatting: true) works as documented" do
|
433
|
+
expect(Money.new(10000000, 'INR').format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.00"
|
434
|
+
expect(Money.new(1000000000, 'INDIAN_BAR').format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.0000"
|
435
|
+
expect(Money.new(10000000).format(south_asian_number_formatting: true)).to eq "$1,00,000.00"
|
388
436
|
end
|
389
437
|
|
390
|
-
specify "(:
|
391
|
-
expect(Money.new(10000000, 'INR').format(:
|
392
|
-
expect(Money.new(1000000000, 'INDIAN_BAR').format(:
|
393
|
-
expect(Money.new(10000000).format(:south_asian_number_formatting => true)).to eq "$1,00,000.00"
|
438
|
+
specify "(south_asian_number_formatting: true and no_cents_if_whole => true) works as documented" do
|
439
|
+
expect(Money.new(10000000, 'INR').format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000"
|
440
|
+
expect(Money.new(1000000000, 'INDIAN_BAR').format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000"
|
394
441
|
end
|
395
442
|
end
|
396
443
|
|
397
444
|
describe ":thousands_separator option" do
|
398
|
-
specify "(:
|
399
|
-
expect(Money.us_dollar(100000).format(:
|
400
|
-
expect(Money.us_dollar(200000).format(:
|
445
|
+
specify "(thousands_separator: a thousands_separator string) works as documented" do
|
446
|
+
expect(Money.us_dollar(100000).format(thousands_separator: ".")).to eq "$1.000.00"
|
447
|
+
expect(Money.us_dollar(200000).format(thousands_separator: "")).to eq "$2000.00"
|
401
448
|
end
|
402
449
|
|
403
|
-
specify "(:
|
404
|
-
expect(Money.us_dollar(100000).format(:
|
405
|
-
expect(Money.us_dollar(200000).format(:
|
450
|
+
specify "(thousands_separator: false or nil) works as documented" do
|
451
|
+
expect(Money.us_dollar(100000).format(thousands_separator: false)).to eq "$1000.00"
|
452
|
+
expect(Money.us_dollar(200000).format(thousands_separator: nil)).to eq "$2000.00"
|
406
453
|
end
|
407
454
|
|
408
|
-
specify "(:
|
409
|
-
expect(Money.us_dollar(100000).format(:
|
410
|
-
expect(Money.us_dollar(200000).format(:
|
455
|
+
specify "(delimiter: a delimiter string) works as documented" do
|
456
|
+
expect(Money.us_dollar(100000).format(delimiter: ".")).to eq "$1.000.00"
|
457
|
+
expect(Money.us_dollar(200000).format(delimiter: "")).to eq "$2000.00"
|
411
458
|
end
|
412
459
|
|
413
|
-
specify "(:
|
414
|
-
expect(Money.us_dollar(100000).format(:
|
415
|
-
expect(Money.us_dollar(200000).format(:
|
460
|
+
specify "(delimiter: false or nil) works as documented" do
|
461
|
+
expect(Money.us_dollar(100000).format(delimiter: false)).to eq "$1000.00"
|
462
|
+
expect(Money.us_dollar(200000).format(delimiter: nil)).to eq "$2000.00"
|
416
463
|
end
|
417
464
|
|
418
465
|
it "defaults to ',' if currency isn't recognized" do
|
419
466
|
expect(Money.new(100000, "ZWD").format).to eq "$1,000.00"
|
420
467
|
end
|
468
|
+
|
469
|
+
context "without i18n" do
|
470
|
+
before { Money.use_i18n = false }
|
471
|
+
|
472
|
+
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies that have no subunit" do
|
473
|
+
expect(Money.new(300_000, 'ISK').format(thousands_separator: ",", decimal_mark: '.')).to eq "kr300,000"
|
474
|
+
end
|
475
|
+
|
476
|
+
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies with subunits that drop_trailing_zeros" do
|
477
|
+
expect(Money.new(300_000, 'USD').format(thousands_separator: ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000"
|
478
|
+
end
|
479
|
+
|
480
|
+
after { Money.use_i18n = true}
|
481
|
+
end
|
421
482
|
end
|
422
483
|
|
423
484
|
describe ":thousands_separator and :decimal_mark option" do
|
424
|
-
specify "(:
|
485
|
+
specify "(thousands_separator: a thousands_separator string, decimal_mark: a decimal_mark string) works as documented" do
|
425
486
|
expect(Money.new(123_456_789, "USD").format(thousands_separator: ".", decimal_mark: ",")).to eq("$1.234.567,89")
|
426
487
|
expect(Money.new(987_654_321, "USD").format(thousands_separator: " ", decimal_mark: ".")).to eq("$9 876 543.21")
|
427
488
|
end
|
428
489
|
end
|
429
490
|
|
430
491
|
describe ":html option" do
|
431
|
-
specify "(:
|
432
|
-
string = Money.ca_dollar(570).format(:
|
492
|
+
specify "(html: true) works as documented" do
|
493
|
+
string = Money.ca_dollar(570).format(html: true, with_currency: true)
|
433
494
|
expect(string).to eq "$5.70 <span class=\"currency\">CAD</span>"
|
434
495
|
end
|
435
496
|
|
436
497
|
specify "should fallback to symbol if entity is not available" do
|
437
|
-
string = Money.new(570, 'DKK').format(:
|
438
|
-
expect(string).to eq "5,70 kr"
|
498
|
+
string = Money.new(570, 'DKK').format(html: true)
|
499
|
+
expect(string).to eq "5,70 kr."
|
439
500
|
end
|
440
501
|
end
|
441
502
|
|
442
503
|
describe ":html_wrap_symbol option" do
|
443
|
-
specify "(:
|
444
|
-
string = Money.ca_dollar(570).format(:
|
504
|
+
specify "(html_wrap_symbol: true) works as documented" do
|
505
|
+
string = Money.ca_dollar(570).format(html_wrap_symbol: true)
|
445
506
|
expect(string).to eq "<span class=\"currency_symbol\">$</span>5.70"
|
446
507
|
end
|
447
508
|
end
|
448
509
|
|
510
|
+
describe ":html_wrap option" do
|
511
|
+
specify "(html_wrap: true) works as documented" do
|
512
|
+
string = Money.ca_dollar(570).format(html_wrap: true)
|
513
|
+
expect(string).to eq "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span>"
|
514
|
+
end
|
515
|
+
|
516
|
+
specify "(html_wrap: true, with_currency: true)" do
|
517
|
+
string = Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
|
518
|
+
expect(string).to eq "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
|
519
|
+
end
|
520
|
+
|
521
|
+
specify "should fallback to symbol if entity is not available" do
|
522
|
+
string = Money.new(570, 'DKK').format(html_wrap: true)
|
523
|
+
expect(string).to eq "<span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">,</span><span class=\"money-decimal\">70</span> <span class=\"money-currency-symbol\">kr.</span>"
|
524
|
+
end
|
525
|
+
end
|
526
|
+
|
449
527
|
describe ":symbol_position option" do
|
450
528
|
it "inserts currency symbol before the amount when set to :before" do
|
451
|
-
expect(Money.euro(1_234_567_12).format(:
|
529
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :before)).to eq "€1.234.567,12"
|
452
530
|
end
|
453
531
|
|
454
532
|
it "inserts currency symbol after the amount when set to :after" do
|
455
|
-
expect(Money.us_dollar(1_000_000_000_12).format(:
|
533
|
+
expect(Money.us_dollar(1_000_000_000_12).format(symbol_position: :after)).to eq "1,000,000,000.12 $"
|
456
534
|
end
|
457
535
|
|
458
536
|
it "raises an ArgumentError when passed an invalid option" do
|
459
|
-
expect{Money.euro(0).format(:
|
537
|
+
expect{Money.euro(0).format(symbol_position: :befor)}.to raise_error(ArgumentError)
|
460
538
|
end
|
461
539
|
end
|
462
540
|
|
463
541
|
describe ":sign_before_symbol option" do
|
464
|
-
specify "(:
|
465
|
-
expect(Money.us_dollar(-100000).format(:
|
542
|
+
specify "(sign_before_symbol: true) works as documented" do
|
543
|
+
expect(Money.us_dollar(-100000).format(sign_before_symbol: true)).to eq "-$1,000.00"
|
466
544
|
end
|
467
545
|
|
468
|
-
specify "(:
|
469
|
-
expect(Money.us_dollar(-100000).format(:
|
470
|
-
expect(Money.us_dollar(-100000).format(:
|
546
|
+
specify "(sign_before_symbol: false) works as documented" do
|
547
|
+
expect(Money.us_dollar(-100000).format(sign_before_symbol: false)).to eq "$-1,000.00"
|
548
|
+
expect(Money.us_dollar(-100000).format(sign_before_symbol: nil)).to eq "$-1,000.00"
|
471
549
|
end
|
472
550
|
end
|
473
551
|
|
474
552
|
describe ":symbol_before_without_space option" do
|
475
553
|
it "does not insert space between currency symbol and amount when set to true" do
|
476
|
-
expect(Money.euro(1_234_567_12).format(:
|
554
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :before, symbol_before_without_space: true)).to eq "€1.234.567,12"
|
477
555
|
end
|
478
556
|
|
479
557
|
it "inserts space between currency symbol and amount when set to false" do
|
480
|
-
expect(Money.euro(1_234_567_12).format(:
|
558
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :before, symbol_before_without_space: false)).to eq "€ 1.234.567,12"
|
481
559
|
end
|
482
560
|
|
483
561
|
it "defaults to true" do
|
484
|
-
expect(Money.euro(1_234_567_12).format(:
|
562
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :before)).to eq "€1.234.567,12"
|
485
563
|
end
|
486
564
|
end
|
487
565
|
|
488
566
|
describe ":symbol_after_without_space option" do
|
489
567
|
it "does not insert space between amount and currency symbol when set to true" do
|
490
|
-
expect(Money.euro(1_234_567_12).format(:
|
568
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :after, symbol_after_without_space: true)).to eq "1.234.567,12€"
|
491
569
|
end
|
492
570
|
|
493
571
|
it "inserts space between amount and currency symbol when set to false" do
|
494
|
-
expect(Money.euro(1_234_567_12).format(:
|
572
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :after, symbol_after_without_space: false)).to eq "1.234.567,12 €"
|
495
573
|
end
|
496
574
|
|
497
575
|
it "defaults to false" do
|
498
|
-
expect(Money.euro(1_234_567_12).format(:
|
576
|
+
expect(Money.euro(1_234_567_12).format(symbol_position: :after)).to eq "1.234.567,12 €"
|
499
577
|
end
|
500
578
|
end
|
501
579
|
|
502
580
|
describe ":sign_positive option" do
|
503
|
-
specify "(:
|
504
|
-
expect(Money.us_dollar( 0).format(:
|
505
|
-
expect(Money.us_dollar( 100000).format(:
|
506
|
-
expect(Money.us_dollar(-100000).format(:
|
581
|
+
specify "(sign_positive: true, sign_before_symbol: true) works as documented" do
|
582
|
+
expect(Money.us_dollar( 0).format(sign_positive: true, sign_before_symbol: true)).to eq "$0.00"
|
583
|
+
expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: true)).to eq "+$1,000.00"
|
584
|
+
expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: true)).to eq "-$1,000.00"
|
507
585
|
end
|
508
586
|
|
509
|
-
specify "(:
|
510
|
-
expect(Money.us_dollar( 0).format(:
|
511
|
-
expect(Money.us_dollar( 100000).format(:
|
512
|
-
expect(Money.us_dollar( 100000).format(:
|
513
|
-
expect(Money.us_dollar(-100000).format(:
|
514
|
-
expect(Money.us_dollar(-100000).format(:
|
587
|
+
specify "(sign_positive: true, sign_before_symbol: false) works as documented" do
|
588
|
+
expect(Money.us_dollar( 0).format(sign_positive: true, sign_before_symbol: false)).to eq "$0.00"
|
589
|
+
expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: false)).to eq "$+1,000.00"
|
590
|
+
expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: nil)).to eq "$+1,000.00"
|
591
|
+
expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: false)).to eq "$-1,000.00"
|
592
|
+
expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: nil)).to eq "$-1,000.00"
|
515
593
|
end
|
516
594
|
|
517
|
-
specify "(:
|
518
|
-
expect(Money.us_dollar( 100000).format(:
|
519
|
-
expect(Money.us_dollar(-100000).format(:
|
595
|
+
specify "(sign_positive: false, sign_before_symbol: true) works as documented" do
|
596
|
+
expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: true)).to eq "$1,000.00"
|
597
|
+
expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: true)).to eq "-$1,000.00"
|
520
598
|
end
|
521
599
|
|
522
|
-
specify "(:
|
523
|
-
expect(Money.us_dollar( 100000).format(:
|
524
|
-
expect(Money.us_dollar( 100000).format(:
|
525
|
-
expect(Money.us_dollar(-100000).format(:
|
526
|
-
expect(Money.us_dollar(-100000).format(:
|
600
|
+
specify "(sign_positive: false, sign_before_symbol: false) works as documented" do
|
601
|
+
expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: false)).to eq "$1,000.00"
|
602
|
+
expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: nil)).to eq "$1,000.00"
|
603
|
+
expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: false)).to eq "$-1,000.00"
|
604
|
+
expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: nil)).to eq "$-1,000.00"
|
527
605
|
end
|
528
606
|
end
|
529
607
|
|
530
608
|
describe ":rounded_infinite_precision option", :infinite_precision do
|
531
609
|
it "does round fractional when set to true" do
|
532
|
-
expect(Money.new(BigDecimal
|
533
|
-
expect(Money.new(BigDecimal
|
534
|
-
expect(Money.new(BigDecimal
|
535
|
-
expect(Money.new(BigDecimal
|
536
|
-
expect(Money.new(BigDecimal
|
537
|
-
expect(Money.new(BigDecimal
|
538
|
-
expect(Money.new(BigDecimal
|
610
|
+
expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: true)).to eq "$0.12"
|
611
|
+
expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: true)).to eq "$0.13"
|
612
|
+
expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0.123"
|
613
|
+
expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0.124"
|
614
|
+
expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: true)).to eq "$1.00"
|
615
|
+
expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: true)).to eq "$1.10"
|
616
|
+
expect(Money.new(BigDecimal('1.7'), "MGA").format(rounded_infinite_precision: true)).to eq "Ar0.4"
|
539
617
|
end
|
540
618
|
|
541
619
|
it "does not round fractional when set to false" do
|
542
|
-
expect(Money.new(BigDecimal
|
543
|
-
expect(Money.new(BigDecimal
|
544
|
-
expect(Money.new(BigDecimal
|
545
|
-
expect(Money.new(BigDecimal
|
546
|
-
expect(Money.new(BigDecimal
|
547
|
-
expect(Money.new(BigDecimal
|
548
|
-
expect(Money.new(BigDecimal
|
620
|
+
expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: false)).to eq "$0.121"
|
621
|
+
expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: false)).to eq "$0.125"
|
622
|
+
expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: false)).to eq "ب.د0.1231"
|
623
|
+
expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: false)).to eq "ب.د0.1235"
|
624
|
+
expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: false)).to eq "$1.001"
|
625
|
+
expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: false)).to eq "$1.095"
|
626
|
+
expect(Money.new(BigDecimal('1.7'), "MGA").format(rounded_infinite_precision: false)).to eq "Ar0.34"
|
549
627
|
end
|
550
628
|
|
551
629
|
describe "with i18n = false" do
|
@@ -558,13 +636,13 @@ describe Money, "formatting" do
|
|
558
636
|
end
|
559
637
|
|
560
638
|
it 'does round fractional when set to true' do
|
561
|
-
expect(Money.new(BigDecimal
|
562
|
-
expect(Money.new(BigDecimal
|
563
|
-
expect(Money.new(BigDecimal
|
564
|
-
expect(Money.new(BigDecimal
|
639
|
+
expect(Money.new(BigDecimal('12.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€0,12"
|
640
|
+
expect(Money.new(BigDecimal('12.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€0,13"
|
641
|
+
expect(Money.new(BigDecimal('100.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€1,00"
|
642
|
+
expect(Money.new(BigDecimal('109.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€1,10"
|
565
643
|
|
566
|
-
expect(Money.new(BigDecimal
|
567
|
-
expect(Money.new(BigDecimal
|
644
|
+
expect(Money.new(BigDecimal('100012.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€1.000,12"
|
645
|
+
expect(Money.new(BigDecimal('100012.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€1.000,13"
|
568
646
|
end
|
569
647
|
end
|
570
648
|
|
@@ -575,7 +653,7 @@ describe Money, "formatting" do
|
|
575
653
|
I18n.locale = :de
|
576
654
|
I18n.backend.store_translations(
|
577
655
|
:de,
|
578
|
-
:
|
656
|
+
number: { currency: { format: { delimiter: ".", separator: "," } } }
|
579
657
|
)
|
580
658
|
end
|
581
659
|
|
@@ -585,45 +663,81 @@ describe Money, "formatting" do
|
|
585
663
|
end
|
586
664
|
|
587
665
|
it 'does round fractional when set to true' do
|
588
|
-
expect(Money.new(BigDecimal
|
589
|
-
expect(Money.new(BigDecimal
|
590
|
-
expect(Money.new(BigDecimal
|
591
|
-
expect(Money.new(BigDecimal
|
592
|
-
expect(Money.new(BigDecimal
|
593
|
-
expect(Money.new(BigDecimal
|
594
|
-
expect(Money.new(BigDecimal
|
666
|
+
expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: true)).to eq "$0,12"
|
667
|
+
expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: true)).to eq "$0,13"
|
668
|
+
expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0,123"
|
669
|
+
expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0,124"
|
670
|
+
expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: true)).to eq "$1,00"
|
671
|
+
expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: true)).to eq "$1,10"
|
672
|
+
expect(Money.new(BigDecimal('1'), "MGA").format(rounded_infinite_precision: true)).to eq "Ar0,2"
|
595
673
|
end
|
596
674
|
end
|
597
675
|
end
|
598
676
|
|
677
|
+
describe ':format option' do
|
678
|
+
let(:money) { Money.new(99_99, 'USD') }
|
679
|
+
|
680
|
+
it 'uses provided format as a template' do
|
681
|
+
expect(money.format(format: '%n')).to eq('99.99')
|
682
|
+
expect(money.format(format: '%u')).to eq('$')
|
683
|
+
expect(money.format(format: '%u%n')).to eq('$99.99')
|
684
|
+
expect(money.format(format: '%n%u')).to eq('99.99$')
|
685
|
+
expect(money.format(format: '%u %n')).to eq('$ 99.99')
|
686
|
+
expect(money.format(format: 'Your balance is: %u%n')).to eq('Your balance is: $99.99')
|
687
|
+
end
|
688
|
+
|
689
|
+
it 'ignores :symbol_position in favour of format' do
|
690
|
+
expect(money.format(format: '%u%n', symbol_position: :after)).to eq('$99.99')
|
691
|
+
expect(money.format(format: '%n%u', symbol_position: :before)).to eq('99.99$')
|
692
|
+
end
|
693
|
+
|
694
|
+
it 'ignores :symbol_before_without_space in favour of format' do
|
695
|
+
expect(money.format(format: '%u %n', symbol_position: :before, symbol_before_without_space: true)).to eq('$ 99.99')
|
696
|
+
expect(money.format(format: '%u%n', symbol_position: :before, symbol_before_without_space: false)).to eq('$99.99')
|
697
|
+
end
|
698
|
+
|
699
|
+
it 'ignores :symbol_after_without_space in favour of format' do
|
700
|
+
expect(money.format(format: '%n %u', symbol_position: :after, symbol_after_without_space: true)).to eq('99.99 $')
|
701
|
+
expect(money.format(format: '%n%u', symbol_position: :after, symbol_after_without_space: false)).to eq('99.99$')
|
702
|
+
end
|
703
|
+
|
704
|
+
it 'works with sign' do
|
705
|
+
money = Money.new(-99_99, 'USD')
|
706
|
+
|
707
|
+
expect(money.format(format: '%n%u', sign_before_symbol: false)).to eq('-99.99$')
|
708
|
+
expect(money.format(format: '%u%n', sign_before_symbol: false)).to eq('$-99.99')
|
709
|
+
expect(money.format(format: '%u%n', sign_before_symbol: true)).to eq('-$99.99')
|
710
|
+
end
|
711
|
+
end
|
712
|
+
|
599
713
|
context "when the monetary value is 0" do
|
600
714
|
let(:money) { Money.us_dollar(0) }
|
601
715
|
|
602
716
|
it "returns 'free' when :display_free is true" do
|
603
|
-
expect(money.format(:
|
717
|
+
expect(money.format(display_free: true)).to eq 'free'
|
604
718
|
end
|
605
719
|
|
606
720
|
it "returns '$0.00' when :display_free is false or not given" do
|
607
721
|
expect(money.format).to eq '$0.00'
|
608
|
-
expect(money.format(:
|
609
|
-
expect(money.format(:
|
722
|
+
expect(money.format(display_free: false)).to eq '$0.00'
|
723
|
+
expect(money.format(display_free: nil)).to eq '$0.00'
|
610
724
|
end
|
611
725
|
|
612
726
|
it "returns the value specified by :display_free if it's a string-like object" do
|
613
|
-
expect(money.format(:
|
727
|
+
expect(money.format(display_free: 'gratis')).to eq 'gratis'
|
614
728
|
end
|
615
729
|
end
|
616
730
|
end
|
617
731
|
|
618
732
|
context "custom currencies with 4 decimal places" do
|
619
733
|
before :each do
|
620
|
-
Money::Currency.register(JSON.parse(BAR, :
|
621
|
-
Money::Currency.register(JSON.parse(EU4, :
|
734
|
+
Money::Currency.register(JSON.parse(BAR, symbolize_names: true))
|
735
|
+
Money::Currency.register(JSON.parse(EU4, symbolize_names: true))
|
622
736
|
end
|
623
737
|
|
624
738
|
after :each do
|
625
|
-
Money::Currency.unregister(JSON.parse(BAR, :
|
626
|
-
Money::Currency.unregister(JSON.parse(EU4, :
|
739
|
+
Money::Currency.unregister(JSON.parse(BAR, symbolize_names: true))
|
740
|
+
Money::Currency.unregister(JSON.parse(EU4, symbolize_names: true))
|
627
741
|
end
|
628
742
|
|
629
743
|
it "respects custom subunit to unit, decimal and thousands separator" do
|
@@ -662,25 +776,46 @@ describe Money, "formatting" do
|
|
662
776
|
it "returns ambiguous signs when disambiguate is not set" do
|
663
777
|
expect(Money.new(1999_98, "USD").format).to eq("$1,999.98")
|
664
778
|
expect(Money.new(1999_98, "CAD").format).to eq("$1,999.98")
|
665
|
-
expect(Money.new(1999_98, "DKK").format).to eq("1.999,98 kr")
|
779
|
+
expect(Money.new(1999_98, "DKK").format).to eq("1.999,98 kr.")
|
666
780
|
expect(Money.new(1999_98, "NOK").format).to eq("1.999,98 kr")
|
667
781
|
expect(Money.new(1999_98, "SEK").format).to eq("1 999,98 kr")
|
782
|
+
expect(Money.new(1999_98, "BCH").format).to eq("0.00199998 ₿")
|
668
783
|
end
|
669
784
|
|
670
785
|
it "returns ambiguous signs when disambiguate is false" do
|
671
786
|
expect(Money.new(1999_98, "USD").format(disambiguate: false)).to eq("$1,999.98")
|
672
787
|
expect(Money.new(1999_98, "CAD").format(disambiguate: false)).to eq("$1,999.98")
|
673
|
-
expect(Money.new(1999_98, "DKK").format(disambiguate: false)).to eq("1.999,98 kr")
|
788
|
+
expect(Money.new(1999_98, "DKK").format(disambiguate: false)).to eq("1.999,98 kr.")
|
674
789
|
expect(Money.new(1999_98, "NOK").format(disambiguate: false)).to eq("1.999,98 kr")
|
675
790
|
expect(Money.new(1999_98, "SEK").format(disambiguate: false)).to eq("1 999,98 kr")
|
791
|
+
expect(Money.new(1999_98, "BCH").format(disambiguate: false)).to eq("0.00199998 ₿")
|
676
792
|
end
|
677
793
|
|
678
794
|
it "returns disambiguate signs when disambiguate: true" do
|
679
|
-
expect(Money.new(1999_98, "USD").format(disambiguate: true)).to eq("$1,999.98")
|
795
|
+
expect(Money.new(1999_98, "USD").format(disambiguate: true)).to eq("US$1,999.98")
|
680
796
|
expect(Money.new(1999_98, "CAD").format(disambiguate: true)).to eq("C$1,999.98")
|
681
797
|
expect(Money.new(1999_98, "DKK").format(disambiguate: true)).to eq("1.999,98 DKK")
|
682
798
|
expect(Money.new(1999_98, "NOK").format(disambiguate: true)).to eq("1.999,98 NOK")
|
683
799
|
expect(Money.new(1999_98, "SEK").format(disambiguate: true)).to eq("1 999,98 SEK")
|
800
|
+
expect(Money.new(1999_98, "BCH").format(disambiguate: true)).to eq("0.00199998 ₿CH")
|
801
|
+
end
|
802
|
+
|
803
|
+
it "returns disambiguate signs when disambiguate: true and symbol: true" do
|
804
|
+
expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)).to eq("US$1,999.98")
|
805
|
+
expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: true)).to eq("C$1,999.98")
|
806
|
+
expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: true)).to eq("1.999,98 DKK")
|
807
|
+
expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: true)).to eq("1.999,98 NOK")
|
808
|
+
expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: true)).to eq("1 999,98 SEK")
|
809
|
+
expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: true)).to eq("0.00199998 ₿CH")
|
810
|
+
end
|
811
|
+
|
812
|
+
it "returns no signs when disambiguate: true and symbol: false" do
|
813
|
+
expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: false)).to eq("1,999.98")
|
814
|
+
expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: false)).to eq("1,999.98")
|
815
|
+
expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: false)).to eq("1.999,98")
|
816
|
+
expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: false)).to eq("1.999,98")
|
817
|
+
expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: false)).to eq("1 999,98")
|
818
|
+
expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: false)).to eq("0.00199998")
|
684
819
|
end
|
685
820
|
|
686
821
|
it "should never return an ambiguous format with disambiguate: true" do
|
@@ -696,18 +831,24 @@ describe Money, "formatting" do
|
|
696
831
|
end
|
697
832
|
|
698
833
|
describe ":drop_trailing_zeros option" do
|
699
|
-
specify "(:
|
700
|
-
expect(Money.new(89000, "BTC").format(:
|
701
|
-
expect(Money.new(
|
702
|
-
expect(Money.new(
|
703
|
-
expect(Money.new(
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
expect(Money.new(
|
834
|
+
specify "(drop_trailing_zeros: true) works as documented" do
|
835
|
+
expect(Money.new(89000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089"
|
836
|
+
expect(Money.new(89000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089"
|
837
|
+
expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089"
|
838
|
+
expect(Money.new(100089000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089"
|
839
|
+
expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1"
|
840
|
+
expect(Money.new(100000000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "1"
|
841
|
+
expect(Money.new(110, "AUD").format(drop_trailing_zeros: true, symbol: false)).to eq "1.1"
|
842
|
+
end
|
843
|
+
|
844
|
+
specify "(drop_trailing_zeros: false) works as documented" do
|
845
|
+
expect(Money.new(89000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000"
|
846
|
+
expect(Money.new(89000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000"
|
847
|
+
expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000"
|
848
|
+
expect(Money.new(100089000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000"
|
849
|
+
expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000"
|
850
|
+
expect(Money.new(100000000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000"
|
851
|
+
expect(Money.new(110, "AUD").format(drop_trailing_zeros: false, symbol: false)).to eq "1.10"
|
711
852
|
end
|
712
853
|
end
|
713
854
|
end
|