money 6.1.1 → 6.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.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/AUTHORS +6 -0
- data/CHANGELOG.md +16 -0
- data/README.md +30 -1
- data/config/currency_iso.json +6 -6
- data/lib/money/currency.rb +16 -5
- data/lib/money/money/arithmetic.rb +16 -10
- data/lib/money/money/formatting.rb +19 -6
- data/lib/money/money.rb +18 -13
- data/lib/money/version.rb +1 -1
- data/money.gemspec +2 -2
- data/spec/bank/base_spec.rb +11 -11
- data/spec/bank/variable_exchange_spec.rb +37 -37
- data/spec/currency/heuristics_spec.rb +25 -25
- data/spec/currency_spec.rb +56 -57
- data/spec/money/arithmetic_spec.rb +107 -99
- data/spec/money/formatting_spec.rb +230 -181
- data/spec/money_spec.rb +161 -128
- metadata +12 -6
@@ -22,11 +22,11 @@ describe Money::Bank::VariableExchange do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "exchanges one currency to another" do
|
25
|
-
bank.exchange_with(Money.new(100, 'USD'), 'EUR').
|
25
|
+
expect(bank.exchange_with(Money.new(100, 'USD'), 'EUR')).to eq Money.new(133, 'EUR')
|
26
26
|
end
|
27
27
|
|
28
28
|
it "truncates extra digits" do
|
29
|
-
bank.exchange_with(Money.new(10, 'USD'), 'EUR').
|
29
|
+
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR')).to eq Money.new(13, 'EUR')
|
30
30
|
end
|
31
31
|
|
32
32
|
it "raises an UnknownCurrency exception when an unknown currency is requested" do
|
@@ -40,13 +40,13 @@ describe Money::Bank::VariableExchange do
|
|
40
40
|
#it "rounds the exchanged result down" do
|
41
41
|
# bank.add_rate("USD", "EUR", 0.788332676)
|
42
42
|
# bank.add_rate("EUR", "YEN", 122.631477)
|
43
|
-
# bank.exchange_with(Money.new(10_00, "USD"), "EUR").
|
44
|
-
# bank.exchange_with(Money.new(500_00, "EUR"), "YEN").
|
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
45
|
#end
|
46
46
|
|
47
47
|
it "accepts a custom truncation method" do
|
48
48
|
proc = Proc.new { |n| n.ceil }
|
49
|
-
bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc).
|
49
|
+
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc)).to eq Money.new(14, 'EUR')
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -61,12 +61,12 @@ describe Money::Bank::VariableExchange do
|
|
61
61
|
|
62
62
|
describe "#exchange_with" do
|
63
63
|
it "uses the stored truncation method" do
|
64
|
-
bank.exchange_with(Money.new(10, 'USD'), 'EUR').
|
64
|
+
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR')).to eq Money.new(14, 'EUR')
|
65
65
|
end
|
66
66
|
|
67
67
|
it "accepts a custom truncation method" do
|
68
68
|
proc = Proc.new { |n| n.ceil + 1 }
|
69
|
-
bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc).
|
69
|
+
expect(bank.exchange_with(Money.new(10, 'USD'), 'EUR', &proc)).to eq Money.new(15, 'EUR')
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
@@ -77,20 +77,20 @@ describe Money::Bank::VariableExchange do
|
|
77
77
|
subject.add_rate("USD", "EUR", 0.788332676)
|
78
78
|
subject.add_rate("EUR", "YEN", 122.631477)
|
79
79
|
|
80
|
-
subject.instance_variable_get(:@rates)['USD_TO_EUR'].
|
81
|
-
subject.instance_variable_get(:@rates)['EUR_TO_JPY'].
|
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
82
|
end
|
83
83
|
|
84
84
|
it "treats currency names case-insensitively" do
|
85
85
|
subject.add_rate("usd", "eur", 1)
|
86
|
-
subject.instance_variable_get(:@rates)['USD_TO_EUR'].
|
86
|
+
expect(subject.instance_variable_get(:@rates)['USD_TO_EUR']).to eq 1
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
90
|
describe "#set_rate" do
|
91
91
|
it "sets a rate" do
|
92
92
|
subject.set_rate('USD', 'EUR', 1.25)
|
93
|
-
subject.instance_variable_get(:@rates)['USD_TO_EUR'].
|
93
|
+
expect(subject.instance_variable_get(:@rates)['USD_TO_EUR']).to eq 1.25
|
94
94
|
end
|
95
95
|
|
96
96
|
it "raises an UnknownCurrency exception when an unknown currency is passed" do
|
@@ -98,12 +98,12 @@ describe Money::Bank::VariableExchange do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it "uses a mutex by default" do
|
101
|
-
subject.instance_variable_get(:@mutex).
|
101
|
+
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
102
102
|
subject.set_rate('USD', 'EUR', 1.25)
|
103
103
|
end
|
104
104
|
|
105
105
|
it "doesn't use mutex if requested not to" do
|
106
|
-
subject.instance_variable_get(:@mutex).
|
106
|
+
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
107
107
|
subject.set_rate('USD', 'EUR', 1.25, :without_mutex => true)
|
108
108
|
end
|
109
109
|
end
|
@@ -111,7 +111,7 @@ describe Money::Bank::VariableExchange do
|
|
111
111
|
describe "#get_rate" do
|
112
112
|
it "returns a rate" do
|
113
113
|
subject.set_rate('USD', 'EUR', 1.25)
|
114
|
-
subject.get_rate('USD', 'EUR').
|
114
|
+
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
115
115
|
end
|
116
116
|
|
117
117
|
it "raises an UnknownCurrency exception when an unknown currency is passed" do
|
@@ -119,12 +119,12 @@ describe Money::Bank::VariableExchange do
|
|
119
119
|
end
|
120
120
|
|
121
121
|
it "uses a mutex by default" do
|
122
|
-
subject.instance_variable_get(:@mutex).
|
122
|
+
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
123
123
|
subject.get_rate('USD', 'EUR')
|
124
124
|
end
|
125
125
|
|
126
126
|
it "doesn't use mutex if requested not to" do
|
127
|
-
subject.instance_variable_get(:@mutex).
|
127
|
+
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
128
128
|
subject.get_rate('USD', 'EUR', :without_mutex => true)
|
129
129
|
end
|
130
130
|
end
|
@@ -140,20 +140,20 @@ describe Money::Bank::VariableExchange do
|
|
140
140
|
context "with format == :json" do
|
141
141
|
it "should return rates formatted as json" do
|
142
142
|
json = subject.export_rates(:json)
|
143
|
-
JSON.load(json).
|
143
|
+
expect(JSON.load(json)).to eq @rates
|
144
144
|
end
|
145
145
|
end
|
146
146
|
|
147
147
|
context "with format == :ruby" do
|
148
148
|
it "should return rates formatted as ruby objects" do
|
149
|
-
Marshal.load(subject.export_rates(:ruby)).
|
149
|
+
expect(Marshal.load(subject.export_rates(:ruby))).to eq @rates
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
153
153
|
context "with format == :yaml" do
|
154
154
|
it "should return rates formatted as yaml" do
|
155
155
|
yaml = subject.export_rates(:yaml)
|
156
|
-
YAML.load(yaml).
|
156
|
+
expect(YAML.load(yaml)).to eq @rates
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
@@ -166,20 +166,20 @@ describe Money::Bank::VariableExchange do
|
|
166
166
|
context "with :file provided" do
|
167
167
|
it "writes rates to file" do
|
168
168
|
f = double('IO')
|
169
|
-
File.
|
170
|
-
f.
|
169
|
+
expect(File).to receive(:open).with('null', 'w').and_yield(f)
|
170
|
+
expect(f).to receive(:write).with(JSON.dump(@rates))
|
171
171
|
|
172
172
|
subject.export_rates(:json, 'null')
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
176
176
|
it "uses a mutex by default" do
|
177
|
-
subject.instance_variable_get(:@mutex).
|
177
|
+
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
178
178
|
subject.export_rates(:yaml)
|
179
179
|
end
|
180
180
|
|
181
181
|
it "doesn't use mutex if requested not to" do
|
182
|
-
subject.instance_variable_get(:@mutex).
|
182
|
+
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
183
183
|
subject.export_rates(:yaml, nil, :without_mutex => true)
|
184
184
|
end
|
185
185
|
end
|
@@ -189,8 +189,8 @@ describe Money::Bank::VariableExchange do
|
|
189
189
|
it "loads the rates provided" do
|
190
190
|
s = '{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}'
|
191
191
|
subject.import_rates(:json, s)
|
192
|
-
subject.get_rate('USD', 'EUR').
|
193
|
-
subject.get_rate('USD', 'JPY').
|
192
|
+
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
193
|
+
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
|
194
194
|
end
|
195
195
|
end
|
196
196
|
|
@@ -198,8 +198,8 @@ describe Money::Bank::VariableExchange do
|
|
198
198
|
it "loads the rates provided" do
|
199
199
|
s = Marshal.dump({"USD_TO_EUR"=>1.25,"USD_TO_JPY"=>2.55})
|
200
200
|
subject.import_rates(:ruby, s)
|
201
|
-
subject.get_rate('USD', 'EUR').
|
202
|
-
subject.get_rate('USD', 'JPY').
|
201
|
+
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
202
|
+
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
@@ -207,8 +207,8 @@ describe Money::Bank::VariableExchange do
|
|
207
207
|
it "loads the rates provided" do
|
208
208
|
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
|
209
209
|
subject.import_rates(:yaml, s)
|
210
|
-
subject.get_rate('USD', 'EUR').
|
211
|
-
subject.get_rate('USD', 'JPY').
|
210
|
+
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
|
211
|
+
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
@@ -219,13 +219,13 @@ describe Money::Bank::VariableExchange do
|
|
219
219
|
end
|
220
220
|
|
221
221
|
it "uses a mutex by default" do
|
222
|
-
subject.instance_variable_get(:@mutex).
|
222
|
+
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
223
223
|
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
|
224
224
|
subject.import_rates(:yaml, s)
|
225
225
|
end
|
226
226
|
|
227
227
|
it "doesn't use mutex if requested not to" do
|
228
|
-
subject.instance_variable_get(:@mutex).
|
228
|
+
expect(subject.instance_variable_get(:@mutex)).not_to receive(:synchronize)
|
229
229
|
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
|
230
230
|
subject.import_rates(:yaml, s, :without_mutex => true)
|
231
231
|
end
|
@@ -249,10 +249,10 @@ describe Money::Bank::VariableExchange do
|
|
249
249
|
end
|
250
250
|
|
251
251
|
it "returns a hashkey based on the passed arguments" do
|
252
|
-
subject.send(:rate_key_for, 'USD', 'EUR').
|
253
|
-
subject.send(:rate_key_for, Money::Currency.wrap('USD'), 'EUR').
|
254
|
-
subject.send(:rate_key_for, 'USD', Money::Currency.wrap('EUR')).
|
255
|
-
subject.send(:rate_key_for, Money::Currency.wrap('USD'), Money::Currency.wrap('EUR')).
|
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
256
|
end
|
257
257
|
|
258
258
|
it "raises a Money::Currency::UnknownCurrency exception when an unknown currency is passed" do
|
@@ -268,8 +268,8 @@ describe Money::Bank::VariableExchange do
|
|
268
268
|
it "works with Marshal.load" do
|
269
269
|
bank = Marshal.load(Marshal.dump(subject))
|
270
270
|
|
271
|
-
bank.rates.
|
272
|
-
bank.rounding_method.
|
271
|
+
expect(bank.rates).to eq subject.rates
|
272
|
+
expect(bank.rounding_method).to eq subject.rounding_method
|
273
273
|
end
|
274
274
|
end
|
275
275
|
end
|
@@ -6,79 +6,79 @@ describe Money::Currency::Heuristics do
|
|
6
6
|
let(:it) { Money::Currency }
|
7
7
|
|
8
8
|
it "is not affected by blank characters and numbers" do
|
9
|
-
it.analyze('123').
|
10
|
-
it.analyze('\n123 \t').
|
9
|
+
expect(it.analyze('123')).to eq []
|
10
|
+
expect(it.analyze('\n123 \t')).to eq []
|
11
11
|
end
|
12
12
|
|
13
13
|
it "returns nothing when given nothing" do
|
14
|
-
it.analyze('').
|
15
|
-
it.analyze(nil).
|
14
|
+
expect(it.analyze('')).to eq []
|
15
|
+
expect(it.analyze(nil)).to eq []
|
16
16
|
end
|
17
17
|
|
18
18
|
it "finds a currency by use of its symbol" do
|
19
|
-
it.analyze('zł').
|
19
|
+
expect(it.analyze('zł')).to eq ['PLN']
|
20
20
|
end
|
21
21
|
|
22
22
|
it "is not affected by trailing dot" do
|
23
|
-
it.analyze('zł.').
|
23
|
+
expect(it.analyze('zł.')).to eq ['PLN']
|
24
24
|
end
|
25
25
|
|
26
26
|
it "finds match even if has numbers after" do
|
27
|
-
it.analyze('zł 123').
|
27
|
+
expect(it.analyze('zł 123')).to eq ['PLN']
|
28
28
|
end
|
29
29
|
|
30
30
|
it "finds match even if has numbers before" do
|
31
|
-
it.analyze('123 zł').
|
31
|
+
expect(it.analyze('123 zł')).to eq ['PLN']
|
32
32
|
end
|
33
33
|
|
34
34
|
it "find match even if symbol is next to number" do
|
35
|
-
it.analyze('300zł').
|
35
|
+
expect(it.analyze('300zł')).to eq ['PLN']
|
36
36
|
end
|
37
37
|
|
38
38
|
it "finds match even if has numbers with delimiters" do
|
39
|
-
it.analyze('zł 123,000.50').
|
40
|
-
it.analyze('zł123,000.50').
|
39
|
+
expect(it.analyze('zł 123,000.50')).to eq ['PLN']
|
40
|
+
expect(it.analyze('zł123,000.50')).to eq ['PLN']
|
41
41
|
end
|
42
42
|
|
43
43
|
it "finds currencies with dots in symbols" do
|
44
|
-
it.analyze('L.E.').
|
44
|
+
expect(it.analyze('L.E.')).to eq ['EGP']
|
45
45
|
end
|
46
46
|
|
47
47
|
it "finds by name" do
|
48
|
-
it.analyze('1900 bulgarian lev').
|
49
|
-
it.analyze('Swedish Krona').
|
48
|
+
expect(it.analyze('1900 bulgarian lev')).to eq ['BGN']
|
49
|
+
expect(it.analyze('Swedish Krona')).to eq ['SEK']
|
50
50
|
end
|
51
51
|
|
52
52
|
it "Finds several currencies when several match" do
|
53
53
|
r = it.analyze('$400')
|
54
|
-
r.
|
55
|
-
r.
|
56
|
-
r.
|
54
|
+
expect(r).to include("ARS")
|
55
|
+
expect(r).to include("USD")
|
56
|
+
expect(r).to include("NZD")
|
57
57
|
|
58
58
|
r = it.analyze('9000 £')
|
59
|
-
r.
|
60
|
-
r.
|
61
|
-
r.
|
59
|
+
expect(r).to include("GBP")
|
60
|
+
expect(r).to include("SHP")
|
61
|
+
expect(r).to include("SYP")
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should use alternate symbols" do
|
65
|
-
it.analyze('US$').
|
65
|
+
expect(it.analyze('US$')).to eq ['USD']
|
66
66
|
end
|
67
67
|
|
68
68
|
it "finds a currency by use of its iso code" do
|
69
|
-
it.analyze('USD 200').
|
69
|
+
expect(it.analyze('USD 200')).to eq ['USD']
|
70
70
|
end
|
71
71
|
|
72
72
|
it "finds currencies in the middle of a sentence!" do
|
73
|
-
it.analyze('It would be nice to have 10000 Albanian lek by tomorrow!').
|
73
|
+
expect(it.analyze('It would be nice to have 10000 Albanian lek by tomorrow!')).to eq ['ALL']
|
74
74
|
end
|
75
75
|
|
76
76
|
it "finds several currencies in the same text!" do
|
77
|
-
it.analyze("10EUR is less than 100:- but really, I want US$1").
|
77
|
+
expect(it.analyze("10EUR is less than 100:- but really, I want US$1")).to eq ['EUR', 'SEK', 'USD']
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should function with unicode characters" do
|
81
|
-
it.analyze("10 դր.").
|
81
|
+
expect(it.analyze("10 դր.")).to eq ["AMD"]
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
data/spec/currency_spec.rb
CHANGED
@@ -18,72 +18,72 @@ describe Money::Currency do
|
|
18
18
|
it "returns currency matching given id" do
|
19
19
|
|
20
20
|
expected = Money::Currency.new(:foo)
|
21
|
-
Money::Currency.find(:foo).
|
22
|
-
Money::Currency.find(:FOO).
|
23
|
-
Money::Currency.find("foo").
|
24
|
-
Money::Currency.find("FOO").
|
21
|
+
expect(Money::Currency.find(:foo)).to eq expected
|
22
|
+
expect(Money::Currency.find(:FOO)).to eq expected
|
23
|
+
expect(Money::Currency.find("foo")).to eq expected
|
24
|
+
expect(Money::Currency.find("FOO")).to eq expected
|
25
25
|
end
|
26
26
|
|
27
27
|
it "returns nil unless currency matching given id" do
|
28
|
-
Money::Currency.find("ZZZ").
|
28
|
+
expect(Money::Currency.find("ZZZ")).to be_nil
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
describe ".find_by_iso_numeric" do
|
33
33
|
it "returns currency matching given numeric code" do
|
34
|
-
Money::Currency.find_by_iso_numeric(978).
|
35
|
-
Money::Currency.find_by_iso_numeric(208).
|
36
|
-
Money::Currency.find_by_iso_numeric('840').
|
34
|
+
expect(Money::Currency.find_by_iso_numeric(978)).to eq Money::Currency.new(:eur)
|
35
|
+
expect(Money::Currency.find_by_iso_numeric(208)).not_to eq Money::Currency.new(:eur)
|
36
|
+
expect(Money::Currency.find_by_iso_numeric('840')).to eq Money::Currency.new(:usd)
|
37
37
|
|
38
38
|
class Mock
|
39
39
|
def to_s
|
40
40
|
'208'
|
41
41
|
end
|
42
42
|
end
|
43
|
-
Money::Currency.find_by_iso_numeric(Mock.new).
|
44
|
-
Money::Currency.find_by_iso_numeric(Mock.new).
|
43
|
+
expect(Money::Currency.find_by_iso_numeric(Mock.new)).to eq Money::Currency.new(:dkk)
|
44
|
+
expect(Money::Currency.find_by_iso_numeric(Mock.new)).not_to eq Money::Currency.new(:usd)
|
45
45
|
end
|
46
46
|
|
47
47
|
it "returns nil if no currency has the given numeric code" do
|
48
|
-
Money::Currency.find_by_iso_numeric('non iso 4217 numeric code').
|
49
|
-
Money::Currency.find_by_iso_numeric(0).
|
48
|
+
expect(Money::Currency.find_by_iso_numeric('non iso 4217 numeric code')).to be_nil
|
49
|
+
expect(Money::Currency.find_by_iso_numeric(0)).to be_nil
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
describe ".wrap" do
|
54
54
|
it "returns nil if object is nil" do
|
55
|
-
Money::Currency.wrap(nil).
|
56
|
-
Money::Currency.wrap(Money::Currency.new(:usd)).
|
57
|
-
Money::Currency.wrap(:usd).
|
55
|
+
expect(Money::Currency.wrap(nil)).to be_nil
|
56
|
+
expect(Money::Currency.wrap(Money::Currency.new(:usd))).to eq Money::Currency.new(:usd)
|
57
|
+
expect(Money::Currency.wrap(:usd)).to eq Money::Currency.new(:usd)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
describe ".all" do
|
62
62
|
it "returns an array of currencies" do
|
63
|
-
Money::Currency.all.
|
63
|
+
expect(Money::Currency.all).to include Money::Currency.new(:usd)
|
64
64
|
end
|
65
65
|
it "includes registered currencies" do
|
66
66
|
Money::Currency.register(JSON.parse(FOO, :symbolize_names => true))
|
67
|
-
Money::Currency.all.
|
67
|
+
expect(Money::Currency.all).to include Money::Currency.new(:foo)
|
68
68
|
Money::Currency.unregister(JSON.parse(FOO, :symbolize_names => true))
|
69
69
|
end
|
70
70
|
it 'is sorted by priority' do
|
71
|
-
Money::Currency.all.first.priority.
|
71
|
+
expect(Money::Currency.all.first.priority).to eq 1
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
describe "#initialize" do
|
76
76
|
it "lookups data from loaded config" do
|
77
77
|
currency = Money::Currency.new("USD")
|
78
|
-
currency.id.
|
79
|
-
currency.priority.
|
80
|
-
currency.iso_code.
|
81
|
-
currency.iso_numeric.
|
82
|
-
currency.name.
|
83
|
-
currency.decimal_mark.
|
84
|
-
currency.separator.
|
85
|
-
currency.thousands_separator.
|
86
|
-
currency.delimiter.
|
78
|
+
expect(currency.id).to eq :usd
|
79
|
+
expect(currency.priority).to eq 1
|
80
|
+
expect(currency.iso_code).to eq "USD"
|
81
|
+
expect(currency.iso_numeric).to eq "840"
|
82
|
+
expect(currency.name).to eq "United States Dollar"
|
83
|
+
expect(currency.decimal_mark).to eq "."
|
84
|
+
expect(currency.separator).to eq "."
|
85
|
+
expect(currency.thousands_separator).to eq ","
|
86
|
+
expect(currency.delimiter).to eq ","
|
87
87
|
end
|
88
88
|
|
89
89
|
it "raises UnknownMoney::Currency with unknown currency" do
|
@@ -93,99 +93,98 @@ describe Money::Currency do
|
|
93
93
|
|
94
94
|
describe "#<=>" do
|
95
95
|
it "compares objects by priority" do
|
96
|
-
Money::Currency.new(:cad).
|
97
|
-
Money::Currency.new(:usd).
|
96
|
+
expect(Money::Currency.new(:cad)).to be > Money::Currency.new(:usd)
|
97
|
+
expect(Money::Currency.new(:usd)).to be < Money::Currency.new(:eur)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
describe "#==" do
|
102
102
|
it "returns true if self === other" do
|
103
103
|
currency = Money::Currency.new(:eur)
|
104
|
-
currency.
|
104
|
+
expect(currency).to eq currency
|
105
105
|
end
|
106
106
|
|
107
107
|
it "returns true if the id is equal ignorning case" do
|
108
|
-
Money::Currency.new(:eur).
|
109
|
-
Money::Currency.new(:eur).
|
110
|
-
Money::Currency.new(:eur).
|
108
|
+
expect(Money::Currency.new(:eur)).to eq Money::Currency.new(:eur)
|
109
|
+
expect(Money::Currency.new(:eur)).to eq Money::Currency.new(:EUR)
|
110
|
+
expect(Money::Currency.new(:eur)).not_to eq Money::Currency.new(:usd)
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
it "allows direct comparison of currencies and symbols/strings" do
|
114
|
-
Money::Currency.new(:eur).
|
115
|
-
Money::Currency.new(:eur).
|
116
|
-
Money::Currency.new(:eur).
|
117
|
-
Money::Currency.new(:eur).
|
118
|
-
Money::Currency.new(:eur).
|
114
|
+
expect(Money::Currency.new(:eur)).to eq 'eur'
|
115
|
+
expect(Money::Currency.new(:eur)).to eq 'EUR'
|
116
|
+
expect(Money::Currency.new(:eur)).to eq :eur
|
117
|
+
expect(Money::Currency.new(:eur)).to eq :EUR
|
118
|
+
expect(Money::Currency.new(:eur)).not_to eq 'usd'
|
119
119
|
end
|
120
120
|
|
121
121
|
it "allows comparison with nil and returns false" do
|
122
|
-
Money::Currency.new(:eur).
|
122
|
+
expect(Money::Currency.new(:eur)).not_to be_nil
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
126
126
|
describe "#eql?" do
|
127
127
|
it "returns true if #== returns true" do
|
128
|
-
Money::Currency.new(:eur).eql?(Money::Currency.new(:eur)).
|
129
|
-
Money::Currency.new(:eur).eql?(Money::Currency.new(:usd)).
|
128
|
+
expect(Money::Currency.new(:eur).eql?(Money::Currency.new(:eur))).to be true
|
129
|
+
expect(Money::Currency.new(:eur).eql?(Money::Currency.new(:usd))).to be false
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
133
|
describe "#hash" do
|
134
134
|
it "returns the same value for equal objects" do
|
135
|
-
Money::Currency.new(:eur).hash.
|
136
|
-
Money::Currency.new(:eur).hash.
|
135
|
+
expect(Money::Currency.new(:eur).hash).to eq Money::Currency.new(:eur).hash
|
136
|
+
expect(Money::Currency.new(:eur).hash).not_to eq Money::Currency.new(:usd).hash
|
137
137
|
end
|
138
138
|
|
139
139
|
it "can be used to return the intersection of Currency object arrays" do
|
140
140
|
intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
|
141
|
-
intersection.
|
141
|
+
expect(intersection).to eq [Money::Currency.new(:eur)]
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
145
145
|
describe "#inspect" do
|
146
146
|
it "works as documented" do
|
147
|
-
Money::Currency.new(:usd).inspect.
|
148
|
-
%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>}
|
147
|
+
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>}
|
149
148
|
end
|
150
149
|
end
|
151
150
|
|
152
151
|
describe "#to_s" do
|
153
152
|
it "works as documented" do
|
154
|
-
Money::Currency.new(:usd).to_s.
|
155
|
-
Money::Currency.new(:eur).to_s.
|
153
|
+
expect(Money::Currency.new(:usd).to_s).to eq("USD")
|
154
|
+
expect(Money::Currency.new(:eur).to_s).to eq("EUR")
|
156
155
|
end
|
157
156
|
end
|
158
157
|
|
159
158
|
describe "#to_str" do
|
160
159
|
it "works as documented" do
|
161
|
-
Money::Currency.new(:usd).to_str.
|
162
|
-
Money::Currency.new(:eur).to_str.
|
160
|
+
expect(Money::Currency.new(:usd).to_str).to eq("USD")
|
161
|
+
expect(Money::Currency.new(:eur).to_str).to eq("EUR")
|
163
162
|
end
|
164
163
|
end
|
165
164
|
|
166
165
|
describe "#to_sym" do
|
167
166
|
it "works as documented" do
|
168
|
-
Money::Currency.new(:usd).to_sym.
|
169
|
-
Money::Currency.new(:eur).to_sym.
|
167
|
+
expect(Money::Currency.new(:usd).to_sym).to eq(:USD)
|
168
|
+
expect(Money::Currency.new(:eur).to_sym).to eq(:EUR)
|
170
169
|
end
|
171
170
|
end
|
172
171
|
|
173
172
|
describe "#to_currency" do
|
174
173
|
it "works as documented" do
|
175
174
|
usd = Money::Currency.new(:usd)
|
176
|
-
usd.to_currency.
|
175
|
+
expect(usd.to_currency).to eq usd
|
177
176
|
end
|
178
177
|
|
179
178
|
it "doesn't create new symbols indefinitely" do
|
180
179
|
expect { Money::Currency.new("bogus") }.to raise_exception(Money::Currency::UnknownCurrency)
|
181
|
-
Symbol.all_symbols.map{|s| s.to_s}.
|
180
|
+
expect(Symbol.all_symbols.map{|s| s.to_s}).not_to include("bogus")
|
182
181
|
end
|
183
182
|
end
|
184
183
|
|
185
184
|
describe "#code" do
|
186
185
|
it "works as documented" do
|
187
|
-
Money::Currency.new(:usd).code.
|
188
|
-
Money::Currency.new(:azn).code.
|
186
|
+
expect(Money::Currency.new(:usd).code).to eq "$"
|
187
|
+
expect(Money::Currency.new(:azn).code).to eq "\u20BC"
|
189
188
|
end
|
190
189
|
end
|
191
190
|
|