From 6846446b405b9e3879282bc6da19b4f8c22199d6 Mon Sep 17 00:00:00 2001 From: nils-mathieu Date: Tue, 7 Nov 2023 13:47:43 +0100 Subject: [PATCH] add tests for NumAsHex serialization/deserialization routines --- .../starknet-types-rpc/src/custom/block_id.rs | 64 +++++++++++++++ .../src/custom/syncing_status.rs | 23 ++++++ .../src/custom_serde/mod.rs | 5 ++ .../num_as_hex.rs} | 71 +++++++++++++++++ crates/starknet-types-rpc/tests/v0_5_0.rs | 79 ------------------- 5 files changed, 163 insertions(+), 79 deletions(-) create mode 100644 crates/starknet-types-rpc/src/custom_serde/mod.rs rename crates/starknet-types-rpc/src/{custom_serde.rs => custom_serde/num_as_hex.rs} (84%) delete mode 100644 crates/starknet-types-rpc/tests/v0_5_0.rs diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index ba44636..4c933a8 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -63,3 +63,67 @@ impl<'de> serde::Deserialize<'de> for BlockId { } } } + +#[test] +fn block_id_from_hash() { + use crate::Felt; + + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); +} + +#[test] +fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); +} + +#[test] +fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); +} + +#[test] +fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); +} + +#[cfg(test)] +#[test] +fn block_id_to_hash() { + use crate::Felt; + + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); +} + +#[cfg(test)] +#[test] +fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); +} + +#[cfg(test)] +#[test] +fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); +} + +#[cfg(test)] +#[test] +fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); +} diff --git a/crates/starknet-types-rpc/src/custom/syncing_status.rs b/crates/starknet-types-rpc/src/custom/syncing_status.rs index 317732c..2f23e40 100644 --- a/crates/starknet-types-rpc/src/custom/syncing_status.rs +++ b/crates/starknet-types-rpc/src/custom/syncing_status.rs @@ -63,3 +63,26 @@ impl<'de> Deserialize<'de> for SyncingStatus { deserializer.deserialize_any(SyncingStatusVisitor) } } + +#[cfg(test)] +#[test] +fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); +} + +#[cfg(test)] +#[test] +fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); +} + +#[cfg(test)] +#[test] +fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); +} diff --git a/crates/starknet-types-rpc/src/custom_serde/mod.rs b/crates/starknet-types-rpc/src/custom_serde/mod.rs new file mode 100644 index 0000000..69302c4 --- /dev/null +++ b/crates/starknet-types-rpc/src/custom_serde/mod.rs @@ -0,0 +1,5 @@ +//! Custom serialization and deserialization routines. + +mod num_as_hex; + +pub use self::num_as_hex::NumAsHex; diff --git a/crates/starknet-types-rpc/src/custom_serde.rs b/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs similarity index 84% rename from crates/starknet-types-rpc/src/custom_serde.rs rename to crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs index 5ce1455..9dec560 100644 --- a/crates/starknet-types-rpc/src/custom_serde.rs +++ b/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs @@ -196,3 +196,74 @@ where deserializer.deserialize_option(OptionVisitor(PhantomData)) } } + +#[cfg(test)] +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(transparent)] +struct Helper { + #[serde(with = "NumAsHex")] + num: u64, +} + +#[cfg(test)] +fn serialize(num: u64) -> serde_json::Result { + let helper = Helper { num }; + serde_json::to_string(&helper) +} + +#[cfg(test)] +fn deserialize(s: &str) -> serde_json::Result { + let helper: Helper = serde_json::from_str(s)?; + Ok(helper.num) +} + +#[test] +#[cfg(test)] +fn serialize_0_hex() { + assert_eq!(serialize(0x0).unwrap(), "\"0x0\""); +} + +#[test] +#[cfg(test)] +fn srialize_hex() { + assert_eq!(serialize(0x1234).unwrap(), "\"0x1234\""); +} + +#[test] +#[cfg(test)] +fn srialize_max() { + assert_eq!(serialize(u64::MAX).unwrap(), "\"0xffffffffffffffff\""); +} + +#[test] +#[cfg(test)] +fn deserialize_zero() { + assert_eq!(deserialize("\"0x0\"").unwrap(), 0); +} + +#[test] +#[cfg(test)] +fn deserialize_zeros() { + assert_eq!(deserialize("\"0x00000\"").unwrap(), 0); +} + +#[test] +#[cfg(test)] +fn deserialize_max() { + assert_eq!(deserialize("\"0xFFFFFFFFFFFFFFFF\"").unwrap(), u64::MAX); +} + +#[test] +#[cfg(test)] +fn deserialize_big_one() { + assert_eq!( + deserialize("\"0x000000000000000000000000000001\"").unwrap(), + 1 + ); +} + +#[test] +#[cfg(test)] +fn deserialize_hex() { + assert_eq!(deserialize("\"0x1234\"").unwrap(), 0x1234); +} diff --git a/crates/starknet-types-rpc/tests/v0_5_0.rs b/crates/starknet-types-rpc/tests/v0_5_0.rs deleted file mode 100644 index bcdffbb..0000000 --- a/crates/starknet-types-rpc/tests/v0_5_0.rs +++ /dev/null @@ -1,79 +0,0 @@ -//! Integeration tests for the 0.5.0 version of the StarkNet RPC API Specification. - -use starknet_types_rpc::{BlockId, BlockTag, Felt, SyncingStatus}; - -#[test] -fn block_id_from_hash() { - let s = "{\"block_hash\":\"0x123\"}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); -} - -#[test] -fn block_id_from_number() { - let s = "{\"block_number\":123}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Number(123)); -} - -#[test] -fn block_id_from_latest() { - let s = "\"latest\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); -} - -#[test] -fn block_id_from_pending() { - let s = "\"pending\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); -} - -#[test] -fn block_id_to_hash() { - let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_hash\":\"0x123\"}"); -} - -#[test] -fn block_id_to_number() { - let block_id = BlockId::Number(123); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_number\":123}"); -} - -#[test] -fn block_id_to_latest() { - let block_id = BlockId::Tag(BlockTag::Latest); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"latest\""); -} - -#[test] -fn block_id_to_pending() { - let block_id = BlockId::Tag(BlockTag::Pending); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"pending\""); -} - -#[test] -fn syncing_status_from_false() { - let s = "false"; - let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); - assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); -} - -#[test] -fn syncing_status_to_false() { - let syncing_status = SyncingStatus::NotSyncing; - let s = serde_json::to_string(&syncing_status).unwrap(); - assert_eq!(s, "false"); -} - -#[test] -fn syncing_status_from_true() { - let s = "true"; - assert!(serde_json::from_str::(s).is_err()); -}