Allow uncommoditized amounts to +/- with an amount

Thus, you can say "$100 + 10" to increase it by $10.
This commit is contained in:
John Wiegley 2009-02-25 23:40:15 -04:00
parent 7f37d7edcc
commit 55a6d588ff
2 changed files with 16 additions and 12 deletions

View file

@ -271,7 +271,8 @@ amount_t& amount_t::operator+=(const amount_t& amt)
throw_(amount_error, _("Cannot add two uninitialized amounts")); throw_(amount_error, _("Cannot add two uninitialized amounts"));
} }
if (commodity() != amt.commodity()) if (has_commodity() && amt.has_commodity() &&
commodity() != amt.commodity())
throw_(amount_error, throw_(amount_error,
_("Adding amounts with different commodities: %1 != %2") _("Adding amounts with different commodities: %1 != %2")
<< (has_commodity() ? commodity().symbol() : _("NONE")) << (has_commodity() ? commodity().symbol() : _("NONE"))
@ -281,8 +282,9 @@ amount_t& amount_t::operator+=(const amount_t& amt)
mpq_add(MP(quantity), MP(quantity), MP(amt.quantity)); mpq_add(MP(quantity), MP(quantity), MP(amt.quantity));
if (quantity->prec < amt.quantity->prec) if (has_commodity() == amt.has_commodity())
quantity->prec = amt.quantity->prec; if (quantity->prec < amt.quantity->prec)
quantity->prec = amt.quantity->prec;
return *this; return *this;
} }
@ -300,7 +302,8 @@ amount_t& amount_t::operator-=(const amount_t& amt)
throw_(amount_error, _("Cannot subtract two uninitialized amounts")); throw_(amount_error, _("Cannot subtract two uninitialized amounts"));
} }
if (commodity() != amt.commodity()) if (has_commodity() && amt.has_commodity() &&
commodity() != amt.commodity())
throw_(amount_error, throw_(amount_error,
_("Subtracting amounts with different commodities: %1 != %2") _("Subtracting amounts with different commodities: %1 != %2")
<< (has_commodity() ? commodity().symbol() : _("NONE")) << (has_commodity() ? commodity().symbol() : _("NONE"))
@ -310,8 +313,9 @@ amount_t& amount_t::operator-=(const amount_t& amt)
mpq_sub(MP(quantity), MP(quantity), MP(amt.quantity)); mpq_sub(MP(quantity), MP(quantity), MP(amt.quantity));
if (quantity->prec < amt.quantity->prec) if (has_commodity() == amt.has_commodity())
quantity->prec = amt.quantity->prec; if (quantity->prec < amt.quantity->prec)
quantity->prec = amt.quantity->prec;
return *this; return *this;
} }

View file

@ -540,11 +540,11 @@ void AmountTestCase::testCommodityAddition()
assertThrow(x1 + x3, amount_error); assertThrow(x1 + x3, amount_error);
assertThrow(x1 + x4, amount_error); assertThrow(x1 + x4, amount_error);
assertThrow(x1 + x5, amount_error); assertThrow(x1 + x5, amount_error);
assertThrow(x1 + x6, amount_error); assertEqual(string("$246.90"), (x1 + x6).to_string());
#ifndef NOT_FOR_PYTHON #ifndef NOT_FOR_PYTHON
assertThrow(x1 + 123.45, amount_error); assertEqual(string("$246.90"), (x1 + 123.45).to_string());
#endif // NOT_FOR_PYTHON #endif // NOT_FOR_PYTHON
assertThrow(x1 + 123L, amount_error); assertEqual(string("$246.45"), (x1 + 123L).to_string());
assertEqual(amount_t("DM 246.90"), x3 + x3); assertEqual(amount_t("DM 246.90"), x3 + x3);
assertEqual(amount_t("246.90 euro"), x4 + x4); assertEqual(amount_t("246.90 euro"), x4 + x4);
@ -656,11 +656,11 @@ void AmountTestCase::testCommoditySubtraction()
assertThrow(x1 - x3, amount_error); assertThrow(x1 - x3, amount_error);
assertThrow(x1 - x4, amount_error); assertThrow(x1 - x4, amount_error);
assertThrow(x1 - x5, amount_error); assertThrow(x1 - x5, amount_error);
assertThrow(x1 - x6, amount_error); assertEqual(string("$0.00"), (x1 - x6).to_string());
#ifndef NOT_FOR_PYTHON #ifndef NOT_FOR_PYTHON
assertThrow(x1 - 123.45, amount_error); assertEqual(string("$-0.00"), (x1 - 123.45).to_string());
#endif // NOT_FOR_PYTHON #endif // NOT_FOR_PYTHON
assertThrow(x1 - 123L, amount_error); assertEqual(string("$0.45"), (x1 - 123L).to_string());
assertEqual(amount_t("DM 0.00"), x3 - x3); assertEqual(amount_t("DM 0.00"), x3 - x3);
assertEqual(amount_t("DM 23.45"), x3 - amount_t("DM 100.00")); assertEqual(amount_t("DM 23.45"), x3 - amount_t("DM 100.00"));