money 3.7.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/CHANGELOG.md +384 -351
  2. data/LICENSE +21 -21
  3. data/README.md +243 -214
  4. data/Rakefile +49 -49
  5. data/lib/money.rb +28 -27
  6. data/lib/money/bank/base.rb +131 -131
  7. data/lib/money/bank/variable_exchange.rb +252 -252
  8. data/lib/money/core_extensions.rb +82 -82
  9. data/lib/money/currency.rb +263 -422
  10. data/lib/money/currency_loader.rb +19 -0
  11. data/lib/money/money.rb +405 -405
  12. data/lib/money/money/arithmetic.rb +246 -246
  13. data/lib/money/money/formatting.rb +260 -244
  14. data/lib/money/money/parsing.rb +350 -350
  15. data/money.gemspec +29 -35
  16. data/spec/bank/base_spec.rb +72 -72
  17. data/spec/bank/variable_exchange_spec.rb +238 -238
  18. data/spec/core_extensions_spec.rb +158 -158
  19. data/spec/currency_spec.rb +120 -133
  20. data/spec/money/arithmetic_spec.rb +479 -479
  21. data/spec/money/formatting_spec.rb +383 -375
  22. data/spec/money/parsing_spec.rb +197 -197
  23. data/spec/money_spec.rb +292 -292
  24. data/spec/spec_helper.rb +28 -28
  25. metadata +54 -126
  26. data/lib/money.rbc +0 -184
  27. data/lib/money/bank/base.rbc +0 -818
  28. data/lib/money/bank/variable_exchange.rbc +0 -2550
  29. data/lib/money/core_extensions.rbc +0 -664
  30. data/lib/money/currency.rbc +0 -22708
  31. data/lib/money/money.rbc +0 -3861
  32. data/lib/money/money/arithmetic.rbc +0 -2778
  33. data/lib/money/money/formatting.rbc +0 -2265
  34. data/lib/money/money/parsing.rbc +0 -2737
  35. data/spec/bank/base_spec.rbc +0 -2461
  36. data/spec/bank/variable_exchange_spec.rbc +0 -7541
  37. data/spec/core_extensions_spec.rbc +0 -5921
  38. data/spec/currency_spec.rbc +0 -4535
  39. data/spec/money/arithmetic_spec.rbc +0 -25140
  40. data/spec/money/formatting_spec.rbc +0 -12545
  41. data/spec/money/parsing_spec.rbc +0 -6511
  42. data/spec/money_spec.rbc +0 -9824
  43. data/spec/spec_helper.rbc +0 -575
@@ -1,375 +1,383 @@
1
- # encoding: utf-8
2
-
3
- require "spec_helper"
4
-
5
- describe Money do
6
- specify "Money.format brute force :subunit_to_unit = 1" do
7
- ("0".."9").each do |amt|
8
- amt.to_money("VUV").format(:symbol => false).should == amt
9
- end
10
- ("-1".."-9").each do |amt|
11
- amt.to_money("VUV").format(:symbol => false).should == amt
12
- end
13
- "1000".to_money("VUV").format(:symbol => false).should == "1,000"
14
- "-1000".to_money("VUV").format(:symbol => false).should == "-1,000"
15
- end
16
-
17
- specify "Money.format brute force :subunit_to_unit = 5" do
18
- ("0.0".."9.4").each do |amt|
19
- next if amt[-1].to_i > 4
20
- amt.to_money("MGA").format(:symbol => false).should == amt
21
- end
22
- ("-0.1".."-9.4").each do |amt|
23
- next if amt[-1].to_i > 4
24
- amt.to_money("MGA").format(:symbol => false).should == amt
25
- end
26
- "1000.0".to_money("MGA").format(:symbol => false).should == "1,000.0"
27
- "-1000.0".to_money("MGA").format(:symbol => false).should == "-1,000.0"
28
- end
29
-
30
- specify "Money.format brute force :subunit_to_unit = 10" do
31
- ("0.0".."9.9").each do |amt|
32
- amt.to_money("VND").format(:symbol => false).should == amt.to_s.gsub(/\./, ",")
33
- end
34
- ("-0.1".."-9.9").each do |amt|
35
- amt.to_money("VND").format(:symbol => false).should == amt.to_s.gsub(/\./, ",")
36
- end
37
- "1000.0".to_money("VND").format(:symbol => false).should == "1.000,0"
38
- "-1000.0".to_money("VND").format(:symbol => false).should == "-1.000,0"
39
- end
40
-
41
- specify "Money.format brute force :subunit_to_unit = 100" do
42
- ("0.00".."9.99").each do |amt|
43
- amt.to_money("USD").format(:symbol => false).should == amt
44
- end
45
- ("-0.01".."-9.99").each do |amt|
46
- amt.to_money("USD").format(:symbol => false).should == amt
47
- end
48
- "1000.00".to_money("USD").format(:symbol => false).should == "1,000.00"
49
- "-1000.00".to_money("USD").format(:symbol => false).should == "-1,000.00"
50
- end
51
-
52
- specify "Money.format brute force :subunit_to_unit = 1000" do
53
- ("0.000".."9.999").each do |amt|
54
- amt.to_money("IQD").format(:symbol => false).should == amt
55
- end
56
- ("-0.001".."-9.999").each do |amt|
57
- amt.to_money("IQD").format(:symbol => false).should == amt
58
- end
59
- "1000.000".to_money("IQD").format(:symbol => false).should == "1,000.000"
60
- "-1000.000".to_money("IQD").format(:symbol => false).should == "-1,000.000"
61
- end
62
-
63
- context "without i18n" do
64
- subject{ Money.empty("USD") }
65
-
66
- its(:thousands_separator){should == ","}
67
- its(:decimal_mark){should == "."}
68
- end
69
-
70
- context "with i18n but use_i18n = false" do
71
- before :each do
72
- reset_i18n
73
- I18n.locale = :de
74
- I18n.backend.store_translations(
75
- :de, :number => {:currency => {:format => {:delimiter => ".",
76
- :separator => ","}}}
77
- )
78
-
79
- Money.use_i18n = false
80
- end
81
-
82
- after :each do
83
- reset_i18n
84
- I18n.locale = :en
85
- Money.use_i18n = true
86
- end
87
-
88
- subject{ Money.new(1000_00, :usd) }
89
-
90
- its(:format){should == "$1,000.00"}
91
- end
92
-
93
- context "with i18n" do
94
- after :each do
95
- reset_i18n
96
- I18n.locale = :en
97
- end
98
-
99
- context "with number.format.*" do
100
- before :each do
101
- reset_i18n
102
- I18n.locale = :de
103
- I18n.backend.store_translations(
104
- :de, :number => {:format => {:delimiter => ".",
105
- :separator => ","}}
106
- )
107
- end
108
-
109
- subject{ Money.empty("USD") }
110
-
111
- its(:thousands_separator){should == "."}
112
- its(:decimal_mark){should == ","}
113
- end
114
-
115
- context "with number.currency.format.*" do
116
- before :each do
117
- reset_i18n
118
- I18n.locale = :de
119
- I18n.backend.store_translations(
120
- :de, :number => {:currency => {:format => {:delimiter => ".",
121
- :separator => ","}}}
122
- )
123
- end
124
-
125
- subject{ Money.empty("USD") }
126
-
127
- its(:thousands_separator){should == "."}
128
- its(:decimal_mark){should == ","}
129
- end
130
- end
131
-
132
- describe "#format" do
133
- it "returns the monetary value as a string" do
134
- Money.ca_dollar(100).format.should == "$1.00"
135
- Money.new(40008).format.should == "$400.08"
136
- end
137
-
138
- it "should respect :subunit_to_unit currency property" do
139
- Money.new(10_00, "BHD").format.should == "ب.د1.000"
140
- end
141
-
142
- it "doesn't display a decimal when :subunit_to_unit is 1" do
143
- Money.new(10_00, "CLP").format.should == "$1.000"
144
- end
145
-
146
- it "respects the thousands_separator and decimal_mark defaults" do
147
- one_thousand = Proc.new do |currency|
148
- Money.new(1000_00, currency).format
149
- end
150
-
151
- # Pounds
152
- one_thousand["GBP"].should == "£1,000.00"
153
-
154
- # Dollars
155
- one_thousand["USD"].should == "$1,000.00"
156
- one_thousand["CAD"].should == "$1,000.00"
157
- one_thousand["AUD"].should == "$1,000.00"
158
- one_thousand["NZD"].should == "$1,000.00"
159
- one_thousand["ZWD"].should == "$1,000.00"
160
-
161
- # Yen
162
- one_thousand["JPY"].should == "¥1,000.00"
163
- one_thousand["CNY"].should == "¥1,000.00"
164
-
165
- # Euro
166
- one_thousand["EUR"].should == "1.000,00"
167
-
168
- # Rupees
169
- one_thousand["INR"].should == "₨1,000.00"
170
- one_thousand["NPR"].should == "₨1,000.00"
171
- one_thousand["SCR"].should == "1,000.00"
172
- one_thousand["LKR"].should == "1,000.00 ₨"
173
-
174
- # Brazilian Real
175
- one_thousand["BRL"].should == "R$ 1.000,00"
176
-
177
- # Other
178
- one_thousand["SEK"].should == "kr1,000.00"
179
- one_thousand["GHC"].should == "1,000.00"
180
- end
181
-
182
- describe "if the monetary value is 0" do
183
- before :each do
184
- @money = Money.us_dollar(0)
185
- end
186
-
187
- it "returns 'free' when :display_free is true" do
188
- @money.format(:display_free => true).should == 'free'
189
- end
190
-
191
- it "returns '$0.00' when :display_free is false or not given" do
192
- @money.format.should == '$0.00'
193
- @money.format(:display_free => false).should == '$0.00'
194
- @money.format(:display_free => nil).should == '$0.00'
195
- end
196
-
197
- it "returns the value specified by :display_free if it's a string-like object" do
198
- @money.format(:display_free => 'gratis').should == 'gratis'
199
- end
200
- end
201
-
202
- specify "#format(:with_currency => true) works as documented" do
203
- Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
204
- Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
205
- end
206
-
207
- specify "#format(:no_cents => true) works as documented" do
208
- Money.ca_dollar(100).format(:no_cents => true).should == "$1"
209
- Money.ca_dollar(599).format(:no_cents => true).should == "$5"
210
- Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
211
- Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
212
- end
213
-
214
- specify "#format(:no_cents => true) should respect :subunit_to_unit currency property" do
215
- Money.new(10_00, "BHD").format(:no_cents => true).should == "ب.د1"
216
- end
217
-
218
- specify "#format(:no_cents_if_whole => true) works as documented" do
219
- Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,000"
220
- Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,034"
221
- Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,000"
222
- Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,006.4"
223
- Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.000"
224
- Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.003,4"
225
- Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100"
226
- Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100.34"
227
- Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10"
228
- Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10.034"
229
- end
230
-
231
- specify "#format(:no_cents_if_whole => false) works as documented" do
232
- Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,000"
233
- Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,034"
234
- Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,000.0"
235
- Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,006.4"
236
- Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.000,0"
237
- Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.003,4"
238
- Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.00"
239
- Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.34"
240
- Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.000"
241
- Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.034"
242
- end
243
-
244
- specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
245
- Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
246
- end
247
-
248
- specify "#format(:symbol => true) returns symbol based on the given currency code" do
249
- one = Proc.new do |currency|
250
- Money.new(100, currency).format(:symbol => true)
251
- end
252
-
253
- # Pounds
254
- one["GBP"].should == "£1.00"
255
-
256
- # Dollars
257
- one["USD"].should == "$1.00"
258
- one["CAD"].should == "$1.00"
259
- one["AUD"].should == "$1.00"
260
- one["NZD"].should == "$1.00"
261
- one["ZWD"].should == "$1.00"
262
-
263
- # Yen
264
- one["JPY"].should == "¥1.00"
265
- one["CNY"].should == "¥1.00"
266
-
267
- # Euro
268
- one["EUR"].should == "1,00"
269
-
270
- # Rupees
271
- one["INR"].should == "₨1.00"
272
- one["NPR"].should == "₨1.00"
273
- one["SCR"].should == "1.00"
274
- one["LKR"].should == "1.00 ₨"
275
-
276
- # Brazilian Real
277
- one["BRL"].should == "R$ 1,00"
278
-
279
- # Other
280
- one["SEK"].should == "kr1.00"
281
- one["GHC"].should == "1.00"
282
- end
283
-
284
- specify "#format(:symbol => true) returns $ when currency code is not recognized" do
285
- currency = Money::Currency.new("EUR")
286
- currency.should_receive(:symbol).and_return(nil)
287
- Money.new(100, currency).format(:symbol => true).should == "1,00 ¤"
288
- end
289
-
290
- specify "#format(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
291
- Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
292
- Money.new(100, "EUR").format(:symbol => true).should == "1,00 €"
293
- Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
294
- end
295
-
296
- specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
297
- money = Money.new(100, "GBP")
298
- money.format(:symbol => "").should == "1.00"
299
- money.format(:symbol => nil).should == "1.00"
300
- money.format(:symbol => false).should == "1.00"
301
- end
302
-
303
- specify "#format without :symbol assumes that :symbol is set to true" do
304
- money = Money.new(100)
305
- money.format.should == "$1.00"
306
-
307
- money = Money.new(100, "GBP")
308
- money.format.should == "£1.00"
309
-
310
- money = Money.new(100, "EUR")
311
- money.format.should == "1,00 €"
312
- end
313
-
314
- specify "#format(:decimal_mark => a decimal_mark string) works as documented" do
315
- Money.us_dollar(100).format(:decimal_mark => ",").should == "$1,00"
316
- end
317
-
318
- specify "#format will default decimal_mark to '.' if currency isn't recognized" do
319
- Money.new(100, "ZWD").format.should == "$1.00"
320
- end
321
-
322
- specify "#format(:separator => a separator string) works as documented" do
323
- Money.us_dollar(100).format(:separator => ",").should == "$1,00"
324
- end
325
-
326
- specify "#format(:thousands_separator => a thousands_separator string) works as documented" do
327
- Money.us_dollar(100000).format(:thousands_separator => ".").should == "$1.000.00"
328
- Money.us_dollar(200000).format(:thousands_separator => "").should == "$2000.00"
329
- end
330
-
331
- specify "#format(:thousands_separator => false or nil) works as documented" do
332
- Money.us_dollar(100000).format(:thousands_separator => false).should == "$1000.00"
333
- Money.us_dollar(200000).format(:thousands_separator => nil).should == "$2000.00"
334
- end
335
-
336
- specify "#format(:delimiter => a delimiter string) works as documented" do
337
- Money.us_dollar(100000).format(:delimiter => ".").should == "$1.000.00"
338
- Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
339
- end
340
-
341
- specify "#format(:delimiter => false or nil) works as documented" do
342
- Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
343
- Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
344
- end
345
-
346
- specify "#format will default thousands_separator to ',' if currency isn't recognized" do
347
- Money.new(100000, "ZWD").format.should == "$1,000.00"
348
- end
349
-
350
- specify "#format(:html => true) works as documented" do
351
- string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
352
- string.should == "$5.70 <span class=\"currency\">CAD</span>"
353
- end
354
-
355
- it "should insert commas into the result if the amount is sufficiently large" do
356
- Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
357
- Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
358
- end
359
-
360
- it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
361
- Money.euro(1_234_567_12).format.should == "1.234.567,12 €"
362
- Money.euro(1_234_567_12).format(:no_cents => true).should == "1.234.567 €"
363
- end
364
-
365
- it "inserts currency symbol before the amount when :symbol_position is set to :before" do
366
- Money.euro(1_234_567_12).format(:symbol_position => :before).should == "€1.234.567,12"
367
- end
368
-
369
- it "inserts currency symbol after the amount when :symbol_position is set to :after" do
370
- Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after).should == "1,000,000,000.12 $"
371
- end
372
- end
373
-
374
- end
375
-
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Money do
6
+ specify "Money.format brute force :subunit_to_unit = 1" do
7
+ ("0".."9").each do |amt|
8
+ amt.to_money("VUV").format(:symbol => false).should == amt
9
+ end
10
+ ("-1".."-9").each do |amt|
11
+ amt.to_money("VUV").format(:symbol => false).should == amt
12
+ end
13
+ "1000".to_money("VUV").format(:symbol => false).should == "1,000"
14
+ "-1000".to_money("VUV").format(:symbol => false).should == "-1,000"
15
+ end
16
+
17
+ specify "Money.format brute force :subunit_to_unit = 5" do
18
+ ("0.0".."9.4").each do |amt|
19
+ next if amt[-1].to_i > 4
20
+ amt.to_money("MGA").format(:symbol => false).should == amt
21
+ end
22
+ ("-0.1".."-9.4").each do |amt|
23
+ next if amt[-1].to_i > 4
24
+ amt.to_money("MGA").format(:symbol => false).should == amt
25
+ end
26
+ "1000.0".to_money("MGA").format(:symbol => false).should == "1,000.0"
27
+ "-1000.0".to_money("MGA").format(:symbol => false).should == "-1,000.0"
28
+ end
29
+
30
+ specify "Money.format brute force :subunit_to_unit = 10" do
31
+ ("0.0".."9.9").each do |amt|
32
+ amt.to_money("VND").format(:symbol => false).should == amt.to_s.gsub(/\./, ",")
33
+ end
34
+ ("-0.1".."-9.9").each do |amt|
35
+ amt.to_money("VND").format(:symbol => false).should == amt.to_s.gsub(/\./, ",")
36
+ end
37
+ "1000.0".to_money("VND").format(:symbol => false).should == "1.000,0"
38
+ "-1000.0".to_money("VND").format(:symbol => false).should == "-1.000,0"
39
+ end
40
+
41
+ specify "Money.format brute force :subunit_to_unit = 100" do
42
+ ("0.00".."9.99").each do |amt|
43
+ amt.to_money("USD").format(:symbol => false).should == amt
44
+ end
45
+ ("-0.01".."-9.99").each do |amt|
46
+ amt.to_money("USD").format(:symbol => false).should == amt
47
+ end
48
+ "1000.00".to_money("USD").format(:symbol => false).should == "1,000.00"
49
+ "-1000.00".to_money("USD").format(:symbol => false).should == "-1,000.00"
50
+ end
51
+
52
+ specify "Money.format brute force :subunit_to_unit = 1000" do
53
+ ("0.000".."9.999").each do |amt|
54
+ amt.to_money("IQD").format(:symbol => false).should == amt
55
+ end
56
+ ("-0.001".."-9.999").each do |amt|
57
+ amt.to_money("IQD").format(:symbol => false).should == amt
58
+ end
59
+ "1000.000".to_money("IQD").format(:symbol => false).should == "1,000.000"
60
+ "-1000.000".to_money("IQD").format(:symbol => false).should == "-1,000.000"
61
+ end
62
+
63
+ context "without i18n" do
64
+ subject{ Money.empty("USD") }
65
+
66
+ its(:thousands_separator){should == ","}
67
+ its(:decimal_mark){should == "."}
68
+ end
69
+
70
+ context "with i18n but use_i18n = false" do
71
+ before :each do
72
+ reset_i18n
73
+ I18n.locale = :de
74
+ I18n.backend.store_translations(
75
+ :de, :number => {:currency => {:format => {:delimiter => ".",
76
+ :separator => ","}}}
77
+ )
78
+
79
+ Money.use_i18n = false
80
+ end
81
+
82
+ after :each do
83
+ reset_i18n
84
+ I18n.locale = :en
85
+ Money.use_i18n = true
86
+ end
87
+
88
+ subject{ Money.new(1000_00, :usd) }
89
+
90
+ its(:format){should == "$1,000.00"}
91
+ end
92
+
93
+ context "with i18n" do
94
+ after :each do
95
+ reset_i18n
96
+ I18n.locale = :en
97
+ end
98
+
99
+ context "with number.format.*" do
100
+ before :each do
101
+ reset_i18n
102
+ I18n.locale = :de
103
+ I18n.backend.store_translations(
104
+ :de, :number => {:format => {:delimiter => ".",
105
+ :separator => ","}}
106
+ )
107
+ end
108
+
109
+ subject{ Money.empty("USD") }
110
+
111
+ its(:thousands_separator){should == "."}
112
+ its(:decimal_mark){should == ","}
113
+ end
114
+
115
+ context "with number.currency.format.*" do
116
+ before :each do
117
+ reset_i18n
118
+ I18n.locale = :de
119
+ I18n.backend.store_translations(
120
+ :de, :number => {:currency => {:format => {:delimiter => ".",
121
+ :separator => ","}}}
122
+ )
123
+ end
124
+
125
+ subject{ Money.empty("USD") }
126
+
127
+ its(:thousands_separator){should == "."}
128
+ its(:decimal_mark){should == ","}
129
+ end
130
+ end
131
+
132
+ describe "#format" do
133
+ context "Japanese Locale" do
134
+ before { @before = I18n.locale; I18n.locale = :ja }
135
+ it "should format Japanese currency in Japanese properly" do
136
+ Money.new(1000, "JPY").format.should == "1,000円"
137
+ end
138
+ after { I18n.locale = @before }
139
+ end
140
+
141
+ it "returns the monetary value as a string" do
142
+ Money.ca_dollar(100).format.should == "$1.00"
143
+ Money.new(40008).format.should == "$400.08"
144
+ end
145
+
146
+ it "should respect :subunit_to_unit currency property" do
147
+ Money.new(10_00, "BHD").format.should == "ب.د1.000"
148
+ end
149
+
150
+ it "doesn't display a decimal when :subunit_to_unit is 1" do
151
+ Money.new(10_00, "CLP").format.should == "$1.000"
152
+ end
153
+
154
+ it "respects the thousands_separator and decimal_mark defaults" do
155
+ one_thousand = Proc.new do |currency|
156
+ Money.new(1000_00, currency).format
157
+ end
158
+
159
+ # Pounds
160
+ one_thousand["GBP"].should == "£1,000.00"
161
+
162
+ # Dollars
163
+ one_thousand["USD"].should == "$1,000.00"
164
+ one_thousand["CAD"].should == "$1,000.00"
165
+ one_thousand["AUD"].should == "$1,000.00"
166
+ one_thousand["NZD"].should == "$1,000.00"
167
+ one_thousand["ZWD"].should == "$1,000.00"
168
+
169
+ # Yen
170
+ one_thousand["JPY"].should == "¥100,000"
171
+ one_thousand["CNY"].should == "¥1,000.00"
172
+
173
+ # Euro
174
+ one_thousand["EUR"].should == "1.000,00 €"
175
+
176
+ # Rupees
177
+ one_thousand["INR"].should == "₨1,000.00"
178
+ one_thousand["NPR"].should == "₨1,000.00"
179
+ one_thousand["SCR"].should == "1,000.00"
180
+ one_thousand["LKR"].should == "1,000.00 ₨"
181
+
182
+ # Brazilian Real
183
+ one_thousand["BRL"].should == "R$ 1.000,00"
184
+
185
+ # Other
186
+ one_thousand["SEK"].should == "kr1,000.00"
187
+ one_thousand["GHC"].should == "₵1,000.00"
188
+ end
189
+
190
+ describe "if the monetary value is 0" do
191
+ before :each do
192
+ @money = Money.us_dollar(0)
193
+ end
194
+
195
+ it "returns 'free' when :display_free is true" do
196
+ @money.format(:display_free => true).should == 'free'
197
+ end
198
+
199
+ it "returns '$0.00' when :display_free is false or not given" do
200
+ @money.format.should == '$0.00'
201
+ @money.format(:display_free => false).should == '$0.00'
202
+ @money.format(:display_free => nil).should == '$0.00'
203
+ end
204
+
205
+ it "returns the value specified by :display_free if it's a string-like object" do
206
+ @money.format(:display_free => 'gratis').should == 'gratis'
207
+ end
208
+ end
209
+
210
+ specify "#format(:with_currency => true) works as documented" do
211
+ Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
212
+ Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
213
+ end
214
+
215
+ specify "#format(:no_cents => true) works as documented" do
216
+ Money.ca_dollar(100).format(:no_cents => true).should == "$1"
217
+ Money.ca_dollar(599).format(:no_cents => true).should == "$5"
218
+ Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
219
+ Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
220
+ end
221
+
222
+ specify "#format(:no_cents => true) should respect :subunit_to_unit currency property" do
223
+ Money.new(10_00, "BHD").format(:no_cents => true).should == "ب.د1"
224
+ end
225
+
226
+ specify "#format(:no_cents_if_whole => true) works as documented" do
227
+ Money.new(10000, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,000"
228
+ Money.new(10034, "VUV").format(:no_cents_if_whole => true, :symbol => false).should == "10,034"
229
+ Money.new(10000, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,000"
230
+ Money.new(10034, "MGA").format(:no_cents_if_whole => true, :symbol => false).should == "2,006.4"
231
+ Money.new(10000, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.000"
232
+ Money.new(10034, "VND").format(:no_cents_if_whole => true, :symbol => false).should == "1.003,4"
233
+ Money.new(10000, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100"
234
+ Money.new(10034, "USD").format(:no_cents_if_whole => true, :symbol => false).should == "100.34"
235
+ Money.new(10000, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10"
236
+ Money.new(10034, "IQD").format(:no_cents_if_whole => true, :symbol => false).should == "10.034"
237
+ end
238
+
239
+ specify "#format(:no_cents_if_whole => false) works as documented" do
240
+ Money.new(10000, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,000"
241
+ Money.new(10034, "VUV").format(:no_cents_if_whole => false, :symbol => false).should == "10,034"
242
+ Money.new(10000, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,000.0"
243
+ Money.new(10034, "MGA").format(:no_cents_if_whole => false, :symbol => false).should == "2,006.4"
244
+ Money.new(10000, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.000,0"
245
+ Money.new(10034, "VND").format(:no_cents_if_whole => false, :symbol => false).should == "1.003,4"
246
+ Money.new(10000, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.00"
247
+ Money.new(10034, "USD").format(:no_cents_if_whole => false, :symbol => false).should == "100.34"
248
+ Money.new(10000, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.000"
249
+ Money.new(10034, "IQD").format(:no_cents_if_whole => false, :symbol => false).should == "10.034"
250
+ end
251
+
252
+ specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
253
+ Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
254
+ end
255
+
256
+ specify "#format(:symbol => true) returns symbol based on the given currency code" do
257
+ one = Proc.new do |currency|
258
+ Money.new(100, currency).format(:symbol => true)
259
+ end
260
+
261
+ # Pounds
262
+ one["GBP"].should == "£1.00"
263
+
264
+ # Dollars
265
+ one["USD"].should == "$1.00"
266
+ one["CAD"].should == "$1.00"
267
+ one["AUD"].should == "$1.00"
268
+ one["NZD"].should == "$1.00"
269
+ one["ZWD"].should == "$1.00"
270
+
271
+ # Yen
272
+ one["JPY"].should == "¥100"
273
+ one["CNY"].should == "¥1.00"
274
+
275
+ # Euro
276
+ one["EUR"].should == "1,00 €"
277
+
278
+ # Rupees
279
+ one["INR"].should == "₨1.00"
280
+ one["NPR"].should == "₨1.00"
281
+ one["SCR"].should == "1.00"
282
+ one["LKR"].should == "1.00 ₨"
283
+
284
+ # Brazilian Real
285
+ one["BRL"].should == "R$ 1,00"
286
+
287
+ # Other
288
+ one["SEK"].should == "kr1.00"
289
+ one["GHC"].should == "₵1.00"
290
+ end
291
+
292
+ specify "#format(:symbol => true) returns $ when currency code is not recognized" do
293
+ currency = Money::Currency.new("EUR")
294
+ currency.should_receive(:symbol).and_return(nil)
295
+ Money.new(100, currency).format(:symbol => true).should == "1,00 ¤"
296
+ end
297
+
298
+ specify "#format(:symbol => some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
299
+ Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
300
+ Money.new(100, "EUR").format(:symbol => true).should == "1,00"
301
+ Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
302
+ end
303
+
304
+ specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
305
+ money = Money.new(100, "GBP")
306
+ money.format(:symbol => "").should == "1.00"
307
+ money.format(:symbol => nil).should == "1.00"
308
+ money.format(:symbol => false).should == "1.00"
309
+ end
310
+
311
+ specify "#format without :symbol assumes that :symbol is set to true" do
312
+ money = Money.new(100)
313
+ money.format.should == "$1.00"
314
+
315
+ money = Money.new(100, "GBP")
316
+ money.format.should == "£1.00"
317
+
318
+ money = Money.new(100, "EUR")
319
+ money.format.should == "1,00"
320
+ end
321
+
322
+ specify "#format(:decimal_mark => a decimal_mark string) works as documented" do
323
+ Money.us_dollar(100).format(:decimal_mark => ",").should == "$1,00"
324
+ end
325
+
326
+ specify "#format will default decimal_mark to '.' if currency isn't recognized" do
327
+ Money.new(100, "ZWD").format.should == "$1.00"
328
+ end
329
+
330
+ specify "#format(:separator => a separator string) works as documented" do
331
+ Money.us_dollar(100).format(:separator => ",").should == "$1,00"
332
+ end
333
+
334
+ specify "#format(:thousands_separator => a thousands_separator string) works as documented" do
335
+ Money.us_dollar(100000).format(:thousands_separator => ".").should == "$1.000.00"
336
+ Money.us_dollar(200000).format(:thousands_separator => "").should == "$2000.00"
337
+ end
338
+
339
+ specify "#format(:thousands_separator => false or nil) works as documented" do
340
+ Money.us_dollar(100000).format(:thousands_separator => false).should == "$1000.00"
341
+ Money.us_dollar(200000).format(:thousands_separator => nil).should == "$2000.00"
342
+ end
343
+
344
+ specify "#format(:delimiter => a delimiter string) works as documented" do
345
+ Money.us_dollar(100000).format(:delimiter => ".").should == "$1.000.00"
346
+ Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
347
+ end
348
+
349
+ specify "#format(:delimiter => false or nil) works as documented" do
350
+ Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
351
+ Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
352
+ end
353
+
354
+ specify "#format will default thousands_separator to ',' if currency isn't recognized" do
355
+ Money.new(100000, "ZWD").format.should == "$1,000.00"
356
+ end
357
+
358
+ specify "#format(:html => true) works as documented" do
359
+ string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
360
+ string.should == "$5.70 <span class=\"currency\">CAD</span>"
361
+ end
362
+
363
+ it "should insert commas into the result if the amount is sufficiently large" do
364
+ Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
365
+ Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
366
+ end
367
+
368
+ it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
369
+ Money.euro(1_234_567_12).format.should == "1.234.567,12 "
370
+ Money.euro(1_234_567_12).format(:no_cents => true).should == "1.234.567 "
371
+ end
372
+
373
+ it "inserts currency symbol before the amount when :symbol_position is set to :before" do
374
+ Money.euro(1_234_567_12).format(:symbol_position => :before).should == "€1.234.567,12"
375
+ end
376
+
377
+ it "inserts currency symbol after the amount when :symbol_position is set to :after" do
378
+ Money.us_dollar(1_000_000_000_12).format(:symbol_position => :after).should == "1,000,000,000.12 $"
379
+ end
380
+ end
381
+
382
+ end
383
+