Skip to content

Commit

Permalink
proptest: use test-strategy crate to enable formating of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Feb 27, 2024
1 parent b8095e1 commit 14663ff
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 108 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
41 changes: 20 additions & 21 deletions src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::<Address>().unwrap();
assert_eq!(address, a);
}
#[proptest]
fn roundtrip_display_fromstr(address: Address) {
let s = address.to_string();
let a = s.parse::<Address>().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);
}
}
13 changes: 6 additions & 7 deletions src/types/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
29 changes: 14 additions & 15 deletions src/types/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
43 changes: 21 additions & 22 deletions src/types/crypto/zklogin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::<u8>(), 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::<u8>(), 33..1024))] bytes: Vec<u8>,
) {
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::<u8>(), 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::<u8>(), 1..=32))] bytes: Vec<u8>,
) {
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();
}
}

Expand Down
41 changes: 20 additions & 21 deletions src/types/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Digest>().unwrap();
assert_eq!(digest, d);
}
#[proptest]
fn roundtrip_display_fromstr(digest: Digest) {
let s = digest.to_string();
let d = s.parse::<Digest>().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);
}
}
43 changes: 21 additions & 22 deletions src/types/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<u8>(), 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::<u8>(), 33..1024))] bytes: Vec<u8>,
) {
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::<u8>(), 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::<u8>(), 1..=32))] bytes: Vec<u8>,
) {
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());
}
}

0 comments on commit 14663ff

Please sign in to comment.