Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise error when comparing or casting to a different currency #312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,7 @@ def arithmetic(other)
def ensure_compatible_currency(other_currency, msg)
return if currency.compatible?(other_currency)

if Money.config.legacy_deprecations
Money.deprecate("#{msg}. A Money::IncompatibleCurrencyError will raise in the next major release")
else
raise Money::IncompatibleCurrencyError, msg
end
raise Money::IncompatibleCurrencyError, msg
end

def calculated_currency(other)
Expand Down
51 changes: 5 additions & 46 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@
expect(money.currency.to_s).to eq('USD')
end

it "legacy_deprecations #to_money doesn't overwrite the money object's currency" do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).with(match(/to_money is attempting to change currency of an existing money object/)).once
expect(Money.new(1, 'USD').to_money('CAD')).to eq(Money.new(1, 'USD'))
end
end

it "#to_money raises when changing currency" do
expect{ Money.new(1, 'USD').to_money('CAD') }.to raise_error(Money::IncompatibleCurrencyError)
end
Expand Down Expand Up @@ -216,13 +209,6 @@
expect((Money.new + Money.new)).to eq(Money.new)
end

it "legacy_deprecations adds inconsistent currencies" do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).once
expect(Money.new(5, 'USD') + Money.new(1, 'CAD')).to eq(Money.new(6, 'USD'))
end
end

it "raises when adding inconsistent currencies" do
expect{ Money.new(5, 'USD') + Money.new(1, 'CAD') }.to raise_error(Money::IncompatibleCurrencyError)
end
Expand All @@ -239,13 +225,6 @@
expect((Money.new(5.00) - Money.new(5.00))).to eq(Money.new)
end

it "logs a deprecation warning when adding across currencies" do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).with(match(/mathematical operation not permitted for Money objects with different currencies/))
expect(Money.new(10, 'USD') - Money.new(1, 'JPY')).to eq(Money.new(9, 'USD'))
end
end

it "keeps currency when doing a computation with a null currency" do
currency = Money.new(10, 'JPY')
no_currency = Money.new(1, Money::NULL_CURRENCY)
Expand Down Expand Up @@ -448,11 +427,6 @@

it "generates a true rational" do
expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'USD'))).to eq(Rational(2,3))

configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).once
expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'JPY'))).to eq(Rational(2,3))
end
end

it "does not allocate a new money object when multiplying by 1" do
Expand Down Expand Up @@ -534,14 +508,6 @@
expect(Money.new(2000, 'JPY') <=> Money.new(1000, Money::NULL_CURRENCY)).to eq(1)
end

it "<=> issues deprecation warning when comparing incompatible currency" do
configure(legacy_deprecations: true) do
expect(Money).to receive(:deprecate).twice
expect(Money.new(1000, 'USD') <=> Money.new(2000, 'JPY')).to eq(-1)
expect(Money.new(2000, 'JPY') <=> Money.new(1000, 'USD')).to eq(1)
end
end

describe('same values') do
describe('same currencies') do
it { expect(cad_10 <=> cad_10).to(eq(0)) }
Expand All @@ -561,20 +527,13 @@
it { expect(cad_10 < nil_10).to(eq(false)) }
end

describe('legacy different currencies') do
around(:each) do |test|
configure(legacy_deprecations: true) { test.run }
end

it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <=> usd_10).to(eq(0)) }
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 > usd_10).to(eq(false)) }
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 >= usd_10).to(eq(true)) }
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <= usd_10).to(eq(true)) }
it { expect(Money).to(receive(:deprecate).once); expect(cad_10 < usd_10).to(eq(false)) }
end

describe('different currencies') do
it { expect(cad_10 == usd_10).to(eq(false)) }
it { expect { cad_10 <=> usd_10 }.to raise_error(Money::IncompatibleCurrencyError) }
it { expect { cad_10 > usd_10 }.to raise_error(Money::IncompatibleCurrencyError) }
it { expect { cad_10 >= usd_10 }.to raise_error(Money::IncompatibleCurrencyError) }
it { expect { cad_10 <= usd_10 }.to raise_error(Money::IncompatibleCurrencyError) }
it { expect { cad_10 < usd_10 }.to raise_error(Money::IncompatibleCurrencyError) }
end

describe('coerced types') do
Expand Down
Loading