From eb28e8766268cdfb4a4188b58bce7b8aee053e07 Mon Sep 17 00:00:00 2001 From: Stephen Waits <1+swaits@noreply.git.swaits.com> Date: Sun, 11 Oct 2026 02:25:12 +0000 Subject: [PATCH] asset slice + testing initiative: fixed assets, property/mutation/fuzz testing, docs --- crates/ledger-core/src/money.rs | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/ledger-core/src/money.rs b/crates/ledger-core/src/money.rs index 0d1d37b..f038c37 100644 --- a/crates/ledger-core/src/money.rs +++ b/crates/ledger-core/src/money.rs @@ -123,3 +123,40 @@ impl fmt::Display for Money { write!(f, "{} {}", self.amount, self.currency) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn usd(amount: Decimal) -> Money { + Money::new(amount, Currency::USD) + } + + fn d(cents: i64) -> Decimal { + Decimal::new(cents, 2) + } + + #[test] + fn checked_arithmetic() { + assert_eq!( + usd(d(1050)).checked_add(usd(d(425))).unwrap().amount, + d(1475) + ); + assert_eq!( + usd(d(1050)).checked_sub(usd(d(425))).unwrap().amount, + d(625) + ); + // Subtraction is not division, and not addition. + assert_ne!( + usd(d(1000)).checked_sub(usd(d(200))).unwrap().amount, + d(500) + ); + } + + #[test] + fn checked_arithmetic_rejects_currency_mismatch() { + let eur = Money::new(d(100), Currency::EUR); + assert!(usd(d(100)).checked_add(eur).is_err()); + assert!(usd(d(100)).checked_sub(eur).is_err()); + } +}