money 2.1.5 → 2.2.0
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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/{MIT-LICENSE → LICENSE} +21 -21
- data/README.rdoc +97 -97
- data/Rakefile +44 -18
- data/VERSION +1 -0
- data/lib/money.rb +26 -26
- data/lib/money/core_extensions.rb +138 -138
- data/lib/money/{symbols.rb → defaults.rb} +31 -19
- data/lib/money/errors.rb +4 -4
- data/lib/money/money.rb +388 -299
- data/lib/money/variable_exchange_bank.rb +72 -72
- data/money.gemspec +65 -24
- data/test/core_extensions_spec.rb +73 -73
- data/test/exchange_bank_spec.rb +45 -45
- data/test/money_spec.rb +413 -263
- metadata +39 -13
data/test/money_spec.rb
CHANGED
@@ -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/
|
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 "
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
Money.
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
Money.
|
86
|
-
Money.new(
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
@money
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
end
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
end
|
256
|
-
|
257
|
-
specify "
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
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
|