money 4.0.1 → 4.0.2

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