spec/danconia/money_spec.rb in danconia-0.2.1 vs spec/danconia/money_spec.rb in danconia-0.2.2
- old
+ new
@@ -7,15 +7,16 @@
expect(Money(10).amount).to eq BigDecimal('10')
expect(Money('10.235').amount).to eq BigDecimal('10.235')
end
it 'non numeric values are treated as zero' do
+ expect(Money(nil)).to eq Money(0)
expect(Money('a')).to eq Money(0)
end
it 'should use the default currency if not specified' do
- with_config do |config|
+ TestHelpers.with_config do |config|
config.default_currency = 'ARS'
expect(Money(0).currency.code).to eq 'ARS'
config.default_currency = 'EUR'
expect(Money(0).currency.code).to eq 'EUR'
@@ -69,13 +70,11 @@
expect(Money(1)).not_to eq 2
expect(Money(1, 'ARS')).not_to eq 1
end
it 'should exchange to the source currency if they differ' do
- with_config do |config|
- config.default_exchange = fake_exchange(rate: 4)
-
+ TestHelpers.with_rates 'USDARS' => 4 do |config|
expect(Money(3, 'ARS') < Money(1, 'USD')).to be true
expect(Money(4, 'ARS') < Money(1, 'USD')).to be false
end
end
@@ -84,16 +83,22 @@
expect([Money(1), Money(1.1)].uniq.size).to eq 2
expect([Money(1, 'ARS'), Money(1, 'USD')].uniq.size).to eq 2
end
end
+ context 'format' do
+ it 'allow to override the number of decimals' do
+ expect(Money(3.561, decimals: 3).format(decimals: 1)).to eq '$3.6'
+ end
+ end
+
context 'to_s' do
it 'should add the currency symbol' do
expect(Money(3.25).to_s).to eq '$3.25'
- with_config do |config|
- config.default_exchange = double 'exchange', available_currencies: [{code: 'EUR', symbol: '€'}, {code: 'JPY', symbol: '¥'}]
+ TestHelpers.with_config do |config|
+ config.default_exchange = Exchanges::FixedRates.new currencies: [{code: 'EUR', symbol: '€'}, {code: 'JPY', symbol: '¥'}]
expect(Money(1, 'EUR').to_s).to eq '€1.00'
expect(Money(1, 'JPY').to_s).to eq '¥1.00'
expect(Money(1, 'OTHER').to_s).to eq '$1.00'
end
@@ -101,30 +106,25 @@
it 'should round according to decimals' do
expect(Money(3.256, decimals: 2).to_s).to eq '$3.26'
expect(Money(3.2517, decimals: 3).to_s).to eq '$3.252'
end
-
- it 'nil should be like zero' do
- expect(Money(nil).to_s).to eq '$0.00'
- end
end
context '.inspect' do
it 'should display the object internals' do
- expect(Money(10.25, 'ARS', decimals: 3).inspect).to eq '#<Danconia::Money 10.25 ARS>'
+ expect(Money(10.25, 'ARS', decimals: 3).inspect).to eq '10.25 ARS'
end
end
context 'exchange_to' do
it 'should use the exchange passed to the instance to get the rate' do
expect(Money(2, 'USD', exchange: fake_exchange(rate: 3)).exchange_to('ARS')).to eq Money(6, 'ARS')
end
it 'should use the default exchange if not set' do
- with_config do |config|
- config.default_exchange = Exchanges::FixedRates.new(rates: {'USDEUR' => 3, 'USDARS' => 4})
+ TestHelpers.with_rates 'USDEUR' => 3, 'USDARS' => 4 do
expect(Money(2, 'USD').exchange_to('EUR')).to eq Money(6, 'EUR')
expect(Money(2, 'USD').exchange_to('ARS')).to eq Money(8, 'ARS')
end
end
@@ -141,19 +141,34 @@
m2 = m1.exchange_to('ARS')
expect(m2).to_not eql m1
expect(m2.decimals).to eq 0
expect(m1).to eq Money(1, 'USD')
end
+
+ it 'if the destination currency is blank should not do the conversion' do
+ expect(Money(1, 'USD').exchange_to('')).to eq Money(1, 'USD')
+ expect(Money(1, 'ARS').exchange_to('')).to eq Money(1, 'ARS')
+ end
end
+ context 'default_currency?' do
+ it 'is true if the currency is the configured default' do
+ TestHelpers.with_config do |config|
+ config.default_currency = 'ARS'
+ expect(Money(1, 'ARS')).to be_default_currency
+ expect(Money(1, 'USD')).to_not be_default_currency
+ end
+ end
+ end
+
context 'delegation' do
it 'should delegate missing methods to the amount' do
expect(Money(10).positive?).to be true
expect(Money(10).respond_to?(:positive?)).to be true
end
end
def fake_exchange args = {}
- double 'exchange', args.reverse_merge(rate: nil, available_currencies: [])
+ double 'exchange', args.reverse_merge(rate: nil, currencies: [])
end
end
end