money-joshm1 5.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,466 @@
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": "," }'
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}'
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": "." }'
10
+
11
+ context "without i18n" do
12
+ subject { Money.empty("USD") }
13
+
14
+ its(:thousands_separator) { should == "," }
15
+ its(:decimal_mark) { should == "." }
16
+ end
17
+
18
+ context "with i18n but use_i18n = false" do
19
+ before :each do
20
+ reset_i18n
21
+ I18n.locale = :de
22
+ I18n.backend.store_translations(
23
+ :de,
24
+ :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } }
25
+ )
26
+ Money.use_i18n = false
27
+ end
28
+
29
+ after :each do
30
+ reset_i18n
31
+ I18n.locale = :en
32
+ Money.use_i18n = true
33
+ end
34
+
35
+ subject { Money.empty("USD") }
36
+
37
+ its(:thousands_separator) { should == "," }
38
+ its(:decimal_mark) { should == "." }
39
+ end
40
+
41
+ context "with i18n" do
42
+ after :each do
43
+ reset_i18n
44
+ I18n.locale = :en
45
+ end
46
+
47
+ context "with number.format.*" do
48
+ before :each do
49
+ reset_i18n
50
+ I18n.locale = :de
51
+ I18n.backend.store_translations(
52
+ :de,
53
+ :number => { :format => { :delimiter => ".", :separator => "," } }
54
+ )
55
+ end
56
+
57
+ subject { Money.empty("USD") }
58
+
59
+ its(:thousands_separator) { should == "." }
60
+ its(:decimal_mark) { should == "," }
61
+ end
62
+
63
+ context "with number.currency.format.*" do
64
+ before :each do
65
+ reset_i18n
66
+ I18n.locale = :de
67
+ I18n.backend.store_translations(
68
+ :de,
69
+ :number => { :currency => { :format => { :delimiter => ".", :separator => "," } } }
70
+ )
71
+ end
72
+
73
+ subject { Money.empty("USD") }
74
+
75
+ its(:thousands_separator) { should == "." }
76
+ its(:decimal_mark) { should == "," }
77
+ end
78
+ end
79
+
80
+ describe "#format" do
81
+ context "Locale :ja" do
82
+ before { @_locale = I18n.locale; I18n.locale = :ja }
83
+
84
+ it "formats Japanese currency in Japanese properly" do
85
+ Money.new(1000, "JPY").format.should == "1,000円"
86
+ end
87
+
88
+ after { I18n.locale = @_locale }
89
+ end
90
+
91
+ it "returns the monetary value as a string" do
92
+ Money.ca_dollar(100).format.should == "$1.00"
93
+ Money.new(40008).format.should == "$400.08"
94
+ end
95
+
96
+ it "respects :subunit_to_unit currency property" do
97
+ Money.new(10_00, "BHD").format.should == "ب.د1.000"
98
+ end
99
+
100
+ it "does not display a decimal when :subunit_to_unit is 1" do
101
+ Money.new(10_00, "CLP").format.should == "$1.000"
102
+ end
103
+
104
+ it "respects the thousands_separator and decimal_mark defaults" do
105
+ one_thousand = Proc.new do |currency|
106
+ Money.new(1000_00, currency).format
107
+ end
108
+
109
+ # Pounds
110
+ one_thousand["GBP"].should == "£1,000.00"
111
+
112
+ # Dollars
113
+ one_thousand["USD"].should == "$1,000.00"
114
+ one_thousand["CAD"].should == "$1,000.00"
115
+ one_thousand["AUD"].should == "$1,000.00"
116
+ one_thousand["NZD"].should == "$1,000.00"
117
+ one_thousand["ZWD"].should == "$1,000.00"
118
+
119
+ # Yen
120
+ one_thousand["JPY"].should == "¥100,000"
121
+ one_thousand["CNY"].should == "¥1,000.00"
122
+
123
+ # Euro
124
+ one_thousand["EUR"].should == "€1.000,00"
125
+
126
+ # Rupees
127
+ one_thousand["INR"].should == "₹1,000.00"
128
+ one_thousand["NPR"].should == "₨1,000.00"
129
+ one_thousand["SCR"].should == "1,000.00 ₨"
130
+ one_thousand["LKR"].should == "1,000.00 ₨"
131
+
132
+ # Brazilian Real
133
+ one_thousand["BRL"].should == "R$ 1.000,00"
134
+
135
+ # Other
136
+ one_thousand["SEK"].should == "1.000,00 kr"
137
+ one_thousand["GHC"].should == "₵1,000.00"
138
+ end
139
+
140
+ it "inserts commas into the result if the amount is sufficiently large" do
141
+ Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
142
+ Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
143
+ end
144
+
145
+ it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
146
+ Money.euro(1_234_567_12).format.should == "€1.234.567,12"
147
+ Money.euro(1_234_567_12).format(:no_cents => true).should == "€1.234.567"
148
+ end
149
+
150
+ describe ":with_currency option" do
151
+ specify "(:with_currency option => true) works as documented" do
152
+ Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
153
+ Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
154
+ end
155
+ end
156
+
157
+ describe ":no_cents option" do
158
+ specify "(:with_currency option => true) works as documented" do
159
+ Money.ca_dollar(100).format(:no_cents => true).should == "$1"
160
+ Money.ca_dollar(599).format(:no_cents => true).should == "$5"
161
+ Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
162
+ Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
163
+ end
164
+
165
+ it "respects :subunit_to_unit currency property" do
166
+ Money.new(10_00, "BHD").format(:no_cents => true).should == "ب.د1"
167
+ end
168
+
169
+ it "inserts thousand separators if symbol contains decimal mark and no_cents is true" do
170
+ Money.new(100000000, "AMD").format(no_cents: true).should == "1,000,000 դր."
171
+ Money.new(100000000, "USD").format(no_cents: true).should == "$1,000,000"
172
+ Money.new(100000000, "RUB").format(no_cents: true).should == "1.000.000 р."
173
+ end
174
+
175
+ it "doesn't incorrectly format HTML" do
176
+ money = ::Money.new(1999, "RUB")
177
+ output = money.format(:html => true, :no_cents => true)
178
+ output.should == "19 руб"
179
+ end
180
+ end
181
+
182
+ describe ":no_cents_if_whole option" do
183
+ specify "(:no_cents_if_whole => true) works as documented" do
184
+ Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,000"
185
+ Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,034"
186
+ Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,000"
187
+ Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,006.4"
188
+ Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.000"
189
+ Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.003,4"
190
+ Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100"
191
+ Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100.34"
192
+ Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10"
193
+ Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10.034"
194
+ end
195
+
196
+ specify "(:no_cents_if_whole => false) works as documented" do
197
+ Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,000"
198
+ Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,034"
199
+ Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,000.0"
200
+ Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,006.4"
201
+ Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.000,0"
202
+ Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.003,4"
203
+ Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.00"
204
+ Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.34"
205
+ Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.000"
206
+ Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.034"
207
+ end
208
+ end
209
+
210
+ describe ":symbol option" do
211
+ specify "(:symbol => a symbol string) uses the given value as the money symbol" do
212
+ Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
213
+ end
214
+
215
+ specify "(:symbol => true) returns symbol based on the given currency code" do
216
+ one = Proc.new do |currency|
217
+ Money.new(100, currency).format(:symbol => true)
218
+ end
219
+
220
+ # Pounds
221
+ one["GBP"].should == "£1.00"
222
+
223
+ # Dollars
224
+ one["USD"].should == "$1.00"
225
+ one["CAD"].should == "$1.00"
226
+ one["AUD"].should == "$1.00"
227
+ one["NZD"].should == "$1.00"
228
+ one["ZWD"].should == "$1.00"
229
+
230
+ # Yen
231
+ one["JPY"].should == "¥100"
232
+ one["CNY"].should == "¥1.00"
233
+
234
+ # Euro
235
+ one["EUR"].should == "€1,00"
236
+
237
+ # Rupees
238
+ one["INR"].should == "₹1.00"
239
+ one["NPR"].should == "₨1.00"
240
+ one["SCR"].should == "1.00 ₨"
241
+ one["LKR"].should == "1.00 ₨"
242
+
243
+ # Brazilian Real
244
+ one["BRL"].should == "R$ 1,00"
245
+
246
+ # Other
247
+ one["SEK"].should == "1,00 kr"
248
+ one["GHC"].should == "₵1.00"
249
+ end
250
+
251
+ specify "(:symbol => true) returns $ when currency code is not recognized" do
252
+ currency = Money::Currency.new("EUR")
253
+ currency.should_receive(:symbol).and_return(nil)
254
+ Money.new(100, currency).format(:symbol => true).should == "¤1,00"
255
+ end
256
+
257
+ specify "(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
258
+ Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
259
+ Money.new(100, "EUR").format(:symbol => true).should == "€1,00"
260
+ Money.new(100, "SEK").format(:symbol => true).should == "1,00 kr"
261
+ end
262
+
263
+ specify "(:symbol => "", nil or false) returns the amount without a symbol" do
264
+ money = Money.new(100, "GBP")
265
+ money.format(:symbol => "").should == "1.00"
266
+ money.format(:symbol => nil).should == "1.00"
267
+ money.format(:symbol => false).should == "1.00"
268
+ end
269
+
270
+ it "defaults :symbol to true" do
271
+ money = Money.new(100)
272
+ money.format.should == "$1.00"
273
+
274
+ money = Money.new(100, "GBP")
275
+ money.format.should == "£1.00"
276
+
277
+ money = Money.new(100, "EUR")
278
+ money.format.should == "€1,00"
279
+ end
280
+ end
281
+
282
+ describe ":decimal_mark option" do
283
+ specify "(:decimal_mark => a decimal_mark string) works as documented" do
284
+ Money.us_dollar(100).format(:decimal_mark => ",").should == "$1,00"
285
+ end
286
+
287
+ it "defaults to '.' if currency isn't recognized" do
288
+ Money.new(100, "ZWD").format.should == "$1.00"
289
+ end
290
+ end
291
+
292
+ describe ":separator option" do
293
+ specify "(:separator => a separator string) works as documented" do
294
+ Money.us_dollar(100).format(:separator => ",").should == "$1,00"
295
+ end
296
+ end
297
+
298
+ describe ":south_asian_number_formatting delimiter" do
299
+ before(:each) do
300
+ Money::Currency.register(JSON.parse(INDIAN_BAR, :symbolize_names => true))
301
+ end
302
+
303
+ specify "(:south_asian_number_formatting => true) works as documented" do
304
+ Money.new(10000000, 'INR').format(:south_asian_number_formatting => true, :symbol => false).should == "1,00,000.00"
305
+ Money.new(1000000000, 'INDIAN_BAR').format(:south_asian_number_formatting => true, :symbol => false).should == "1,00,000.0000"
306
+ Money.new(10000000).format(:south_asian_number_formatting => true).should == "$1,00,000.00"
307
+ end
308
+ end
309
+
310
+ describe ":thousands_separator option" do
311
+ specify "(:thousands_separator => a thousands_separator string) works as documented" do
312
+ Money.us_dollar(100000).format(:thousands_separator => ".").should == "$1.000.00"
313
+ Money.us_dollar(200000).format(:thousands_separator => "").should == "$2000.00"
314
+ end
315
+
316
+ specify "(:thousands_separator => false or nil) works as documented" do
317
+ Money.us_dollar(100000).format(:thousands_separator => false).should == "$1000.00"
318
+ Money.us_dollar(200000).format(:thousands_separator => nil).should == "$2000.00"
319
+ end
320
+
321
+ specify "(:delimiter => a delimiter string) works as documented" do
322
+ Money.us_dollar(100000).format(:delimiter => ".").should == "$1.000.00"
323
+ Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
324
+ end
325
+
326
+ specify "(:delimiter => false or nil) works as documented" do
327
+ Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
328
+ Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
329
+ end
330
+
331
+ it "defaults to ',' if currency isn't recognized" do
332
+ Money.new(100000, "ZWD").format.should == "$1,000.00"
333
+ end
334
+ end
335
+
336
+ describe ":html option" do
337
+ specify "(:html => true) works as documented" do
338
+ string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
339
+ string.should == "$5.70 <span class=\"currency\">CAD</span>"
340
+ end
341
+
342
+ specify "should fallback to symbol if entity is not available" do
343
+ string = Money.new(570, 'DKK').format(:html => true)
344
+ string.should == "5,70 kr"
345
+ end
346
+ end
347
+
348
+ describe ":html_wrap_symbol option" do
349
+ specify "(:html_wrap_symbol => true) works as documented" do
350
+ string = Money.ca_dollar(570).format(:html_wrap_symbol => true)
351
+ string.should == "<span class=\"currency_symbol\">$</span>5.70"
352
+ end
353
+ end
354
+
355
+
356
+ describe ":symbol_position option" do
357
+ it "inserts currency symbol before the amount when set to :before" do
358
+ Money.euro(1_234_567_12).format(:symbol_position => :before).should == "€1.234.567,12"
359
+ end
360
+
361
+ it "inserts currency symbol after the amount when set to :after" do
362
+ Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after).should == "1,000,000,000.12 $"
363
+ end
364
+ end
365
+
366
+ describe ":sign_before_symbol option" do
367
+ specify "(:sign_before_symbol => true) works as documented" do
368
+ Money.us_dollar(-100000).format(:sign_before_symbol => true).should == "-$1,000.00"
369
+ end
370
+
371
+ specify "(:sign_before_symbol => false) works as documented" do
372
+ Money.us_dollar(-100000).format(:sign_before_symbol => false).should == "$-1,000.00"
373
+ Money.us_dollar(-100000).format(:sign_before_symbol => nil).should == "$-1,000.00"
374
+ end
375
+ end
376
+
377
+ describe ":symbol_before_without_space option" do
378
+ it "does not insert space between currency symbol and amount when set to true" do
379
+ Money.euro(1_234_567_12).format(:symbol_position => :before, :symbol_before_without_space => true).should == "€1.234.567,12"
380
+ end
381
+
382
+ it "inserts space between currency symbol and amount when set to false" do
383
+ Money.euro(1_234_567_12).format(:symbol_position => :before, :symbol_before_without_space => false).should == "€ 1.234.567,12"
384
+ end
385
+
386
+ it "defaults to true" do
387
+ Money.euro(1_234_567_12).format(:symbol_position => :before).should == "€1.234.567,12"
388
+ end
389
+ end
390
+
391
+ describe ":symbol_after_without_space option" do
392
+ it "does not insert space between amount and currency symbol when set to true" do
393
+ Money.euro(1_234_567_12).format(:symbol_position => :after, :symbol_after_without_space => true).should == "1.234.567,12€"
394
+ end
395
+
396
+ it "inserts space between amount and currency symbol when set to false" do
397
+ Money.euro(1_234_567_12).format(:symbol_position => :after, :symbol_after_without_space => false).should == "1.234.567,12 €"
398
+ end
399
+
400
+ it "defaults to false" do
401
+ Money.euro(1_234_567_12).format(:symbol_position => :after).should == "1.234.567,12 €"
402
+ end
403
+ end
404
+
405
+ context "when the monetary value is 0" do
406
+ let(:money) { Money.us_dollar(0) }
407
+
408
+ it "returns 'free' when :display_free is true" do
409
+ money.format(:display_free => true).should == 'free'
410
+ end
411
+
412
+ it "returns '$0.00' when :display_free is false or not given" do
413
+ money.format.should == '$0.00'
414
+ money.format(:display_free => false).should == '$0.00'
415
+ money.format(:display_free => nil).should == '$0.00'
416
+ end
417
+
418
+ it "returns the value specified by :display_free if it's a string-like object" do
419
+ money.format(:display_free => 'gratis').should == 'gratis'
420
+ end
421
+ end
422
+
423
+ it "maintains floating point precision" do
424
+ "0.01".to_money("USD").format(:symbol => false).should == "0.01"
425
+ end
426
+
427
+ end
428
+
429
+ context "custom currencies with 4 decimal places" do
430
+ before :each do
431
+ Money::Currency.register(JSON.parse(BAR, :symbolize_names => true))
432
+ Money::Currency.register(JSON.parse(EU4, :symbolize_names => true))
433
+ end
434
+
435
+ it "respects custom subunit to unit, decimal and thousands separator" do
436
+ Money.new(4, "BAR").format.should == "$0.0004"
437
+ Money.new(4, "EU4").format.should == "€0,0004"
438
+
439
+ Money.new(24, "BAR").format.should == "$0.0024"
440
+ Money.new(24, "EU4").format.should == "€0,0024"
441
+
442
+ Money.new(324, "BAR").format.should == "$0.0324"
443
+ Money.new(324, "EU4").format.should == "€0,0324"
444
+
445
+ Money.new(5324, "BAR").format.should == "$0.5324"
446
+ Money.new(5324, "EU4").format.should == "€0,5324"
447
+
448
+ Money.new(65324, "BAR").format.should == "$6.5324"
449
+ Money.new(65324, "EU4").format.should == "€6,5324"
450
+
451
+ Money.new(865324, "BAR").format.should == "$86.5324"
452
+ Money.new(865324, "EU4").format.should == "€86,5324"
453
+
454
+ Money.new(1865324, "BAR").format.should == "$186.5324"
455
+ Money.new(1865324, "EU4").format.should == "€186,5324"
456
+
457
+ Money.new(33310034, "BAR").format.should == "$3,331.0034"
458
+ Money.new(33310034, "EU4").format.should == "€3.331,0034"
459
+
460
+ Money.new(88833310034, "BAR").format.should == "$8,883,331.0034"
461
+ Money.new(88833310034, "EU4").format.should == "€8.883.331,0034"
462
+ end
463
+
464
+ end
465
+ end
466
+