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`.
This commit is contained in:
parent
9c90bf49c9
commit
7bcdd5ff66
|
@ -13,7 +13,7 @@ use std::panic;
|
||||||
use ::serde::{Deserialize, Serialize};
|
use ::serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::NumOpResult;
|
use crate::{MathOp, NumOpResult};
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
use crate::{FeeRate, Weight};
|
use crate::{FeeRate, Weight};
|
||||||
|
|
||||||
|
@ -120,6 +120,75 @@ fn from_int_btc() {
|
||||||
assert_eq!(ssat(-200_000_000), amt);
|
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]
|
#[test]
|
||||||
fn amount_try_from_signed_amount() {
|
fn amount_try_from_signed_amount() {
|
||||||
let sa_positive = ssat(123);
|
let sa_positive = ssat(123);
|
||||||
|
|
|
@ -503,4 +503,15 @@ mod tests {
|
||||||
serde_round_trip!(Time::MIN);
|
serde_round_trip!(Time::MIN);
|
||||||
serde_round_trip!(Time::MAX);
|
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)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue