money 6.13.2 → 6.13.3

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,400 +0,0 @@
1
- # encoding: utf-8
2
-
3
- class Money
4
- describe Currency do
5
-
6
- FOO = '{ "priority": 1, "iso_code": "FOO", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 1000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "smallest_denomination": 1 }'
7
-
8
- def register_foo(opts={})
9
- foo_attrs = JSON.parse(FOO, symbolize_names: true)
10
- # Pass an array of attribute names to 'skip' to remove them from the 'FOO'
11
- # json before registering foo as a currency.
12
- Array(opts[:skip]).each { |attr| foo_attrs.delete(attr) }
13
- Money::Currency.register(foo_attrs)
14
- end
15
-
16
- def unregister_foo
17
- Currency.unregister(JSON.parse(FOO, symbolize_names: true))
18
- end
19
-
20
- describe "UnknownCurrency" do
21
- it "is a subclass of ArgumentError" do
22
- expect(Currency::UnknownCurrency < ArgumentError).to be true
23
- end
24
- end
25
-
26
- describe ".find" do
27
- before { register_foo }
28
- after { unregister_foo }
29
-
30
- it "returns currency matching given id" do
31
- expected = Currency.new(:foo)
32
- expect(Currency.find(:foo)).to be expected
33
- expect(Currency.find(:FOO)).to be expected
34
- expect(Currency.find("foo")).to be expected
35
- expect(Currency.find("FOO")).to be expected
36
- end
37
-
38
- it "returns nil unless currency matching given id" do
39
- expect(Currency.find("ZZZ")).to be_nil
40
- end
41
- end
42
-
43
- describe ".find_by_iso_numeric" do
44
- it "returns currency matching given numeric code" do
45
- expect(Currency.find_by_iso_numeric(978)).to eq Currency.new(:eur)
46
- expect(Currency.find_by_iso_numeric(208)).not_to eq Currency.new(:eur)
47
- expect(Currency.find_by_iso_numeric('840')).to eq Currency.new(:usd)
48
-
49
- class Mock
50
- def to_s
51
- '208'
52
- end
53
- end
54
- expect(Currency.find_by_iso_numeric(Mock.new)).to eq Currency.new(:dkk)
55
- expect(Currency.find_by_iso_numeric(Mock.new)).not_to eq Currency.new(:usd)
56
- end
57
-
58
- it "returns nil if no currency has the given numeric code" do
59
- expect(Currency.find_by_iso_numeric('non iso 4217 numeric code')).to be_nil
60
- expect(Currency.find_by_iso_numeric(0)).to be_nil
61
- end
62
-
63
- it "returns nil when given empty input" do
64
- expect(Currency.find_by_iso_numeric('')).to be_nil
65
- expect(Currency.find_by_iso_numeric(nil)).to be_nil
66
- end
67
- end
68
-
69
- describe ".wrap" do
70
- it "returns nil if object is nil" do
71
- expect(Currency.wrap(nil)).to be_nil
72
- expect(Currency.wrap(Currency.new(:usd))).to eq Currency.new(:usd)
73
- expect(Currency.wrap(:usd)).to eq Currency.new(:usd)
74
- end
75
- end
76
-
77
- describe ".all" do
78
- it "returns an array of currencies" do
79
- expect(Currency.all).to include Currency.new(:usd)
80
- end
81
- it "includes registered currencies" do
82
- register_foo
83
- expect(Currency.all).to include Currency.new(:foo)
84
- unregister_foo
85
- end
86
- it 'is sorted by priority' do
87
- expect(Currency.all.first.priority).to eq 1
88
- end
89
- it "raises a MissingAttributeError if any currency has no priority" do
90
- register_foo(skip: :priority)
91
-
92
- expect{Money::Currency.all}.to \
93
- raise_error(Money::Currency::MissingAttributeError, /foo.*priority/)
94
- unregister_foo
95
- end
96
- end
97
-
98
-
99
- describe ".register" do
100
- after { Currency.unregister(iso_code: "XXX") if Currency.find("XXX") }
101
-
102
- it "registers a new currency" do
103
- Currency.register(
104
- iso_code: "XXX",
105
- name: "Golden Doubloon",
106
- symbol: "%",
107
- subunit_to_unit: 100
108
- )
109
- new_currency = Currency.find("XXX")
110
- expect(new_currency).not_to be_nil
111
- expect(new_currency.name).to eq "Golden Doubloon"
112
- expect(new_currency.symbol).to eq "%"
113
- end
114
-
115
- specify ":iso_code must be present" do
116
- expect {
117
- Currency.register(name: "New Currency")
118
- }.to raise_error(KeyError)
119
- end
120
- end
121
-
122
-
123
- describe ".inherit" do
124
- after do
125
- Currency.unregister(iso_code: "XXX") if Currency.find("XXX")
126
- Currency.unregister(iso_code: "YYY") if Currency.find("YYY")
127
- end
128
-
129
- it "inherit a new currency" do
130
- Currency.register(
131
- iso_code: "XXX",
132
- name: "Golden Doubloon",
133
- symbol: "%",
134
- subunit_to_unit: 100
135
- )
136
- Currency.inherit("XXX",
137
- iso_code: "YYY",
138
- symbol: "@"
139
- )
140
- new_currency = Currency.find("YYY")
141
- expect(new_currency).not_to be_nil
142
- expect(new_currency.name).to eq "Golden Doubloon"
143
- expect(new_currency.symbol).to eq "@"
144
- expect(new_currency.subunit_to_unit).to eq 100
145
- end
146
- end
147
-
148
-
149
- describe ".unregister" do
150
- it "unregisters a currency" do
151
- Currency.register(iso_code: "XXX")
152
- expect(Currency.find("XXX")).not_to be_nil # Sanity check
153
- Currency.unregister(iso_code: "XXX")
154
- expect(Currency.find("XXX")).to be_nil
155
- end
156
-
157
- it "returns true iff the currency existed" do
158
- Currency.register(iso_code: "XXX")
159
- expect(Currency.unregister(iso_code: "XXX")).to be_truthy
160
- expect(Currency.unregister(iso_code: "XXX")).to be_falsey
161
- end
162
-
163
- it "can be passed an ISO code" do
164
- Currency.register(iso_code: "XXX")
165
- Currency.register(iso_code: "YYZ")
166
- # Test with string:
167
- Currency.unregister("XXX")
168
- expect(Currency.find("XXX")).to be_nil
169
- # Test with symbol:
170
- Currency.unregister(:yyz)
171
- expect(Currency.find(:yyz)).to be_nil
172
- end
173
- end
174
-
175
-
176
- describe ".each" do
177
- it "yields each currency to the block" do
178
- expect(Currency).to respond_to(:each)
179
- currencies = []
180
- Currency.each do |currency|
181
- currencies.push(currency)
182
- end
183
-
184
- # Don't bother testing every single currency
185
- expect(currencies[0]).to eq Currency.all[0]
186
- expect(currencies[1]).to eq Currency.all[1]
187
- expect(currencies[-1]).to eq Currency.all[-1]
188
- end
189
- end
190
-
191
-
192
- it "implements Enumerable" do
193
- expect(Currency).to respond_to(:all?)
194
- expect(Currency).to respond_to(:each_with_index)
195
- expect(Currency).to respond_to(:map)
196
- expect(Currency).to respond_to(:select)
197
- expect(Currency).to respond_to(:reject)
198
- end
199
-
200
-
201
- describe "#initialize" do
202
- before { Currency._instances.clear }
203
-
204
- it "lookups data from loaded config" do
205
- currency = Currency.new("USD")
206
- expect(currency.id).to eq :usd
207
- expect(currency.priority).to eq 1
208
- expect(currency.iso_code).to eq "USD"
209
- expect(currency.iso_numeric).to eq "840"
210
- expect(currency.name).to eq "United States Dollar"
211
- expect(currency.decimal_mark).to eq "."
212
- expect(currency.separator).to eq "."
213
- expect(currency.thousands_separator).to eq ","
214
- expect(currency.delimiter).to eq ","
215
- expect(currency.smallest_denomination).to eq 1
216
- end
217
-
218
- it 'caches instances' do
219
- currency = Currency.new("USD")
220
-
221
- expect(Currency._instances.length).to eq 1
222
- expect(Currency._instances["usd"].object_id).to eq currency.object_id
223
- end
224
-
225
- it "raises UnknownCurrency with unknown currency" do
226
- expect { Currency.new("xxx") }.to raise_error(Currency::UnknownCurrency, /xxx/)
227
- end
228
-
229
- it 'returns old object for the same :key' do
230
- expect(Currency.new("USD")).to be(Currency.new("USD"))
231
- expect(Currency.new("USD")).to be(Currency.new(:usd))
232
- expect(Currency.new("USD")).to be(Currency.new(:USD))
233
- expect(Currency.new("USD")).to be(Currency.new('usd'))
234
- expect(Currency.new("USD")).to be(Currency.new('Usd'))
235
- end
236
-
237
- it 'returns new object for the different :key' do
238
- expect(Currency.new("USD")).to_not be(Currency.new("EUR"))
239
- end
240
-
241
- it 'is thread safe' do
242
- ids = []
243
- 2.times.map{ Thread.new{ ids << Currency.new("USD").object_id }}.each(&:join)
244
- expect(ids.uniq.length).to eq(1)
245
- end
246
- end
247
-
248
- describe "#<=>" do
249
- it "compares objects by priority" do
250
- expect(Currency.new(:cad)).to be > Currency.new(:usd)
251
- expect(Currency.new(:usd)).to be < Currency.new(:eur)
252
- end
253
-
254
- it "compares by id when priority is the same" do
255
- Currency.register(iso_code: "ABD", priority: 15)
256
- Currency.register(iso_code: "ABC", priority: 15)
257
- Currency.register(iso_code: "ABE", priority: 15)
258
- abd = Currency.find("ABD")
259
- abc = Currency.find("ABC")
260
- abe = Currency.find("ABE")
261
- expect(abd).to be > abc
262
- expect(abe).to be > abd
263
- Currency.unregister("ABD")
264
- Currency.unregister("ABC")
265
- Currency.unregister("ABE")
266
- end
267
-
268
- context "when one of the currencies has no 'priority' set" do
269
- it "compares by id" do
270
- Currency.register(iso_code: "ABD") # No priority
271
- abd = Currency.find(:abd)
272
- usd = Currency.find(:usd)
273
- expect(abd).to be < usd
274
- Currency.unregister(iso_code: "ABD")
275
- end
276
- end
277
- end
278
-
279
- describe "#==" do
280
- it "returns true if self === other" do
281
- currency = Currency.new(:eur)
282
- expect(currency).to eq currency
283
- end
284
-
285
- it "returns true if the id is equal ignorning case" do
286
- expect(Currency.new(:eur)).to eq Currency.new(:eur)
287
- expect(Currency.new(:eur)).to eq Currency.new(:EUR)
288
- expect(Currency.new(:eur)).not_to eq Currency.new(:usd)
289
- end
290
-
291
- it "allows direct comparison of currencies and symbols/strings" do
292
- expect(Currency.new(:eur)).to eq 'eur'
293
- expect(Currency.new(:eur)).to eq 'EUR'
294
- expect(Currency.new(:eur)).to eq :eur
295
- expect(Currency.new(:eur)).to eq :EUR
296
- expect(Currency.new(:eur)).not_to eq 'usd'
297
- end
298
-
299
- it "allows comparison with nil and returns false" do
300
- expect(Currency.new(:eur)).not_to be_nil
301
- end
302
- end
303
-
304
- describe "#eql?" do
305
- it "returns true if #== returns true" do
306
- expect(Currency.new(:eur).eql?(Currency.new(:eur))).to be true
307
- expect(Currency.new(:eur).eql?(Currency.new(:usd))).to be false
308
- end
309
- end
310
-
311
- describe "#hash" do
312
- it "returns the same value for equal objects" do
313
- expect(Currency.new(:eur).hash).to eq Currency.new(:eur).hash
314
- expect(Currency.new(:eur).hash).not_to eq Currency.new(:usd).hash
315
- end
316
-
317
- it "can be used to return the intersection of Currency object arrays" do
318
- intersection = [Currency.new(:eur), Currency.new(:usd)] & [Currency.new(:eur)]
319
- expect(intersection).to eq [Currency.new(:eur)]
320
- end
321
- end
322
-
323
- describe "#inspect" do
324
- it "works as documented" do
325
- expect(Currency.new(:usd).inspect).to eq %Q{#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, exponent: 2, iso_code: USD, iso_numeric: 840, subunit: Cent, smallest_denomination: 1>}
326
- end
327
- end
328
-
329
- describe "#iso?" do
330
- it "returns true for iso currency" do
331
- expect(Money::Currency.new(:eur).iso?).to be true
332
- end
333
-
334
- it "returns false if the currency is not iso" do
335
- expect(Money::Currency.new(:btc).iso?).to be false
336
- end
337
- end
338
-
339
- describe "#to_s" do
340
- it "works as documented" do
341
- expect(Currency.new(:usd).to_s).to eq("USD")
342
- expect(Currency.new(:eur).to_s).to eq("EUR")
343
- end
344
- end
345
-
346
- describe "#to_str" do
347
- it "works as documented" do
348
- expect(Currency.new(:usd).to_str).to eq("USD")
349
- expect(Currency.new(:eur).to_str).to eq("EUR")
350
- end
351
- end
352
-
353
- describe "#to_sym" do
354
- it "works as documented" do
355
- expect(Currency.new(:usd).to_sym).to eq(:USD)
356
- expect(Currency.new(:eur).to_sym).to eq(:EUR)
357
- end
358
- end
359
-
360
- describe "#to_currency" do
361
- it "works as documented" do
362
- usd = Currency.new(:usd)
363
- expect(usd.to_currency).to eq usd
364
- end
365
-
366
- it "doesn't create new symbols indefinitely" do
367
- expect { Currency.new("bogus") }.to raise_exception(Currency::UnknownCurrency)
368
- expect(Symbol.all_symbols.map{|s| s.to_s}).not_to include("bogus")
369
- end
370
- end
371
-
372
- describe "#code" do
373
- it "works as documented" do
374
- expect(Currency.new(:usd).code).to eq "$"
375
- expect(Currency.new(:azn).code).to eq "\u20BC"
376
- end
377
- end
378
-
379
- describe "#exponent" do
380
- it "conforms to iso 4217" do
381
- expect(Currency.new(:jpy).exponent).to eq 0
382
- expect(Currency.new(:usd).exponent).to eq 2
383
- expect(Currency.new(:iqd).exponent).to eq 3
384
- end
385
- end
386
-
387
- describe "#decimal_places" do
388
- it "proper places for known currency" do
389
- expect(Currency.new(:mro).decimal_places).to eq 1
390
- expect(Currency.new(:usd).decimal_places).to eq 2
391
- end
392
-
393
- it "proper places for custom currency" do
394
- register_foo
395
- expect(Currency.new(:foo).decimal_places).to eq 3
396
- unregister_foo
397
- end
398
- end
399
- end
400
- end
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe Money::LocaleBackend::Currency do
4
- describe '#lookup' do
5
- let(:currency) { Money::Currency.new('EUR') }
6
-
7
- it 'returns thousands_separator as defined in currency' do
8
- expect(subject.lookup(:thousands_separator, currency)).to eq('.')
9
- end
10
-
11
- it 'returns decimal_mark based as defined in currency' do
12
- expect(subject.lookup(:decimal_mark, currency)).to eq(',')
13
- end
14
- end
15
- end
@@ -1,70 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe Money::LocaleBackend::I18n do
4
- describe '#initialize' do
5
- it 'raises an error when I18n is not defined' do
6
- hide_const('I18n')
7
-
8
- expect { described_class.new }.to raise_error(Money::LocaleBackend::NotSupported)
9
- end
10
- end
11
-
12
- describe '#lookup' do
13
- after do
14
- reset_i18n
15
- I18n.locale = :en
16
- end
17
-
18
- subject { described_class.new }
19
-
20
- context 'with number.currency.format defined' do
21
- before do
22
- I18n.locale = :de
23
- I18n.backend.store_translations(:de, number: {
24
- currency: { format: { delimiter: '.', separator: ',', unit: '$' } }
25
- })
26
- end
27
-
28
- it 'returns thousands_separator based on the current locale' do
29
- expect(subject.lookup(:thousands_separator, nil)).to eq('.')
30
- end
31
-
32
- it 'returns decimal_mark based on the current locale' do
33
- expect(subject.lookup(:decimal_mark, nil)).to eq(',')
34
- end
35
-
36
- it 'returns symbol based on the current locale' do
37
- expect(subject.lookup(:symbol, nil)).to eq('$')
38
- end
39
- end
40
-
41
- context 'with number.format defined' do
42
- before do
43
- I18n.locale = :de
44
- I18n.backend.store_translations(:de, number: { format: { delimiter: '.', separator: ',' } })
45
- end
46
-
47
- it 'returns thousands_separator based on the current locale' do
48
- expect(subject.lookup(:thousands_separator, nil)).to eq('.')
49
- end
50
-
51
- it 'returns decimal_mark based on the current locale' do
52
- expect(subject.lookup(:decimal_mark, nil)).to eq(',')
53
- end
54
- end
55
-
56
- context 'with no translation defined' do
57
- it 'returns thousands_separator based on the current locale' do
58
- expect(subject.lookup(:thousands_separator, nil)).to eq(nil)
59
- end
60
-
61
- it 'returns decimal_mark based on the current locale' do
62
- expect(subject.lookup(:decimal_mark, nil)).to eq(nil)
63
- end
64
-
65
- it 'returns symbol based on the current locale' do
66
- expect(subject.lookup(:symbol, nil)).to eq(nil)
67
- end
68
- end
69
- end
70
- end