Skip to content

Commit

Permalink
add tests for NumAsHex serialization/deserialization routines
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-mathieu committed Nov 7, 2023
1 parent 1a0980e commit 6846446
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 79 deletions.
64 changes: 64 additions & 0 deletions crates/starknet-types-rpc/src/custom/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\"");
}
23 changes: 23 additions & 0 deletions crates/starknet-types-rpc/src/custom/syncing_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<SyncingStatus>(s).is_err());
}
5 changes: 5 additions & 0 deletions crates/starknet-types-rpc/src/custom_serde/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Custom serialization and deserialization routines.
mod num_as_hex;

pub use self::num_as_hex::NumAsHex;
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let helper = Helper { num };
serde_json::to_string(&helper)
}

#[cfg(test)]
fn deserialize(s: &str) -> serde_json::Result<u64> {
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);
}
79 changes: 0 additions & 79 deletions crates/starknet-types-rpc/tests/v0_5_0.rs

This file was deleted.

0 comments on commit 6846446

Please sign in to comment.