From 2b1bb05ca31dcdf2a0dc08f8690595b127b7c827 Mon Sep 17 00:00:00 2001 From: "Mariano A. Nicolini" Date: Tue, 13 Dec 2022 13:02:55 -0300 Subject: [PATCH] tests(chain): add unit tests (#411) * add chain id and display unit tests * add from, into and default implementation tests * add encodable length test for id chain --- crates/primitives/src/chain.rs | 115 +++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/crates/primitives/src/chain.rs b/crates/primitives/src/chain.rs index a88fb7dc44..40a974e903 100644 --- a/crates/primitives/src/chain.rs +++ b/crates/primitives/src/chain.rs @@ -137,3 +137,118 @@ impl Default for Chain { ethers_core::types::Chain::Mainnet.into() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_id() { + let chain = Chain::Id(1234); + assert_eq!(chain.id(), 1234); + } + + #[test] + fn test_named_id() { + let chain = Chain::Named(ethers_core::types::Chain::Goerli); + assert_eq!(chain.id(), 5); + } + + #[test] + fn test_legacy_named_chain() { + let chain = Chain::Named(ethers_core::types::Chain::Optimism); + assert!(chain.is_legacy()); + } + + #[test] + fn test_not_legacy_named_chain() { + let chain = Chain::Named(ethers_core::types::Chain::Mainnet); + assert!(!chain.is_legacy()); + } + + #[test] + fn test_not_legacy_id_chain() { + let chain = Chain::Id(1234); + assert!(!chain.is_legacy()); + } + + #[test] + fn test_display_named_chain() { + let chain = Chain::Named(ethers_core::types::Chain::Mainnet); + assert_eq!(format!("{}", chain), "mainnet"); + } + + #[test] + fn test_display_id_chain() { + let chain = Chain::Id(1234); + assert_eq!(format!("{}", chain), "1234"); + } + + #[test] + fn test_from_u256() { + let n = U256::from(1234); + let chain = Chain::from(n); + let expected = Chain::Id(1234); + + assert_eq!(chain, expected); + } + + #[test] + fn test_into_u256() { + let chain = Chain::Named(ethers_core::types::Chain::Goerli); + let n: U256 = chain.into(); + let expected = U256::from(5); + + assert_eq!(n, expected); + } + + #[test] + #[allow(non_snake_case)] + fn test_into_U64() { + let chain = Chain::Named(ethers_core::types::Chain::Goerli); + let n: U64 = chain.into(); + let expected = U64::from(5); + + assert_eq!(n, expected); + } + + #[test] + fn test_from_str_named_chain() { + let result = Chain::from_str("mainnet"); + let expected = Chain::Named(ethers_core::types::Chain::Mainnet); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_from_str_named_chain_error() { + let result = Chain::from_str("chain"); + + assert!(result.is_err()); + } + + #[test] + fn test_from_str_id_chain() { + let result = Chain::from_str("1234"); + let expected = Chain::Id(1234); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_default() { + let default = Chain::default(); + let expected = Chain::Named(ethers_core::types::Chain::Mainnet); + + assert_eq!(default, expected); + } + + #[test] + fn test_id_chain_encodable_length() { + let chain = Chain::Id(1234); + + assert_eq!(chain.length(), 3); + } +}