money 6.13.2 → 6.13.3

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