money 6.13.0 → 6.13.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +49 -2
- data/README.md +93 -3
- data/config/currency_backwards_compatible.json +31 -0
- data/config/currency_iso.json +18 -18
- data/lib/money/bank/variable_exchange.rb +14 -6
- data/lib/money/currency.rb +4 -4
- data/lib/money/currency/loader.rb +15 -13
- data/lib/money/locale_backend/currency.rb +11 -0
- data/lib/money/locale_backend/i18n.rb +2 -1
- data/lib/money/locale_backend/legacy.rb +2 -2
- data/lib/money/money.rb +100 -52
- data/lib/money/money/allocation.rb +8 -5
- data/lib/money/money/arithmetic.rb +20 -10
- data/lib/money/money/formatter.rb +3 -0
- data/lib/money/money/formatting_rules.rb +15 -5
- data/lib/money/money/locale_backend.rb +3 -1
- data/lib/money/rates_store/memory.rb +24 -23
- data/lib/money/version.rb +1 -1
- data/money.gemspec +4 -4
- metadata +11 -51
- data/.coveralls.yml +0 -1
- data/.gitignore +0 -23
- data/.rspec +0 -2
- data/.travis.yml +0 -32
- data/AUTHORS +0 -131
- data/CONTRIBUTING.md +0 -17
- data/Gemfile +0 -16
- data/Rakefile +0 -17
- data/spec/bank/base_spec.rb +0 -79
- data/spec/bank/single_currency_spec.rb +0 -13
- data/spec/bank/variable_exchange_spec.rb +0 -265
- data/spec/currency/heuristics_spec.rb +0 -11
- data/spec/currency/loader_spec.rb +0 -19
- data/spec/currency_spec.rb +0 -400
- data/spec/locale_backend/i18n_spec.rb +0 -62
- data/spec/locale_backend/legacy_spec.rb +0 -74
- data/spec/money/allocation_spec.rb +0 -130
- data/spec/money/arithmetic_spec.rb +0 -756
- data/spec/money/constructors_spec.rb +0 -91
- data/spec/money/formatting_spec.rb +0 -855
- data/spec/money/locale_backend_spec.rb +0 -14
- data/spec/money_spec.rb +0 -880
- data/spec/rates_store/memory_spec.rb +0 -80
- data/spec/spec_helper.rb +0 -30
- data/spec/support/shared_examples/money_examples.rb +0 -14
@@ -1,91 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Money::Constructors do
|
4
|
-
|
5
|
-
describe "::empty" do
|
6
|
-
it "creates a new Money object of 0 cents" do
|
7
|
-
expect(Money.empty).to eq Money.new(0)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "instantiates a subclass when inheritance is used" do
|
11
|
-
special_money_class = Class.new(Money)
|
12
|
-
expect(special_money_class.empty).to be_a special_money_class
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
describe "::zero" do
|
18
|
-
subject { Money.zero }
|
19
|
-
it { is_expected.to eq Money.empty }
|
20
|
-
|
21
|
-
it "instantiates a subclass when inheritance is used" do
|
22
|
-
special_money_class = Class.new(Money)
|
23
|
-
expect(special_money_class.zero).to be_a special_money_class
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
describe "::ca_dollar" do
|
29
|
-
it "creates a new Money object of the given value in CAD" do
|
30
|
-
expect(Money.ca_dollar(50)).to eq Money.new(50, "CAD")
|
31
|
-
end
|
32
|
-
|
33
|
-
it "is aliased to ::cad" do
|
34
|
-
expect(Money.cad(50)).to eq Money.ca_dollar(50)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "instantiates a subclass when inheritance is used" do
|
38
|
-
special_money_class = Class.new(Money)
|
39
|
-
expect(special_money_class.ca_dollar(0)).to be_a special_money_class
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
describe "::us_dollar" do
|
45
|
-
it "creates a new Money object of the given value in USD" do
|
46
|
-
expect(Money.us_dollar(50)).to eq Money.new(50, "USD")
|
47
|
-
end
|
48
|
-
|
49
|
-
it "is aliased to ::usd" do
|
50
|
-
expect(Money.usd(50)).to eq Money.us_dollar(50)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "instantiates a subclass when inheritance is used" do
|
54
|
-
special_money_class = Class.new(Money)
|
55
|
-
expect(special_money_class.us_dollar(0)).to be_a special_money_class
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
|
-
describe "::euro" do
|
61
|
-
it "creates a new Money object of the given value in EUR" do
|
62
|
-
expect(Money.euro(50)).to eq Money.new(50, "EUR")
|
63
|
-
end
|
64
|
-
|
65
|
-
it "is aliased to ::eur" do
|
66
|
-
expect(Money.eur(50)).to eq Money.euro(50)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "instantiates a subclass when inheritance is used" do
|
70
|
-
special_money_class = Class.new(Money)
|
71
|
-
expect(special_money_class.euro(0)).to be_a special_money_class
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
|
76
|
-
describe "::pound_sterling" do
|
77
|
-
it "creates a new Money object of the given value in GBP" do
|
78
|
-
expect(Money.pound_sterling(50)).to eq Money.new(50, "GBP")
|
79
|
-
end
|
80
|
-
|
81
|
-
it "is aliased to ::gbp" do
|
82
|
-
expect(Money.gbp(50)).to eq Money.pound_sterling(50)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "instantiates a subclass when inheritance is used" do
|
86
|
-
special_money_class = Class.new(Money)
|
87
|
-
expect(special_money_class.pound_sterling(0)).to be_a special_money_class
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
@@ -1,855 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Money, "formatting" do
|
4
|
-
|
5
|
-
BAR = '{ "priority": 1, "iso_code": "BAR", "iso_numeric": "840", "name": "Dollar with 4 decimal places", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "smallest_denomination": 1 }'
|
6
|
-
INDIAN_BAR = '{ "priority": 1, "iso_code": "INDIAN_BAR", "iso_numeric": "840", "name": "Dollar with 4 decimal places", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "south_asian_number_formatting": true, "smallest_denomination": 1}'
|
7
|
-
EU4 = '{ "priority": 1, "iso_code": "EU4", "iso_numeric": "841", "name": "Euro with 4 decimal places", "symbol": "€", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "€", "decimal_mark": ",", "thousands_separator": ".", "smallest_denomination": 1 }'
|
8
|
-
|
9
|
-
context "without i18n" do
|
10
|
-
subject(:money) { Money.empty("USD") }
|
11
|
-
|
12
|
-
it "should use ',' as the thousands separator" do
|
13
|
-
expect(money.thousands_separator).to eq ','
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should use '.' as the decimal mark" do
|
17
|
-
expect(money.decimal_mark).to eq '.'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'without locale_backend' do
|
22
|
-
before { Money.locale_backend = nil }
|
23
|
-
after { Money.locale_backend = :legacy }
|
24
|
-
|
25
|
-
subject(:money) { Money.new(1099_99, 'USD') }
|
26
|
-
|
27
|
-
it 'falls back to using defaults' do
|
28
|
-
expect(money.thousands_separator).to eq('')
|
29
|
-
expect(money.decimal_mark).to eq('.')
|
30
|
-
expect(money.format).to eq('$1099.99')
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "with i18n but use_i18n = false" do
|
35
|
-
before :each do
|
36
|
-
reset_i18n
|
37
|
-
I18n.locale = :de
|
38
|
-
I18n.backend.store_translations(
|
39
|
-
:de,
|
40
|
-
number: { currency: { format: { delimiter: ".", separator: "," } } }
|
41
|
-
)
|
42
|
-
Money.use_i18n = false
|
43
|
-
end
|
44
|
-
|
45
|
-
after :each do
|
46
|
-
reset_i18n
|
47
|
-
I18n.locale = :en
|
48
|
-
Money.use_i18n = true
|
49
|
-
end
|
50
|
-
|
51
|
-
subject(:money) { Money.empty("USD") }
|
52
|
-
|
53
|
-
it "should use ',' as the thousands separator" do
|
54
|
-
expect(money.thousands_separator).to eq ','
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should use '.' as the decimal mark" do
|
58
|
-
expect(money.decimal_mark).to eq '.'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "with i18n" do
|
63
|
-
after :each do
|
64
|
-
reset_i18n
|
65
|
-
I18n.locale = :en
|
66
|
-
end
|
67
|
-
|
68
|
-
context "with number.format.*" do
|
69
|
-
before :each do
|
70
|
-
reset_i18n
|
71
|
-
I18n.locale = :de
|
72
|
-
I18n.backend.store_translations(
|
73
|
-
:de,
|
74
|
-
number: { format: { delimiter: ".", separator: "," } }
|
75
|
-
)
|
76
|
-
end
|
77
|
-
|
78
|
-
subject(:money) { Money.empty("USD") }
|
79
|
-
|
80
|
-
it "should use '.' as the thousands separator" do
|
81
|
-
expect(money.thousands_separator).to eq '.'
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should use ',' as the decimal mark" do
|
85
|
-
expect(money.decimal_mark).to eq ','
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context "with number.currency.format.*" do
|
90
|
-
before :each do
|
91
|
-
reset_i18n
|
92
|
-
I18n.locale = :de
|
93
|
-
I18n.backend.store_translations(
|
94
|
-
:de,
|
95
|
-
number: { currency: { format: { delimiter: ".", separator: "," } } }
|
96
|
-
)
|
97
|
-
end
|
98
|
-
|
99
|
-
subject(:money) { Money.empty("USD") }
|
100
|
-
|
101
|
-
it "should use '.' as the thousands separator" do
|
102
|
-
expect(money.thousands_separator).to eq '.'
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should use ',' as the decimal mark" do
|
106
|
-
expect(money.decimal_mark).to eq ','
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
context "with number.currency.symbol.*" do
|
111
|
-
before :each do
|
112
|
-
reset_i18n
|
113
|
-
I18n.locale = :de
|
114
|
-
I18n.backend.store_translations(
|
115
|
-
:de,
|
116
|
-
number: { currency: { symbol: { CAD: "CAD$" } } }
|
117
|
-
)
|
118
|
-
end
|
119
|
-
|
120
|
-
subject(:money) { Money.empty("CAD") }
|
121
|
-
|
122
|
-
it "should use 'CAD$' as the currency symbol" do
|
123
|
-
expect(money.format(translate: true)).to eq("CAD$0.00")
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context "with overridden i18n settings" do
|
128
|
-
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies that have no subunit" do
|
129
|
-
expect(Money.new(300_000, 'ISK').format(thousands_separator: ".", decimal_mark: ',')).to eq "kr300.000"
|
130
|
-
end
|
131
|
-
|
132
|
-
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies with subunits that drop_trailing_zeros" do
|
133
|
-
expect(Money.new(300_000, 'USD').format(thousands_separator: ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000"
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
describe "#format" do
|
139
|
-
context "Locale :ja" do
|
140
|
-
before { @_locale = I18n.locale; I18n.locale = :ja }
|
141
|
-
|
142
|
-
it "formats Japanese currency in Japanese properly" do
|
143
|
-
money = Money.new(1000, "JPY")
|
144
|
-
expect(money.format).to eq "1,000円"
|
145
|
-
expect(money.format(symbol: false)).to eq "1,000"
|
146
|
-
end
|
147
|
-
|
148
|
-
after { I18n.locale = @_locale }
|
149
|
-
end
|
150
|
-
|
151
|
-
it "returns the monetary value as a string" do
|
152
|
-
expect(Money.ca_dollar(100).format).to eq "$1.00"
|
153
|
-
expect(Money.new(40008).format).to eq "$400.08"
|
154
|
-
end
|
155
|
-
|
156
|
-
it "respects :subunit_to_unit currency property" do
|
157
|
-
expect(Money.new(10_00, "BHD").format).to eq "ب.د1.000"
|
158
|
-
end
|
159
|
-
|
160
|
-
context "when :subunit_to_unit is 1" do
|
161
|
-
it "does not display a decimal part" do
|
162
|
-
expect(Money.new(10_00, "VUV").format).to eq "Vt1,000"
|
163
|
-
end
|
164
|
-
|
165
|
-
it "does not displays a decimal part when infinite_precision is false" do
|
166
|
-
expect(Money.new(10_00.1, "VUV").format).to eq "Vt1,000"
|
167
|
-
end
|
168
|
-
|
169
|
-
it "displays a decimal part when infinite_precision is true", :infinite_precision do
|
170
|
-
expect(Money.new(10_00.1, "VUV").format).to eq "Vt1,000.1"
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
it "respects the thousands_separator and decimal_mark defaults" do
|
175
|
-
one_thousand = Proc.new do |currency|
|
176
|
-
Money.new(1000_00, currency).format
|
177
|
-
end
|
178
|
-
|
179
|
-
# Pounds
|
180
|
-
expect(one_thousand["GBP"]).to eq "£1,000.00"
|
181
|
-
|
182
|
-
# Dollars
|
183
|
-
expect(one_thousand["USD"]).to eq "$1,000.00"
|
184
|
-
expect(one_thousand["CAD"]).to eq "$1,000.00"
|
185
|
-
expect(one_thousand["AUD"]).to eq "$1,000.00"
|
186
|
-
expect(one_thousand["NZD"]).to eq "$1,000.00"
|
187
|
-
expect(one_thousand["ZWD"]).to eq "$1,000.00"
|
188
|
-
|
189
|
-
# Yen
|
190
|
-
expect(one_thousand["JPY"]).to eq "¥100,000"
|
191
|
-
expect(one_thousand["CNY"]).to eq "¥1,000.00"
|
192
|
-
|
193
|
-
# Euro
|
194
|
-
expect(one_thousand["EUR"]).to eq "€1.000,00"
|
195
|
-
|
196
|
-
# Rupees
|
197
|
-
expect(one_thousand["INR"]).to eq "₹1,000.00"
|
198
|
-
expect(one_thousand["NPR"]).to eq "₨1,000.00"
|
199
|
-
expect(one_thousand["SCR"]).to eq "1,000.00 ₨"
|
200
|
-
expect(one_thousand["LKR"]).to eq "1,000.00 ₨"
|
201
|
-
|
202
|
-
# Brazilian Real
|
203
|
-
expect(one_thousand["BRL"]).to eq "R$1.000,00"
|
204
|
-
|
205
|
-
# Other
|
206
|
-
expect(one_thousand["SEK"]).to eq "1 000,00 kr"
|
207
|
-
expect(one_thousand["GHC"]).to eq "₵1,000.00"
|
208
|
-
end
|
209
|
-
|
210
|
-
it "inserts commas into the result if the amount is sufficiently large" do
|
211
|
-
expect(Money.us_dollar(1_000_000_000_12).format).to eq "$1,000,000,000.12"
|
212
|
-
expect(Money.us_dollar(1_000_000_000_12).format(no_cents: true)).to eq "$1,000,000,000"
|
213
|
-
end
|
214
|
-
|
215
|
-
it "inserts thousands separator into the result if the amount is sufficiently large and the currency symbol is at the end" do
|
216
|
-
expect(Money.euro(1_234_567_12).format).to eq "€1.234.567,12"
|
217
|
-
expect(Money.euro(1_234_567_12).format(no_cents: true)).to eq "€1.234.567"
|
218
|
-
end
|
219
|
-
|
220
|
-
context 'when default_formatting_rules defines (display_free: true)' do
|
221
|
-
before do
|
222
|
-
Money.default_formatting_rules = { display_free: "you won't pay a thing" }
|
223
|
-
end
|
224
|
-
|
225
|
-
after do
|
226
|
-
Money.default_formatting_rules = nil
|
227
|
-
end
|
228
|
-
|
229
|
-
context 'with no rule provided' do
|
230
|
-
it 'acknowledges default rule' do
|
231
|
-
expect(Money.new(0, 'USD').format).to eq "you won't pay a thing"
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
context 'with rule (display_free: false) provided' do
|
236
|
-
it 'acknowledges provided rule' do
|
237
|
-
expect(Money.new(0, 'USD').format(display_free: false)).to eq '$0.00'
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
context 'when default_formatting_rules is not defined' do
|
243
|
-
before do
|
244
|
-
Money.default_formatting_rules = nil
|
245
|
-
end
|
246
|
-
|
247
|
-
context 'acknowledges provided rule' do
|
248
|
-
it 'acknowledges provided rule' do
|
249
|
-
expect(Money.new(100, 'USD').format(with_currency: true)).to eq '$1.00 USD'
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
describe ":with_currency option" do
|
255
|
-
specify "(:with_currency option => true) works as documented" do
|
256
|
-
expect(Money.ca_dollar(100).format(with_currency: true)).to eq "$1.00 CAD"
|
257
|
-
expect(Money.us_dollar(85).format(with_currency: true)).to eq "$0.85 USD"
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
describe ":no_cents option" do
|
262
|
-
specify "(:with_currency option => true) works as documented" do
|
263
|
-
expect(Money.ca_dollar(100).format(no_cents: true)).to eq "$1"
|
264
|
-
expect(Money.ca_dollar(599).format(no_cents: true)).to eq "$5"
|
265
|
-
expect(Money.ca_dollar(570).format(no_cents: true, with_currency: true)).to eq "$5 CAD"
|
266
|
-
expect(Money.ca_dollar(39000).format(no_cents: true)).to eq "$390"
|
267
|
-
end
|
268
|
-
|
269
|
-
it "respects :subunit_to_unit currency property" do
|
270
|
-
expect(Money.new(10_00, "BHD").format(no_cents: true)).to eq "ب.د1"
|
271
|
-
end
|
272
|
-
|
273
|
-
it "inserts thousand separators if symbol contains decimal mark and no_cents is true" do
|
274
|
-
expect(Money.new(100000000, "AMD").format(no_cents: true)).to eq "1,000,000 դր."
|
275
|
-
expect(Money.new(100000000, "USD").format(no_cents: true)).to eq "$1,000,000"
|
276
|
-
expect(Money.new(100000000, "RUB").format(no_cents: true)).to eq "1.000.000 ₽"
|
277
|
-
end
|
278
|
-
|
279
|
-
it "doesn't incorrectly format HTML" do
|
280
|
-
money = ::Money.new(1999, "RUB")
|
281
|
-
output = money.format(html: true, no_cents: true)
|
282
|
-
expect(output).to eq "19 ₽"
|
283
|
-
end
|
284
|
-
|
285
|
-
it "doesn't incorrectly format HTML (html_wrap)" do
|
286
|
-
money = ::Money.new(1999, "RUB")
|
287
|
-
output = money.format(html_wrap: true, no_cents: true)
|
288
|
-
expect(output).to eq "<span class=\"money-whole\">19</span> <span class=\"money-currency-symbol\">₽</span>"
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
describe ":no_cents_if_whole option" do
|
293
|
-
specify "(no_cents_if_whole: true) works as documented" do
|
294
|
-
expect(Money.new(10000, "VUV").format(no_cents_if_whole: true, symbol: false)).to eq "10,000"
|
295
|
-
expect(Money.new(10034, "VUV").format(no_cents_if_whole: true, symbol: false)).to eq "10,034"
|
296
|
-
expect(Money.new(10000, "MGA").format(no_cents_if_whole: true, symbol: false)).to eq "2,000"
|
297
|
-
expect(Money.new(10034, "MGA").format(no_cents_if_whole: true, symbol: false)).to eq "2,006.8"
|
298
|
-
expect(Money.new(10000, "VND").format(no_cents_if_whole: true, symbol: false)).to eq "10.000"
|
299
|
-
expect(Money.new(10034, "VND").format(no_cents_if_whole: true, symbol: false)).to eq "10.034"
|
300
|
-
expect(Money.new(10000, "USD").format(no_cents_if_whole: true, symbol: false)).to eq "100"
|
301
|
-
expect(Money.new(10034, "USD").format(no_cents_if_whole: true, symbol: false)).to eq "100.34"
|
302
|
-
expect(Money.new(10000, "IQD").format(no_cents_if_whole: true, symbol: false)).to eq "10"
|
303
|
-
expect(Money.new(10034, "IQD").format(no_cents_if_whole: true, symbol: false)).to eq "10.034"
|
304
|
-
end
|
305
|
-
|
306
|
-
specify "(no_cents_if_whole: false) works as documented" do
|
307
|
-
expect(Money.new(10000, "VUV").format(no_cents_if_whole: false, symbol: false)).to eq "10,000"
|
308
|
-
expect(Money.new(10034, "VUV").format(no_cents_if_whole: false, symbol: false)).to eq "10,034"
|
309
|
-
expect(Money.new(10000, "MGA").format(no_cents_if_whole: false, symbol: false)).to eq "2,000.0"
|
310
|
-
expect(Money.new(10034, "MGA").format(no_cents_if_whole: false, symbol: false)).to eq "2,006.8"
|
311
|
-
expect(Money.new(10000, "VND").format(no_cents_if_whole: false, symbol: false)).to eq "10.000"
|
312
|
-
expect(Money.new(10034, "VND").format(no_cents_if_whole: false, symbol: false)).to eq "10.034"
|
313
|
-
expect(Money.new(10000, "USD").format(no_cents_if_whole: false, symbol: false)).to eq "100.00"
|
314
|
-
expect(Money.new(10034, "USD").format(no_cents_if_whole: false, symbol: false)).to eq "100.34"
|
315
|
-
expect(Money.new(10000, "IQD").format(no_cents_if_whole: false, symbol: false)).to eq "10.000"
|
316
|
-
expect(Money.new(10034, "IQD").format(no_cents_if_whole: false, symbol: false)).to eq "10.034"
|
317
|
-
end
|
318
|
-
end
|
319
|
-
|
320
|
-
describe ":symbol option" do
|
321
|
-
specify "(symbol: a symbol string) uses the given value as the money symbol" do
|
322
|
-
expect(Money.new(100, "GBP").format(symbol: "£")).to eq "£1.00"
|
323
|
-
end
|
324
|
-
|
325
|
-
specify "(symbol: true) returns symbol based on the given currency code" do
|
326
|
-
one = Proc.new do |currency|
|
327
|
-
Money.new(100, currency).format(symbol: true)
|
328
|
-
end
|
329
|
-
|
330
|
-
# Pounds
|
331
|
-
expect(one["GBP"]).to eq "£1.00"
|
332
|
-
|
333
|
-
# Dollars
|
334
|
-
expect(one["USD"]).to eq "$1.00"
|
335
|
-
expect(one["CAD"]).to eq "$1.00"
|
336
|
-
expect(one["AUD"]).to eq "$1.00"
|
337
|
-
expect(one["NZD"]).to eq "$1.00"
|
338
|
-
expect(one["ZWD"]).to eq "$1.00"
|
339
|
-
|
340
|
-
# Yen
|
341
|
-
expect(one["JPY"]).to eq "¥100"
|
342
|
-
expect(one["CNY"]).to eq "¥1.00"
|
343
|
-
|
344
|
-
# Euro
|
345
|
-
expect(one["EUR"]).to eq "€1,00"
|
346
|
-
|
347
|
-
# Rupees
|
348
|
-
expect(one["INR"]).to eq "₹1.00"
|
349
|
-
expect(one["NPR"]).to eq "₨1.00"
|
350
|
-
expect(one["SCR"]).to eq "1.00 ₨"
|
351
|
-
expect(one["LKR"]).to eq "1.00 ₨"
|
352
|
-
|
353
|
-
# Brazilian Real
|
354
|
-
expect(one["BRL"]).to eq "R$1,00"
|
355
|
-
|
356
|
-
# Vietnamese Dong
|
357
|
-
expect(one["VND"]).to eq "100 ₫"
|
358
|
-
|
359
|
-
# Other
|
360
|
-
expect(one["SEK"]).to eq "1,00 kr"
|
361
|
-
expect(one["GHC"]).to eq "₵1.00"
|
362
|
-
end
|
363
|
-
|
364
|
-
specify "(symbol: true) returns $ when currency code is not recognized" do
|
365
|
-
currency = Money::Currency.new("EUR")
|
366
|
-
expect(currency).to receive(:symbol).and_return(nil)
|
367
|
-
expect(Money.new(100, currency).format(symbol: true)).to eq "¤1,00"
|
368
|
-
end
|
369
|
-
|
370
|
-
specify "(symbol: some non-Boolean value that evaluates to true) returns symbol based on the given currency code" do
|
371
|
-
expect(Money.new(100, "GBP").format(symbol: true)).to eq "£1.00"
|
372
|
-
expect(Money.new(100, "EUR").format(symbol: true)).to eq "€1,00"
|
373
|
-
expect(Money.new(100, "SEK").format(symbol: true)).to eq "1,00 kr"
|
374
|
-
end
|
375
|
-
|
376
|
-
specify "(symbol: "", nil or false) returns the amount without a symbol" do
|
377
|
-
money = Money.new(100, "GBP")
|
378
|
-
expect(money.format(symbol: "")).to eq "1.00"
|
379
|
-
expect(money.format(symbol: nil)).to eq "1.00"
|
380
|
-
expect(money.format(symbol: false)).to eq "1.00"
|
381
|
-
|
382
|
-
money = Money.new(100, "JPY")
|
383
|
-
expect(money.format(symbol: false)).to eq "100"
|
384
|
-
end
|
385
|
-
|
386
|
-
it "defaults :symbol to true" do
|
387
|
-
money = Money.new(100)
|
388
|
-
expect(money.format).to eq "$1.00"
|
389
|
-
|
390
|
-
money = Money.new(100, "GBP")
|
391
|
-
expect(money.format).to eq "£1.00"
|
392
|
-
|
393
|
-
money = Money.new(100, "EUR")
|
394
|
-
expect(money.format).to eq "€1,00"
|
395
|
-
end
|
396
|
-
|
397
|
-
specify "(symbol: false) returns a signed amount without a symbol" do
|
398
|
-
money = Money.new(-100, "EUR")
|
399
|
-
expect(money.format(symbol: false)).to eq "-1,00"
|
400
|
-
|
401
|
-
money = Money.new(100, "EUR")
|
402
|
-
expect(money.format(symbol: false,
|
403
|
-
sign_positive: true)).to eq "+1,00"
|
404
|
-
end
|
405
|
-
end
|
406
|
-
|
407
|
-
describe ":decimal_mark option" do
|
408
|
-
specify "(decimal_mark: a decimal_mark string) works as documented" do
|
409
|
-
expect(Money.us_dollar(100).format(decimal_mark: ",")).to eq "$1,00"
|
410
|
-
end
|
411
|
-
|
412
|
-
it "defaults to '.' if currency isn't recognized" do
|
413
|
-
expect(Money.new(100, "ZWD").format).to eq "$1.00"
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
describe ":separator option" do
|
418
|
-
specify "(separator: a separator string) works as documented" do
|
419
|
-
expect(Money.us_dollar(100).format(separator: ",")).to eq "$1,00"
|
420
|
-
end
|
421
|
-
end
|
422
|
-
|
423
|
-
describe ":south_asian_number_formatting delimiter" do
|
424
|
-
before(:each) do
|
425
|
-
Money::Currency.register(JSON.parse(INDIAN_BAR, symbolize_names: true))
|
426
|
-
end
|
427
|
-
|
428
|
-
after(:each) do
|
429
|
-
Money::Currency.unregister(JSON.parse(INDIAN_BAR, symbolize_names: true))
|
430
|
-
end
|
431
|
-
|
432
|
-
specify "(south_asian_number_formatting: true) works as documented" do
|
433
|
-
expect(Money.new(10000000, 'INR').format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.00"
|
434
|
-
expect(Money.new(1000000000, 'INDIAN_BAR').format(south_asian_number_formatting: true, symbol: false)).to eq "1,00,000.0000"
|
435
|
-
expect(Money.new(10000000).format(south_asian_number_formatting: true)).to eq "$1,00,000.00"
|
436
|
-
end
|
437
|
-
|
438
|
-
specify "(south_asian_number_formatting: true and no_cents_if_whole => true) works as documented" do
|
439
|
-
expect(Money.new(10000000, 'INR').format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000"
|
440
|
-
expect(Money.new(1000000000, 'INDIAN_BAR').format(south_asian_number_formatting: true, symbol: false, no_cents_if_whole: true)).to eq "1,00,000"
|
441
|
-
end
|
442
|
-
end
|
443
|
-
|
444
|
-
describe ":thousands_separator option" do
|
445
|
-
specify "(thousands_separator: a thousands_separator string) works as documented" do
|
446
|
-
expect(Money.us_dollar(100000).format(thousands_separator: ".")).to eq "$1.000.00"
|
447
|
-
expect(Money.us_dollar(200000).format(thousands_separator: "")).to eq "$2000.00"
|
448
|
-
end
|
449
|
-
|
450
|
-
specify "(thousands_separator: false or nil) works as documented" do
|
451
|
-
expect(Money.us_dollar(100000).format(thousands_separator: false)).to eq "$1000.00"
|
452
|
-
expect(Money.us_dollar(200000).format(thousands_separator: nil)).to eq "$2000.00"
|
453
|
-
end
|
454
|
-
|
455
|
-
specify "(delimiter: a delimiter string) works as documented" do
|
456
|
-
expect(Money.us_dollar(100000).format(delimiter: ".")).to eq "$1.000.00"
|
457
|
-
expect(Money.us_dollar(200000).format(delimiter: "")).to eq "$2000.00"
|
458
|
-
end
|
459
|
-
|
460
|
-
specify "(delimiter: false or nil) works as documented" do
|
461
|
-
expect(Money.us_dollar(100000).format(delimiter: false)).to eq "$1000.00"
|
462
|
-
expect(Money.us_dollar(200000).format(delimiter: nil)).to eq "$2000.00"
|
463
|
-
end
|
464
|
-
|
465
|
-
it "defaults to ',' if currency isn't recognized" do
|
466
|
-
expect(Money.new(100000, "ZWD").format).to eq "$1,000.00"
|
467
|
-
end
|
468
|
-
|
469
|
-
context "without i18n" do
|
470
|
-
before { Money.use_i18n = false }
|
471
|
-
|
472
|
-
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies that have no subunit" do
|
473
|
-
expect(Money.new(300_000, 'ISK').format(thousands_separator: ",", decimal_mark: '.')).to eq "kr300,000"
|
474
|
-
end
|
475
|
-
|
476
|
-
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component for currencies with subunits that drop_trailing_zeros" do
|
477
|
-
expect(Money.new(300_000, 'USD').format(thousands_separator: ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000"
|
478
|
-
end
|
479
|
-
|
480
|
-
after { Money.use_i18n = true}
|
481
|
-
end
|
482
|
-
end
|
483
|
-
|
484
|
-
describe ":thousands_separator and :decimal_mark option" do
|
485
|
-
specify "(thousands_separator: a thousands_separator string, decimal_mark: a decimal_mark string) works as documented" do
|
486
|
-
expect(Money.new(123_456_789, "USD").format(thousands_separator: ".", decimal_mark: ",")).to eq("$1.234.567,89")
|
487
|
-
expect(Money.new(987_654_321, "USD").format(thousands_separator: " ", decimal_mark: ".")).to eq("$9 876 543.21")
|
488
|
-
end
|
489
|
-
end
|
490
|
-
|
491
|
-
describe ":html option" do
|
492
|
-
specify "(html: true) works as documented" do
|
493
|
-
string = Money.ca_dollar(570).format(html: true, with_currency: true)
|
494
|
-
expect(string).to eq "$5.70 <span class=\"currency\">CAD</span>"
|
495
|
-
end
|
496
|
-
|
497
|
-
specify "should fallback to symbol if entity is not available" do
|
498
|
-
string = Money.new(570, 'DKK').format(html: true)
|
499
|
-
expect(string).to eq "5,70 kr."
|
500
|
-
end
|
501
|
-
end
|
502
|
-
|
503
|
-
describe ":html_wrap_symbol option" do
|
504
|
-
specify "(html_wrap_symbol: true) works as documented" do
|
505
|
-
string = Money.ca_dollar(570).format(html_wrap_symbol: true)
|
506
|
-
expect(string).to eq "<span class=\"currency_symbol\">$</span>5.70"
|
507
|
-
end
|
508
|
-
end
|
509
|
-
|
510
|
-
describe ":html_wrap option" do
|
511
|
-
specify "(html_wrap: true) works as documented" do
|
512
|
-
string = Money.ca_dollar(570).format(html_wrap: true)
|
513
|
-
expect(string).to eq "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span>"
|
514
|
-
end
|
515
|
-
|
516
|
-
specify "(html_wrap: true, with_currency: true)" do
|
517
|
-
string = Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
|
518
|
-
expect(string).to eq "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
|
519
|
-
end
|
520
|
-
|
521
|
-
specify "should fallback to symbol if entity is not available" do
|
522
|
-
string = Money.new(570, 'DKK').format(html_wrap: true)
|
523
|
-
expect(string).to eq "<span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">,</span><span class=\"money-decimal\">70</span> <span class=\"money-currency-symbol\">kr.</span>"
|
524
|
-
end
|
525
|
-
end
|
526
|
-
|
527
|
-
describe ":symbol_position option" do
|
528
|
-
it "inserts currency symbol before the amount when set to :before" do
|
529
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :before)).to eq "€1.234.567,12"
|
530
|
-
end
|
531
|
-
|
532
|
-
it "inserts currency symbol after the amount when set to :after" do
|
533
|
-
expect(Money.us_dollar(1_000_000_000_12).format(symbol_position: :after)).to eq "1,000,000,000.12 $"
|
534
|
-
end
|
535
|
-
|
536
|
-
it "raises an ArgumentError when passed an invalid option" do
|
537
|
-
expect{Money.euro(0).format(symbol_position: :befor)}.to raise_error(ArgumentError)
|
538
|
-
end
|
539
|
-
end
|
540
|
-
|
541
|
-
describe ":sign_before_symbol option" do
|
542
|
-
specify "(sign_before_symbol: true) works as documented" do
|
543
|
-
expect(Money.us_dollar(-100000).format(sign_before_symbol: true)).to eq "-$1,000.00"
|
544
|
-
end
|
545
|
-
|
546
|
-
specify "(sign_before_symbol: false) works as documented" do
|
547
|
-
expect(Money.us_dollar(-100000).format(sign_before_symbol: false)).to eq "$-1,000.00"
|
548
|
-
expect(Money.us_dollar(-100000).format(sign_before_symbol: nil)).to eq "$-1,000.00"
|
549
|
-
end
|
550
|
-
end
|
551
|
-
|
552
|
-
describe ":symbol_before_without_space option" do
|
553
|
-
it "does not insert space between currency symbol and amount when set to true" do
|
554
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :before, symbol_before_without_space: true)).to eq "€1.234.567,12"
|
555
|
-
end
|
556
|
-
|
557
|
-
it "inserts space between currency symbol and amount when set to false" do
|
558
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :before, symbol_before_without_space: false)).to eq "€ 1.234.567,12"
|
559
|
-
end
|
560
|
-
|
561
|
-
it "defaults to true" do
|
562
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :before)).to eq "€1.234.567,12"
|
563
|
-
end
|
564
|
-
end
|
565
|
-
|
566
|
-
describe ":symbol_after_without_space option" do
|
567
|
-
it "does not insert space between amount and currency symbol when set to true" do
|
568
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :after, symbol_after_without_space: true)).to eq "1.234.567,12€"
|
569
|
-
end
|
570
|
-
|
571
|
-
it "inserts space between amount and currency symbol when set to false" do
|
572
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :after, symbol_after_without_space: false)).to eq "1.234.567,12 €"
|
573
|
-
end
|
574
|
-
|
575
|
-
it "defaults to false" do
|
576
|
-
expect(Money.euro(1_234_567_12).format(symbol_position: :after)).to eq "1.234.567,12 €"
|
577
|
-
end
|
578
|
-
end
|
579
|
-
|
580
|
-
describe ":sign_positive option" do
|
581
|
-
specify "(sign_positive: true, sign_before_symbol: true) works as documented" do
|
582
|
-
expect(Money.us_dollar( 0).format(sign_positive: true, sign_before_symbol: true)).to eq "$0.00"
|
583
|
-
expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: true)).to eq "+$1,000.00"
|
584
|
-
expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: true)).to eq "-$1,000.00"
|
585
|
-
end
|
586
|
-
|
587
|
-
specify "(sign_positive: true, sign_before_symbol: false) works as documented" do
|
588
|
-
expect(Money.us_dollar( 0).format(sign_positive: true, sign_before_symbol: false)).to eq "$0.00"
|
589
|
-
expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: false)).to eq "$+1,000.00"
|
590
|
-
expect(Money.us_dollar( 100000).format(sign_positive: true, sign_before_symbol: nil)).to eq "$+1,000.00"
|
591
|
-
expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: false)).to eq "$-1,000.00"
|
592
|
-
expect(Money.us_dollar(-100000).format(sign_positive: true, sign_before_symbol: nil)).to eq "$-1,000.00"
|
593
|
-
end
|
594
|
-
|
595
|
-
specify "(sign_positive: false, sign_before_symbol: true) works as documented" do
|
596
|
-
expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: true)).to eq "$1,000.00"
|
597
|
-
expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: true)).to eq "-$1,000.00"
|
598
|
-
end
|
599
|
-
|
600
|
-
specify "(sign_positive: false, sign_before_symbol: false) works as documented" do
|
601
|
-
expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: false)).to eq "$1,000.00"
|
602
|
-
expect(Money.us_dollar( 100000).format(sign_positive: false, sign_before_symbol: nil)).to eq "$1,000.00"
|
603
|
-
expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: false)).to eq "$-1,000.00"
|
604
|
-
expect(Money.us_dollar(-100000).format(sign_positive: false, sign_before_symbol: nil)).to eq "$-1,000.00"
|
605
|
-
end
|
606
|
-
end
|
607
|
-
|
608
|
-
describe ":rounded_infinite_precision option", :infinite_precision do
|
609
|
-
it "does round fractional when set to true" do
|
610
|
-
expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: true)).to eq "$0.12"
|
611
|
-
expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: true)).to eq "$0.13"
|
612
|
-
expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0.123"
|
613
|
-
expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0.124"
|
614
|
-
expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: true)).to eq "$1.00"
|
615
|
-
expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: true)).to eq "$1.10"
|
616
|
-
expect(Money.new(BigDecimal('1.7'), "MGA").format(rounded_infinite_precision: true)).to eq "Ar0.4"
|
617
|
-
end
|
618
|
-
|
619
|
-
it "does not round fractional when set to false" do
|
620
|
-
expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: false)).to eq "$0.121"
|
621
|
-
expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: false)).to eq "$0.125"
|
622
|
-
expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: false)).to eq "ب.د0.1231"
|
623
|
-
expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: false)).to eq "ب.د0.1235"
|
624
|
-
expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: false)).to eq "$1.001"
|
625
|
-
expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: false)).to eq "$1.095"
|
626
|
-
expect(Money.new(BigDecimal('1.7'), "MGA").format(rounded_infinite_precision: false)).to eq "Ar0.34"
|
627
|
-
end
|
628
|
-
|
629
|
-
describe "with i18n = false" do
|
630
|
-
before do
|
631
|
-
Money.use_i18n = false
|
632
|
-
end
|
633
|
-
|
634
|
-
after do
|
635
|
-
Money.use_i18n = true
|
636
|
-
end
|
637
|
-
|
638
|
-
it 'does round fractional when set to true' do
|
639
|
-
expect(Money.new(BigDecimal('12.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€0,12"
|
640
|
-
expect(Money.new(BigDecimal('12.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€0,13"
|
641
|
-
expect(Money.new(BigDecimal('100.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€1,00"
|
642
|
-
expect(Money.new(BigDecimal('109.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€1,10"
|
643
|
-
|
644
|
-
expect(Money.new(BigDecimal('100012.1'), "EUR").format(rounded_infinite_precision: true)).to eq "€1.000,12"
|
645
|
-
expect(Money.new(BigDecimal('100012.5'), "EUR").format(rounded_infinite_precision: true)).to eq "€1.000,13"
|
646
|
-
end
|
647
|
-
end
|
648
|
-
|
649
|
-
describe "with i18n = true" do
|
650
|
-
before do
|
651
|
-
Money.use_i18n = true
|
652
|
-
reset_i18n
|
653
|
-
I18n.locale = :de
|
654
|
-
I18n.backend.store_translations(
|
655
|
-
:de,
|
656
|
-
number: { currency: { format: { delimiter: ".", separator: "," } } }
|
657
|
-
)
|
658
|
-
end
|
659
|
-
|
660
|
-
after do
|
661
|
-
reset_i18n
|
662
|
-
I18n.locale = :en
|
663
|
-
end
|
664
|
-
|
665
|
-
it 'does round fractional when set to true' do
|
666
|
-
expect(Money.new(BigDecimal('12.1'), "USD").format(rounded_infinite_precision: true)).to eq "$0,12"
|
667
|
-
expect(Money.new(BigDecimal('12.5'), "USD").format(rounded_infinite_precision: true)).to eq "$0,13"
|
668
|
-
expect(Money.new(BigDecimal('123.1'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0,123"
|
669
|
-
expect(Money.new(BigDecimal('123.5'), "BHD").format(rounded_infinite_precision: true)).to eq "ب.د0,124"
|
670
|
-
expect(Money.new(BigDecimal('100.1'), "USD").format(rounded_infinite_precision: true)).to eq "$1,00"
|
671
|
-
expect(Money.new(BigDecimal('109.5'), "USD").format(rounded_infinite_precision: true)).to eq "$1,10"
|
672
|
-
expect(Money.new(BigDecimal('1'), "MGA").format(rounded_infinite_precision: true)).to eq "Ar0,2"
|
673
|
-
end
|
674
|
-
end
|
675
|
-
end
|
676
|
-
|
677
|
-
describe ':format option' do
|
678
|
-
let(:money) { Money.new(99_99, 'USD') }
|
679
|
-
|
680
|
-
it 'uses provided format as a template' do
|
681
|
-
expect(money.format(format: '%n')).to eq('99.99')
|
682
|
-
expect(money.format(format: '%u')).to eq('$')
|
683
|
-
expect(money.format(format: '%u%n')).to eq('$99.99')
|
684
|
-
expect(money.format(format: '%n%u')).to eq('99.99$')
|
685
|
-
expect(money.format(format: '%u %n')).to eq('$ 99.99')
|
686
|
-
expect(money.format(format: 'Your balance is: %u%n')).to eq('Your balance is: $99.99')
|
687
|
-
end
|
688
|
-
|
689
|
-
it 'ignores :symbol_position in favour of format' do
|
690
|
-
expect(money.format(format: '%u%n', symbol_position: :after)).to eq('$99.99')
|
691
|
-
expect(money.format(format: '%n%u', symbol_position: :before)).to eq('99.99$')
|
692
|
-
end
|
693
|
-
|
694
|
-
it 'ignores :symbol_before_without_space in favour of format' do
|
695
|
-
expect(money.format(format: '%u %n', symbol_position: :before, symbol_before_without_space: true)).to eq('$ 99.99')
|
696
|
-
expect(money.format(format: '%u%n', symbol_position: :before, symbol_before_without_space: false)).to eq('$99.99')
|
697
|
-
end
|
698
|
-
|
699
|
-
it 'ignores :symbol_after_without_space in favour of format' do
|
700
|
-
expect(money.format(format: '%n %u', symbol_position: :after, symbol_after_without_space: true)).to eq('99.99 $')
|
701
|
-
expect(money.format(format: '%n%u', symbol_position: :after, symbol_after_without_space: false)).to eq('99.99$')
|
702
|
-
end
|
703
|
-
|
704
|
-
it 'works with sign' do
|
705
|
-
money = Money.new(-99_99, 'USD')
|
706
|
-
|
707
|
-
expect(money.format(format: '%n%u', sign_before_symbol: false)).to eq('-99.99$')
|
708
|
-
expect(money.format(format: '%u%n', sign_before_symbol: false)).to eq('$-99.99')
|
709
|
-
expect(money.format(format: '%u%n', sign_before_symbol: true)).to eq('-$99.99')
|
710
|
-
end
|
711
|
-
end
|
712
|
-
|
713
|
-
context "when the monetary value is 0" do
|
714
|
-
let(:money) { Money.us_dollar(0) }
|
715
|
-
|
716
|
-
it "returns 'free' when :display_free is true" do
|
717
|
-
expect(money.format(display_free: true)).to eq 'free'
|
718
|
-
end
|
719
|
-
|
720
|
-
it "returns '$0.00' when :display_free is false or not given" do
|
721
|
-
expect(money.format).to eq '$0.00'
|
722
|
-
expect(money.format(display_free: false)).to eq '$0.00'
|
723
|
-
expect(money.format(display_free: nil)).to eq '$0.00'
|
724
|
-
end
|
725
|
-
|
726
|
-
it "returns the value specified by :display_free if it's a string-like object" do
|
727
|
-
expect(money.format(display_free: 'gratis')).to eq 'gratis'
|
728
|
-
end
|
729
|
-
end
|
730
|
-
end
|
731
|
-
|
732
|
-
context "custom currencies with 4 decimal places" do
|
733
|
-
before :each do
|
734
|
-
Money::Currency.register(JSON.parse(BAR, symbolize_names: true))
|
735
|
-
Money::Currency.register(JSON.parse(EU4, symbolize_names: true))
|
736
|
-
end
|
737
|
-
|
738
|
-
after :each do
|
739
|
-
Money::Currency.unregister(JSON.parse(BAR, symbolize_names: true))
|
740
|
-
Money::Currency.unregister(JSON.parse(EU4, symbolize_names: true))
|
741
|
-
end
|
742
|
-
|
743
|
-
it "respects custom subunit to unit, decimal and thousands separator" do
|
744
|
-
expect(Money.new(4, "BAR").format).to eq "$0.0004"
|
745
|
-
expect(Money.new(4, "EU4").format).to eq "€0,0004"
|
746
|
-
|
747
|
-
expect(Money.new(24, "BAR").format).to eq "$0.0024"
|
748
|
-
expect(Money.new(24, "EU4").format).to eq "€0,0024"
|
749
|
-
|
750
|
-
expect(Money.new(324, "BAR").format).to eq "$0.0324"
|
751
|
-
expect(Money.new(324, "EU4").format).to eq "€0,0324"
|
752
|
-
|
753
|
-
expect(Money.new(5324, "BAR").format).to eq "$0.5324"
|
754
|
-
expect(Money.new(5324, "EU4").format).to eq "€0,5324"
|
755
|
-
|
756
|
-
expect(Money.new(65324, "BAR").format).to eq "$6.5324"
|
757
|
-
expect(Money.new(65324, "EU4").format).to eq "€6,5324"
|
758
|
-
|
759
|
-
expect(Money.new(865324, "BAR").format).to eq "$86.5324"
|
760
|
-
expect(Money.new(865324, "EU4").format).to eq "€86,5324"
|
761
|
-
|
762
|
-
expect(Money.new(1865324, "BAR").format).to eq "$186.5324"
|
763
|
-
expect(Money.new(1865324, "EU4").format).to eq "€186,5324"
|
764
|
-
|
765
|
-
expect(Money.new(33310034, "BAR").format).to eq "$3,331.0034"
|
766
|
-
expect(Money.new(33310034, "EU4").format).to eq "€3.331,0034"
|
767
|
-
|
768
|
-
expect(Money.new(88833310034, "BAR").format).to eq "$8,883,331.0034"
|
769
|
-
expect(Money.new(88833310034, "EU4").format).to eq "€8.883.331,0034"
|
770
|
-
end
|
771
|
-
|
772
|
-
end
|
773
|
-
|
774
|
-
context "currencies with ambiguous signs" do
|
775
|
-
|
776
|
-
it "returns ambiguous signs when disambiguate is not set" do
|
777
|
-
expect(Money.new(1999_98, "USD").format).to eq("$1,999.98")
|
778
|
-
expect(Money.new(1999_98, "CAD").format).to eq("$1,999.98")
|
779
|
-
expect(Money.new(1999_98, "DKK").format).to eq("1.999,98 kr.")
|
780
|
-
expect(Money.new(1999_98, "NOK").format).to eq("1.999,98 kr")
|
781
|
-
expect(Money.new(1999_98, "SEK").format).to eq("1 999,98 kr")
|
782
|
-
expect(Money.new(1999_98, "BCH").format).to eq("0.00199998 ₿")
|
783
|
-
end
|
784
|
-
|
785
|
-
it "returns ambiguous signs when disambiguate is false" do
|
786
|
-
expect(Money.new(1999_98, "USD").format(disambiguate: false)).to eq("$1,999.98")
|
787
|
-
expect(Money.new(1999_98, "CAD").format(disambiguate: false)).to eq("$1,999.98")
|
788
|
-
expect(Money.new(1999_98, "DKK").format(disambiguate: false)).to eq("1.999,98 kr.")
|
789
|
-
expect(Money.new(1999_98, "NOK").format(disambiguate: false)).to eq("1.999,98 kr")
|
790
|
-
expect(Money.new(1999_98, "SEK").format(disambiguate: false)).to eq("1 999,98 kr")
|
791
|
-
expect(Money.new(1999_98, "BCH").format(disambiguate: false)).to eq("0.00199998 ₿")
|
792
|
-
end
|
793
|
-
|
794
|
-
it "returns disambiguate signs when disambiguate: true" do
|
795
|
-
expect(Money.new(1999_98, "USD").format(disambiguate: true)).to eq("US$1,999.98")
|
796
|
-
expect(Money.new(1999_98, "CAD").format(disambiguate: true)).to eq("C$1,999.98")
|
797
|
-
expect(Money.new(1999_98, "DKK").format(disambiguate: true)).to eq("1.999,98 DKK")
|
798
|
-
expect(Money.new(1999_98, "NOK").format(disambiguate: true)).to eq("1.999,98 NOK")
|
799
|
-
expect(Money.new(1999_98, "SEK").format(disambiguate: true)).to eq("1 999,98 SEK")
|
800
|
-
expect(Money.new(1999_98, "BCH").format(disambiguate: true)).to eq("0.00199998 ₿CH")
|
801
|
-
end
|
802
|
-
|
803
|
-
it "returns disambiguate signs when disambiguate: true and symbol: true" do
|
804
|
-
expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)).to eq("US$1,999.98")
|
805
|
-
expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: true)).to eq("C$1,999.98")
|
806
|
-
expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: true)).to eq("1.999,98 DKK")
|
807
|
-
expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: true)).to eq("1.999,98 NOK")
|
808
|
-
expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: true)).to eq("1 999,98 SEK")
|
809
|
-
expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: true)).to eq("0.00199998 ₿CH")
|
810
|
-
end
|
811
|
-
|
812
|
-
it "returns no signs when disambiguate: true and symbol: false" do
|
813
|
-
expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: false)).to eq("1,999.98")
|
814
|
-
expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: false)).to eq("1,999.98")
|
815
|
-
expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: false)).to eq("1.999,98")
|
816
|
-
expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: false)).to eq("1.999,98")
|
817
|
-
expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: false)).to eq("1 999,98")
|
818
|
-
expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: false)).to eq("0.00199998")
|
819
|
-
end
|
820
|
-
|
821
|
-
it "should never return an ambiguous format with disambiguate: true" do
|
822
|
-
formatted_results = {}
|
823
|
-
|
824
|
-
# When we format the same amount in all known currencies, disambiguate should return
|
825
|
-
# all different values
|
826
|
-
Money::Currency.all.each do |currency|
|
827
|
-
format = Money.new(1999_98, currency).format(disambiguate: true)
|
828
|
-
expect(formatted_results.keys).not_to include(format), "Format '#{format}' for #{currency} is ambiguous with currency #{formatted_results[format]}."
|
829
|
-
formatted_results[format] = currency
|
830
|
-
end
|
831
|
-
end
|
832
|
-
|
833
|
-
describe ":drop_trailing_zeros option" do
|
834
|
-
specify "(drop_trailing_zeros: true) works as documented" do
|
835
|
-
expect(Money.new(89000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089"
|
836
|
-
expect(Money.new(89000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089"
|
837
|
-
expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089"
|
838
|
-
expect(Money.new(100089000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089"
|
839
|
-
expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1"
|
840
|
-
expect(Money.new(100000000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "1"
|
841
|
-
expect(Money.new(110, "AUD").format(drop_trailing_zeros: true, symbol: false)).to eq "1.1"
|
842
|
-
end
|
843
|
-
|
844
|
-
specify "(drop_trailing_zeros: false) works as documented" do
|
845
|
-
expect(Money.new(89000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000"
|
846
|
-
expect(Money.new(89000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000"
|
847
|
-
expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000"
|
848
|
-
expect(Money.new(100089000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000"
|
849
|
-
expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000"
|
850
|
-
expect(Money.new(100000000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000"
|
851
|
-
expect(Money.new(110, "AUD").format(drop_trailing_zeros: false, symbol: false)).to eq "1.10"
|
852
|
-
end
|
853
|
-
end
|
854
|
-
end
|
855
|
-
end
|