From 14663ff3929e429fcef6bdbaa0a2989097c3c7ce Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 26 Feb 2024 22:15:34 -0600 Subject: [PATCH] proptest: use test-strategy crate to enable formating of tests --- Cargo.toml | 1 + src/types/address.rs | 41 ++++++++++++++++----------------- src/types/crypto/mod.rs | 13 +++++------ src/types/crypto/signature.rs | 29 ++++++++++++----------- src/types/crypto/zklogin.rs | 43 +++++++++++++++++------------------ src/types/digest.rs | 41 ++++++++++++++++----------------- src/types/u256.rs | 43 +++++++++++++++++------------------ 7 files changed, 103 insertions(+), 108 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7dfdf30c..f9f91f042 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,7 @@ num-bigint = "0.4.4" # Pin to this specific commit in order to work around an issue where proptest doesn't build properly in wasm environments # see https://github.com/proptest-rs/proptest/pull/270 for more info proptest = { git = "https://github.com/bmwill/proptest.git", rev = "1a0fe8676aeb61a83f67731404363972c8462424", default-features = false, features = ["std"] } +test-strategy = "0.3.1" proptest-derive = { version = "0.4.0" } [target.wasm32-unknown-unknown.dev-dependencies] diff --git a/src/types/address.rs b/src/types/address.rs index 698abc74d..b01663930 100644 --- a/src/types/address.rs +++ b/src/types/address.rs @@ -177,6 +177,7 @@ impl std::error::Error for AddressParseError {} #[cfg(test)] mod test { use super::*; + use test_strategy::proptest; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test as test; @@ -200,28 +201,26 @@ mod test { println!("{a}"); } - proptest::proptest! { - #[test] - fn roundtrip_display_fromstr(address: Address) { - let s = address.to_string(); - let a = s.parse::
().unwrap(); - assert_eq!(address, a); - } + #[proptest] + fn roundtrip_display_fromstr(address: Address) { + let s = address.to_string(); + let a = s.parse::
().unwrap(); + assert_eq!(address, a); + } - #[test] - #[cfg(feature = "serde")] - fn roundtrip_bcs(address: Address) { - let b = bcs::to_bytes(&address).unwrap(); - let a = bcs::from_bytes(&b).unwrap(); - assert_eq!(address, a); - } + #[proptest] + #[cfg(feature = "serde")] + fn roundtrip_bcs(address: Address) { + let b = bcs::to_bytes(&address).unwrap(); + let a = bcs::from_bytes(&b).unwrap(); + assert_eq!(address, a); + } - #[test] - #[cfg(feature = "serde")] - fn roundtrip_json(address: Address) { - let s = serde_json::to_string(&address).unwrap(); - let a = serde_json::from_str(&s).unwrap(); - assert_eq!(address, a); - } + #[proptest] + #[cfg(feature = "serde")] + fn roundtrip_json(address: Address) { + let s = serde_json::to_string(&address).unwrap(); + let a = serde_json::from_str(&s).unwrap(); + assert_eq!(address, a); } } diff --git a/src/types/crypto/mod.rs b/src/types/crypto/mod.rs index 9797b2880..b0becc88b 100644 --- a/src/types/crypto/mod.rs +++ b/src/types/crypto/mod.rs @@ -100,17 +100,16 @@ macro_rules! impl_base64_helper { mod $test_module { use super::$display; use super::$fromstr; + use test_strategy::proptest; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test as test; - proptest::proptest! { - #[test] - fn roundtrip_display_fromstr(array: $fromstr) { - let s = $display(&array.0).to_string(); - let a = s.parse::<$fromstr>().unwrap(); - assert_eq!(array, a); - } + #[proptest] + fn roundtrip_display_fromstr(array: $fromstr) { + let s = $display(&array.0).to_string(); + let a = s.parse::<$fromstr>().unwrap(); + assert_eq!(array, a); } } }; diff --git a/src/types/crypto/signature.rs b/src/types/crypto/signature.rs index 17f870d40..902378942 100644 --- a/src/types/crypto/signature.rs +++ b/src/types/crypto/signature.rs @@ -467,26 +467,25 @@ mod serialization { use super::*; use base64ct::Base64; use base64ct::Encoding; + use test_strategy::proptest; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test as test; - proptest::proptest! { - #[test] - #[cfg(feature = "serde")] - fn roundtrip_bcs(signature: UserSignature) { - let b = bcs::to_bytes(&signature).unwrap(); - let s = bcs::from_bytes(&b).unwrap(); - assert_eq!(signature, s); - } + #[proptest] + #[cfg(feature = "serde")] + fn roundtrip_bcs(signature: UserSignature) { + let b = bcs::to_bytes(&signature).unwrap(); + let s = bcs::from_bytes(&b).unwrap(); + assert_eq!(signature, s); + } - #[test] - #[cfg(feature = "serde")] - fn roundtrip_json(signature: UserSignature) { - let s = serde_json::to_string(&signature).unwrap(); - let sig = serde_json::from_str(&s).unwrap(); - assert_eq!(signature, sig); - } + #[proptest] + #[cfg(feature = "serde")] + fn roundtrip_json(signature: UserSignature) { + let s = serde_json::to_string(&signature).unwrap(); + let sig = serde_json::from_str(&s).unwrap(); + assert_eq!(signature, sig); } #[test] diff --git a/src/types/crypto/zklogin.rs b/src/types/crypto/zklogin.rs index 2b053d9dd..09cc5f93f 100644 --- a/src/types/crypto/zklogin.rs +++ b/src/types/crypto/zklogin.rs @@ -167,6 +167,7 @@ mod test { use num_bigint::BigUint; use proptest::prelude::*; use std::str::FromStr; + use test_strategy::proptest; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test as test; @@ -182,30 +183,28 @@ mod test { assert_eq!(seed.unpadded(), [1; 31].as_slice()); } - proptest! { - #[test] - fn dont_crash_on_large_inputs( - bytes in proptest::collection::vec(any::(), 33..1024) - ) { - let big_int = BigUint::from_bytes_be(&bytes); - let radix10 = big_int.to_str_radix(10); + #[proptest] + fn dont_crash_on_large_inputs( + #[strategy(proptest::collection::vec(any::(), 33..1024))] bytes: Vec, + ) { + let big_int = BigUint::from_bytes_be(&bytes); + let radix10 = big_int.to_str_radix(10); - // doesn't crash - let _ = AddressSeed::from_str(&radix10); - } + // doesn't crash + let _ = AddressSeed::from_str(&radix10); + } - #[test] - fn valid_address_seeds( - bytes in proptest::collection::vec(any::(), 1..=32) - ) { - let big_int = BigUint::from_bytes_be(&bytes); - let radix10 = big_int.to_str_radix(10); - - let seed = AddressSeed::from_str(&radix10).unwrap(); - assert_eq!(radix10, seed.to_string()); - // Ensure unpadded doesn't crash - seed.unpadded(); - } + #[proptest] + fn valid_address_seeds( + #[strategy(proptest::collection::vec(any::(), 1..=32))] bytes: Vec, + ) { + let big_int = BigUint::from_bytes_be(&bytes); + let radix10 = big_int.to_str_radix(10); + + let seed = AddressSeed::from_str(&radix10).unwrap(); + assert_eq!(radix10, seed.to_string()); + // Ensure unpadded doesn't crash + seed.unpadded(); } } diff --git a/src/types/digest.rs b/src/types/digest.rs index 665785ba0..6728a9e86 100644 --- a/src/types/digest.rs +++ b/src/types/digest.rs @@ -310,32 +310,31 @@ impl_digest!(ObjectDigest); #[cfg(test)] mod test { use super::*; + use test_strategy::proptest; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test as test; - proptest::proptest! { - #[test] - fn roundtrip_display_fromstr(digest: Digest) { - let s = digest.to_string(); - let d = s.parse::().unwrap(); - assert_eq!(digest, d); - } + #[proptest] + fn roundtrip_display_fromstr(digest: Digest) { + let s = digest.to_string(); + let d = s.parse::().unwrap(); + assert_eq!(digest, d); + } - #[test] - #[cfg(feature = "serde")] - fn roundtrip_bcs(digest: Digest) { - let b = bcs::to_bytes(&digest).unwrap(); - let d = bcs::from_bytes(&b).unwrap(); - assert_eq!(digest, d); - } + #[proptest] + #[cfg(feature = "serde")] + fn roundtrip_bcs(digest: Digest) { + let b = bcs::to_bytes(&digest).unwrap(); + let d = bcs::from_bytes(&b).unwrap(); + assert_eq!(digest, d); + } - #[test] - #[cfg(feature = "serde")] - fn roundtrip_json(digest: Digest) { - let s = serde_json::to_string(&digest).unwrap(); - let d = serde_json::from_str(&s).unwrap(); - assert_eq!(digest, d); - } + #[proptest] + #[cfg(feature = "serde")] + fn roundtrip_json(digest: Digest) { + let s = serde_json::to_string(&digest).unwrap(); + let d = serde_json::from_str(&s).unwrap(); + assert_eq!(digest, d); } } diff --git a/src/types/u256.rs b/src/types/u256.rs index b1d4a777a..5a0575c45 100644 --- a/src/types/u256.rs +++ b/src/types/u256.rs @@ -67,6 +67,7 @@ mod test { use num_bigint::BigUint; use proptest::prelude::*; use std::str::FromStr; + use test_strategy::proptest; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test as test; @@ -100,32 +101,30 @@ mod test { assert_eq!(one_platform, U256::from_be(U256::from_digits(one_be))); } - proptest! { - #[test] - fn dont_crash_on_large_inputs( - bytes in proptest::collection::vec(any::(), 33..1024) - ) { - let big_int = BigUint::from_bytes_be(&bytes); - let radix10 = big_int.to_str_radix(10); + #[proptest] + fn dont_crash_on_large_inputs( + #[strategy(proptest::collection::vec(any::(), 33..1024))] bytes: Vec, + ) { + let big_int = BigUint::from_bytes_be(&bytes); + let radix10 = big_int.to_str_radix(10); - // doesn't crash - let _ = U256::from_str_radix(&radix10, 10); - } + // doesn't crash + let _ = U256::from_str_radix(&radix10, 10); + } - #[test] - fn valid_u256_strings( - bytes in proptest::collection::vec(any::(), 1..=32) - ) { - let big_int = BigUint::from_bytes_be(&bytes); - let radix10 = big_int.to_str_radix(10); + #[proptest] + fn valid_u256_strings( + #[strategy(proptest::collection::vec(any::(), 1..=32))] bytes: Vec, + ) { + let big_int = BigUint::from_bytes_be(&bytes); + let radix10 = big_int.to_str_radix(10); - let u256 = U256::from_str_radix(&radix10, 10).unwrap(); + let u256 = U256::from_str_radix(&radix10, 10).unwrap(); - assert_eq!(radix10, u256.to_str_radix(10)); + assert_eq!(radix10, u256.to_str_radix(10)); - let from_str = U256::from_str(&radix10).unwrap(); - assert_eq!(from_str, u256); - assert_eq!(radix10, from_str.to_string()); - } + let from_str = U256::from_str(&radix10).unwrap(); + assert_eq!(from_str, u256); + assert_eq!(radix10, from_str.to_string()); } }