money 2.1.5 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,263 +1,413 @@
1
- # encoding: utf-8
2
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
3
- require 'money/money'
4
- require 'money/symbols'
5
-
6
- describe Money do
7
- it "is associated to the singleton instance of VariableExchangeBank by default" do
8
- Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
9
- end
10
-
11
- specify "#cents returns the amount of cents passed to the constructor" do
12
- Money.new(200_00, "USD").cents.should == 200_00
13
- end
14
-
15
- it "rounds the given cents to an integer" do
16
- Money.new(1.00, "USD").cents.should == 1
17
- Money.new(1.01, "USD").cents.should == 1
18
- Money.new(1.50, "USD").cents.should == 2
19
- end
20
-
21
- specify "#currency returns the currency passed to the constructor" do
22
- Money.new(200_00, "USD").currency.should == "USD"
23
- end
24
-
25
- specify "#zero? returns whether the amount is 0" do
26
- Money.new(0, "USD").should be_zero
27
- Money.new(0, "EUR").should be_zero
28
- Money.new(1, "USD").should_not be_zero
29
- Money.new(10, "YEN").should_not be_zero
30
- Money.new(-1, "EUR").should_not be_zero
31
- end
32
-
33
- specify "#exchange_to exchanges the amount via its exchange bank" do
34
- money = Money.new(100_00, "USD")
35
- money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
36
- money.exchange_to("EUR")
37
- end
38
-
39
- specify "#exchange_to exchanges the amount properly" do
40
- money = Money.new(100_00, "USD")
41
- money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
42
- money.exchange_to("EUR").should == Money.new(200_00, "EUR")
43
- end
44
-
45
- specify "#== returns true if and only if their amount and currency are equal" do
46
- Money.new(1_00, "USD").should == Money.new(1_00, "USD")
47
- Money.new(1_00, "USD").should_not == Money.new(1_00, "EUR")
48
- Money.new(1_00, "USD").should_not == Money.new(2_00, "USD")
49
- Money.new(1_00, "USD").should_not == Money.new(99_00, "EUR")
50
- end
51
-
52
- specify "#* multiplies the money's amount by the multiplier while retaining the currency" do
53
- (Money.new(1_00, "USD") * 10).should == Money.new(10_00, "USD")
54
- end
55
-
56
- specify "#* divides the money's amount by the divisor while retaining the currency" do
57
- (Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
58
- end
59
-
60
- specify "Money.empty creates a new Money object of 0 cents" do
61
- Money.empty.should == Money.new(0)
62
- end
63
-
64
- specify "Money.ca_dollar creates a new Money object of the given value in CAD" do
65
- Money.ca_dollar(50).should == Money.new(50, "CAD")
66
- end
67
-
68
- specify "Money.ca_dollar creates a new Money object of the given value in USD" do
69
- Money.us_dollar(50).should == Money.new(50, "USD")
70
- end
71
-
72
- specify "Money.ca_dollar creates a new Money object of the given value in EUR" do
73
- Money.euro(50).should == Money.new(50, "EUR")
74
- end
75
-
76
- specify "Money.new accepts { :currency => 'foo' } as the value for the 'currency' argument" do
77
- money = Money.new(20, :currency => "EUR")
78
- money.currency.should == "EUR"
79
-
80
- money = Money.new(20, :currency => nil)
81
- money.currency.should == Money.default_currency
82
- end
83
-
84
- specify "Money.add_rate works" do
85
- Money.add_rate("EUR", "USD", 10)
86
- Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
87
- end
88
-
89
- describe "#format" do
90
- it "returns the monetary value as a string" do
91
- Money.ca_dollar(100).format.should == "$1.00"
92
- end
93
-
94
- describe "if the monetary value is 0" do
95
- before :each do
96
- @money = Money.us_dollar(0)
97
- end
98
-
99
- it "returns 'free' when :display_free is true" do
100
- @money.format(:display_free => true).should == 'free'
101
- end
102
-
103
- it "returns '$0.00' when :display_free is false or not given" do
104
- @money.format.should == '$0.00'
105
- @money.format(:display_free => false).should == '$0.00'
106
- @money.format(:display_free => nil).should == '$0.00'
107
- end
108
-
109
- it "returns the value specified by :display_free if it's a string-like object" do
110
- @money.format(:display_free => 'gratis').should == 'gratis'
111
- end
112
- end
113
-
114
- specify "#format(:with_currency => true) works as documented" do
115
- Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
116
- Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
117
- Money.us_dollar(85).format(:with_currency).should == "$0.85 USD"
118
- end
119
-
120
- specify "#format(:with_currency) works as documented" do
121
- Money.ca_dollar(100).format(:with_currency).should == "$1.00 CAD"
122
- Money.us_dollar(85).format(:with_currency).should == "$0.85 USD"
123
- end
124
-
125
- specify "#format(:no_cents => true) works as documented" do
126
- Money.ca_dollar(100).format(:no_cents => true).should == "$1"
127
- Money.ca_dollar(599).format(:no_cents => true).should == "$5"
128
- Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
129
- Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
130
- end
131
-
132
- specify "#format(:no_cents) works as documented" do
133
- Money.ca_dollar(100).format(:no_cents).should == "$1"
134
- Money.ca_dollar(599).format(:no_cents).should == "$5"
135
- Money.ca_dollar(570).format(:no_cents, :with_currency).should == "$5 CAD"
136
- Money.ca_dollar(39000).format(:no_cents).should == "$390"
137
- end
138
-
139
- specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
140
- Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
141
- end
142
-
143
- specify "#format(:symbol => true) returns symbol based on the given currency code" do
144
- one = Proc.new do |currency|
145
- Money.new(100, currency).format(:symbol => true)
146
- end
147
-
148
- # Pounds
149
- one["GBP"].should == "£1.00"
150
-
151
- # Dollars
152
- one["USD"].should == "$1.00"
153
- one["CAD"].should == "$1.00"
154
- one["AUD"].should == "$1.00"
155
- one["NZD"].should == "$1.00"
156
- one["ZWD"].should == "Z$1.00"
157
-
158
- # Yen
159
- one["JPY"].should == "¥1.00"
160
- one["CNY"].should == "¥1.00"
161
-
162
- # Euro
163
- one["EUR"].should == "€1.00"
164
-
165
- # Rupees
166
- one["INR"].should == "₨1.00"
167
- one["NPR"].should == "₨1.00"
168
- one["SCR"].should == "₨1.00"
169
- one["LKR"].should == "₨1.00"
170
-
171
- # Brazilian Real
172
- one["BRL"].should == "R$1.00"
173
-
174
- # Other
175
- one["SEK"].should == "kr1.00"
176
- one["GHC"].should == "¢1.00"
177
- end
178
-
179
- specify "#format(:symbol => true) returns $ when currency code is not recognized" do
180
- Money.new(100, "XYZ").format(:symbol => true).should == "$1.00"
181
- end
182
-
183
- specify "#format(:symbol => some non-Boolean value that evaluates to true) returs symbol based on the given currency code" do
184
- Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
185
- Money.new(100, "EUR").format(:symbol => true).should == "1.00"
186
- Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
187
- end
188
-
189
- specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
190
- money = Money.new(100, "GBP")
191
- money.format(:symbol => "").should == "1.00"
192
- money.format(:symbol => nil).should == "1.00"
193
- money.format(:symbol => false).should == "1.00"
194
- end
195
-
196
- specify "#format without :symbol assumes that :symbol is set to true" do
197
- money = Money.new(100)
198
- money.format.should == "$1.00"
199
-
200
- money = Money.new(100, "GBP")
201
- money.format.should == "£1.00"
202
-
203
- money = Money.new(100, "XYZ")
204
- money.format.should == "$1.00"
205
- end
206
-
207
- specify "#format(:html => true) works as documented" do
208
- string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
209
- string.should == "$5.70 <span class=\"currency\">CAD</span>"
210
- end
211
-
212
- it "should insert commas into the result if the amount is sufficiently large" do
213
- Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
214
- Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
215
- end
216
- end
217
- end
218
-
219
- describe "Actions involving two Money objects" do
220
- describe "if the other Money object has the same currency" do
221
- specify "#<=> compares the two objects' amounts" do
222
- (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
223
- (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
224
- (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
225
- end
226
-
227
- specify "#+ adds the other object's amount to the current object's amount while retaining the currency" do
228
- (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
229
- end
230
-
231
- specify "#- substracts the other object's amount from the current object's amount while retaining the currency" do
232
- (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
233
- end
234
- end
235
-
236
- describe "if the other Money object has a different currency" do
237
- specify "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
238
- target = Money.new(200_00, "EUR")
239
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
240
- (Money.new(100_00, "USD") <=> target).should < 0
241
-
242
- target = Money.new(200_00, "EUR")
243
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
244
- (Money.new(100_00, "USD") <=> target).should == 0
245
-
246
- target = Money.new(200_00, "EUR")
247
- target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
248
- (Money.new(100_00, "USD") <=> target).should > 0
249
- end
250
-
251
- specify "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
252
- other = Money.new(90, "EUR")
253
- other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
254
- (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
255
- end
256
-
257
- specify "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
258
- other = Money.new(90, "EUR")
259
- other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
260
- (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
261
- end
262
- end
263
- end
1
+ # encoding: utf-8
2
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
3
+ require 'money/money'
4
+ require 'money/defaults'
5
+
6
+ describe Money do
7
+ it "is associated to the singleton instance of VariableExchangeBank by default" do
8
+ Money.new(0).bank.object_id.should == Money::VariableExchangeBank.instance.object_id
9
+ end
10
+
11
+ specify "#cents returns the amount of cents passed to the constructor" do
12
+ Money.new(200_00, "USD").cents.should == 200_00
13
+ end
14
+
15
+ it "rounds the given cents to an integer" do
16
+ Money.new(1.00, "USD").cents.should == 1
17
+ Money.new(1.01, "USD").cents.should == 1
18
+ Money.new(1.50, "USD").cents.should == 2
19
+ end
20
+
21
+ specify "#currency returns the currency passed to the constructor" do
22
+ Money.new(200_00, "USD").currency.should == "USD"
23
+ end
24
+
25
+ specify "#zero? returns whether the amount is 0" do
26
+ Money.new(0, "USD").should be_zero
27
+ Money.new(0, "EUR").should be_zero
28
+ Money.new(1, "USD").should_not be_zero
29
+ Money.new(10, "YEN").should_not be_zero
30
+ Money.new(-1, "EUR").should_not be_zero
31
+ end
32
+
33
+ specify "#exchange_to exchanges the amount via its exchange bank" do
34
+ money = Money.new(100_00, "USD")
35
+ money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
36
+ money.exchange_to("EUR")
37
+ end
38
+
39
+ specify "#exchange_to exchanges the amount properly" do
40
+ money = Money.new(100_00, "USD")
41
+ money.bank.should_receive(:exchange).with(100_00, "USD", "EUR").and_return(200_00)
42
+ money.exchange_to("EUR").should == Money.new(200_00, "EUR")
43
+ end
44
+
45
+ specify "#== returns true if and only if their amount and currency are equal" do
46
+ Money.new(1_00, "USD").should == Money.new(1_00, "USD")
47
+ Money.new(1_00, "USD").should_not == Money.new(1_00, "EUR")
48
+ Money.new(1_00, "USD").should_not == Money.new(2_00, "USD")
49
+ Money.new(1_00, "USD").should_not == Money.new(99_00, "EUR")
50
+ end
51
+
52
+ specify "#== can be used to compare with a String money value" do
53
+ Money.new(1_00, "USD").should == "1.00"
54
+ Money.new(1_00, "USD").should_not == "2.00"
55
+ Money.new(1_00, "GBP").should_not == "1.00"
56
+ end
57
+
58
+ specify "#== can be used to compare with a Numeric money value" do
59
+ Money.new(1_00, "USD").should == 1
60
+ Money.new(1_57, "USD").should == 1.57
61
+ Money.new(1_00, "USD").should_not == 2
62
+ Money.new(1_00, "GBP").should_not == 1
63
+ end
64
+
65
+ specify "#== can be used to compare with an object that responds to #to_money" do
66
+ klass = Class.new do
67
+ def initialize(money)
68
+ @money = money
69
+ end
70
+
71
+ def to_money
72
+ @money
73
+ end
74
+ end
75
+
76
+ Money.new(1_00, "USD").should == klass.new(Money.new(1_00, "USD"))
77
+ Money.new(2_50, "USD").should == klass.new(Money.new(2_50, "USD"))
78
+ Money.new(2_50, "USD").should_not == klass.new(Money.new(3_00, "USD"))
79
+ Money.new(1_00, "GBP").should_not == klass.new(Money.new(1_00, "USD"))
80
+ end
81
+
82
+ specify "#== returns false if used to compare with an object that doesn't respond to #to_money" do
83
+ Money.new(1_00, "USD").should_not == Object.new
84
+ Money.new(1_00, "USD").should_not == Class
85
+ Money.new(1_00, "USD").should_not == Kernel
86
+ Money.new(1_00, "USD").should_not == /foo/
87
+ Money.new(1_00, "USD").should_not == nil
88
+ end
89
+
90
+ specify "#<=> can be used to compare with a String money value" do
91
+ (Money.new(1_00) <=> "1.00").should == 0
92
+ (Money.new(1_00) <=> ".99").should > 0
93
+ (Money.new(1_00) <=> "2.00").should < 0
94
+ end
95
+
96
+ specify "#<=> can be used to compare with a Numeric money value" do
97
+ (Money.new(1_00) <=> 1).should == 0
98
+ (Money.new(1_00) <=> 0.99).should > 0
99
+ (Money.new(1_00) <=> 2.00).should < 0
100
+ end
101
+
102
+ specify "#<=> can be used to compare with an object that responds to #to_money" do
103
+ klass = Class.new do
104
+ def initialize(money)
105
+ @money = money
106
+ end
107
+
108
+ def to_money
109
+ @money
110
+ end
111
+ end
112
+
113
+ (Money.new(1_00) <=> klass.new(Money.new(1_00))).should == 0
114
+ (Money.new(1_00) <=> klass.new(Money.new(99))).should > 0
115
+ (Money.new(1_00) <=> klass.new(Money.new(2_00))).should < 0
116
+ end
117
+
118
+ specify "#<=> raises ArgumentError when used to compare with an object that doesn't respond to #to_money" do
119
+ expected_message = /comparison .+ failed/
120
+ lambda{ Money.new(1_00) <=> Object.new }.should raise_error(ArgumentError, expected_message)
121
+ lambda{ Money.new(1_00) <=> Class }.should raise_error(ArgumentError, expected_message)
122
+ lambda{ Money.new(1_00) <=> Kernel }.should raise_error(ArgumentError, expected_message)
123
+ lambda{ Money.new(1_00) <=> /foo/ }.should raise_error(ArgumentError, expected_message)
124
+ end
125
+
126
+ specify "#* multiplies the money's amount by the multiplier while retaining the currency" do
127
+ (Money.new(1_00, "USD") * 10).should == Money.new(10_00, "USD")
128
+ end
129
+
130
+ specify "#/ divides the money's amount by the divisor while retaining the currency" do
131
+ (Money.new(10_00, "USD") / 10).should == Money.new(1_00, "USD")
132
+ end
133
+
134
+ specify "Money.empty creates a new Money object of 0 cents" do
135
+ Money.empty.should == Money.new(0)
136
+ end
137
+
138
+ specify "Money.ca_dollar creates a new Money object of the given value in CAD" do
139
+ Money.ca_dollar(50).should == Money.new(50, "CAD")
140
+ end
141
+
142
+ specify "Money.ca_dollar creates a new Money object of the given value in USD" do
143
+ Money.us_dollar(50).should == Money.new(50, "USD")
144
+ end
145
+
146
+ specify "Money.ca_dollar creates a new Money object of the given value in EUR" do
147
+ Money.euro(50).should == Money.new(50, "EUR")
148
+ end
149
+
150
+ specify "Money.new accepts { :currency => 'foo' } as the value for the 'currency' argument" do
151
+ money = Money.new(20, :currency => "EUR")
152
+ money.currency.should == "EUR"
153
+
154
+ money = Money.new(20, :currency => nil)
155
+ money.currency.should == Money.default_currency
156
+ end
157
+
158
+ specify "Money.add_rate works" do
159
+ Money.add_rate("EUR", "USD", 10)
160
+ Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
161
+ end
162
+
163
+ specify "Money.to_s works" do
164
+ Money.new(10_00).to_s.should == "10.00"
165
+ end
166
+
167
+ specify "Money.to_f works" do
168
+ Money.new(10_00).to_f.should == 10.0
169
+ end
170
+
171
+ describe "#format" do
172
+ it "returns the monetary value as a string" do
173
+ Money.ca_dollar(100).format.should == "$1.00"
174
+ end
175
+
176
+ specify "respects the delimiter and separator defaults" do
177
+ one_thousand = Proc.new do |currency|
178
+ Money.new(1000_00, currency).format
179
+ end
180
+
181
+ # Pounds
182
+ one_thousand["GBP"].should == "£1,000.00"
183
+
184
+ # Dollars
185
+ one_thousand["USD"].should == "$1,000.00"
186
+ one_thousand["CAD"].should == "$1,000.00"
187
+ one_thousand["AUD"].should == "$1,000.00"
188
+ one_thousand["NZD"].should == "$1,000.00"
189
+ one_thousand["ZWD"].should == "Z$1,000.00"
190
+
191
+ # Yen
192
+ one_thousand["JPY"].should == "¥1,000.00"
193
+ one_thousand["CNY"].should == "¥1,000.00"
194
+
195
+ # Euro
196
+ one_thousand["EUR"].should == "€1,000.00"
197
+
198
+ # Rupees
199
+ one_thousand["INR"].should == "₨1,000.00"
200
+ one_thousand["NPR"].should == "₨1,000.00"
201
+ one_thousand["SCR"].should == "1,000.00"
202
+ one_thousand["LKR"].should == "₨1,000.00"
203
+
204
+ # Brazilian Real
205
+ one_thousand["BRL"].should == "R$ 1.000,00"
206
+
207
+ # Other
208
+ one_thousand["SEK"].should == "kr1,000.00"
209
+ one_thousand["GHC"].should == "¢1,000.00"
210
+ end
211
+
212
+ describe "if the monetary value is 0" do
213
+ before :each do
214
+ @money = Money.us_dollar(0)
215
+ end
216
+
217
+ it "returns 'free' when :display_free is true" do
218
+ @money.format(:display_free => true).should == 'free'
219
+ end
220
+
221
+ it "returns '$0.00' when :display_free is false or not given" do
222
+ @money.format.should == '$0.00'
223
+ @money.format(:display_free => false).should == '$0.00'
224
+ @money.format(:display_free => nil).should == '$0.00'
225
+ end
226
+
227
+ it "returns the value specified by :display_free if it's a string-like object" do
228
+ @money.format(:display_free => 'gratis').should == 'gratis'
229
+ end
230
+ end
231
+
232
+ specify "#format(:with_currency => true) works as documented" do
233
+ Money.ca_dollar(100).format(:with_currency => true).should == "$1.00 CAD"
234
+ Money.us_dollar(85).format(:with_currency => true).should == "$0.85 USD"
235
+ Money.us_dollar(85).format(:with_currency).should == "$0.85 USD"
236
+ end
237
+
238
+ specify "#format(:with_currency) works as documented" do
239
+ Money.ca_dollar(100).format(:with_currency).should == "$1.00 CAD"
240
+ Money.us_dollar(85).format(:with_currency).should == "$0.85 USD"
241
+ end
242
+
243
+ specify "#format(:no_cents => true) works as documented" do
244
+ Money.ca_dollar(100).format(:no_cents => true).should == "$1"
245
+ Money.ca_dollar(599).format(:no_cents => true).should == "$5"
246
+ Money.ca_dollar(570).format(:no_cents => true, :with_currency => true).should == "$5 CAD"
247
+ Money.ca_dollar(39000).format(:no_cents => true).should == "$390"
248
+ end
249
+
250
+ specify "#format(:no_cents) works as documented" do
251
+ Money.ca_dollar(100).format(:no_cents).should == "$1"
252
+ Money.ca_dollar(599).format(:no_cents).should == "$5"
253
+ Money.ca_dollar(570).format(:no_cents, :with_currency).should == "$5 CAD"
254
+ Money.ca_dollar(39000).format(:no_cents).should == "$390"
255
+ end
256
+
257
+ specify "#format(:symbol => a symbol string) uses the given value as the money symbol" do
258
+ Money.new(100, "GBP").format(:symbol => "£").should == "£1.00"
259
+ end
260
+
261
+ specify "#format(:symbol => true) returns symbol based on the given currency code" do
262
+ one = Proc.new do |currency|
263
+ Money.new(100, currency).format(:symbol => true)
264
+ end
265
+
266
+ # Pounds
267
+ one["GBP"].should == "£1.00"
268
+
269
+ # Dollars
270
+ one["USD"].should == "$1.00"
271
+ one["CAD"].should == "$1.00"
272
+ one["AUD"].should == "$1.00"
273
+ one["NZD"].should == "$1.00"
274
+ one["ZWD"].should == "Z$1.00"
275
+
276
+ # Yen
277
+ one["JPY"].should == "¥1.00"
278
+ one["CNY"].should == "¥1.00"
279
+
280
+ # Euro
281
+ one["EUR"].should == "€1.00"
282
+
283
+ # Rupees
284
+ one["INR"].should == "₨1.00"
285
+ one["NPR"].should == "₨1.00"
286
+ one["SCR"].should == "₨1.00"
287
+ one["LKR"].should == "₨1.00"
288
+
289
+ # Brazilian Real
290
+ one["BRL"].should == "R$ 1,00"
291
+
292
+ # Other
293
+ one["SEK"].should == "kr1.00"
294
+ one["GHC"].should == "¢1.00"
295
+ end
296
+
297
+ specify "#format(:symbol => true) returns $ when currency code is not recognized" do
298
+ Money.new(100, "XYZ").format(:symbol => true).should == "$1.00"
299
+ end
300
+
301
+ specify "#format(:symbol => some non-Boolean value that evaluates to true) returs symbol based on the given currency code" do
302
+ Money.new(100, "GBP").format(:symbol => true).should == "£1.00"
303
+ Money.new(100, "EUR").format(:symbol => true).should == "€1.00"
304
+ Money.new(100, "SEK").format(:symbol => true).should == "kr1.00"
305
+ end
306
+
307
+ specify "#format with :symbol == "", nil or false returns the amount without a symbol" do
308
+ money = Money.new(100, "GBP")
309
+ money.format(:symbol => "").should == "1.00"
310
+ money.format(:symbol => nil).should == "1.00"
311
+ money.format(:symbol => false).should == "1.00"
312
+ end
313
+
314
+ specify "#format without :symbol assumes that :symbol is set to true" do
315
+ money = Money.new(100)
316
+ money.format.should == "$1.00"
317
+
318
+ money = Money.new(100, "GBP")
319
+ money.format.should == "£1.00"
320
+
321
+ money = Money.new(100, "XYZ")
322
+ money.format.should == "$1.00"
323
+ end
324
+
325
+ specify "#format(:separator => a separator string) works as documented" do
326
+ Money.us_dollar(100).format(:separator => ",").should == "$1,00"
327
+ end
328
+
329
+ specify "#format will default separator to '.' if currency isn't recognized" do
330
+ Money.new(100, "FOO").format.should == "$1.00"
331
+ end
332
+
333
+ specify "#format(:delimiter => a delimiter string) works as documented" do
334
+ Money.us_dollar(100000).format(:delimiter => ".").should == "$1.000.00"
335
+ Money.us_dollar(200000).format(:delimiter => "").should == "$2000.00"
336
+ end
337
+
338
+ specify "#format(:delimiter => false or nil) works as documented" do
339
+ Money.us_dollar(100000).format(:delimiter => false).should == "$1000.00"
340
+ Money.us_dollar(200000).format(:delimiter => nil).should == "$2000.00"
341
+ end
342
+
343
+ specify "#format will default delimiter to ',' if currency isn't recognized" do
344
+ Money.new(100000, "FOO").format.should == "$1,000.00"
345
+ end
346
+
347
+ specify "#format(:html => true) works as documented" do
348
+ string = Money.ca_dollar(570).format(:html => true, :with_currency => true)
349
+ string.should == "$5.70 <span class=\"currency\">CAD</span>"
350
+ end
351
+
352
+ it "should insert commas into the result if the amount is sufficiently large" do
353
+ Money.us_dollar(1_000_000_000_12).format.should == "$1,000,000,000.12"
354
+ Money.us_dollar(1_000_000_000_12).format(:no_cents => true).should == "$1,000,000,000"
355
+ end
356
+ end
357
+ end
358
+
359
+ describe "Actions involving two Money objects" do
360
+ describe "if the other Money object has the same currency" do
361
+ specify "#<=> compares the two objects' amounts" do
362
+ (Money.new(1_00, "USD") <=> Money.new(1_00, "USD")).should == 0
363
+ (Money.new(1_00, "USD") <=> Money.new(99, "USD")).should > 0
364
+ (Money.new(1_00, "USD") <=> Money.new(2_00, "USD")).should < 0
365
+ end
366
+
367
+ specify "#+ adds the other object's amount to the current object's amount while retaining the currency" do
368
+ (Money.new(10_00, "USD") + Money.new(90, "USD")).should == Money.new(10_90, "USD")
369
+ end
370
+
371
+ specify "#- substracts the other object's amount from the current object's amount while retaining the currency" do
372
+ (Money.new(10_00, "USD") - Money.new(90, "USD")).should == Money.new(9_10, "USD")
373
+ end
374
+
375
+ specify "#/ divides the current object's amount by the other object's amount resulting in a float" do
376
+ (Money.new(10_00, "USD") / Money.new(100_00, "USD")).should == 0.1
377
+ end
378
+ end
379
+
380
+ describe "if the other Money object has a different currency" do
381
+ specify "#<=> compares the two objects' amount after converting the other object's amount to its own currency" do
382
+ target = Money.new(200_00, "EUR")
383
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(300_00, "USD"))
384
+ (Money.new(100_00, "USD") <=> target).should < 0
385
+
386
+ target = Money.new(200_00, "EUR")
387
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
388
+ (Money.new(100_00, "USD") <=> target).should == 0
389
+
390
+ target = Money.new(200_00, "EUR")
391
+ target.should_receive(:exchange_to).with("USD").and_return(Money.new(99_00, "USD"))
392
+ (Money.new(100_00, "USD") <=> target).should > 0
393
+ end
394
+
395
+ specify "#+ adds the other object's amount, converted to this object's currency, to this object's amount while retaining its currency" do
396
+ other = Money.new(90, "EUR")
397
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
398
+ (Money.new(10_00, "USD") + other).should == Money.new(19_00, "USD")
399
+ end
400
+
401
+ specify "#- substracts the other object's amount, converted to this object's currency, from this object's amount while retaining its currency" do
402
+ other = Money.new(90, "EUR")
403
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(9_00, "USD"))
404
+ (Money.new(10_00, "USD") - other).should == Money.new(1_00, "USD")
405
+ end
406
+
407
+ specify "#/ divides the this object's amount by the other objects's amount, converted to this object's currency, resulting in a float" do
408
+ other = Money.new(1000, "EUR")
409
+ other.should_receive(:exchange_to).with("USD").and_return(Money.new(100_00, "USD"))
410
+ (Money.new(10_00, "USD") / other).should == 0.1
411
+ end
412
+ end
413
+ end