money 6.5.1 → 6.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +209 -5
- data/LICENSE +18 -16
- data/README.md +321 -70
- data/config/currency_backwards_compatible.json +65 -0
- data/config/currency_iso.json +280 -94
- data/config/currency_non_iso.json +101 -3
- data/lib/money/bank/base.rb +1 -3
- data/lib/money/bank/variable_exchange.rb +88 -96
- data/lib/money/currency/heuristics.rb +1 -143
- data/lib/money/currency/loader.rb +15 -13
- data/lib/money/currency.rb +98 -81
- data/lib/money/locale_backend/base.rb +7 -0
- data/lib/money/locale_backend/currency.rb +11 -0
- data/lib/money/locale_backend/errors.rb +6 -0
- data/lib/money/locale_backend/i18n.rb +25 -0
- data/lib/money/locale_backend/legacy.rb +28 -0
- data/lib/money/money/allocation.rb +46 -0
- data/lib/money/money/arithmetic.rb +97 -52
- data/lib/money/money/constructors.rb +5 -6
- data/lib/money/money/formatter.rb +399 -0
- data/lib/money/money/formatting_rules.rb +142 -0
- data/lib/money/money/locale_backend.rb +22 -0
- data/lib/money/money.rb +268 -194
- data/lib/money/rates_store/memory.rb +120 -0
- data/lib/money/version.rb +1 -1
- data/money.gemspec +15 -20
- metadata +36 -59
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -22
- data/.travis.yml +0 -13
- data/AUTHORS +0 -116
- data/CONTRIBUTING.md +0 -17
- data/Gemfile +0 -7
- data/Rakefile +0 -17
- data/lib/money/money/formatting.rb +0 -386
- data/spec/bank/base_spec.rb +0 -77
- data/spec/bank/single_currency_spec.rb +0 -11
- data/spec/bank/variable_exchange_spec.rb +0 -275
- data/spec/currency/heuristics_spec.rb +0 -84
- data/spec/currency_spec.rb +0 -321
- data/spec/money/arithmetic_spec.rb +0 -568
- data/spec/money/constructors_spec.rb +0 -75
- data/spec/money/formatting_spec.rb +0 -667
- data/spec/money_spec.rb +0 -745
- data/spec/spec_helper.rb +0 -23
@@ -1,275 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'json'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
describe Money::Bank::VariableExchange do
|
6
|
-
|
7
|
-
describe "#initialize" do
|
8
|
-
context "without &block" do
|
9
|
-
let(:bank) {
|
10
|
-
Money::Bank::VariableExchange.new.tap do |bank|
|
11
|
-
bank.add_rate('USD', 'EUR', 1.33)
|
12
|
-
end
|
13
|
-
}
|
14
|
-
|
15
|
-
describe "#exchange_with" do
|
16
|
-
it "accepts str" do
|
17
|
-
expect { bank.exchange_with(Money.new(100, 'USD'), 'EUR') }.to_not raise_exception
|
18
|
-
end
|
19
|
-
|
20
|
-
it "accepts currency" do
|
21
|
-
expect { bank.exchange_with(Money.new(100, 'USD'), Money::Currency.wrap('EUR')) }.to_not raise_exception
|
22
|
-
end
|
23
|
-
|
24
|
-
it "exchanges one currency to another" do
|
25
|
-
expect(bank.exchange_with(Money.new(100, 'USD'), 'EUR')).to eq Money.new(133, 'EUR')
|
26
|
-
end
|
27
|
-
|
28
|
-
it "truncates extra digits" do
|
29
|
-
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR')).to eq Money.new(13, 'EUR')
|
30
|
-
end
|
31
|
-
|
32
|
-
it "raises an UnknownCurrency exception when an unknown currency is requested" do
|
33
|
-
expect { bank.exchange_with(Money.new(100, 'USD'), 'BBB') }.to raise_exception(Money::Currency::UnknownCurrency)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "raises an UnknownRate exception when an unknown rate is requested" do
|
37
|
-
expect { bank.exchange_with(Money.new(100, 'USD'), 'JPY') }.to raise_exception(Money::Bank::UnknownRate)
|
38
|
-
end
|
39
|
-
|
40
|
-
#it "rounds the exchanged result down" do
|
41
|
-
# bank.add_rate("USD", "EUR", 0.788332676)
|
42
|
-
# bank.add_rate("EUR", "YEN", 122.631477)
|
43
|
-
# expect(bank.exchange_with(Money.new(10_00, "USD"), "EUR")).to eq Money.new(788, "EUR")
|
44
|
-
# expect(bank.exchange_with(Money.new(500_00, "EUR"), "YEN")).to eq Money.new(6131573, "YEN")
|
45
|
-
#end
|
46
|
-
|
47
|
-
it "accepts a custom truncation method" do
|
48
|
-
proc = Proc.new { |n| n.ceil }
|
49
|
-
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc)).to eq Money.new(14, 'EUR')
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "with &block" do
|
55
|
-
let(:bank) {
|
56
|
-
proc = Proc.new { |n| n.ceil }
|
57
|
-
Money::Bank::VariableExchange.new(&proc).tap do |bank|
|
58
|
-
bank.add_rate('USD', 'EUR', 1.33)
|
59
|
-
end
|
60
|
-
}
|
61
|
-
|
62
|
-
describe "#exchange_with" do
|
63
|
-
it "uses the stored truncation method" do
|
64
|
-
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR')).to eq Money.new(14, 'EUR')
|
65
|
-
end
|
66
|
-
|
67
|
-
it "accepts a custom truncation method" do
|
68
|
-
proc = Proc.new { |n| n.ceil + 1 }
|
69
|
-
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc)).to eq Money.new(15, 'EUR')
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
describe "#add_rate" do
|
76
|
-
it "adds rates correctly" do
|
77
|
-
subject.add_rate("USD", "EUR", 0.788332676)
|
78
|
-
subject.add_rate("EUR", "YEN", 122.631477)
|
79
|
-
|
80
|
-
expect(subject.instance_variable_get(:@rates)['USD_TO_EUR']).to eq 0.788332676
|
81
|
-
expect(subject.instance_variable_get(:@rates)['EUR_TO_JPY']).to eq 122.631477
|
82
|
-
end
|
83
|
-
|
84
|
-
it "treats currency names case-insensitively" do
|
85
|
-
subject.add_rate("usd", "eur", 1)
|
86
|
-
expect(subject.instance_variable_get(:@rates)['USD_TO_EUR']).to eq 1
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "#set_rate" do
|
91
|
-
it "sets a rate" do
|
92
|
-
subject.set_rate('USD', 'EUR', 1.25)
|
93
|
-
expect(subject.instance_variable_get(:@rates)['USD_TO_EUR']).to eq 1.25
|
94
|
-
end
|
95
|
-
|
96
|
-
it "raises an UnknownCurrency exception when an unknown currency is passed" do
|
97
|
-
expect { subject.set_rate('AAA', 'BBB', 1.25) }.to raise_exception(Money::Currency::UnknownCurrency)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "uses a mutex by default" do
|
101
|
-
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
102
|
-
subject.set_rate('USD', 'EUR', 1.25)
|
103
|
-
end
|
104
|
-
|
105
|
-
it "doesn't use mutex if requested not to" do
|
106
|
-
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
107
|
-
subject.set_rate('USD', 'EUR', 1.25, :without_mutex => true)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
describe "#get_rate" do
|
112
|
-
it "returns a rate" do
|
113
|
-
subject.set_rate('USD', 'EUR', 1.25)
|
114
|
-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
115
|
-
end
|
116
|
-
|
117
|
-
it "raises an UnknownCurrency exception when an unknown currency is passed" do
|
118
|
-
expect { subject.get_rate('AAA', 'BBB') }.to raise_exception(Money::Currency::UnknownCurrency)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "uses a mutex by default" do
|
122
|
-
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
123
|
-
subject.get_rate('USD', 'EUR')
|
124
|
-
end
|
125
|
-
|
126
|
-
it "doesn't use mutex if requested not to" do
|
127
|
-
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
128
|
-
subject.get_rate('USD', 'EUR', :without_mutex => true)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe "#export_rates" do
|
133
|
-
before :each do
|
134
|
-
subject.set_rate('USD', 'EUR', 1.25)
|
135
|
-
subject.set_rate('USD', 'JPY', 2.55)
|
136
|
-
|
137
|
-
@rates = { "USD_TO_EUR" => 1.25, "USD_TO_JPY" => 2.55 }
|
138
|
-
end
|
139
|
-
|
140
|
-
context "with format == :json" do
|
141
|
-
it "should return rates formatted as json" do
|
142
|
-
json = subject.export_rates(:json)
|
143
|
-
expect(JSON.load(json)).to eq @rates
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
context "with format == :ruby" do
|
148
|
-
it "should return rates formatted as ruby objects" do
|
149
|
-
expect(Marshal.load(subject.export_rates(:ruby))).to eq @rates
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
context "with format == :yaml" do
|
154
|
-
it "should return rates formatted as yaml" do
|
155
|
-
yaml = subject.export_rates(:yaml)
|
156
|
-
expect(YAML.load(yaml)).to eq @rates
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
context "with unknown format" do
|
161
|
-
it "raises Money::Bank::UnknownRateFormat" do
|
162
|
-
expect { subject.export_rates(:foo)}.to raise_error Money::Bank::UnknownRateFormat
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
context "with :file provided" do
|
167
|
-
it "writes rates to file" do
|
168
|
-
f = double('IO')
|
169
|
-
expect(File).to receive(:open).with('null', 'w').and_yield(f)
|
170
|
-
expect(f).to receive(:write).with(JSON.dump(@rates))
|
171
|
-
|
172
|
-
subject.export_rates(:json, 'null')
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
it "uses a mutex by default" do
|
177
|
-
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
178
|
-
subject.export_rates(:yaml)
|
179
|
-
end
|
180
|
-
|
181
|
-
it "doesn't use mutex if requested not to" do
|
182
|
-
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
183
|
-
subject.export_rates(:yaml, nil, :without_mutex => true)
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
describe "#import_rates" do
|
188
|
-
context "with format == :json" do
|
189
|
-
it "loads the rates provided" do
|
190
|
-
s = '{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}'
|
191
|
-
subject.import_rates(:json, s)
|
192
|
-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
193
|
-
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
context "with format == :ruby" do
|
198
|
-
it "loads the rates provided" do
|
199
|
-
s = Marshal.dump({"USD_TO_EUR"=>1.25,"USD_TO_JPY"=>2.55})
|
200
|
-
subject.import_rates(:ruby, s)
|
201
|
-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
202
|
-
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
context "with format == :yaml" do
|
207
|
-
it "loads the rates provided" do
|
208
|
-
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
|
209
|
-
subject.import_rates(:yaml, s)
|
210
|
-
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
211
|
-
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
context "with unknown format" do
|
216
|
-
it "raises Money::Bank::UnknownRateFormat" do
|
217
|
-
expect { subject.import_rates(:foo, "")}.to raise_error Money::Bank::UnknownRateFormat
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
it "uses a mutex by default" do
|
222
|
-
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
223
|
-
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
|
224
|
-
subject.import_rates(:yaml, s)
|
225
|
-
end
|
226
|
-
|
227
|
-
it "doesn't use mutex if requested not to" do
|
228
|
-
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
229
|
-
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
|
230
|
-
subject.import_rates(:yaml, s, :without_mutex => true)
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
describe "#rate_key_for" do
|
235
|
-
it "accepts str/str" do
|
236
|
-
expect { subject.send(:rate_key_for, 'USD', 'EUR')}.to_not raise_exception
|
237
|
-
end
|
238
|
-
|
239
|
-
it "accepts currency/str" do
|
240
|
-
expect { subject.send(:rate_key_for, Money::Currency.wrap('USD'), 'EUR')}.to_not raise_exception
|
241
|
-
end
|
242
|
-
|
243
|
-
it "accepts str/currency" do
|
244
|
-
expect { subject.send(:rate_key_for, 'USD', Money::Currency.wrap('EUR'))}.to_not raise_exception
|
245
|
-
end
|
246
|
-
|
247
|
-
it "accepts currency/currency" do
|
248
|
-
expect { subject.send(:rate_key_for, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR'))}.to_not raise_exception
|
249
|
-
end
|
250
|
-
|
251
|
-
it "returns a hashkey based on the passed arguments" do
|
252
|
-
expect(subject.send(:rate_key_for, 'USD', 'EUR')).to eq 'USD_TO_EUR'
|
253
|
-
expect(subject.send(:rate_key_for, Money::Currency.wrap('USD'), 'EUR')).to eq 'USD_TO_EUR'
|
254
|
-
expect(subject.send(:rate_key_for, 'USD', Money::Currency.wrap('EUR'))).to eq 'USD_TO_EUR'
|
255
|
-
expect(subject.send(:rate_key_for, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR'))).to eq 'USD_TO_EUR'
|
256
|
-
end
|
257
|
-
|
258
|
-
it "raises a Money::Currency::UnknownCurrency exception when an unknown currency is passed" do
|
259
|
-
expect { subject.send(:rate_key_for, 'AAA', 'BBB')}.to raise_exception(Money::Currency::UnknownCurrency)
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
describe "#marshal_dump" do
|
264
|
-
it "does not raise an error" do
|
265
|
-
expect { Marshal.dump(subject) }.to_not raise_error
|
266
|
-
end
|
267
|
-
|
268
|
-
it "works with Marshal.load" do
|
269
|
-
bank = Marshal.load(Marshal.dump(subject))
|
270
|
-
|
271
|
-
expect(bank.rates).to eq subject.rates
|
272
|
-
expect(bank.rounding_method).to eq subject.rounding_method
|
273
|
-
end
|
274
|
-
end
|
275
|
-
end
|
@@ -1,84 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe Money::Currency::Heuristics do
|
5
|
-
describe "#analyze_string" do
|
6
|
-
let(:it) { Money::Currency }
|
7
|
-
|
8
|
-
it "is not affected by blank characters and numbers" do
|
9
|
-
expect(it.analyze('123')).to eq []
|
10
|
-
expect(it.analyze('\n123 \t')).to eq []
|
11
|
-
end
|
12
|
-
|
13
|
-
it "returns nothing when given nothing" do
|
14
|
-
expect(it.analyze('')).to eq []
|
15
|
-
expect(it.analyze(nil)).to eq []
|
16
|
-
end
|
17
|
-
|
18
|
-
it "finds a currency by use of its symbol" do
|
19
|
-
expect(it.analyze('zł')).to eq ['PLN']
|
20
|
-
end
|
21
|
-
|
22
|
-
it "is not affected by trailing dot" do
|
23
|
-
expect(it.analyze('zł.')).to eq ['PLN']
|
24
|
-
end
|
25
|
-
|
26
|
-
it "finds match even if has numbers after" do
|
27
|
-
expect(it.analyze('zł 123')).to eq ['PLN']
|
28
|
-
end
|
29
|
-
|
30
|
-
it "finds match even if has numbers before" do
|
31
|
-
expect(it.analyze('123 zł')).to eq ['PLN']
|
32
|
-
end
|
33
|
-
|
34
|
-
it "find match even if symbol is next to number" do
|
35
|
-
expect(it.analyze('300zł')).to eq ['PLN']
|
36
|
-
end
|
37
|
-
|
38
|
-
it "finds match even if has numbers with delimiters" do
|
39
|
-
expect(it.analyze('zł 123,000.50')).to eq ['PLN']
|
40
|
-
expect(it.analyze('zł123,000.50')).to eq ['PLN']
|
41
|
-
end
|
42
|
-
|
43
|
-
it "finds currencies with dots in symbols" do
|
44
|
-
expect(it.analyze('L.E.')).to eq ['EGP']
|
45
|
-
end
|
46
|
-
|
47
|
-
it "finds by name" do
|
48
|
-
expect(it.analyze('1900 bulgarian lev')).to eq ['BGN']
|
49
|
-
expect(it.analyze('Swedish Krona')).to eq ['SEK']
|
50
|
-
end
|
51
|
-
|
52
|
-
it "Finds several currencies when several match" do
|
53
|
-
r = it.analyze('$400')
|
54
|
-
expect(r).to include("ARS")
|
55
|
-
expect(r).to include("USD")
|
56
|
-
expect(r).to include("NZD")
|
57
|
-
|
58
|
-
r = it.analyze('9000 £')
|
59
|
-
expect(r).to include("GBP")
|
60
|
-
expect(r).to include("SHP")
|
61
|
-
expect(r).to include("SYP")
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should use alternate symbols" do
|
65
|
-
expect(it.analyze('US$')).to eq ['USD']
|
66
|
-
end
|
67
|
-
|
68
|
-
it "finds a currency by use of its iso code" do
|
69
|
-
expect(it.analyze('USD 200')).to eq ['USD']
|
70
|
-
end
|
71
|
-
|
72
|
-
it "finds currencies in the middle of a sentence!" do
|
73
|
-
expect(it.analyze('It would be nice to have 10000 Albanian lek by tomorrow!')).to eq ['ALL']
|
74
|
-
end
|
75
|
-
|
76
|
-
it "finds several currencies in the same text!" do
|
77
|
-
expect(it.analyze("10EUR is less than 100:- but really, I want US$1")).to eq ['EUR', 'SEK', 'USD']
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should function with unicode characters" do
|
81
|
-
expect(it.analyze("10 դր.")).to eq ["AMD"]
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
data/spec/currency_spec.rb
DELETED
@@ -1,321 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe Money::Currency do
|
6
|
-
|
7
|
-
FOO = '{ "priority": 1, "iso_code": "FOO", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 450, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "smallest_denomination": 1 }'
|
8
|
-
|
9
|
-
def register_foo
|
10
|
-
Money::Currency.register(JSON.parse(FOO, :symbolize_names => true))
|
11
|
-
end
|
12
|
-
|
13
|
-
def unregister_foo
|
14
|
-
Money::Currency.unregister(JSON.parse(FOO, :symbolize_names => true))
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "UnknownMoney::Currency" do
|
18
|
-
it "is a subclass of ArgumentError" do
|
19
|
-
expect(Money::Currency::UnknownCurrency < ArgumentError).to be true
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
describe ".find" do
|
24
|
-
before { register_foo }
|
25
|
-
after { unregister_foo }
|
26
|
-
|
27
|
-
it "returns currency matching given id" do
|
28
|
-
expected = Money::Currency.new(:foo)
|
29
|
-
expect(Money::Currency.find(:foo)).to eq expected
|
30
|
-
expect(Money::Currency.find(:FOO)).to eq expected
|
31
|
-
expect(Money::Currency.find("foo")).to eq expected
|
32
|
-
expect(Money::Currency.find("FOO")).to eq expected
|
33
|
-
end
|
34
|
-
|
35
|
-
it "returns nil unless currency matching given id" do
|
36
|
-
expect(Money::Currency.find("ZZZ")).to be_nil
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe ".find_by_iso_numeric" do
|
41
|
-
it "returns currency matching given numeric code" do
|
42
|
-
expect(Money::Currency.find_by_iso_numeric(978)).to eq Money::Currency.new(:eur)
|
43
|
-
expect(Money::Currency.find_by_iso_numeric(208)).not_to eq Money::Currency.new(:eur)
|
44
|
-
expect(Money::Currency.find_by_iso_numeric('840')).to eq Money::Currency.new(:usd)
|
45
|
-
|
46
|
-
class Mock
|
47
|
-
def to_s
|
48
|
-
'208'
|
49
|
-
end
|
50
|
-
end
|
51
|
-
expect(Money::Currency.find_by_iso_numeric(Mock.new)).to eq Money::Currency.new(:dkk)
|
52
|
-
expect(Money::Currency.find_by_iso_numeric(Mock.new)).not_to eq Money::Currency.new(:usd)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "returns nil if no currency has the given numeric code" do
|
56
|
-
expect(Money::Currency.find_by_iso_numeric('non iso 4217 numeric code')).to be_nil
|
57
|
-
expect(Money::Currency.find_by_iso_numeric(0)).to be_nil
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe ".wrap" do
|
62
|
-
it "returns nil if object is nil" do
|
63
|
-
expect(Money::Currency.wrap(nil)).to be_nil
|
64
|
-
expect(Money::Currency.wrap(Money::Currency.new(:usd))).to eq Money::Currency.new(:usd)
|
65
|
-
expect(Money::Currency.wrap(:usd)).to eq Money::Currency.new(:usd)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe ".all" do
|
70
|
-
it "returns an array of currencies" do
|
71
|
-
expect(Money::Currency.all).to include Money::Currency.new(:usd)
|
72
|
-
end
|
73
|
-
it "includes registered currencies" do
|
74
|
-
register_foo
|
75
|
-
expect(Money::Currency.all).to include Money::Currency.new(:foo)
|
76
|
-
unregister_foo
|
77
|
-
end
|
78
|
-
it 'is sorted by priority' do
|
79
|
-
expect(Money::Currency.all.first.priority).to eq 1
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
|
84
|
-
describe ".register" do
|
85
|
-
after { Money::Currency.unregister(iso_code: "XXX") if Money::Currency.find("XXX") }
|
86
|
-
|
87
|
-
it "registers a new currency" do
|
88
|
-
Money::Currency.register(
|
89
|
-
iso_code: "XXX",
|
90
|
-
name: "Golden Doubloon",
|
91
|
-
symbol: "%",
|
92
|
-
subunit_to_unit: 100
|
93
|
-
)
|
94
|
-
new_currency = Money::Currency.find("XXX")
|
95
|
-
expect(new_currency).not_to be_nil
|
96
|
-
expect(new_currency.name).to eq "Golden Doubloon"
|
97
|
-
expect(new_currency.symbol).to eq "%"
|
98
|
-
end
|
99
|
-
|
100
|
-
specify ":iso_code must be present" do
|
101
|
-
expect {
|
102
|
-
Money::Currency.register(name: "New Currency")
|
103
|
-
}.to raise_error(KeyError)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
describe ".unregister" do
|
109
|
-
it "unregisters a currency" do
|
110
|
-
Money::Currency.register(iso_code: "XXX")
|
111
|
-
expect(Money::Currency.find("XXX")).not_to be_nil # Sanity check
|
112
|
-
Money::Currency.unregister(iso_code: "XXX")
|
113
|
-
expect(Money::Currency.find("XXX")).to be_nil
|
114
|
-
end
|
115
|
-
|
116
|
-
it "returns true iff the currency existed" do
|
117
|
-
Money::Currency.register(iso_code: "XXX")
|
118
|
-
expect(Money::Currency.unregister(iso_code: "XXX")).to be_truthy
|
119
|
-
expect(Money::Currency.unregister(iso_code: "XXX")).to be_falsey
|
120
|
-
end
|
121
|
-
|
122
|
-
it "can be passed an ISO code" do
|
123
|
-
Money::Currency.register(iso_code: "XXX")
|
124
|
-
Money::Currency.register(iso_code: "YYZ")
|
125
|
-
# Test with string:
|
126
|
-
Money::Currency.unregister("XXX")
|
127
|
-
expect(Money::Currency.find("XXX")).to be_nil
|
128
|
-
# Test with symbol:
|
129
|
-
Money::Currency.unregister(:yyz)
|
130
|
-
expect(Money::Currency.find(:yyz)).to be_nil
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
|
135
|
-
describe ".each" do
|
136
|
-
it "yields each currency to the block" do
|
137
|
-
expect(Money::Currency).to respond_to(:each)
|
138
|
-
currencies = []
|
139
|
-
Money::Currency.each do |currency|
|
140
|
-
currencies.push(currency)
|
141
|
-
end
|
142
|
-
|
143
|
-
# Don't bother testing every single currency
|
144
|
-
expect(currencies[0]).to eq Money::Currency.all[0]
|
145
|
-
expect(currencies[1]).to eq Money::Currency.all[1]
|
146
|
-
expect(currencies[-1]).to eq Money::Currency.all[-1]
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
|
151
|
-
it "implements Enumerable" do
|
152
|
-
expect(Money::Currency).to respond_to(:all?)
|
153
|
-
expect(Money::Currency).to respond_to(:each_with_index)
|
154
|
-
expect(Money::Currency).to respond_to(:map)
|
155
|
-
expect(Money::Currency).to respond_to(:select)
|
156
|
-
expect(Money::Currency).to respond_to(:reject)
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
describe "#initialize" do
|
161
|
-
it "lookups data from loaded config" do
|
162
|
-
currency = Money::Currency.new("USD")
|
163
|
-
expect(currency.id).to eq :usd
|
164
|
-
expect(currency.priority).to eq 1
|
165
|
-
expect(currency.iso_code).to eq "USD"
|
166
|
-
expect(currency.iso_numeric).to eq "840"
|
167
|
-
expect(currency.name).to eq "United States Dollar"
|
168
|
-
expect(currency.decimal_mark).to eq "."
|
169
|
-
expect(currency.separator).to eq "."
|
170
|
-
expect(currency.thousands_separator).to eq ","
|
171
|
-
expect(currency.delimiter).to eq ","
|
172
|
-
expect(currency.smallest_denomination).to eq 1
|
173
|
-
end
|
174
|
-
|
175
|
-
it "raises UnknownMoney::Currency with unknown currency" do
|
176
|
-
expect { Money::Currency.new("xxx") }.to raise_error(Money::Currency::UnknownCurrency, /xxx/)
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe "#<=>" do
|
181
|
-
it "compares objects by priority" do
|
182
|
-
expect(Money::Currency.new(:cad)).to be > Money::Currency.new(:usd)
|
183
|
-
expect(Money::Currency.new(:usd)).to be < Money::Currency.new(:eur)
|
184
|
-
end
|
185
|
-
|
186
|
-
it "compares by id when priority is the same" do
|
187
|
-
Money::Currency.register(iso_code: "ABD", priority: 15)
|
188
|
-
Money::Currency.register(iso_code: "ABC", priority: 15)
|
189
|
-
Money::Currency.register(iso_code: "ABE", priority: 15)
|
190
|
-
abd = Money::Currency.find("ABD")
|
191
|
-
abc = Money::Currency.find("ABC")
|
192
|
-
abe = Money::Currency.find("ABE")
|
193
|
-
expect(abd).to be > abc
|
194
|
-
expect(abe).to be > abd
|
195
|
-
Money::Currency.unregister("ABD")
|
196
|
-
Money::Currency.unregister("ABC")
|
197
|
-
Money::Currency.unregister("ABE")
|
198
|
-
end
|
199
|
-
|
200
|
-
context "when one of the currencies has no 'priority' set" do
|
201
|
-
it "compares by id" do
|
202
|
-
Money::Currency.register(iso_code: "ABD") # No priority
|
203
|
-
abd = Money::Currency.find(:abd)
|
204
|
-
usd = Money::Currency.find(:usd)
|
205
|
-
expect(abd).to be < usd
|
206
|
-
Money::Currency.unregister(iso_code: "ABD")
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
describe "#==" do
|
212
|
-
it "returns true if self === other" do
|
213
|
-
currency = Money::Currency.new(:eur)
|
214
|
-
expect(currency).to eq currency
|
215
|
-
end
|
216
|
-
|
217
|
-
it "returns true if the id is equal ignorning case" do
|
218
|
-
expect(Money::Currency.new(:eur)).to eq Money::Currency.new(:eur)
|
219
|
-
expect(Money::Currency.new(:eur)).to eq Money::Currency.new(:EUR)
|
220
|
-
expect(Money::Currency.new(:eur)).not_to eq Money::Currency.new(:usd)
|
221
|
-
end
|
222
|
-
|
223
|
-
it "allows direct comparison of currencies and symbols/strings" do
|
224
|
-
expect(Money::Currency.new(:eur)).to eq 'eur'
|
225
|
-
expect(Money::Currency.new(:eur)).to eq 'EUR'
|
226
|
-
expect(Money::Currency.new(:eur)).to eq :eur
|
227
|
-
expect(Money::Currency.new(:eur)).to eq :EUR
|
228
|
-
expect(Money::Currency.new(:eur)).not_to eq 'usd'
|
229
|
-
end
|
230
|
-
|
231
|
-
it "allows comparison with nil and returns false" do
|
232
|
-
expect(Money::Currency.new(:eur)).not_to be_nil
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
describe "#eql?" do
|
237
|
-
it "returns true if #== returns true" do
|
238
|
-
expect(Money::Currency.new(:eur).eql?(Money::Currency.new(:eur))).to be true
|
239
|
-
expect(Money::Currency.new(:eur).eql?(Money::Currency.new(:usd))).to be false
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
describe "#hash" do
|
244
|
-
it "returns the same value for equal objects" do
|
245
|
-
expect(Money::Currency.new(:eur).hash).to eq Money::Currency.new(:eur).hash
|
246
|
-
expect(Money::Currency.new(:eur).hash).not_to eq Money::Currency.new(:usd).hash
|
247
|
-
end
|
248
|
-
|
249
|
-
it "can be used to return the intersection of Currency object arrays" do
|
250
|
-
intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
|
251
|
-
expect(intersection).to eq [Money::Currency.new(:eur)]
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
describe "#inspect" do
|
256
|
-
it "works as documented" do
|
257
|
-
expect(Money::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.0, iso_code: USD, iso_numeric: 840, subunit: Cent, smallest_denomination: 1>}
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
describe "#to_s" do
|
262
|
-
it "works as documented" do
|
263
|
-
expect(Money::Currency.new(:usd).to_s).to eq("USD")
|
264
|
-
expect(Money::Currency.new(:eur).to_s).to eq("EUR")
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe "#to_str" do
|
269
|
-
it "works as documented" do
|
270
|
-
expect(Money::Currency.new(:usd).to_str).to eq("USD")
|
271
|
-
expect(Money::Currency.new(:eur).to_str).to eq("EUR")
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
describe "#to_sym" do
|
276
|
-
it "works as documented" do
|
277
|
-
expect(Money::Currency.new(:usd).to_sym).to eq(:USD)
|
278
|
-
expect(Money::Currency.new(:eur).to_sym).to eq(:EUR)
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
describe "#to_currency" do
|
283
|
-
it "works as documented" do
|
284
|
-
usd = Money::Currency.new(:usd)
|
285
|
-
expect(usd.to_currency).to eq usd
|
286
|
-
end
|
287
|
-
|
288
|
-
it "doesn't create new symbols indefinitely" do
|
289
|
-
expect { Money::Currency.new("bogus") }.to raise_exception(Money::Currency::UnknownCurrency)
|
290
|
-
expect(Symbol.all_symbols.map{|s| s.to_s}).not_to include("bogus")
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
describe "#code" do
|
295
|
-
it "works as documented" do
|
296
|
-
expect(Money::Currency.new(:usd).code).to eq "$"
|
297
|
-
expect(Money::Currency.new(:azn).code).to eq "\u20BC"
|
298
|
-
end
|
299
|
-
end
|
300
|
-
|
301
|
-
describe "#exponent" do
|
302
|
-
it "conforms to iso 4217" do
|
303
|
-
Money::Currency.new(:jpy).exponent == 0
|
304
|
-
Money::Currency.new(:usd).exponent == 2
|
305
|
-
Money::Currency.new(:iqd).exponent == 3
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
describe "#decimal_places" do
|
310
|
-
it "proper places for known currency" do
|
311
|
-
Money::Currency.new(:mro).decimal_places == 1
|
312
|
-
Money::Currency.new(:usd).decimal_places == 2
|
313
|
-
end
|
314
|
-
|
315
|
-
it "proper places for custom currency" do
|
316
|
-
register_foo
|
317
|
-
Money::Currency.new(:foo).decimal_places == 3
|
318
|
-
unregister_foo
|
319
|
-
end
|
320
|
-
end
|
321
|
-
end
|