From c27f4435208cc3ca7b98580fd7e2784e089b545e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 2 Dec 2024 14:25:55 +1100 Subject: [PATCH] Add basic unit tests for Amount serde Add a test each for serializing `Amount` as sat, as BTC, and as str. --- units/src/amount/serde.rs | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/units/src/amount/serde.rs b/units/src/amount/serde.rs index f5ddc595f..2caa8c74e 100644 --- a/units/src/amount/serde.rs +++ b/units/src/amount/serde.rs @@ -385,3 +385,70 @@ pub mod as_str { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_serde_as_sat() { + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] + pub struct HasAmount { + #[serde(with = "crate::amount::serde::as_sat")] + pub amount: Amount, + } + + let orig = HasAmount { + amount: Amount::ONE_BTC, + }; + + let json = serde_json::to_string(&orig).expect("failed to ser"); + let want = "{\"amount\":100000000}"; + assert_eq!(json, want); + + let rinsed: HasAmount = serde_json::from_str(&json).expect("failed to deser"); + assert_eq!(rinsed, orig) + } + + #[test] + #[cfg(feature = "alloc")] + fn can_serde_as_btc() { + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] + pub struct HasAmount { + #[serde(with = "crate::amount::serde::as_btc")] + pub amount: Amount, + } + + let orig = HasAmount { + amount: Amount::ONE_BTC, + }; + + let json = serde_json::to_string(&orig).expect("failed to ser"); + let want = "{\"amount\":1.0}"; + assert_eq!(json, want); + + let rinsed: HasAmount = serde_json::from_str(&json).expect("failed to deser"); + assert_eq!(rinsed, orig) + } + + #[test] + #[cfg(feature = "alloc")] + fn can_serde_as_str() { + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] + pub struct HasAmount { + #[serde(with = "crate::amount::serde::as_str")] + pub amount: Amount, + } + + let orig = HasAmount { + amount: Amount::ONE_BTC, + }; + + let json = serde_json::to_string(&orig).expect("failed to ser"); + let want = "{\"amount\":\"1\"}"; + assert_eq!(json, want); + + let rinsed: HasAmount = serde_json::from_str(&json).expect("failed to deser"); + assert_eq!(rinsed, orig); + } +}