Skip to content
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
14 changes: 11 additions & 3 deletions lib/money/money/arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,15 @@ def *(value)
#
def /(value)
if value.is_a?(self.class)
fractional / as_d(value.exchange_to(currency).fractional).to_f
exchanged = value.exchange_to(currency)
raise ZeroDivisionError, "divided by Money(0)" if exchanged.zero?
fractional / as_d(exchanged.fractional).to_f
else
raise TypeError, 'Can not divide by Money' if value.is_a?(CoercedNumeric)
dup_with(fractional: fractional / as_d(value))

value = as_d(value)
raise ZeroDivisionError, "divided by zero" if value.zero?
dup_with(fractional: fractional / value)
end
end

Expand Down Expand Up @@ -240,13 +245,16 @@ def divmod(val)

def divmod_money(val)
cents = val.exchange_to(currency).cents
raise ZeroDivisionError, "divided by Money(0)" if cents == 0
quotient, remainder = fractional.divmod(cents)
[quotient, dup_with(fractional: remainder)]
end
private :divmod_money

def divmod_other(val)
quotient, remainder = fractional.divmod(as_d(val))
val = as_d(val)
raise ZeroDivisionError, "divided by zero" if val.zero?
quotient, remainder = fractional.divmod(val)
[dup_with(fractional: quotient), dup_with(fractional: remainder)]
end
private :divmod_other
Expand Down
44 changes: 44 additions & 0 deletions spec/money/arithmetic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,28 @@
end
end

it "raises ZeroDivisionError when dividing by Money with zero value" do
money = Money.new(100, "USD")
zero_money = Money.new(0, "USD")

expect { money / zero_money }.to raise_error(ZeroDivisionError, "divided by Money(0)")
end

it "raises ZeroDivisionError when dividing by Money with zero value in same currency" do
money = Money.new(100, "USD")
zero_usd = Money.new(0, "USD")

expect { money / zero_usd }.to raise_error(ZeroDivisionError, "divided by Money(0)")
end

it "raises ZeroDivisionError when dividing by numeric zero" do
money = Money.new(100, "USD")

expect { money / 0 }.to raise_error(ZeroDivisionError, "divided by zero")
expect { money / 0.0 }.to raise_error(ZeroDivisionError, "divided by zero")
expect { money / BigDecimal("0") }.to raise_error(ZeroDivisionError, "divided by zero")
end

it_behaves_like 'instance with custom bank', :/, 1
end

Expand Down Expand Up @@ -556,6 +578,28 @@
expect(special_money_class.new(10_00, "USD").divmod(special_money_class.new(4_00)).last).to be_a special_money_class
end

it "raises ZeroDivisionError when dividing by Money with zero value" do
money = Money.new(100, "USD")
zero_money = Money.new(0, "USD")

expect { money.divmod(zero_money) }.to raise_error(ZeroDivisionError, "divided by Money(0)")
end

it "raises ZeroDivisionError when dividing by Money with zero value in same currency" do
money = Money.new(100, "USD")
zero_usd = Money.new(0, "USD")

expect { money.divmod(zero_usd) }.to raise_error(ZeroDivisionError, "divided by Money(0)")
end

it "raises ZeroDivisionError when dividing by numeric zero" do
money = Money.new(100, "USD")

expect { money.divmod(0) }.to raise_error(ZeroDivisionError, "divided by zero")
expect { money.divmod(0.0) }.to raise_error(ZeroDivisionError, "divided by zero")
expect { money.divmod(BigDecimal("0")) }.to raise_error(ZeroDivisionError, "divided by zero")
end

it_behaves_like 'instance with custom bank', :divmod, -> { Money.new(1) }
it_behaves_like 'instance with custom bank', :divmod, 1
end
Expand Down