money 6.5.1 → 6.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +209 -5
  3. data/LICENSE +18 -16
  4. data/README.md +321 -70
  5. data/config/currency_backwards_compatible.json +65 -0
  6. data/config/currency_iso.json +280 -94
  7. data/config/currency_non_iso.json +101 -3
  8. data/lib/money/bank/base.rb +1 -3
  9. data/lib/money/bank/variable_exchange.rb +88 -96
  10. data/lib/money/currency/heuristics.rb +1 -143
  11. data/lib/money/currency/loader.rb +15 -13
  12. data/lib/money/currency.rb +98 -81
  13. data/lib/money/locale_backend/base.rb +7 -0
  14. data/lib/money/locale_backend/currency.rb +11 -0
  15. data/lib/money/locale_backend/errors.rb +6 -0
  16. data/lib/money/locale_backend/i18n.rb +25 -0
  17. data/lib/money/locale_backend/legacy.rb +28 -0
  18. data/lib/money/money/allocation.rb +46 -0
  19. data/lib/money/money/arithmetic.rb +97 -52
  20. data/lib/money/money/constructors.rb +5 -6
  21. data/lib/money/money/formatter.rb +399 -0
  22. data/lib/money/money/formatting_rules.rb +142 -0
  23. data/lib/money/money/locale_backend.rb +22 -0
  24. data/lib/money/money.rb +268 -194
  25. data/lib/money/rates_store/memory.rb +120 -0
  26. data/lib/money/version.rb +1 -1
  27. data/money.gemspec +15 -20
  28. metadata +36 -59
  29. data/.coveralls.yml +0 -1
  30. data/.gitignore +0 -22
  31. data/.travis.yml +0 -13
  32. data/AUTHORS +0 -116
  33. data/CONTRIBUTING.md +0 -17
  34. data/Gemfile +0 -7
  35. data/Rakefile +0 -17
  36. data/lib/money/money/formatting.rb +0 -386
  37. data/spec/bank/base_spec.rb +0 -77
  38. data/spec/bank/single_currency_spec.rb +0 -11
  39. data/spec/bank/variable_exchange_spec.rb +0 -275
  40. data/spec/currency/heuristics_spec.rb +0 -84
  41. data/spec/currency_spec.rb +0 -321
  42. data/spec/money/arithmetic_spec.rb +0 -568
  43. data/spec/money/constructors_spec.rb +0 -75
  44. data/spec/money/formatting_spec.rb +0 -667
  45. data/spec/money_spec.rb +0 -745
  46. data/spec/spec_helper.rb +0 -23
@@ -1,667 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "spec_helper"
4
-
5
- describe Money, "formatting" do
6
-
7
- 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 }'
8
- 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}'
9
- 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 }'
10
-
11
- context "without i18n" do
12
- subject(:money) { Money.empty("USD") }
13
-
14
- it "should use ',' as the thousands separator" do
15
- expect(money.thousands_separator).to eq ','
16
- end
17
-
18
- it "should use '.' as the decimal mark" do
19
- expect(money.decimal_mark).to eq '.'
20
- end
21
- end
22
-
23
- context "with i18n but use_i18n = false" do
24
- before :each do
25
- reset_i18n
26
- I18n.locale = :de
27
- I18n.backend.store_translations(
28
- :de,
29
- :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } }
30
- )
31
- Money.use_i18n = false
32
- end
33
-
34
- after :each do
35
- reset_i18n
36
- I18n.locale = :en
37
- Money.use_i18n = true
38
- end
39
-
40
- subject(:money) { Money.empty("USD") }
41
-
42
- it "should use ',' as the thousands separator" do
43
- expect(money.thousands_separator).to eq ','
44
- end
45
-
46
- it "should use '.' as the decimal mark" do
47
- expect(money.decimal_mark).to eq '.'
48
- end
49
- end
50
-
51
- context "with i18n" do
52
- after :each do
53
- reset_i18n
54
- I18n.locale = :en
55
- end
56
-
57
- context "with number.format.*" do
58
- before :each do
59
- reset_i18n
60
- I18n.locale = :de
61
- I18n.backend.store_translations(
62
- :de,
63
- :number => { :format => { :delimiter => ".", :separator => "," } }
64
- )
65
- end
66
-
67
- subject(:money) { Money.empty("USD") }
68
-
69
- it "should use '.' as the thousands separator" do
70
- expect(money.thousands_separator).to eq '.'
71
- end
72
-
73
- it "should use ',' as the decimal mark" do
74
- expect(money.decimal_mark).to eq ','
75
- end
76
- end
77
-
78
- context "with number.currency.format.*" do
79
- before :each do
80
- reset_i18n
81
- I18n.locale = :de
82
- I18n.backend.store_translations(
83
- :de,
84
- :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } }
85
- )
86
- end
87
-
88
- subject(:money) { Money.empty("USD") }
89
-
90
- it "should use '.' as the thousands separator" do
91
- expect(money.thousands_separator).to eq '.'
92
- end
93
-
94
- it "should use ',' as the decimal mark" do
95
- expect(money.decimal_mark).to eq ','
96
- end
97
- end
98
- end
99
-
100
- describe "#format" do
101
- context "Locale :ja" do
102
- before { @_locale = I18n.locale; I18n.locale = :ja }
103
-
104
- it "formats Japanese currency in Japanese properly" do
105
- money = Money.new(1000, "JPY")
106
- expect(money.format).to eq "1,000円"
107
- expect(money.format(:symbol => false)).to eq "1,000"
108
- end
109
-
110
- after { I18n.locale = @_locale }
111
- end
112
-
113
- it "returns the monetary value as a string" do
114
- expect(Money.ca_dollar(100).format).to eq "$1.00"
115
- expect(Money.new(40008).format).to eq "$400.08"
116
- end
117
-
118
- it "respects :subunit_to_unit currency property" do
119
- expect(Money.new(10_00, "BHD").format).to eq "ب.د1.000"
120
- end
121
-
122
- it "does not display a decimal when :subunit_to_unit is 1" do
123
- expect(Money.new(10_00, "VUV").format).to eq "Vt1,000"
124
- end
125
-
126
- it "respects the thousands_separator and decimal_mark defaults" do
127
- one_thousand = Proc.new do |currency|
128
- Money.new(1000_00, currency).format
129
- end
130
-
131
- # Pounds
132
- expect(one_thousand["GBP"]).to eq "£1,000.00"
133
-
134
- # Dollars
135
- expect(one_thousand["USD"]).to eq "$1,000.00"
136
- expect(one_thousand["CAD"]).to eq "$1,000.00"
137
- expect(one_thousand["AUD"]).to eq "$1,000.00"
138
- expect(one_thousand["NZD"]).to eq "$1,000.00"
139
- expect(one_thousand["ZWD"]).to eq "$1,000.00"
140
-
141
- # Yen
142
- expect(one_thousand["JPY"]).to eq "¥100,000"
143
- expect(one_thousand["CNY"]).to eq "¥1,000.00"
144
-
145
- # Euro
146
- expect(one_thousand["EUR"]).to eq "€1.000,00"
147
-
148
- # Rupees
149
- expect(one_thousand["INR"]).to eq "₹1,000.00"
150
- expect(one_thousand["NPR"]).to eq "₨1,000.00"
151
- expect(one_thousand["SCR"]).to eq "1,000.00 ₨"
152
- expect(one_thousand["LKR"]).to eq "1,000.00 ₨"
153
-
154
- # Brazilian Real
155
- expect(one_thousand["BRL"]).to eq "R$1.000,00"
156
-
157
- # Other
158
- expect(one_thousand["SEK"]).to eq "1 000,00 kr"
159
- expect(one_thousand["GHC"]).to eq "₵1,000.00"
160
- end
161
-
162
- it "inserts commas into the result if the amount is sufficiently large" do
163
- expect(Money.us_dollar(1_000_000_000_12).format).to eq "$1,000,000,000.12"
164
- expect(Money.us_dollar(1_000_000_000_12).format(:no_cents => true)).to eq "$1,000,000,000"
165
- end
166
-
167
- it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
168
- expect(Money.euro(1_234_567_12).format).to eq "€1.234.567,12"
169
- expect(Money.euro(1_234_567_12).format(:no_cents => true)).to eq "€1.234.567"
170
- end
171
-
172
- context 'when default_formatting_rules defines (display_free: true)' do
173
- before do
174
- Money.default_formatting_rules = { :display_free => "you won't pay a thing" }
175
- end
176
-
177
- after do
178
- Money.default_formatting_rules = nil
179
- end
180
-
181
- context 'with no rule provided' do
182
- it 'acknowledges default rule' do
183
- expect(Money.new(0, 'USD').format).to eq "you won't pay a thing"
184
- end
185
- end
186
-
187
- context 'with rule (display_free: false) provided' do
188
- it 'acknowledges provided rule' do
189
- expect(Money.new(0, 'USD').format(:display_free => false)).to eq '$0.00'
190
- end
191
- end
192
- end
193
-
194
- context 'when default_formatting_rules is not defined' do
195
- before do
196
- Money.default_formatting_rules = nil
197
- end
198
-
199
- context 'acknowledges provided rule' do
200
- it 'acknowledges provided rule' do
201
- expect(Money.new(100, 'USD').format(:with_currency => true)).to eq '$1.00 USD'
202
- end
203
- end
204
- end
205
-
206
- describe ":with_currency option" do
207
- specify "(:with_currency option => true) works as documented" do
208
- expect(Money.ca_dollar(100).format(:with_currency => true)).to eq "$1.00 CAD"
209
- expect(Money.us_dollar(85).format(:with_currency => true)).to eq "$0.85 USD"
210
- end
211
- end
212
-
213
- describe ":no_cents option" do
214
- specify "(:with_currency option => true) works as documented" do
215
- expect(Money.ca_dollar(100).format(:no_cents => true)).to eq "$1"
216
- expect(Money.ca_dollar(599).format(:no_cents => true)).to eq "$5"
217
- expect(Money.ca_dollar(570).format(:no_cents => true, :with_currency => true)).to eq "$5 CAD"
218
- expect(Money.ca_dollar(39000).format(:no_cents => true)).to eq "$390"
219
- end
220
-
221
- it "respects :subunit_to_unit currency property" do
222
- expect(Money.new(10_00, "BHD").format(:no_cents => true)).to eq "ب.د1"
223
- end
224
-
225
- it "inserts thousand separators if symbol contains decimal mark and no_cents is true" do
226
- expect(Money.new(100000000, "AMD").format(:no_cents => true)).to eq "1,000,000 դր."
227
- expect(Money.new(100000000, "USD").format(:no_cents => true)).to eq "$1,000,000"
228
- expect(Money.new(100000000, "RUB").format(:no_cents => true)).to eq "1.000.000 ₽"
229
- end
230
-
231
- it "doesn't incorrectly format HTML" do
232
- money = ::Money.new(1999, "RUB")
233
- output = money.format(:html => true, :no_cents => true)
234
- expect(output).to eq "19 ₽"
235
- end
236
- end
237
-
238
- describe ":no_cents_if_whole option" do
239
- specify "(:no_cents_if_whole => true) works as documented" do
240
- expect(Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false)).to eq "10,000"
241
- expect(Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false)).to eq "10,034"
242
- expect(Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false)).to eq "2,000"
243
- expect(Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false)).to eq "2,006.4"
244
- expect(Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false)).to eq "10.000"
245
- expect(Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false)).to eq "10.034"
246
- expect(Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false)).to eq "100"
247
- expect(Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false)).to eq "100.34"
248
- expect(Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false)).to eq "10"
249
- expect(Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false)).to eq "10.034"
250
- end
251
-
252
- specify "(:no_cents_if_whole => false) works as documented" do
253
- expect(Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false)).to eq "10,000"
254
- expect(Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false)).to eq "10,034"
255
- expect(Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false)).to eq "2,000.0"
256
- expect(Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false)).to eq "2,006.4"
257
- expect(Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.000"
258
- expect(Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.034"
259
- expect(Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false)).to eq "100.00"
260
- expect(Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false)).to eq "100.34"
261
- expect(Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.000"
262
- expect(Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false)).to eq "10.034"
263
- end
264
- end
265
-
266
- describe ":symbol option" do
267
- specify "(:symbol => a symbol string) uses the given value as the money symbol" do
268
- expect(Money.new(100, "GBP").format(:symbol => "£")).to eq "£1.00"
269
- end
270
-
271
- specify "(:symbol => true) returns symbol based on the given currency code" do
272
- one = Proc.new do |currency|
273
- Money.new(100, currency).format(:symbol => true)
274
- end
275
-
276
- # Pounds
277
- expect(one["GBP"]).to eq "£1.00"
278
-
279
- # Dollars
280
- expect(one["USD"]).to eq "$1.00"
281
- expect(one["CAD"]).to eq "$1.00"
282
- expect(one["AUD"]).to eq "$1.00"
283
- expect(one["NZD"]).to eq "$1.00"
284
- expect(one["ZWD"]).to eq "$1.00"
285
-
286
- # Yen
287
- expect(one["JPY"]).to eq "¥100"
288
- expect(one["CNY"]).to eq "¥1.00"
289
-
290
- # Euro
291
- expect(one["EUR"]).to eq "€1,00"
292
-
293
- # Rupees
294
- expect(one["INR"]).to eq "₹1.00"
295
- expect(one["NPR"]).to eq "₨1.00"
296
- expect(one["SCR"]).to eq "1.00 ₨"
297
- expect(one["LKR"]).to eq "1.00 ₨"
298
-
299
- # Brazilian Real
300
- expect(one["BRL"]).to eq "R$1,00"
301
-
302
- # Other
303
- expect(one["SEK"]).to eq "1,00 kr"
304
- expect(one["GHC"]).to eq "₵1.00"
305
- end
306
-
307
- specify "(:symbol => true) returns $ when currency code is not recognized" do
308
- currency = Money::Currency.new("EUR")
309
- expect(currency).to receive(:symbol).and_return(nil)
310
- expect(Money.new(100, currency).format(:symbol => true)).to eq "¤1,00"
311
- end
312
-
313
- specify "(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
314
- expect(Money.new(100, "GBP").format(:symbol => true)).to eq "£1.00"
315
- expect(Money.new(100, "EUR").format(:symbol => true)).to eq "€1,00"
316
- expect(Money.new(100, "SEK").format(:symbol => true)).to eq "1,00 kr"
317
- end
318
-
319
- specify "(:symbol => "", nil or false) returns the amount without a symbol" do
320
- money = Money.new(100, "GBP")
321
- expect(money.format(:symbol => "")).to eq "1.00"
322
- expect(money.format(:symbol => nil)).to eq "1.00"
323
- expect(money.format(:symbol => false)).to eq "1.00"
324
-
325
- money = Money.new(100, "JPY")
326
- expect(money.format(:symbol => false)).to eq "100"
327
- end
328
-
329
- it "defaults :symbol to true" do
330
- money = Money.new(100)
331
- expect(money.format).to eq "$1.00"
332
-
333
- money = Money.new(100, "GBP")
334
- expect(money.format).to eq "£1.00"
335
-
336
- money = Money.new(100, "EUR")
337
- expect(money.format).to eq "€1,00"
338
- end
339
-
340
- specify "(:symbol => false) returns a signed amount without a symbol" do
341
- money = Money.new(-100, "EUR")
342
- expect(money.format(:symbol => false)).to eq "-1,00"
343
-
344
- money = Money.new(100, "EUR")
345
- expect(money.format(:symbol => false,
346
- :sign_positive => true)).to eq "+1,00"
347
- end
348
- end
349
-
350
- describe ":decimal_mark option" do
351
- specify "(:decimal_mark => a decimal_mark string) works as documented" do
352
- expect(Money.us_dollar(100).format(:decimal_mark => ",")).to eq "$1,00"
353
- end
354
-
355
- it "defaults to '.' if currency isn't recognized" do
356
- expect(Money.new(100, "ZWD").format).to eq "$1.00"
357
- end
358
- end
359
-
360
- describe ":separator option" do
361
- specify "(:separator => a separator string) works as documented" do
362
- expect(Money.us_dollar(100).format(:separator => ",")).to eq "$1,00"
363
- end
364
- end
365
-
366
- describe ":south_asian_number_formatting delimiter" do
367
- before(:each) do
368
- Money::Currency.register(JSON.parse(INDIAN_BAR, :symbolize_names => true))
369
- end
370
-
371
- after(:each) do
372
- Money::Currency.unregister(JSON.parse(INDIAN_BAR, :symbolize_names => true))
373
- end
374
-
375
- specify "(:south_asian_number_formatting => true) works as documented" do
376
- expect(Money.new(10000000, 'INR').format(:south_asian_number_formatting => true, :symbol => false)).to eq "1,00,000.00"
377
- expect(Money.new(1000000000, 'INDIAN_BAR').format(:south_asian_number_formatting => true, :symbol => false)).to eq "1,00,000.0000"
378
- expect(Money.new(10000000).format(:south_asian_number_formatting => true)).to eq "$1,00,000.00"
379
- end
380
- end
381
-
382
- describe ":thousands_separator option" do
383
- specify "(:thousands_separator => a thousands_separator string) works as documented" do
384
- expect(Money.us_dollar(100000).format(:thousands_separator => ".")).to eq "$1.000.00"
385
- expect(Money.us_dollar(200000).format(:thousands_separator => "")).to eq "$2000.00"
386
- end
387
-
388
- specify "(:thousands_separator => false or nil) works as documented" do
389
- expect(Money.us_dollar(100000).format(:thousands_separator => false)).to eq "$1000.00"
390
- expect(Money.us_dollar(200000).format(:thousands_separator => nil)).to eq "$2000.00"
391
- end
392
-
393
- specify "(:delimiter => a delimiter string) works as documented" do
394
- expect(Money.us_dollar(100000).format(:delimiter => ".")).to eq "$1.000.00"
395
- expect(Money.us_dollar(200000).format(:delimiter => "")).to eq "$2000.00"
396
- end
397
-
398
- specify "(:delimiter => false or nil) works as documented" do
399
- expect(Money.us_dollar(100000).format(:delimiter => false)).to eq "$1000.00"
400
- expect(Money.us_dollar(200000).format(:delimiter => nil)).to eq "$2000.00"
401
- end
402
-
403
- it "defaults to ',' if currency isn't recognized" do
404
- expect(Money.new(100000, "ZWD").format).to eq "$1,000.00"
405
- end
406
- end
407
-
408
- describe ":thousands_separator and :decimal_mark option" do
409
- specify "(:thousands_separator => a thousands_separator string, :decimal_mark => a decimal_mark string) works as documented" do
410
- expect(Money.new(123_456_789, "USD").format(thousands_separator: ".", decimal_mark: ",")).to eq("$1.234.567,89")
411
- expect(Money.new(987_654_321, "USD").format(thousands_separator: " ", decimal_mark: ".")).to eq("$9 876 543.21")
412
- end
413
- end
414
-
415
- describe ":html option" do
416
- specify "(:html => true) works as documented" do
417
- string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
418
- expect(string).to eq "$5.70 <span class=\"currency\">CAD</span>"
419
- end
420
-
421
- specify "should fallback to symbol if entity is not available" do
422
- string = Money.new(570, 'DKK').format(:html => true)
423
- expect(string).to eq "5,70 kr"
424
- end
425
- end
426
-
427
- describe ":html_wrap_symbol option" do
428
- specify "(:html_wrap_symbol => true) works as documented" do
429
- string = Money.ca_dollar(570).format(:html_wrap_symbol => true)
430
- expect(string).to eq "<span class=\"currency_symbol\">$</span>5.70"
431
- end
432
- end
433
-
434
- describe ":symbol_position option" do
435
- it "inserts currency symbol before the amount when set to :before" do
436
- expect(Money.euro(1_234_567_12).format(:symbol_position => :before)).to eq "€1.234.567,12"
437
- end
438
-
439
- it "inserts currency symbol after the amount when set to :after" do
440
- expect(Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after)).to eq "1,000,000,000.12 $"
441
- end
442
-
443
- it "raises an ArgumentError when passed an invalid option" do
444
- expect{Money.euro(0).format(:symbol_position => :befor)}.to raise_error(ArgumentError)
445
- end
446
- end
447
-
448
- describe ":sign_before_symbol option" do
449
- specify "(:sign_before_symbol => true) works as documented" do
450
- expect(Money.us_dollar(-100000).format(:sign_before_symbol => true)).to eq "-$1,000.00"
451
- end
452
-
453
- specify "(:sign_before_symbol => false) works as documented" do
454
- expect(Money.us_dollar(-100000).format(:sign_before_symbol => false)).to eq "$-1,000.00"
455
- expect(Money.us_dollar(-100000).format(:sign_before_symbol => nil)).to eq "$-1,000.00"
456
- end
457
- end
458
-
459
- describe ":symbol_before_without_space option" do
460
- it "does not insert space between currency symbol and amount when set to true" do
461
- expect(Money.euro(1_234_567_12).format(:symbol_position => :before, :symbol_before_without_space => true)).to eq "€1.234.567,12"
462
- end
463
-
464
- it "inserts space between currency symbol and amount when set to false" do
465
- expect(Money.euro(1_234_567_12).format(:symbol_position => :before, :symbol_before_without_space => false)).to eq "€ 1.234.567,12"
466
- end
467
-
468
- it "defaults to true" do
469
- expect(Money.euro(1_234_567_12).format(:symbol_position => :before)).to eq "€1.234.567,12"
470
- end
471
- end
472
-
473
- describe ":symbol_after_without_space option" do
474
- it "does not insert space between amount and currency symbol when set to true" do
475
- expect(Money.euro(1_234_567_12).format(:symbol_position => :after, :symbol_after_without_space => true)).to eq "1.234.567,12€"
476
- end
477
-
478
- it "inserts space between amount and currency symbol when set to false" do
479
- expect(Money.euro(1_234_567_12).format(:symbol_position => :after, :symbol_after_without_space => false)).to eq "1.234.567,12 €"
480
- end
481
-
482
- it "defaults to false" do
483
- expect(Money.euro(1_234_567_12).format(:symbol_position => :after)).to eq "1.234.567,12 €"
484
- end
485
- end
486
-
487
- describe ":sign_positive option" do
488
- specify "(:sign_positive => true, :sign_before_symbol => true) works as documented" do
489
- expect(Money.us_dollar( 0).format(:sign_positive => true, :sign_before_symbol => true)).to eq "$0.00"
490
- expect(Money.us_dollar( 100000).format(:sign_positive => true, :sign_before_symbol => true)).to eq "+$1,000.00"
491
- expect(Money.us_dollar(-100000).format(:sign_positive => true, :sign_before_symbol => true)).to eq "-$1,000.00"
492
- end
493
-
494
- specify "(:sign_positive => true, :sign_before_symbol => false) works as documented" do
495
- expect(Money.us_dollar( 0).format(:sign_positive => true, :sign_before_symbol => false)).to eq "$0.00"
496
- expect(Money.us_dollar( 100000).format(:sign_positive => true, :sign_before_symbol => false)).to eq "$+1,000.00"
497
- expect(Money.us_dollar( 100000).format(:sign_positive => true, :sign_before_symbol => nil)).to eq "$+1,000.00"
498
- expect(Money.us_dollar(-100000).format(:sign_positive => true, :sign_before_symbol => false)).to eq "$-1,000.00"
499
- expect(Money.us_dollar(-100000).format(:sign_positive => true, :sign_before_symbol => nil)).to eq "$-1,000.00"
500
- end
501
-
502
- specify "(:sign_positive => false, :sign_before_symbol => true) works as documented" do
503
- expect(Money.us_dollar( 100000).format(:sign_positive => false, :sign_before_symbol => true)).to eq "$1,000.00"
504
- expect(Money.us_dollar(-100000).format(:sign_positive => false, :sign_before_symbol => true)).to eq "-$1,000.00"
505
- end
506
-
507
- specify "(:sign_positive => false, :sign_before_symbol => false) works as documented" do
508
- expect(Money.us_dollar( 100000).format(:sign_positive => false, :sign_before_symbol => false)).to eq "$1,000.00"
509
- expect(Money.us_dollar( 100000).format(:sign_positive => false, :sign_before_symbol => nil)).to eq "$1,000.00"
510
- expect(Money.us_dollar(-100000).format(:sign_positive => false, :sign_before_symbol => false)).to eq "$-1,000.00"
511
- expect(Money.us_dollar(-100000).format(:sign_positive => false, :sign_before_symbol => nil)).to eq "$-1,000.00"
512
- end
513
- end
514
-
515
- describe ":rounded_infinite_precision option" do
516
- before do
517
- Money.infinite_precision = true
518
- end
519
-
520
- after do
521
- Money.infinite_precision = false
522
- end
523
-
524
- it "does round fractional when set to true" do
525
- expect(Money.new(BigDecimal.new('12.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$0.12"
526
- expect(Money.new(BigDecimal.new('12.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$0.13"
527
- expect(Money.new(BigDecimal.new('123.1'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0.123"
528
- expect(Money.new(BigDecimal.new('123.5'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0.124"
529
- expect(Money.new(BigDecimal.new('100.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$1.00"
530
- expect(Money.new(BigDecimal.new('109.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$1.10"
531
- expect(Money.new(BigDecimal.new('1'), "MGA").format(:rounded_infinite_precision => true)).to eq "Ar0.2"
532
- end
533
-
534
- it "does not round fractional when set to false" do
535
- expect(Money.new(BigDecimal.new('12.1'), "USD").format(:rounded_infinite_precision => false)).to eq "$0.121"
536
- expect(Money.new(BigDecimal.new('12.5'), "USD").format(:rounded_infinite_precision => false)).to eq "$0.125"
537
- expect(Money.new(BigDecimal.new('123.1'), "BHD").format(:rounded_infinite_precision => false)).to eq "ب.د0.1231"
538
- expect(Money.new(BigDecimal.new('123.5'), "BHD").format(:rounded_infinite_precision => false)).to eq "ب.د0.1235"
539
- expect(Money.new(BigDecimal.new('100.1'), "USD").format(:rounded_infinite_precision => false)).to eq "$1.001"
540
- expect(Money.new(BigDecimal.new('109.5'), "USD").format(:rounded_infinite_precision => false)).to eq "$1.095"
541
- expect(Money.new(BigDecimal.new('1'), "MGA").format(:rounded_infinite_precision => false)).to eq "Ar0.1"
542
- end
543
-
544
- describe ":rounded_infinite_precision option with i18n = false" do
545
- before do
546
- Money.use_i18n = false
547
- Money.infinite_precision = true
548
- end
549
-
550
- after do
551
- Money.use_i18n = true
552
- Money.infinite_precision = false
553
- end
554
-
555
- it 'does round fractional when set to true' do
556
- expect(Money.new(BigDecimal.new('12.1'), "EUR").format(:rounded_infinite_precision => true)).to eq "€0,12"
557
- expect(Money.new(BigDecimal.new('12.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€0,13"
558
- expect(Money.new(BigDecimal.new('100.1'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1,00"
559
- expect(Money.new(BigDecimal.new('109.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1,10"
560
-
561
- expect(Money.new(BigDecimal.new('100012.1'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1.000,12"
562
- expect(Money.new(BigDecimal.new('100012.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1.000,13"
563
- end
564
- end
565
- end
566
-
567
- context "when the monetary value is 0" do
568
- let(:money) { Money.us_dollar(0) }
569
-
570
- it "returns 'free' when :display_free is true" do
571
- expect(money.format(:display_free => true)).to eq 'free'
572
- end
573
-
574
- it "returns '$0.00' when :display_free is false or not given" do
575
- expect(money.format).to eq '$0.00'
576
- expect(money.format(:display_free => false)).to eq '$0.00'
577
- expect(money.format(:display_free => nil)).to eq '$0.00'
578
- end
579
-
580
- it "returns the value specified by :display_free if it's a string-like object" do
581
- expect(money.format(:display_free => 'gratis')).to eq 'gratis'
582
- end
583
- end
584
- end
585
-
586
- context "custom currencies with 4 decimal places" do
587
- before :each do
588
- Money::Currency.register(JSON.parse(BAR, :symbolize_names => true))
589
- Money::Currency.register(JSON.parse(EU4, :symbolize_names => true))
590
- end
591
-
592
- after :each do
593
- Money::Currency.unregister(JSON.parse(BAR, :symbolize_names => true))
594
- Money::Currency.unregister(JSON.parse(EU4, :symbolize_names => true))
595
- end
596
-
597
- it "respects custom subunit to unit, decimal and thousands separator" do
598
- expect(Money.new(4, "BAR").format).to eq "$0.0004"
599
- expect(Money.new(4, "EU4").format).to eq "€0,0004"
600
-
601
- expect(Money.new(24, "BAR").format).to eq "$0.0024"
602
- expect(Money.new(24, "EU4").format).to eq "€0,0024"
603
-
604
- expect(Money.new(324, "BAR").format).to eq "$0.0324"
605
- expect(Money.new(324, "EU4").format).to eq "€0,0324"
606
-
607
- expect(Money.new(5324, "BAR").format).to eq "$0.5324"
608
- expect(Money.new(5324, "EU4").format).to eq "€0,5324"
609
-
610
- expect(Money.new(65324, "BAR").format).to eq "$6.5324"
611
- expect(Money.new(65324, "EU4").format).to eq "€6,5324"
612
-
613
- expect(Money.new(865324, "BAR").format).to eq "$86.5324"
614
- expect(Money.new(865324, "EU4").format).to eq "€86,5324"
615
-
616
- expect(Money.new(1865324, "BAR").format).to eq "$186.5324"
617
- expect(Money.new(1865324, "EU4").format).to eq "€186,5324"
618
-
619
- expect(Money.new(33310034, "BAR").format).to eq "$3,331.0034"
620
- expect(Money.new(33310034, "EU4").format).to eq "€3.331,0034"
621
-
622
- expect(Money.new(88833310034, "BAR").format).to eq "$8,883,331.0034"
623
- expect(Money.new(88833310034, "EU4").format).to eq "€8.883.331,0034"
624
- end
625
-
626
- end
627
-
628
- context "currencies with ambiguous signs" do
629
-
630
- it "returns ambiguous signs when disambiguate is not set" do
631
- expect(Money.new(1999_98, "USD").format).to eq("$1,999.98")
632
- expect(Money.new(1999_98, "CAD").format).to eq("$1,999.98")
633
- expect(Money.new(1999_98, "DKK").format).to eq("1.999,98 kr")
634
- expect(Money.new(1999_98, "NOK").format).to eq("1.999,98 kr")
635
- expect(Money.new(1999_98, "SEK").format).to eq("1 999,98 kr")
636
- end
637
-
638
- it "returns ambiguous signs when disambiguate is false" do
639
- expect(Money.new(1999_98, "USD").format(disambiguate: false)).to eq("$1,999.98")
640
- expect(Money.new(1999_98, "CAD").format(disambiguate: false)).to eq("$1,999.98")
641
- expect(Money.new(1999_98, "DKK").format(disambiguate: false)).to eq("1.999,98 kr")
642
- expect(Money.new(1999_98, "NOK").format(disambiguate: false)).to eq("1.999,98 kr")
643
- expect(Money.new(1999_98, "SEK").format(disambiguate: false)).to eq("1 999,98 kr")
644
- end
645
-
646
- it "returns disambiguate signs when disambiguate: true" do
647
- expect(Money.new(1999_98, "USD").format(disambiguate: true)).to eq("$1,999.98")
648
- expect(Money.new(1999_98, "CAD").format(disambiguate: true)).to eq("C$1,999.98")
649
- expect(Money.new(1999_98, "DKK").format(disambiguate: true)).to eq("1.999,98 DKK")
650
- expect(Money.new(1999_98, "NOK").format(disambiguate: true)).to eq("1.999,98 NOK")
651
- expect(Money.new(1999_98, "SEK").format(disambiguate: true)).to eq("1 999,98 SEK")
652
- end
653
-
654
- it "should never return an ambiguous format with disambiguate: true" do
655
- formatted_results = {}
656
-
657
- # When we format the same amount in all known currencies, disambiguate should return
658
- # all different values
659
- Money::Currency.all.each do |currency|
660
- format = Money.new(1999_98, currency).format(disambiguate: true)
661
- expect(formatted_results.keys).not_to include(format), "Format '#{format}' for #{currency} is ambiguous with currency #{formatted_results[format]}."
662
- formatted_results[format] = currency
663
- end
664
- end
665
-
666
- end
667
- end