From 7bcdd5ff6655576dac79e14b9c1a50c74db310a2 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Wed, 30 Apr 2025 17:12:29 +0100 Subject: [PATCH] Add regression tests for Display impl The output of `Display` should not change in stable crates for types that have well defined formatting and ones that implement `FromStr`. Error types do not need to be tested. Add missing tests for all implementations in `units`. --- units/src/amount/tests.rs | 71 +++++++++++++++++++++++++++++++++- units/src/locktime/absolute.rs | 11 ++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index a9b19c13c..8f93de9d5 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -13,7 +13,7 @@ use std::panic; use ::serde::{Deserialize, Serialize}; use super::*; -use crate::NumOpResult; +use crate::{MathOp, NumOpResult}; #[cfg(feature = "alloc")] use crate::{FeeRate, Weight}; @@ -120,6 +120,75 @@ fn from_int_btc() { assert_eq!(ssat(-200_000_000), amt); } +#[test] +#[cfg(feature = "alloc")] +fn display_display_struct() { + let display_fixed_btc = Display { + sats_abs: 100_000_000, + is_negative: false, + style: DisplayStyle::FixedDenomination { + denomination: Denomination::Bitcoin, + show_denomination: true, + }, + }; + assert_eq!(format!("{}", display_fixed_btc), "1 BTC"); + + let display_fixed_sat = Display { + sats_abs: 1, + is_negative: false, + style: DisplayStyle::FixedDenomination { + denomination: Denomination::Satoshi, + show_denomination: true, + }, + }; + assert_eq!(format!("{}", display_fixed_sat), "1 satoshi"); + + let display_dynamic_btc = Display { + sats_abs: 100_000_000, + is_negative: false, + style: DisplayStyle::DynamicDenomination, + }; + assert_eq!(format!("{}", display_dynamic_btc), "1 BTC"); + + let display_dynamic_sat = Display { + sats_abs: 99_999_999, + is_negative: false, + style: DisplayStyle::DynamicDenomination, + }; + assert_eq!(format!("{}", display_dynamic_sat), "99999999 satoshi"); + + let display_negative_btc = Display { + sats_abs: 100_000_000, + is_negative: true, + style: DisplayStyle::DynamicDenomination, + }; + assert_eq!(format!("{}", display_negative_btc), "-1 BTC"); + + let display_negative_sat = Display { + sats_abs: 99_999_999, + is_negative: true, + style: DisplayStyle::DynamicDenomination, + }; + assert_eq!(format!("{}", display_negative_sat), "-99999999 satoshi"); +} + +#[test] +#[cfg(feature = "alloc")] +fn display_math_op() { + let cases = [ + (MathOp::Add, "add"), + (MathOp::Sub, "sub"), + (MathOp::Mul, "mul"), + (MathOp::Div, "div"), + (MathOp::Rem, "rem"), + (MathOp::Neg, "neg"), + ]; + + for (op, expected) in cases { + assert_eq!(format!("{}", op), expected); + } +} + #[test] fn amount_try_from_signed_amount() { let sa_positive = ssat(123); diff --git a/units/src/locktime/absolute.rs b/units/src/locktime/absolute.rs index 153aa0e33..71f301e52 100644 --- a/units/src/locktime/absolute.rs +++ b/units/src/locktime/absolute.rs @@ -503,4 +503,15 @@ mod tests { serde_round_trip!(Time::MIN); serde_round_trip!(Time::MAX); } + + #[test] + #[cfg(feature = "alloc")] + fn locktime_unit_display() { + use alloc::format; + let blocks = LockTimeUnit::Blocks; + let seconds = LockTimeUnit::Seconds; + + assert_eq!(format!("{}", blocks), "expected lock-by-blockheight (must be < 500000000)"); + assert_eq!(format!("{}", seconds), "expected lock-by-blocktime (must be >= 500000000)"); + } }