monetize 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +32 -0
- data/CHANGELOG.md +9 -2
- data/Gemfile +2 -2
- data/Rakefile +13 -1
- data/lib/collection.rb +3 -5
- data/lib/monetize.rb +124 -94
- data/lib/monetize/core_extensions.rb +5 -5
- data/lib/monetize/version.rb +1 -1
- data/monetize.gemspec +16 -15
- data/spec/core_extensions_spec.rb +122 -125
- data/spec/monetize_spec.rb +240 -199
- data/spec/spec_helper.rb +3 -3
- metadata +7 -5
@@ -1,19 +1,19 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'monetize'
|
5
|
+
require 'monetize/core_extensions'
|
6
6
|
|
7
|
-
describe Monetize,
|
7
|
+
describe Monetize, 'core extensions' do
|
8
8
|
describe NilClass do
|
9
|
-
describe
|
10
|
-
it
|
9
|
+
describe '#to_money' do
|
10
|
+
it 'work as documented' do
|
11
11
|
money = nil.to_money
|
12
12
|
expect(money.cents).to eq 0
|
13
13
|
expect(money.currency).to eq Money.default_currency
|
14
14
|
end
|
15
15
|
|
16
|
-
it
|
16
|
+
it 'accepts optional currency' do
|
17
17
|
expect(nil.to_money('USD')).to eq Money.new(nil, 'USD')
|
18
18
|
expect(nil.to_money('EUR')).to eq Money.new(nil, 'EUR')
|
19
19
|
end
|
@@ -21,10 +21,10 @@ describe Monetize, "core extensions" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
describe Numeric do
|
24
|
-
describe
|
25
|
-
it
|
24
|
+
describe '#to_money' do
|
25
|
+
it 'work as documented' do
|
26
26
|
money = 1234.to_money
|
27
|
-
expect(money.cents).to eq
|
27
|
+
expect(money.cents).to eq 1_234_00
|
28
28
|
expect(money.currency).to eq Money.default_currency
|
29
29
|
|
30
30
|
money = 100.37.to_money
|
@@ -32,183 +32,180 @@ describe Monetize, "core extensions" do
|
|
32
32
|
expect(money.currency).to eq Money.default_currency
|
33
33
|
|
34
34
|
money = BigDecimal.new('1234').to_money
|
35
|
-
expect(money.cents).to eq
|
35
|
+
expect(money.cents).to eq 1_234_00
|
36
36
|
expect(money.currency).to eq Money.default_currency
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
40
|
-
expect(1234.to_money('USD')).to eq Money.new(
|
41
|
-
expect(1234.to_money('EUR')).to eq Money.new(
|
39
|
+
it 'accepts optional currency' do
|
40
|
+
expect(1234.to_money('USD')).to eq Money.new(1_234_00, 'USD')
|
41
|
+
expect(1234.to_money('EUR')).to eq Money.new(1_234_00, 'EUR')
|
42
42
|
end
|
43
43
|
|
44
|
-
it
|
44
|
+
it 'respects :subunit_to_unit currency property' do
|
45
45
|
expect(10.to_money('USD')).to eq Money.new(10_00, 'USD')
|
46
46
|
expect(10.to_money('TND')).to eq Money.new(10_000, 'TND')
|
47
47
|
expect(10.to_money('JPY')).to eq Money.new(10, 'JPY')
|
48
48
|
end
|
49
49
|
|
50
|
-
specify
|
50
|
+
specify 'GH-15' do
|
51
51
|
amount = 555.55.to_money
|
52
|
-
expect(amount).to eq Money.new(
|
52
|
+
expect(amount).to eq Money.new(555_55)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
describe String do
|
58
|
-
describe
|
59
|
-
|
58
|
+
describe '#to_money' do
|
60
59
|
STRING_TO_MONEY = {
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
60
|
+
'20.15' => Money.new(20_15),
|
61
|
+
'100' => Money.new(100_00),
|
62
|
+
'100.37' => Money.new(100_37),
|
63
|
+
'100,37' => Money.new(100_37),
|
64
|
+
'100 000' => Money.new(100_000_00),
|
65
|
+
'100,000.00' => Money.new(100_000_00),
|
66
|
+
'1,000' => Money.new(1_000_00),
|
67
|
+
'-1,000' => Money.new(-1_000_00),
|
68
|
+
'1,000.5' => Money.new(1_000_50),
|
69
|
+
'1,000.51' => Money.new(1_000_51),
|
70
|
+
'1,000.505' => Money.new(1_000_51),
|
71
|
+
'1,000.504' => Money.new(1_000_50),
|
72
|
+
'1,000.0000' => Money.new(1_000_00),
|
73
|
+
'1,000.5000' => Money.new(1_000_50),
|
74
|
+
'1,000.5099' => Money.new(1_000_51),
|
75
|
+
'1.550' => Money.new(1_55),
|
76
|
+
'25.' => Money.new(25_00),
|
77
|
+
'.75' => Money.new(75),
|
78
|
+
|
79
|
+
'7.21K' => Money.new(7_210_00),
|
80
|
+
'1M' => Money.new(1_000_000_00),
|
81
|
+
'1.2M' => Money.new(1_200_000_00),
|
82
|
+
'1.37M' => Money.new(1_370_000_00),
|
83
|
+
'3.1415927M' => Money.new(3_141_592_70),
|
84
|
+
'.42B' => Money.new(420_000_000_00),
|
85
|
+
'5T' => Money.new(5_000_000_000_000_00),
|
86
|
+
|
87
|
+
'100 USD' => Money.new(100_00, 'USD'),
|
88
|
+
'-100 USD' => Money.new(-100_00, 'USD'),
|
89
|
+
'100 EUR' => Money.new(100_00, 'EUR'),
|
90
|
+
'100.37 EUR' => Money.new(100_37, 'EUR'),
|
91
|
+
'100,37 EUR' => Money.new(100_37, 'EUR'),
|
92
|
+
'100,000.00 USD' => Money.new(100_000_00, 'USD'),
|
93
|
+
'100.000,00 EUR' => Money.new(100_000_00, 'EUR'),
|
94
|
+
'1,000 USD' => Money.new(1_000_00, 'USD'),
|
95
|
+
'-1,000 USD' => Money.new(-1_000_00, 'USD'),
|
96
|
+
'1,000.5500 USD' => Money.new(1_000_55, 'USD'),
|
97
|
+
'-1,000.6500 USD' => Money.new(-1_000_65, 'USD'),
|
98
|
+
'1.550 USD' => Money.new(1_55, 'USD'),
|
99
|
+
|
100
|
+
'USD 100' => Money.new(100_00, 'USD'),
|
101
|
+
'EUR 100' => Money.new(100_00, 'EUR'),
|
102
|
+
'EUR 100.37' => Money.new(100_37, 'EUR'),
|
103
|
+
'CAD -100.37' => Money.new(-100_37, 'CAD'),
|
104
|
+
'EUR 100,37' => Money.new(100_37, 'EUR'),
|
105
|
+
'EUR -100,37' => Money.new(-100_37, 'EUR'),
|
106
|
+
'USD 100,000.00' => Money.new(100_000_00, 'USD'),
|
107
|
+
'EUR 100.000,00' => Money.new(100_000_00, 'EUR'),
|
108
|
+
'USD 1,000' => Money.new(1_000_00, 'USD'),
|
109
|
+
'USD -1,000' => Money.new(-1_000_00, 'USD'),
|
110
|
+
'USD 1,000.9000' => Money.new(1_000_90, 'USD'),
|
111
|
+
'USD -1,000.090' => Money.new(-1_000_09, 'USD'),
|
112
|
+
'USD 1.5500' => Money.new(1_55, 'USD'),
|
113
|
+
|
114
|
+
'$100 USD' => Money.new(100_00, 'USD'),
|
115
|
+
'$1,194.59 USD' => Money.new(1_194_59, 'USD'),
|
116
|
+
'$-1,955 USD' => Money.new(-1_955_00, 'USD'),
|
117
|
+
'$1,194.5900 USD' => Money.new(1_194_59, 'USD'),
|
118
|
+
'$-1,955.000 USD' => Money.new(-1_955_00, 'USD'),
|
119
|
+
'$1.99000 USD' => Money.new(1_99, 'USD')
|
121
120
|
}
|
122
121
|
|
123
|
-
it
|
122
|
+
it 'works as documented' do
|
124
123
|
STRING_TO_MONEY.each do |string, money|
|
125
124
|
expect(string.to_money).to eq money
|
126
125
|
end
|
127
126
|
end
|
128
127
|
|
129
|
-
it
|
130
|
-
expect(Monetize.parse(20,
|
128
|
+
it 'coerces input to string' do
|
129
|
+
expect(Monetize.parse(20, 'USD')).to eq Money.new(20_00, 'USD')
|
131
130
|
end
|
132
131
|
|
133
|
-
it
|
134
|
-
expect(
|
135
|
-
expect(
|
136
|
-
expect(
|
132
|
+
it 'accepts optional currency' do
|
133
|
+
expect('10.10'.to_money('USD')).to eq Money.new(10_10, 'USD')
|
134
|
+
expect('10.10'.to_money('EUR')).to eq Money.new(10_10, 'EUR')
|
135
|
+
expect('10.10 USD'.to_money('USD')).to eq Money.new(10_10, 'USD')
|
137
136
|
end
|
138
137
|
|
139
|
-
it
|
140
|
-
expect(
|
138
|
+
it 'uses parsed currency, even if currency is passed' do
|
139
|
+
expect('10.10 USD'.to_money('EUR')).to eq(Money.new(10_10, 'USD'))
|
141
140
|
end
|
142
141
|
|
143
|
-
it
|
144
|
-
expect(
|
142
|
+
it 'ignores unrecognized data' do
|
143
|
+
expect('hello 2000 world'.to_money).to eq Money.new(2_000_00)
|
145
144
|
end
|
146
145
|
|
147
|
-
it
|
148
|
-
expect(
|
149
|
-
expect(
|
150
|
-
expect(
|
151
|
-
expect(
|
146
|
+
it 'respects :subunit_to_unit currency property' do
|
147
|
+
expect('1'.to_money('USD')).to eq Money.new(1_00, 'USD')
|
148
|
+
expect('1'.to_money('TND')).to eq Money.new(1_000, 'TND')
|
149
|
+
expect('1'.to_money('JPY')).to eq Money.new(1, 'JPY')
|
150
|
+
expect('1.5'.to_money('KWD').cents).to eq 1_500
|
152
151
|
end
|
153
152
|
end
|
154
153
|
|
155
|
-
describe
|
156
|
-
it
|
157
|
-
expect(
|
158
|
-
expect(
|
154
|
+
describe '#to_currency' do
|
155
|
+
it 'converts String to Currency' do
|
156
|
+
expect('USD'.to_currency).to eq Money::Currency.new('USD')
|
157
|
+
expect('EUR'.to_currency).to eq Money::Currency.new('EUR')
|
159
158
|
end
|
160
159
|
|
161
|
-
it
|
162
|
-
expect {
|
163
|
-
expect {
|
160
|
+
it 'raises Money::Currency::UnknownCurrency with unknown Currency' do
|
161
|
+
expect { 'XXX'.to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
162
|
+
expect { ' '.to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
164
163
|
end
|
165
164
|
end
|
166
165
|
end
|
167
166
|
|
168
167
|
describe Hash do
|
169
|
-
describe
|
170
|
-
|
171
|
-
|
172
|
-
subject(:hash){ { cents: 5, currency: "EUR" } }
|
168
|
+
describe '#to_money' do
|
169
|
+
context 'when currency is present in hash' do
|
170
|
+
subject(:hash) { {cents: 5, currency: 'EUR'} }
|
173
171
|
|
174
|
-
it
|
175
|
-
expect(hash.to_money).to eq(Money.new(5,
|
172
|
+
it 'converts Hash to Money using key from hash' do
|
173
|
+
expect(hash.to_money).to eq(Money.new(5, 'EUR'))
|
176
174
|
end
|
177
175
|
end
|
178
176
|
|
179
|
-
context
|
180
|
-
subject(:hash){ {
|
177
|
+
context 'when currency is passed as argument' do
|
178
|
+
subject(:hash) { {cents: 10} }
|
181
179
|
|
182
|
-
it
|
183
|
-
expect(hash.to_money(
|
180
|
+
it 'converts Hash to Money using passed currency argument' do
|
181
|
+
expect(hash.to_money('JPY')).to eq(Money.new(10, 'JPY'))
|
184
182
|
end
|
185
183
|
end
|
186
184
|
|
187
|
-
context
|
188
|
-
subject(:hash){ {
|
185
|
+
context 'when no currency is passed' do
|
186
|
+
subject(:hash) { {cents: 123} }
|
189
187
|
|
190
|
-
before { allow(Money).to receive(:default_currency).and_return(
|
188
|
+
before { allow(Money).to receive(:default_currency).and_return('USD') }
|
191
189
|
|
192
|
-
it
|
193
|
-
expect(hash.to_money(
|
190
|
+
it 'converts Hash to Money using default value' do
|
191
|
+
expect(hash.to_money('USD')).to eq(Money.new(123, 'USD'))
|
194
192
|
end
|
195
193
|
end
|
196
|
-
|
197
194
|
end
|
198
195
|
end
|
199
196
|
|
200
197
|
describe Symbol do
|
201
|
-
describe
|
202
|
-
it
|
203
|
-
expect(:usd.to_currency).to eq Money::Currency.new(
|
204
|
-
expect(:ars.to_currency).to eq Money::Currency.new(
|
198
|
+
describe '#to_currency' do
|
199
|
+
it 'converts Symbol to Currency' do
|
200
|
+
expect(:usd.to_currency).to eq Money::Currency.new('USD')
|
201
|
+
expect(:ars.to_currency).to eq Money::Currency.new('ARS')
|
205
202
|
end
|
206
203
|
|
207
|
-
it
|
208
|
-
expect(:EUR.to_currency).to eq Money::Currency.new(
|
204
|
+
it 'is case-insensitive' do
|
205
|
+
expect(:EUR.to_currency).to eq Money::Currency.new('EUR')
|
209
206
|
end
|
210
207
|
|
211
|
-
it
|
208
|
+
it 'raises Money::Currency::UnknownCurrency with unknown Currency' do
|
212
209
|
expect { :XXX.to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
213
210
|
expect { :" ".to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
214
211
|
end
|
data/spec/monetize_spec.rb
CHANGED
@@ -1,26 +1,52 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'monetize'
|
5
5
|
|
6
6
|
describe Monetize do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
bar = <<-JSON
|
8
|
+
{
|
9
|
+
"priority": 1,
|
10
|
+
"iso_code": "BAR",
|
11
|
+
"iso_numeric": "840",
|
12
|
+
"name": "Dollar with 4 decimal places",
|
13
|
+
"symbol": "$",
|
14
|
+
"subunit": "Cent",
|
15
|
+
"subunit_to_unit": 10000,
|
16
|
+
"symbol_first": true,
|
17
|
+
"html_entity": "$",
|
18
|
+
"decimal_mark": ".",
|
19
|
+
"thousands_separator": ","
|
20
|
+
}
|
21
|
+
JSON
|
22
|
+
|
23
|
+
eu4 = <<-JSON
|
24
|
+
{
|
25
|
+
"priority": 1,
|
26
|
+
"iso_code": "EU4",
|
27
|
+
"iso_numeric": "841",
|
28
|
+
"name": "Euro with 4 decimal places",
|
29
|
+
"symbol": "€",
|
30
|
+
"subunit": "Cent",
|
31
|
+
"subunit_to_unit": 10000,
|
32
|
+
"symbol_first": true,
|
33
|
+
"html_entity": "€",
|
34
|
+
"decimal_mark": ",",
|
35
|
+
"thousands_separator": "."
|
36
|
+
}
|
37
|
+
JSON
|
38
|
+
|
39
|
+
describe '.parse' do
|
40
|
+
it 'parses european-formatted inputs under 10EUR' do
|
14
41
|
expect(Monetize.parse('EUR 5,95')).to eq Money.new(595, 'EUR')
|
15
42
|
end
|
16
43
|
|
17
|
-
it
|
18
|
-
expect(Monetize.parse('EUR 1.234.567,89')).to eq Money.new(
|
19
|
-
expect(Monetize.parse('EUR 1.111.234.567,89')).to eq Money.new(
|
44
|
+
it 'parses european-formatted inputs with multiple thousands-seperators' do
|
45
|
+
expect(Monetize.parse('EUR 1.234.567,89')).to eq Money.new(1_234_567_89, 'EUR')
|
46
|
+
expect(Monetize.parse('EUR 1.111.234.567,89')).to eq Money.new(1_111_234_567_89, 'EUR')
|
20
47
|
end
|
21
48
|
|
22
49
|
describe 'currency detection' do
|
23
|
-
|
24
50
|
context 'opted in' do
|
25
51
|
before :all do
|
26
52
|
Monetize.assume_from_symbol = true
|
@@ -30,69 +56,68 @@ describe Monetize do
|
|
30
56
|
Monetize.assume_from_symbol = false
|
31
57
|
end
|
32
58
|
|
33
|
-
it
|
34
|
-
expect(Monetize.parse(
|
59
|
+
it 'parses formatted inputs with Euros passed as a symbol' do
|
60
|
+
expect(Monetize.parse('€5.95')).to eq Money.new(595, 'EUR')
|
35
61
|
end
|
36
62
|
|
37
|
-
it
|
38
|
-
expect(Monetize.parse(
|
63
|
+
it 'parses formatted inputs with Euros passed as a symbol with surrounding space' do
|
64
|
+
expect(Monetize.parse(' €5.95 ')).to eq Money.new(595, 'EUR')
|
39
65
|
end
|
40
66
|
|
41
|
-
it
|
42
|
-
expect(Monetize.parse(
|
67
|
+
it 'parses formatted inputs with British Pounds Sterling passed as a symbol' do
|
68
|
+
expect(Monetize.parse('£9.99')).to eq Money.new(999, 'GBP')
|
43
69
|
end
|
44
70
|
|
45
|
-
it
|
46
|
-
expect(Monetize.parse(
|
71
|
+
it 'parses formatted inputs with British Pounds Sterling passed as a lira sign symbol' do
|
72
|
+
expect(Monetize.parse('₤9.99')).to eq Money.new(999, 'GBP')
|
47
73
|
end
|
48
74
|
|
49
|
-
it
|
50
|
-
expect(Monetize.parse(
|
75
|
+
it 'parses formatted inputs with South African Rand passed as a symbol' do
|
76
|
+
expect(Monetize.parse('R9.99')).to eq Money.new(999, 'ZAR')
|
51
77
|
end
|
52
78
|
|
53
|
-
it
|
54
|
-
expect(Monetize.parse(
|
79
|
+
it 'parses formatted inputs with Brazilian real passed as a symbol' do
|
80
|
+
expect(Monetize.parse('R$R9.99')).to eq Money.new(999, 'BRL')
|
55
81
|
end
|
56
82
|
|
57
|
-
it
|
58
|
-
expect(Monetize.parse(
|
83
|
+
it 'parses formatted inputs with Japanese Yen passed as a symbol' do
|
84
|
+
expect(Monetize.parse('¥999')).to eq Money.new(999, 'JPY')
|
59
85
|
end
|
60
86
|
|
61
|
-
it
|
62
|
-
expect(Monetize.parse(
|
87
|
+
it 'parses formatted inputs with Canadian Dollar passed as a symbol' do
|
88
|
+
expect(Monetize.parse('C$9.99')).to eq Money.new(999, 'CAD')
|
63
89
|
end
|
64
90
|
|
65
91
|
it 'should assume default currency if not a recognised symbol' do
|
66
|
-
expect(Monetize.parse(
|
92
|
+
expect(Monetize.parse('L9.99')).to eq Money.new(999, 'USD')
|
67
93
|
end
|
68
94
|
|
69
95
|
it 'parses formatted inputs without currency detection when overridden' do
|
70
|
-
expect(Monetize.parse(
|
96
|
+
expect(Monetize.parse('£9.99', nil, assume_from_symbol: false)).to eq Money.new(999, 'USD')
|
71
97
|
end
|
72
98
|
|
73
99
|
it 'parses formatted inputs with minus sign and currency symbol' do
|
74
|
-
expect(Monetize.parse(
|
75
|
-
expect(Monetize.parse(
|
76
|
-
expect(Monetize.parse(
|
77
|
-
expect(Monetize.parse(
|
78
|
-
expect(Monetize.parse(
|
100
|
+
expect(Monetize.parse('-€9.99')).to eq Money.new(-999, 'EUR')
|
101
|
+
expect(Monetize.parse('-£9.99')).to eq Money.new(-999, 'GBP')
|
102
|
+
expect(Monetize.parse('-R$R9.99')).to eq Money.new(-999, 'BRL')
|
103
|
+
expect(Monetize.parse('-¥999')).to eq Money.new(-999, 'JPY')
|
104
|
+
expect(Monetize.parse('-C$9.99')).to eq Money.new(-999, 'CAD')
|
79
105
|
end
|
80
106
|
|
81
107
|
it 'parses formatted inputs with plus and GBP passed as symbol' do
|
82
|
-
expect(Monetize.parse(
|
83
|
-
expect(Monetize.parse(
|
84
|
-
expect(Monetize.parse(
|
85
|
-
expect(Monetize.parse(
|
86
|
-
expect(Monetize.parse(
|
87
|
-
|
108
|
+
expect(Monetize.parse('+€9.99')).to eq Money.new(999, 'EUR')
|
109
|
+
expect(Monetize.parse('+£9.99')).to eq Money.new(999, 'GBP')
|
110
|
+
expect(Monetize.parse('+R$R9.99')).to eq Money.new(999, 'BRL')
|
111
|
+
expect(Monetize.parse('+¥999')).to eq Money.new(999, 'JPY')
|
112
|
+
expect(Monetize.parse('+C$9.99')).to eq Money.new(999, 'CAD')
|
88
113
|
end
|
89
114
|
|
90
115
|
it 'parses formatted inputs with currency symbol and postfix minus sign' do
|
91
|
-
expect(Monetize.parse(
|
116
|
+
expect(Monetize.parse('€9.99-')).to eq Money.new(-999, 'EUR')
|
92
117
|
end
|
93
118
|
|
94
119
|
it 'parses formatted inputs with currency symbol and postfix plus sign' do
|
95
|
-
expect(Monetize.parse(
|
120
|
+
expect(Monetize.parse('€9.99+')).to eq Money.new(999, 'EUR')
|
96
121
|
end
|
97
122
|
|
98
123
|
it 'parses formatted inputs with amounts given with suffixes' do
|
@@ -109,24 +134,24 @@ describe Monetize do
|
|
109
134
|
Monetize.assume_from_symbol = false
|
110
135
|
end
|
111
136
|
|
112
|
-
it
|
113
|
-
expect(Monetize.parse(
|
137
|
+
it 'ignores the Euro symbol' do
|
138
|
+
expect(Monetize.parse('€5.95')).to eq Money.new(595, 'USD')
|
114
139
|
end
|
115
140
|
|
116
|
-
it
|
117
|
-
expect(Monetize.parse(
|
141
|
+
it 'ignores the South African Rand symbol' do
|
142
|
+
expect(Monetize.parse('R5.95')).to eq Money.new(595, 'USD')
|
118
143
|
end
|
119
144
|
|
120
|
-
it
|
121
|
-
expect(Monetize.parse(
|
145
|
+
it 'ignores the Euro symbol with surrounding spaces' do
|
146
|
+
expect(Monetize.parse(' €5.95 ')).to eq Money.new(595, 'USD')
|
122
147
|
end
|
123
148
|
|
124
|
-
it
|
125
|
-
expect(Monetize.parse(
|
149
|
+
it 'ignores the British Pounds Sterling symbol' do
|
150
|
+
expect(Monetize.parse('£9.99')).to eq Money.new(999, 'USD')
|
126
151
|
end
|
127
152
|
|
128
153
|
it 'parses formatted inputs with currency detection when overridden' do
|
129
|
-
expect(Monetize.parse(
|
154
|
+
expect(Monetize.parse('£9.99', nil, assume_from_symbol: true)).to eq Money.new(999, 'GBP')
|
130
155
|
end
|
131
156
|
end
|
132
157
|
|
@@ -135,7 +160,7 @@ describe Monetize do
|
|
135
160
|
end
|
136
161
|
end
|
137
162
|
|
138
|
-
it
|
163
|
+
it 'parses USD-formatted inputs under $10' do
|
139
164
|
five_ninety_five = Money.new(595, 'USD')
|
140
165
|
|
141
166
|
expect(Monetize.parse(5.95)).to eq five_ninety_five
|
@@ -147,215 +172,223 @@ describe Monetize do
|
|
147
172
|
expect(Monetize.parse('$5.95, each')).to eq five_ninety_five
|
148
173
|
end
|
149
174
|
|
150
|
-
it
|
151
|
-
expect(Monetize.parse('1,234,567.89')).to eq Money.new(
|
152
|
-
expect(Monetize.parse('1,111,234,567.89')).to eq Money.new(
|
175
|
+
it 'parses USD-formatted inputs with multiple thousands-seperators' do
|
176
|
+
expect(Monetize.parse('1,234,567.89')).to eq Money.new(1_234_567_89, 'USD')
|
177
|
+
expect(Monetize.parse('1,111,234,567.89')).to eq Money.new(1_111_234_567_89, 'USD')
|
153
178
|
end
|
154
179
|
|
155
|
-
it
|
180
|
+
it 'does not return a price if there is a price range' do
|
156
181
|
expect { Monetize.parse('$5.95-10.95') }.to raise_error ArgumentError
|
157
182
|
expect { Monetize.parse('$5.95 - 10.95') }.to raise_error ArgumentError
|
158
183
|
expect { Monetize.parse('$5.95 - $10.95') }.to raise_error ArgumentError
|
159
184
|
end
|
160
185
|
|
161
|
-
it
|
186
|
+
it 'does not return a price for completely invalid input' do
|
162
187
|
expect(Monetize.parse(nil)).to eq Money.empty
|
163
188
|
expect(Monetize.parse('hellothere')).to eq Money.empty
|
164
189
|
expect(Monetize.parse('')).to eq Money.empty
|
165
190
|
end
|
166
191
|
|
167
|
-
it
|
192
|
+
it 'handles negative inputs' do
|
168
193
|
five_ninety_five = Money.new(-595, 'USD')
|
169
194
|
|
170
|
-
expect(Monetize.parse(
|
171
|
-
expect(Monetize.parse(
|
172
|
-
expect(Monetize.parse(
|
195
|
+
expect(Monetize.parse('$-5.95')).to eq five_ninety_five
|
196
|
+
expect(Monetize.parse('-$5.95')).to eq five_ninety_five
|
197
|
+
expect(Monetize.parse('$5.95-')).to eq five_ninety_five
|
173
198
|
end
|
174
199
|
|
175
|
-
it
|
200
|
+
it 'raises ArgumentError when unable to detect polarity' do
|
176
201
|
expect { Monetize.parse('-$5.95-') }.to raise_error ArgumentError
|
177
202
|
end
|
178
203
|
|
179
|
-
it
|
180
|
-
expect(Monetize.parse(
|
204
|
+
it 'parses correctly strings with exactly 3 decimal digits' do
|
205
|
+
expect(Monetize.parse('6,534', 'EUR')).to eq Money.new(653, 'EUR')
|
206
|
+
expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(653, 'EUR')
|
207
|
+
|
208
|
+
Monetize.enforce_currency_delimiters = true
|
209
|
+
expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(6_534_00, 'EUR')
|
210
|
+
Monetize.enforce_currency_delimiters = false
|
181
211
|
end
|
182
212
|
|
183
|
-
context
|
213
|
+
context 'custom currencies with 4 decimal places' do
|
184
214
|
before :each do
|
185
|
-
Money::Currency.register(JSON.parse(bar, :
|
186
|
-
Money::Currency.register(JSON.parse(eu4, :
|
215
|
+
Money::Currency.register(JSON.parse(bar, symbolize_names: true))
|
216
|
+
Money::Currency.register(JSON.parse(eu4, symbolize_names: true))
|
187
217
|
end
|
188
218
|
|
189
219
|
after :each do
|
190
|
-
Money::Currency.unregister(JSON.parse(bar, :
|
191
|
-
Money::Currency.unregister(JSON.parse(eu4, :
|
220
|
+
Money::Currency.unregister(JSON.parse(bar, symbolize_names: true))
|
221
|
+
Money::Currency.unregister(JSON.parse(eu4, symbolize_names: true))
|
192
222
|
end
|
193
223
|
|
194
224
|
# String#to_money(Currency) is equivalent to Monetize.parse(String, Currency)
|
195
|
-
it
|
196
|
-
expect(
|
197
|
-
expect(
|
225
|
+
it 'parses strings respecting subunit to unit, decimal and thousands separator' do
|
226
|
+
expect('$0.4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
|
227
|
+
expect('€0,4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
|
198
228
|
|
199
|
-
expect(
|
200
|
-
expect(
|
229
|
+
expect('$0.04'.to_money('BAR')).to eq Money.new(400, 'BAR')
|
230
|
+
expect('€0,04'.to_money('EU4')).to eq Money.new(400, 'EU4')
|
201
231
|
|
202
|
-
expect(
|
203
|
-
expect(
|
232
|
+
expect('$0.004'.to_money('BAR')).to eq Money.new(40, 'BAR')
|
233
|
+
expect('€0,004'.to_money('EU4')).to eq Money.new(40, 'EU4')
|
204
234
|
|
205
|
-
expect(
|
206
|
-
expect(
|
235
|
+
expect('$0.0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
|
236
|
+
expect('€0,0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
|
207
237
|
|
208
|
-
expect(
|
209
|
-
expect(
|
238
|
+
expect('$0.0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
|
239
|
+
expect('€0,0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
|
210
240
|
|
211
|
-
expect(
|
212
|
-
expect(
|
241
|
+
expect('$0.0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
|
242
|
+
expect('€0,0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
|
213
243
|
|
214
|
-
expect(
|
215
|
-
expect(
|
244
|
+
expect('$0.5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
|
245
|
+
expect('€0,5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
|
216
246
|
|
217
|
-
|
218
|
-
|
247
|
+
# Following currencies consider 4 decimal places
|
248
|
+
# rubocop:disable Style/NumericLiterals
|
249
|
+
expect('$6.5324'.to_money('BAR')).to eq Money.new(6_5324, 'BAR')
|
250
|
+
expect('€6,5324'.to_money('EU4')).to eq Money.new(6_5324, 'EU4')
|
219
251
|
|
220
|
-
expect(
|
221
|
-
expect(
|
252
|
+
expect('$86.5324'.to_money('BAR')).to eq Money.new(86_5324, 'BAR')
|
253
|
+
expect('€86,5324'.to_money('EU4')).to eq Money.new(86_5324, 'EU4')
|
222
254
|
|
223
|
-
expect(
|
224
|
-
expect(
|
255
|
+
expect('$186.5324'.to_money('BAR')).to eq Money.new(186_5324, 'BAR')
|
256
|
+
expect('€186,5324'.to_money('EU4')).to eq Money.new(186_5324, 'EU4')
|
225
257
|
|
226
|
-
expect(
|
227
|
-
expect(
|
258
|
+
expect('$3,331.0034'.to_money('BAR')).to eq Money.new(3_331_0034, 'BAR')
|
259
|
+
expect('€3.331,0034'.to_money('EU4')).to eq Money.new(3_331_0034, 'EU4')
|
228
260
|
|
229
|
-
expect(
|
230
|
-
expect(
|
261
|
+
expect('$8,883,331.0034'.to_money('BAR')).to eq Money.new(8_883_331_0034, 'BAR')
|
262
|
+
expect('€8.883.331,0034'.to_money('EU4')).to eq Money.new(8_883_331_0034, 'EU4')
|
263
|
+
# rubocop:enable Style/NumericLiterals
|
231
264
|
end
|
232
265
|
end
|
233
266
|
end
|
234
267
|
|
235
|
-
describe
|
236
|
-
it
|
237
|
-
expect(Monetize.parse_collection(
|
268
|
+
describe '.parse_collection' do
|
269
|
+
it 'parses into a Money::Collection' do
|
270
|
+
expect(Monetize.parse_collection('$7')).to be_a Monetize::Collection
|
238
271
|
end
|
239
272
|
|
240
|
-
it
|
241
|
-
collection = Monetize.parse_collection(
|
242
|
-
expect(collection.first).to eq Monetize.parse(
|
243
|
-
expect(collection.last).to eq Monetize.parse(
|
273
|
+
it 'parses comma separated values' do
|
274
|
+
collection = Monetize.parse_collection('$5, $7')
|
275
|
+
expect(collection.first).to eq Monetize.parse('$5')
|
276
|
+
expect(collection.last).to eq Monetize.parse('$7')
|
244
277
|
end
|
245
278
|
|
246
|
-
it
|
247
|
-
collection = Monetize.parse_collection(
|
248
|
-
expect(collection.first).to eq Monetize.parse(
|
249
|
-
expect(collection.last).to eq Monetize.parse(
|
279
|
+
it 'parses slash separated values' do
|
280
|
+
collection = Monetize.parse_collection('£4.50/€6')
|
281
|
+
expect(collection.first).to eq Monetize.parse('£4.50')
|
282
|
+
expect(collection.last).to eq Monetize.parse('€6')
|
250
283
|
end
|
251
284
|
|
252
|
-
it
|
253
|
-
collection = Monetize.parse_collection(
|
254
|
-
expect(collection.first).to eq Monetize.parse(
|
255
|
-
expect(collection.last).to eq Monetize.parse(
|
285
|
+
it 'parses hyphens as ranges' do
|
286
|
+
collection = Monetize.parse_collection('$4 - $10')
|
287
|
+
expect(collection.first).to eq Monetize.parse('$4')
|
288
|
+
expect(collection.last).to eq Monetize.parse('$10')
|
256
289
|
end
|
257
290
|
end
|
258
291
|
|
259
|
-
describe
|
260
|
-
it
|
261
|
-
expect(Monetize.from_string(
|
262
|
-
expect(Monetize.from_string(
|
263
|
-
expect(Monetize.from_string(
|
292
|
+
describe '.from_string' do
|
293
|
+
it 'converts given amount to cents' do
|
294
|
+
expect(Monetize.from_string('1')).to eq Money.new(1_00)
|
295
|
+
expect(Monetize.from_string('1')).to eq Money.new(1_00, 'USD')
|
296
|
+
expect(Monetize.from_string('1', 'EUR')).to eq Money.new(1_00, 'EUR')
|
264
297
|
end
|
265
298
|
|
266
|
-
it
|
267
|
-
expect(Monetize.from_string(
|
268
|
-
expect(Monetize.from_string(
|
269
|
-
expect(Monetize.from_string(
|
299
|
+
it 'respects :subunit_to_unit currency property' do
|
300
|
+
expect(Monetize.from_string('1', 'USD')).to eq Money.new(1_00, 'USD')
|
301
|
+
expect(Monetize.from_string('1', 'TND')).to eq Money.new(1_000, 'TND')
|
302
|
+
expect(Monetize.from_string('1', 'JPY')).to eq Money.new(1, 'JPY')
|
270
303
|
end
|
271
304
|
|
272
|
-
it
|
273
|
-
m = Monetize.from_string(
|
305
|
+
it 'accepts a currency options' do
|
306
|
+
m = Monetize.from_string('1')
|
274
307
|
expect(m.currency).to eq Money.default_currency
|
275
308
|
|
276
|
-
m = Monetize.from_string(
|
277
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
309
|
+
m = Monetize.from_string('1', Money::Currency.wrap('EUR'))
|
310
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
278
311
|
|
279
|
-
m = Monetize.from_string(
|
280
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
312
|
+
m = Monetize.from_string('1', 'EUR')
|
313
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
281
314
|
end
|
282
315
|
end
|
283
316
|
|
284
|
-
describe
|
285
|
-
it
|
317
|
+
describe '.from_fixnum' do
|
318
|
+
it 'converts given amount to cents' do
|
286
319
|
expect(Monetize.from_fixnum(1)).to eq Money.new(1_00)
|
287
|
-
expect(Monetize.from_fixnum(1)).to eq Money.new(1_00,
|
288
|
-
expect(Monetize.from_fixnum(1,
|
320
|
+
expect(Monetize.from_fixnum(1)).to eq Money.new(1_00, 'USD')
|
321
|
+
expect(Monetize.from_fixnum(1, 'EUR')).to eq Money.new(1_00, 'EUR')
|
289
322
|
end
|
290
323
|
|
291
|
-
it
|
292
|
-
expect(Monetize.from_fixnum(1,
|
293
|
-
expect(Monetize.from_fixnum(1,
|
294
|
-
expect(Monetize.from_fixnum(1,
|
324
|
+
it 'should respect :subunit_to_unit currency property' do
|
325
|
+
expect(Monetize.from_fixnum(1, 'USD')).to eq Money.new(1_00, 'USD')
|
326
|
+
expect(Monetize.from_fixnum(1, 'TND')).to eq Money.new(1_000, 'TND')
|
327
|
+
expect(Monetize.from_fixnum(1, 'JPY')).to eq Money.new(1, 'JPY')
|
295
328
|
end
|
296
329
|
|
297
|
-
it
|
330
|
+
it 'accepts a currency options' do
|
298
331
|
m = Monetize.from_fixnum(1)
|
299
332
|
expect(m.currency).to eq Money.default_currency
|
300
333
|
|
301
|
-
m = Monetize.from_fixnum(1, Money::Currency.wrap(
|
302
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
334
|
+
m = Monetize.from_fixnum(1, Money::Currency.wrap('EUR'))
|
335
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
303
336
|
|
304
|
-
m = Monetize.from_fixnum(1,
|
305
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
337
|
+
m = Monetize.from_fixnum(1, 'EUR')
|
338
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
306
339
|
end
|
307
340
|
end
|
308
341
|
|
309
|
-
describe
|
310
|
-
it
|
342
|
+
describe '.from_float' do
|
343
|
+
it 'converts given amount to cents' do
|
311
344
|
expect(Monetize.from_float(1.2)).to eq Money.new(1_20)
|
312
|
-
expect(Monetize.from_float(1.2)).to eq Money.new(1_20,
|
313
|
-
expect(Monetize.from_float(1.2,
|
345
|
+
expect(Monetize.from_float(1.2)).to eq Money.new(1_20, 'USD')
|
346
|
+
expect(Monetize.from_float(1.2, 'EUR')).to eq Money.new(1_20, 'EUR')
|
314
347
|
end
|
315
348
|
|
316
|
-
it
|
317
|
-
expect(Monetize.from_float(1.2,
|
318
|
-
expect(Monetize.from_float(1.2,
|
319
|
-
expect(Monetize.from_float(1.2,
|
349
|
+
it 'respects :subunit_to_unit currency property' do
|
350
|
+
expect(Monetize.from_float(1.2, 'USD')).to eq Money.new(1_20, 'USD')
|
351
|
+
expect(Monetize.from_float(1.2, 'TND')).to eq Money.new(1_200, 'TND')
|
352
|
+
expect(Monetize.from_float(1.2, 'JPY')).to eq Money.new(1, 'JPY')
|
320
353
|
end
|
321
354
|
|
322
|
-
it
|
355
|
+
it 'accepts a currency options' do
|
323
356
|
m = Monetize.from_float(1.2)
|
324
357
|
expect(m.currency).to eq Money.default_currency
|
325
358
|
|
326
|
-
m = Monetize.from_float(1.2, Money::Currency.wrap(
|
327
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
359
|
+
m = Monetize.from_float(1.2, Money::Currency.wrap('EUR'))
|
360
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
328
361
|
|
329
|
-
m = Monetize.from_float(1.2,
|
330
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
362
|
+
m = Monetize.from_float(1.2, 'EUR')
|
363
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
331
364
|
end
|
332
365
|
end
|
333
366
|
|
334
|
-
describe
|
335
|
-
it
|
336
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
337
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
338
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
367
|
+
describe '.from_bigdecimal' do
|
368
|
+
it 'converts given amount to cents' do
|
369
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'))).to eq Money.new(1_00)
|
370
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'))).to eq Money.new(1_00, 'USD')
|
371
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'), 'EUR')).to eq Money.new(1_00, 'EUR')
|
339
372
|
end
|
340
373
|
|
341
|
-
it
|
342
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
343
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
344
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
374
|
+
it 'respects :subunit_to_unit currency property' do
|
375
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'), 'USD')).to eq Money.new(1_00, 'USD')
|
376
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'), 'TND')).to eq Money.new(1_000, 'TND')
|
377
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'), 'JPY')).to eq Money.new(1, 'JPY')
|
345
378
|
end
|
346
379
|
|
347
|
-
it
|
348
|
-
m = Monetize.from_bigdecimal(BigDecimal.new(
|
380
|
+
it 'accepts a currency options' do
|
381
|
+
m = Monetize.from_bigdecimal(BigDecimal.new('1'))
|
349
382
|
expect(m.currency).to eq Money.default_currency
|
350
383
|
|
351
|
-
m = Monetize.from_bigdecimal(BigDecimal.new(
|
352
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
384
|
+
m = Monetize.from_bigdecimal(BigDecimal.new('1'), Money::Currency.wrap('EUR'))
|
385
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
353
386
|
|
354
|
-
m = Monetize.from_bigdecimal(BigDecimal.new(
|
355
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
387
|
+
m = Monetize.from_bigdecimal(BigDecimal.new('1'), 'EUR')
|
388
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
356
389
|
end
|
357
390
|
|
358
|
-
context
|
391
|
+
context 'infinite_precision = true' do
|
359
392
|
before do
|
360
393
|
Money.infinite_precision = true
|
361
394
|
end
|
@@ -364,66 +397,74 @@ describe Monetize do
|
|
364
397
|
Money.infinite_precision = false
|
365
398
|
end
|
366
399
|
|
367
|
-
it
|
368
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
369
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
370
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
371
|
-
expect(Monetize.from_bigdecimal(BigDecimal.new(
|
400
|
+
it 'keeps precision' do
|
401
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1'))).to eq Money.new(100)
|
402
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1.23456'))).to eq Money.new(123.456)
|
403
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('-1.23456'))).to eq Money.new(-123.456)
|
404
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1.23456'))).to eq Money.new(123.456, 'USD')
|
405
|
+
expect(Monetize.from_bigdecimal(BigDecimal.new('1.23456'), 'EUR')).to eq Money.new(123.456, 'EUR')
|
406
|
+
|
407
|
+
expect('1'.to_money).to eq Money.new(100)
|
408
|
+
expect('1.23456'.to_money).to eq Money.new(123.456)
|
409
|
+
expect('-1.23456'.to_money).to eq Money.new(-123.456)
|
410
|
+
expect('$1.23456'.to_money).to eq Money.new(123.456, 'USD')
|
411
|
+
expect('1.23456 EUR'.to_money).to eq Money.new(123.456, 'EUR')
|
372
412
|
end
|
373
413
|
end
|
374
414
|
end
|
375
415
|
|
376
|
-
describe
|
377
|
-
it
|
416
|
+
describe '.from_numeric' do
|
417
|
+
it 'converts given amount to cents' do
|
378
418
|
expect(Monetize.from_numeric(1)).to eq Money.new(1_00)
|
379
419
|
expect(Monetize.from_numeric(1.0)).to eq Money.new(1_00)
|
380
|
-
expect(Monetize.from_numeric(BigDecimal.new(
|
420
|
+
expect(Monetize.from_numeric(BigDecimal.new('1'))).to eq Money.new(1_00)
|
381
421
|
end
|
382
422
|
|
383
|
-
it
|
384
|
-
expect { Monetize.from_numeric(
|
423
|
+
it 'raises ArgumentError with unsupported argument' do
|
424
|
+
expect { Monetize.from_numeric('100') }.to raise_error(ArgumentError)
|
385
425
|
end
|
386
426
|
|
387
|
-
it
|
388
|
-
expect(Monetize).to receive(:from_fixnum).with(1,
|
389
|
-
expect(Monetize.from_numeric(1,
|
390
|
-
expect(Monetize).to receive(:from_bigdecimal).with(BigDecimal.new(
|
391
|
-
|
427
|
+
it 'optimizes workload' do
|
428
|
+
expect(Monetize).to receive(:from_fixnum).with(1, 'USD').and_return(Money.new(1_00, 'USD'))
|
429
|
+
expect(Monetize.from_numeric(1, 'USD')).to eq Money.new(1_00, 'USD')
|
430
|
+
expect(Monetize).to receive(:from_bigdecimal).with(BigDecimal.new('1.0'), 'USD').
|
431
|
+
and_return(Money.new(1_00, 'USD'))
|
432
|
+
expect(Monetize.from_numeric(1.0, 'USD')).to eq Money.new(1_00, 'USD')
|
392
433
|
end
|
393
434
|
|
394
|
-
it
|
395
|
-
expect(Monetize.from_numeric(1,
|
396
|
-
expect(Monetize.from_numeric(1,
|
397
|
-
expect(Monetize.from_numeric(1,
|
435
|
+
it 'respects :subunit_to_unit currency property' do
|
436
|
+
expect(Monetize.from_numeric(1, 'USD')).to eq Money.new(1_00, 'USD')
|
437
|
+
expect(Monetize.from_numeric(1, 'TND')).to eq Money.new(1_000, 'TND')
|
438
|
+
expect(Monetize.from_numeric(1, 'JPY')).to eq Money.new(1, 'JPY')
|
398
439
|
end
|
399
440
|
|
400
|
-
it
|
441
|
+
it 'accepts a bank option' do
|
401
442
|
expect(Monetize.from_numeric(1)).to eq Money.new(1_00)
|
402
|
-
expect(Monetize.from_numeric(1)).to eq Money.new(1_00,
|
403
|
-
expect(Monetize.from_numeric(1,
|
443
|
+
expect(Monetize.from_numeric(1)).to eq Money.new(1_00, 'USD')
|
444
|
+
expect(Monetize.from_numeric(1, 'EUR')).to eq Money.new(1_00, 'EUR')
|
404
445
|
end
|
405
446
|
|
406
|
-
it
|
447
|
+
it 'accepts a currency options' do
|
407
448
|
m = Monetize.from_numeric(1)
|
408
449
|
expect(m.currency).to eq Money.default_currency
|
409
450
|
|
410
|
-
m = Monetize.from_numeric(1, Money::Currency.wrap(
|
411
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
451
|
+
m = Monetize.from_numeric(1, Money::Currency.wrap('EUR'))
|
452
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
412
453
|
|
413
|
-
m = Monetize.from_numeric(1,
|
414
|
-
expect(m.currency).to eq Money::Currency.wrap(
|
454
|
+
m = Monetize.from_numeric(1, 'EUR')
|
455
|
+
expect(m.currency).to eq Money::Currency.wrap('EUR')
|
415
456
|
end
|
416
457
|
end
|
417
458
|
|
418
|
-
describe
|
459
|
+
describe '.extract_cents' do
|
419
460
|
it "correctly treats pipe marks '|' in input (regression test)" do
|
420
461
|
expect(Monetize.extract_cents('100|0')).to eq Monetize.extract_cents('100!0')
|
421
462
|
end
|
422
463
|
end
|
423
464
|
|
424
|
-
context
|
425
|
-
it
|
426
|
-
expect(4.635.to_money).to eq
|
465
|
+
context 'given the same inputs to .parse and .from_*' do
|
466
|
+
it 'gives the same results' do
|
467
|
+
expect(4.635.to_money).to eq '4.635'.to_money
|
427
468
|
end
|
428
469
|
end
|
429
470
|
end
|