money 6.0.1 → 6.1.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.
@@ -1,333 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "spec_helper"
4
-
5
- describe Money, "parsing" do
6
-
7
- bar = '{ "priority": 1, "iso_code": "BAR", "iso_numeric": "840", "name": "Dollar with 4 decimal places", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": "," }'
8
- eu4 = '{ "priority": 1, "iso_code": "EU4", "iso_numeric": "841", "name": "Euro with 4 decimal places", "symbol": "€", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "€", "decimal_mark": ",", "thousands_separator": "." }'
9
-
10
- describe ".parse" do
11
- it "is deprecated" do
12
- Money.should_receive(:deprecate).at_least(1).times
13
- Money.parse("1.95")
14
- end
15
-
16
- it "parses european-formatted inputs under 10EUR" do
17
- five_ninety_five = Money.new(595, 'EUR')
18
-
19
- Money.parse('EUR 5,95').should == five_ninety_five
20
- end
21
-
22
- it "parses european-formatted inputs with multiple thousands-seperators" do
23
- Money.parse('EUR 1.234.567,89').should == Money.new(123456789, 'EUR')
24
- Money.parse('EUR 1.111.234.567,89').should == Money.new(111123456789, 'EUR')
25
- end
26
-
27
- describe 'currency assumption' do
28
- context 'opted in' do
29
- before do
30
- Money.assume_from_symbol = true
31
- end
32
- it "parses formatted inputs with the currency passed as a symbol" do
33
- with_default_currency("EUR") do
34
- Money.parse("$5.95").should == Money.new(595, 'USD')
35
- end
36
- Money.parse("€5.95").should == Money.new(595, 'EUR')
37
- Money.parse(" €5.95 ").should == Money.new(595, 'EUR')
38
- Money.parse("£9.99").should == Money.new(999, 'GBP')
39
- end
40
- it 'should assume default currency if not a recognised symbol' do
41
- Money.parse("L9.99").should == Money.new(999, 'USD')
42
- end
43
- end
44
- context 'opted out' do
45
- before do
46
- Money.assume_from_symbol = false
47
- end
48
- it "parses formatted inputs with the currency passed as a symbol but ignores the symbol" do
49
- Money.parse("$5.95").should == Money.new(595, 'USD')
50
- Money.parse("€5.95").should == Money.new(595, 'USD')
51
- Money.parse(" €5.95 ").should == Money.new(595, 'USD')
52
- Money.parse("£9.99").should == Money.new(999, 'USD')
53
-
54
- end
55
- end
56
- it 'should opt out by default' do
57
- Money.assume_from_symbol.should be_false
58
- end
59
- end
60
-
61
- it "parses USD-formatted inputs under $10" do
62
- five_ninety_five = Money.new(595, 'USD')
63
-
64
- Money.parse(5.95).should == five_ninety_five
65
- Money.parse('5.95').should == five_ninety_five
66
- Money.parse('$5.95').should == five_ninety_five
67
- Money.parse("\n $5.95 \n").should == five_ninety_five
68
- Money.parse('$ 5.95').should == five_ninety_five
69
- Money.parse('$5.95 ea.').should == five_ninety_five
70
- Money.parse('$5.95, each').should == five_ninety_five
71
- end
72
-
73
- it "parses USD-formatted inputs with multiple thousands-seperators" do
74
- Money.parse('1,234,567.89').should == Money.new(123456789, 'USD')
75
- Money.parse('1,111,234,567.89').should == Money.new(111123456789, 'USD')
76
- end
77
-
78
- it "does not return a price if there is a price range" do
79
- expect { Money.parse('$5.95-10.95') }.to raise_error ArgumentError
80
- expect { Money.parse('$5.95 - 10.95') }.to raise_error ArgumentError
81
- expect { Money.parse('$5.95 - $10.95') }.to raise_error ArgumentError
82
- end
83
-
84
- it "does not return a price for completely invalid input" do
85
- # TODO: shouldn't these throw an error instead of being considered
86
- # equal to $0.0?
87
- empty_price = Money.new(0, 'USD')
88
-
89
- Money.parse(nil).should == empty_price
90
- Money.parse('hellothere').should == empty_price
91
- Money.parse('').should == empty_price
92
- end
93
-
94
- it "handles negative inputs" do
95
- five_ninety_five = Money.new(595, 'USD')
96
-
97
- Money.parse("$-5.95").should == -five_ninety_five
98
- Money.parse("-$5.95").should == -five_ninety_five
99
- Money.parse("$5.95-").should == -five_ninety_five
100
- end
101
-
102
- it "raises ArgumentError when unable to detect polarity" do
103
- expect { Money.parse('-$5.95-') }.to raise_error ArgumentError
104
- end
105
-
106
- it "parses correctly strings with exactly 3 decimal digits" do
107
- Money.parse("6,534", "EUR").should == Money.new(653, "EUR")
108
- end
109
-
110
- context "custom currencies with 4 decimal places" do
111
- before :each do
112
- Money::Currency.register(JSON.parse(bar, :symbolize_names => true))
113
- Money::Currency.register(JSON.parse(eu4, :symbolize_names => true))
114
- end
115
-
116
- after :each do
117
- Money::Currency.unregister(JSON.parse(bar, :symbolize_names => true))
118
- Money::Currency.unregister(JSON.parse(eu4, :symbolize_names => true))
119
- end
120
-
121
- # String#to_money(Currency) is equivalent to Money.parse(String, Currency)
122
- it "parses strings respecting subunit to unit, decimal and thousands separator" do
123
- "$0.4".to_money("BAR").should == Money.new(4000, "BAR")
124
- "€0,4".to_money("EU4").should == Money.new(4000, "EU4")
125
-
126
- "$0.04".to_money("BAR").should == Money.new(400, "BAR")
127
- "€0,04".to_money("EU4").should == Money.new(400, "EU4")
128
-
129
- "$0.004".to_money("BAR").should == Money.new(40, "BAR")
130
- "€0,004".to_money("EU4").should == Money.new(40, "EU4")
131
-
132
- "$0.0004".to_money("BAR").should == Money.new(4, "BAR")
133
- "€0,0004".to_money("EU4").should == Money.new(4, "EU4")
134
-
135
- "$0.0024".to_money("BAR").should == Money.new(24, "BAR")
136
- "€0,0024".to_money("EU4").should == Money.new(24, "EU4")
137
-
138
- "$0.0324".to_money("BAR").should == Money.new(324, "BAR")
139
- "€0,0324".to_money("EU4").should == Money.new(324, "EU4")
140
-
141
- "$0.5324".to_money("BAR").should == Money.new(5324, "BAR")
142
- "€0,5324".to_money("EU4").should == Money.new(5324, "EU4")
143
-
144
- "$6.5324".to_money("BAR").should == Money.new(65324, "BAR")
145
- "€6,5324".to_money("EU4").should == Money.new(65324, "EU4")
146
-
147
- "$86.5324".to_money("BAR").should == Money.new(865324, "BAR")
148
- "€86,5324".to_money("EU4").should == Money.new(865324, "EU4")
149
-
150
- "$186.5324".to_money("BAR").should == Money.new(1865324, "BAR")
151
- "€186,5324".to_money("EU4").should == Money.new(1865324, "EU4")
152
-
153
- "$3,331.0034".to_money("BAR").should == Money.new(33310034, "BAR")
154
- "€3.331,0034".to_money("EU4").should == Money.new(33310034, "EU4")
155
-
156
- "$8,883,331.0034".to_money("BAR").should == Money.new(88833310034, "BAR")
157
- "€8.883.331,0034".to_money("EU4").should == Money.new(88833310034, "EU4")
158
- end
159
- end
160
- end
161
-
162
- describe ".from_string" do
163
- it "converts given amount to cents" do
164
- Money.from_string("1").should == Money.new(1_00)
165
- Money.from_string("1").should == Money.new(1_00, "USD")
166
- Money.from_string("1", "EUR").should == Money.new(1_00, "EUR")
167
- end
168
-
169
- it "respects :subunit_to_unit currency property" do
170
- Money.from_string("1", "USD").should == Money.new(1_00, "USD")
171
- Money.from_string("1", "TND").should == Money.new(1_000, "TND")
172
- Money.from_string("1", "CLP").should == Money.new(1, "CLP")
173
- end
174
-
175
- it "accepts a currency options" do
176
- m = Money.from_string("1")
177
- m.currency.should == Money.default_currency
178
-
179
- m = Money.from_string("1", Money::Currency.wrap("EUR"))
180
- m.currency.should == Money::Currency.wrap("EUR")
181
-
182
- m = Money.from_string("1", "EUR")
183
- m.currency.should == Money::Currency.wrap("EUR")
184
- end
185
- end
186
-
187
- describe ".from_fixnum" do
188
- it "converts given amount to cents" do
189
- Money.from_fixnum(1).should == Money.new(1_00)
190
- Money.from_fixnum(1).should == Money.new(1_00, "USD")
191
- Money.from_fixnum(1, "EUR").should == Money.new(1_00, "EUR")
192
- end
193
-
194
- it "should respect :subunit_to_unit currency property" do
195
- Money.from_fixnum(1, "USD").should == Money.new(1_00, "USD")
196
- Money.from_fixnum(1, "TND").should == Money.new(1_000, "TND")
197
- Money.from_fixnum(1, "CLP").should == Money.new(1, "CLP")
198
- end
199
-
200
- it "accepts a currency options" do
201
- m = Money.from_fixnum(1)
202
- m.currency.should == Money.default_currency
203
-
204
- m = Money.from_fixnum(1, Money::Currency.wrap("EUR"))
205
- m.currency.should == Money::Currency.wrap("EUR")
206
-
207
- m = Money.from_fixnum(1, "EUR")
208
- m.currency.should == Money::Currency.wrap("EUR")
209
- end
210
- end
211
-
212
- describe ".from_float" do
213
- it "converts given amount to cents" do
214
- Money.from_float(1.2).should == Money.new(1_20)
215
- Money.from_float(1.2).should == Money.new(1_20, "USD")
216
- Money.from_float(1.2, "EUR").should == Money.new(1_20, "EUR")
217
- end
218
-
219
- it "respects :subunit_to_unit currency property" do
220
- Money.from_float(1.2, "USD").should == Money.new(1_20, "USD")
221
- Money.from_float(1.2, "TND").should == Money.new(1_200, "TND")
222
- Money.from_float(1.2, "CLP").should == Money.new(1, "CLP")
223
- end
224
-
225
- it "accepts a currency options" do
226
- m = Money.from_float(1.2)
227
- m.currency.should == Money.default_currency
228
-
229
- m = Money.from_float(1.2, Money::Currency.wrap("EUR"))
230
- m.currency.should == Money::Currency.wrap("EUR")
231
-
232
- m = Money.from_float(1.2, "EUR")
233
- m.currency.should == Money::Currency.wrap("EUR")
234
- end
235
- end
236
-
237
- describe ".from_bigdecimal" do
238
- it "converts given amount to cents" do
239
- Money.from_bigdecimal(BigDecimal.new("1")).should == Money.new(1_00)
240
- Money.from_bigdecimal(BigDecimal.new("1")).should == Money.new(1_00, "USD")
241
- Money.from_bigdecimal(BigDecimal.new("1"), "EUR").should == Money.new(1_00, "EUR")
242
- end
243
-
244
- it "respects :subunit_to_unit currency property" do
245
- Money.from_bigdecimal(BigDecimal.new("1"), "USD").should == Money.new(1_00, "USD")
246
- Money.from_bigdecimal(BigDecimal.new("1"), "TND").should == Money.new(1_000, "TND")
247
- Money.from_bigdecimal(BigDecimal.new("1"), "CLP").should == Money.new(1, "CLP")
248
- end
249
-
250
- it "accepts a currency options" do
251
- m = Money.from_bigdecimal(BigDecimal.new("1"))
252
- m.currency.should == Money.default_currency
253
-
254
- m = Money.from_bigdecimal(BigDecimal.new("1"), Money::Currency.wrap("EUR"))
255
- m.currency.should == Money::Currency.wrap("EUR")
256
-
257
- m = Money.from_bigdecimal(BigDecimal.new("1"), "EUR")
258
- m.currency.should == Money::Currency.wrap("EUR")
259
- end
260
-
261
- context "infinite_precision = true" do
262
- before do
263
- Money.infinite_precision = true
264
- end
265
-
266
- after do
267
- Money.infinite_precision = false
268
- end
269
-
270
- it "keeps precision" do
271
- Money.from_bigdecimal(BigDecimal.new("1.23456")).should == Money.new(123.456)
272
- Money.from_bigdecimal(BigDecimal.new("-1.23456")).should == Money.new(-123.456)
273
- Money.from_bigdecimal(BigDecimal.new("1.23456")).should == Money.new(123.456, "USD")
274
- Money.from_bigdecimal(BigDecimal.new("1.23456"), "EUR").should == Money.new(123.456, "EUR")
275
- end
276
- end
277
- end
278
-
279
- describe ".from_numeric" do
280
- it "converts given amount to cents" do
281
- Money.from_numeric(1).should == Money.new(1_00)
282
- Money.from_numeric(1.0).should == Money.new(1_00)
283
- Money.from_numeric(BigDecimal.new("1")).should == Money.new(1_00)
284
- end
285
-
286
- it "raises ArgumentError with unsupported argument" do
287
- expect { Money.from_numeric("100") }.to raise_error(ArgumentError)
288
- end
289
-
290
- it "optimizes workload" do
291
- Monetize.should_receive(:from_fixnum).with(1, "USD").and_return(Money.new(1_00, "USD"))
292
- Money.from_numeric(1, "USD").should == Money.new(1_00, "USD")
293
- Monetize.should_receive(:from_bigdecimal).with(BigDecimal.new("1.0"), "USD").and_return(Money.new(1_00, "USD"))
294
- Money.from_numeric(1.0, "USD").should == Money.new(1_00, "USD")
295
- end
296
-
297
- it "respects :subunit_to_unit currency property" do
298
- Money.from_numeric(1, "USD").should == Money.new(1_00, "USD")
299
- Money.from_numeric(1, "TND").should == Money.new(1_000, "TND")
300
- Money.from_numeric(1, "CLP").should == Money.new(1, "CLP")
301
- end
302
-
303
- it "accepts a bank option" do
304
- Money.from_numeric(1).should == Money.new(1_00)
305
- Money.from_numeric(1).should == Money.new(1_00, "USD")
306
- Money.from_numeric(1, "EUR").should == Money.new(1_00, "EUR")
307
- end
308
-
309
- it "accepts a currency options" do
310
- m = Money.from_numeric(1)
311
- m.currency.should == Money.default_currency
312
-
313
- m = Money.from_numeric(1, Money::Currency.wrap("EUR"))
314
- m.currency.should == Money::Currency.wrap("EUR")
315
-
316
- m = Money.from_numeric(1, "EUR")
317
- m.currency.should == Money::Currency.wrap("EUR")
318
- end
319
- end
320
-
321
- describe ".extract_cents" do
322
- it "correctly treats pipe marks '|' in input (regression test)" do
323
- Money.extract_cents('100|0').should == Money.extract_cents('100!0')
324
- end
325
- end
326
-
327
- context "given the same inputs to .parse and .from_*" do
328
- it "gives the same results" do
329
- 4.635.to_money.should == "4.635".to_money
330
- end
331
- end
332
-
333
- end
@@ -1,13 +0,0 @@
1
- module DefaultCurrencyHelper
2
-
3
- def with_default_currency(iso_code)
4
- original_default = Money.default_currency
5
- begin
6
- Money.default_currency = Money::Currency.new(iso_code)
7
- yield
8
- ensure
9
- Money.default_currency = original_default
10
- end
11
- end
12
-
13
- end