From 386932a07bbb2dffabfc0f652df084ef258e159b Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Tue, 6 Jan 2026 20:18:48 +0800 Subject: [PATCH] fix(test): more JSON snapshot tests --- src/lotus_json/entry.rs | 3 +- src/lotus_json/filter_estimate.rs | 52 ++++++----- src/lotus_json/miner_power.rs | 32 +++++-- src/lotus_json/mod.rs | 2 +- src/lotus_json/signature.rs | 150 +++++++++++++++++------------- src/lotus_json/signature_type.rs | 129 +++++++++++++------------ src/lotus_json/token_state.rs | 16 ++-- src/lotus_json/transient_data.rs | 134 ++++++++++++++------------ 8 files changed, 297 insertions(+), 221 deletions(-) diff --git a/src/lotus_json/entry.rs b/src/lotus_json/entry.rs index b327f84cc425..b8b138590e0d 100644 --- a/src/lotus_json/entry.rs +++ b/src/lotus_json/entry.rs @@ -5,7 +5,7 @@ use crate::shim::actors::cron::Entry; use crate::shim::address::Address; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "PascalCase")] pub struct EntryLotusJson { #[schemars(with = "LotusJson
")] @@ -49,3 +49,4 @@ impl HasLotusJson for Entry { Entry::default_latest_version(lotus_json.receiver.into(), lotus_json.method_num) } } +crate::test_snapshots!(Entry); diff --git a/src/lotus_json/filter_estimate.rs b/src/lotus_json/filter_estimate.rs index 99bbb7c982cd..d252dcd9f246 100644 --- a/src/lotus_json/filter_estimate.rs +++ b/src/lotus_json/filter_estimate.rs @@ -5,7 +5,7 @@ use super::*; use num::BigInt; use pastey::paste; -#[derive(Debug, Serialize, Deserialize, JsonSchema)] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "PascalCase")] pub struct FilterEstimateLotusJson { #[schemars(with = "LotusJson")] @@ -21,31 +21,39 @@ macro_rules! impl_filter_estimate_lotus_json { ($($version:literal),+) => { $( paste! { - impl HasLotusJson for fil_actors_shared::[]::reward::FilterEstimate { - type LotusJson = FilterEstimateLotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![( - json!({ - "PositionEstimate": "0", - "VelocityEstimate": "0", - }), - Self::default(), - )] + mod [] { + use super::*; + type T = fil_actors_shared::[]::reward::FilterEstimate; + #[test] + fn snapshots() { + crate::lotus_json::assert_all_snapshots::(); } + impl HasLotusJson for T { + type LotusJson = FilterEstimateLotusJson; - fn into_lotus_json(self) -> Self::LotusJson { - FilterEstimateLotusJson { - position_estimate: self.position, - velocity_estimate: self.velocity, + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![( + json!({ + "PositionEstimate": "0", + "VelocityEstimate": "0", + }), + Self::default(), + )] + } + + fn into_lotus_json(self) -> Self::LotusJson { + FilterEstimateLotusJson { + position_estimate: self.position, + velocity_estimate: self.velocity, + } } - } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - Self { - position: lotus_json.position_estimate, - velocity: lotus_json.velocity_estimate, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + Self { + position: lotus_json.position_estimate, + velocity: lotus_json.velocity_estimate, + } } } } diff --git a/src/lotus_json/miner_power.rs b/src/lotus_json/miner_power.rs index 6a4917f54b7b..c015e2dffaf6 100644 --- a/src/lotus_json/miner_power.rs +++ b/src/lotus_json/miner_power.rs @@ -22,7 +22,30 @@ impl HasLotusJson for MinerPower { type LotusJson = MinerPowerLotusJson; #[cfg(test)] fn snapshots() -> Vec<(serde_json::Value, Self)> { - unimplemented!("see commented-out test, below") + vec![( + json!({ + "MinerPower": { + "RawBytePower": "1", + "QualityAdjPower": "2", + }, + "TotalPower": { + "RawBytePower": "3", + "QualityAdjPower": "4", + }, + "HasMinPower": true, + }), + Self { + miner_power: Claim { + raw_byte_power: 1.into(), + quality_adj_power: 2.into(), + }, + total_power: Claim { + raw_byte_power: 3.into(), + quality_adj_power: 4.into(), + }, + has_min_power: true, + }, + )] } fn into_lotus_json(self) -> Self::LotusJson { MinerPowerLotusJson { @@ -39,9 +62,4 @@ impl HasLotusJson for MinerPower { } } } - -// MinerPower: !Debug -// #[test] -// fn snapshots() { -// assert_all_snapshots::(); -// } +crate::test_snapshots!(MinerPower); diff --git a/src/lotus_json/mod.rs b/src/lotus_json/mod.rs index 906f25a87369..fe0062c97bc5 100644 --- a/src/lotus_json/mod.rs +++ b/src/lotus_json/mod.rs @@ -247,7 +247,7 @@ pub use vec::*; #[macro_export] macro_rules! test_snapshots { - ($ty:ident) => { + ($ty:ty) => { pastey::paste! { #[test] fn []() { diff --git a/src/lotus_json/signature.rs b/src/lotus_json/signature.rs index 44ec3d828800..4680676eda74 100644 --- a/src/lotus_json/signature.rs +++ b/src/lotus_json/signature.rs @@ -82,90 +82,106 @@ impl HasLotusJson for Signature { } } } +crate::test_snapshots!(Signature); + +mod signature2 { + use super::*; + use fvm_shared2::crypto::signature::Signature; + impl HasLotusJson for Signature { + type LotusJson = SignatureV2LotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![( + json!({"Type": 2, "Data": "aGVsbG8gd29ybGQh"}), + Self { + sig_type: fvm_shared2::crypto::signature::SignatureType::BLS, + bytes: Vec::from_iter(*b"hello world!"), + }, + )] + } -impl HasLotusJson for fvm_shared2::crypto::signature::Signature { - type LotusJson = SignatureV2LotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![( - json!({"Type": 2, "Data": "aGVsbG8gd29ybGQh"}), - Self { - sig_type: fvm_shared2::crypto::signature::SignatureType::BLS, - bytes: Vec::from_iter(*b"hello world!"), - }, - )] - } - - fn into_lotus_json(self) -> Self::LotusJson { - SignatureV2LotusJson { - r#type: self.sig_type, - data: self.bytes, + fn into_lotus_json(self) -> Self::LotusJson { + SignatureV2LotusJson { + r#type: self.sig_type, + data: self.bytes, + } } - } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - Self { - sig_type: lotus_json.r#type, - bytes: lotus_json.data, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + Self { + sig_type: lotus_json.r#type, + bytes: lotus_json.data, + } } } + crate::test_snapshots!(Signature); } -impl HasLotusJson for fvm_shared3::crypto::signature::Signature { - type LotusJson = SignatureV3LotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![( - json!({"Type": 1, "Data": "aGVsbG8gd29ybGQh"}), - Self { - sig_type: fvm_shared3::crypto::signature::SignatureType::Secp256k1, - bytes: Vec::from_iter(*b"hello world!"), - }, - )] - } +mod signature3 { + use super::*; + use fvm_shared3::crypto::signature::Signature; + impl HasLotusJson for Signature { + type LotusJson = SignatureV3LotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![( + json!({"Type": 1, "Data": "aGVsbG8gd29ybGQh"}), + Self { + sig_type: fvm_shared3::crypto::signature::SignatureType::Secp256k1, + bytes: Vec::from_iter(*b"hello world!"), + }, + )] + } - fn into_lotus_json(self) -> Self::LotusJson { - SignatureV3LotusJson { - r#type: self.sig_type, - data: self.bytes, + fn into_lotus_json(self) -> Self::LotusJson { + SignatureV3LotusJson { + r#type: self.sig_type, + data: self.bytes, + } } - } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - Self { - sig_type: lotus_json.r#type, - bytes: lotus_json.data, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + Self { + sig_type: lotus_json.r#type, + bytes: lotus_json.data, + } } } + crate::test_snapshots!(Signature); } -impl HasLotusJson for fvm_shared4::crypto::signature::Signature { - type LotusJson = SignatureV4LotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![( - json!({"Type": 1, "Data": "aGVsbG8gd29ybGQh"}), - Self { - sig_type: fvm_shared4::crypto::signature::SignatureType::Secp256k1, - bytes: Vec::from_iter(*b"hello world!"), - }, - )] - } +mod signature4 { + use super::*; + use fvm_shared4::crypto::signature::Signature; + impl HasLotusJson for Signature { + type LotusJson = SignatureV4LotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![( + json!({"Type": 1, "Data": "aGVsbG8gd29ybGQh"}), + Self { + sig_type: fvm_shared4::crypto::signature::SignatureType::Secp256k1, + bytes: Vec::from_iter(*b"hello world!"), + }, + )] + } - fn into_lotus_json(self) -> Self::LotusJson { - SignatureV4LotusJson { - r#type: self.sig_type, - data: self.bytes, + fn into_lotus_json(self) -> Self::LotusJson { + SignatureV4LotusJson { + r#type: self.sig_type, + data: self.bytes, + } } - } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - Self { - sig_type: lotus_json.r#type, - bytes: lotus_json.data, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + Self { + sig_type: lotus_json.r#type, + bytes: lotus_json.data, + } } } + crate::test_snapshots!(Signature); } diff --git a/src/lotus_json/signature_type.rs b/src/lotus_json/signature_type.rs index 562770d858a5..9668d853f5a6 100644 --- a/src/lotus_json/signature_type.rs +++ b/src/lotus_json/signature_type.rs @@ -67,77 +67,92 @@ pub enum SignatureTypeV4LotusJson { Integer(#[schemars(with = "u8")] fvm_shared4::crypto::signature::SignatureType), } -impl HasLotusJson for fvm_shared2::crypto::signature::SignatureType { - type LotusJson = SignatureTypeV2LotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![ - ( - json!(1), - fvm_shared2::crypto::signature::SignatureType::Secp256k1, - ), - (json!(2), fvm_shared2::crypto::signature::SignatureType::BLS), - ] - } +mod signature2 { + use super::*; + use fvm_shared2::crypto::signature::SignatureType; + impl HasLotusJson for SignatureType { + type LotusJson = SignatureTypeV2LotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![ + ( + json!(1), + Self::Secp256k1, + ), + (json!(2), Self::BLS), + ] + } - fn into_lotus_json(self) -> Self::LotusJson { - SignatureTypeV2LotusJson::Integer(self) - } + fn into_lotus_json(self) -> Self::LotusJson { + SignatureTypeV2LotusJson::Integer(self) + } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - match lotus_json { - SignatureTypeV2LotusJson::Integer(inner) => inner, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + match lotus_json { + SignatureTypeV2LotusJson::Integer(inner) => inner, + } } } + crate::test_snapshots!(SignatureType); } -impl HasLotusJson for fvm_shared3::crypto::signature::SignatureType { - type LotusJson = SignatureTypeV3LotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![ - ( - json!(1), - fvm_shared3::crypto::signature::SignatureType::Secp256k1, - ), - (json!(2), fvm_shared3::crypto::signature::SignatureType::BLS), - ] - } +mod signature3 { + use super::*; + use fvm_shared3::crypto::signature::SignatureType; + impl HasLotusJson for SignatureType { + type LotusJson = SignatureTypeV3LotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![ + ( + json!(1), + Self::Secp256k1, + ), + (json!(2), Self::BLS), + ] + } - fn into_lotus_json(self) -> Self::LotusJson { - SignatureTypeV3LotusJson::Integer(self) - } + fn into_lotus_json(self) -> Self::LotusJson { + SignatureTypeV3LotusJson::Integer(self) + } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - match lotus_json { - SignatureTypeV3LotusJson::Integer(inner) => inner, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + match lotus_json { + SignatureTypeV3LotusJson::Integer(inner) => inner, + } } } + crate::test_snapshots!(SignatureType); } -impl HasLotusJson for fvm_shared4::crypto::signature::SignatureType { - type LotusJson = SignatureTypeV4LotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![ - ( - json!(1), - fvm_shared4::crypto::signature::SignatureType::Secp256k1, - ), - (json!(2), fvm_shared4::crypto::signature::SignatureType::BLS), - ] - } +mod signature4 { + use super::*; + use fvm_shared4::crypto::signature::SignatureType; + impl HasLotusJson for SignatureType { + type LotusJson = SignatureTypeV4LotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![ + ( + json!(1), + Self::Secp256k1, + ), + (json!(2), Self::BLS), + ] + } - fn into_lotus_json(self) -> Self::LotusJson { - SignatureTypeV4LotusJson::Integer(self) - } + fn into_lotus_json(self) -> Self::LotusJson { + SignatureTypeV4LotusJson::Integer(self) + } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - match lotus_json { - SignatureTypeV4LotusJson::Integer(inner) => inner, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + match lotus_json { + SignatureTypeV4LotusJson::Integer(inner) => inner, + } } } + crate::test_snapshots!(SignatureType); } diff --git a/src/lotus_json/token_state.rs b/src/lotus_json/token_state.rs index 6eaf65cb6140..1dedc2fff1a0 100644 --- a/src/lotus_json/token_state.rs +++ b/src/lotus_json/token_state.rs @@ -4,8 +4,9 @@ use super::*; use crate::shim::econ::TokenAmount; use ::cid::Cid; use fil_actors_shared::frc46_token::token; +use token::state::TokenState; -#[derive(Debug, Serialize, Deserialize, JsonSchema)] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "PascalCase")] #[schemars(rename = "TokenState")] pub struct TokenStateLotusJson { @@ -24,19 +25,19 @@ pub struct TokenStateLotusJson { pub hamt_bit_width: u32, } -impl HasLotusJson for token::state::TokenState { +impl HasLotusJson for TokenState { type LotusJson = TokenStateLotusJson; #[cfg(test)] fn snapshots() -> Vec<(serde_json::Value, Self)> { vec![( json!({ - "supply": "0", - "balances": {"/":"baeaaaaa"}, - "allowances": {"/":"baeaaaaa"}, - "hamt_bit_width": 0 + "Supply": "0", + "Balances": {"/":"baeaaaaa"}, + "Allowances": {"/":"baeaaaaa"}, + "HamtBitWidth": 0 }), - token::state::TokenState { + Self { supply: TokenAmount::default().into(), balances: Cid::default(), allowances: Cid::default(), @@ -63,3 +64,4 @@ impl HasLotusJson for token::state::TokenState { } } } +crate::test_snapshots!(TokenState); diff --git a/src/lotus_json/transient_data.rs b/src/lotus_json/transient_data.rs index 7a762a3f8ab0..77d0b8aafc04 100644 --- a/src/lotus_json/transient_data.rs +++ b/src/lotus_json/transient_data.rs @@ -28,78 +28,94 @@ macro_rules! impl_transient_data_lotus_json { ($($version:literal),+) => { $( paste! { - impl HasLotusJson for fil_actor_evm_state::[]::TransientData { - type LotusJson = TransientDataLotusJson; + mod [] { + use super::*; + type T = fil_actor_evm_state::[]::TransientData; + #[test] + fn snapshots() { + crate::lotus_json::assert_all_snapshots::(); + } + impl HasLotusJson for T { + type LotusJson = TransientDataLotusJson; - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![( - json! {{ - "TransientDataState": {"/":"baeaaaaa"}, - "TransientDataLifespan": { - "Origin": 2, - "Nonce": 3 - } - }}, - Self { - transient_data_state: Cid::default(), - transient_data_lifespan: fil_actor_evm_state::[]::TransientDataLifespan { - origin: 2, - nonce: 3, + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![( + json! {{ + "TransientDataState": {"/":"baeaaaaa"}, + "TransientDataLifespan": { + "Origin": 2, + "Nonce": 3 + } + }}, + Self { + transient_data_state: Cid::default(), + transient_data_lifespan: fil_actor_evm_state::[]::TransientDataLifespan { + origin: 2, + nonce: 3, + }, }, - }, - )] - } + )] + } - fn into_lotus_json(self) -> Self::LotusJson { - TransientDataLotusJson { - transient_data_state: self.transient_data_state, - transient_data_lifespan: TransientDataLifespanLotusJson { - origin: self.transient_data_lifespan.origin, - nonce: self.transient_data_lifespan.nonce, - }, + fn into_lotus_json(self) -> Self::LotusJson { + TransientDataLotusJson { + transient_data_state: self.transient_data_state, + transient_data_lifespan: TransientDataLifespanLotusJson { + origin: self.transient_data_lifespan.origin, + nonce: self.transient_data_lifespan.nonce, + }, + } } - } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - Self { - transient_data_state: lotus_json.transient_data_state, - transient_data_lifespan: fil_actor_evm_state::[]::TransientDataLifespan { - origin: lotus_json.transient_data_lifespan.origin, - nonce: lotus_json.transient_data_lifespan.nonce, - }, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + Self { + transient_data_state: lotus_json.transient_data_state, + transient_data_lifespan: fil_actor_evm_state::[]::TransientDataLifespan { + origin: lotus_json.transient_data_lifespan.origin, + nonce: lotus_json.transient_data_lifespan.nonce, + }, + } } } } - impl HasLotusJson for fil_actor_evm_state::[]::TransientDataLifespan { - type LotusJson = TransientDataLifespanLotusJson; - - #[cfg(test)] - fn snapshots() -> Vec<(serde_json::Value, Self)> { - vec![( - json! {{ - "Origin": 1, - "Nonce": 2 - }}, - Self { - origin: 1, - nonce: 2, - }, - )] + mod [] { + use super::*; + type T = fil_actor_evm_state::[]::TransientDataLifespan; + #[test] + fn snapshots() { + crate::lotus_json::assert_all_snapshots::(); } + impl HasLotusJson for T { + type LotusJson = TransientDataLifespanLotusJson; + + #[cfg(test)] + fn snapshots() -> Vec<(serde_json::Value, Self)> { + vec![( + json! {{ + "Origin": 1, + "Nonce": 2 + }}, + Self { + origin: 1, + nonce: 2, + }, + )] + } - fn into_lotus_json(self) -> Self::LotusJson { - TransientDataLifespanLotusJson { - origin: self.origin, - nonce: self.nonce, + fn into_lotus_json(self) -> Self::LotusJson { + TransientDataLifespanLotusJson { + origin: self.origin, + nonce: self.nonce, + } } - } - fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { - Self { - origin: lotus_json.origin, - nonce: lotus_json.nonce, + fn from_lotus_json(lotus_json: Self::LotusJson) -> Self { + Self { + origin: lotus_json.origin, + nonce: lotus_json.nonce, + } } } }