From 6cbf18819d7ce3005f8e870bb55bfae9f74db3b2 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Mon, 23 Sep 2024 16:08:27 +0200 Subject: [PATCH] Only use proptests --- packages/std/src/serde.rs | 120 +++++++++++++------------------------- 1 file changed, 41 insertions(+), 79 deletions(-) diff --git a/packages/std/src/serde.rs b/packages/std/src/serde.rs index 086922add..3795d6908 100644 --- a/packages/std/src/serde.rs +++ b/packages/std/src/serde.rs @@ -190,91 +190,53 @@ mod tests { ); } - const MINUS_ONE_MSGPACK: &[u8] = &[0xFF]; - const ONE_MSGPACK: &[u8] = &[0x01]; - macro_rules! test_integer { - (signed $ty:ty) => { - ::paste::paste! { - #[test] - fn []() { - let minus_one = $ty::new(-1).unwrap(); - let one = $ty::new(1).unwrap(); - - let serialized = to_json_string(&minus_one).unwrap(); - assert_eq!(serialized, "-1"); - - let serialized = to_json_string(&one).unwrap(); - assert_eq!(serialized, "1"); - - let deserialized: $ty = from_json("-1").unwrap(); - assert_eq!(deserialized, minus_one); - - let deserialized: $ty = from_json("1").unwrap(); - assert_eq!(deserialized, one); - - assert!(from_json::<$ty>("0").is_err()); - - let serialized = to_msgpack_vec(&one).unwrap(); - assert_eq!(serialized, ONE_MSGPACK); - - let serialized = to_msgpack_vec(&minus_one).unwrap(); - assert_eq!(serialized, MINUS_ONE_MSGPACK); - - let deserialized: $ty = from_msgpack(ONE_MSGPACK).unwrap(); - assert_eq!(deserialized, one); - } - } - }; - - (unsigned $ty:ty) => { - ::paste::paste! { - #[property_test] - fn [](input: $ty) { - let primitive = input.get(); - - // Verify that the serialization is the same as the primitive - let serialized = to_json_string(&input).unwrap(); - let serialized_primitive = to_json_string(&primitive).unwrap(); - prop_assert_eq!(serialized.as_str(), serialized_primitive.as_str()); - - // Verify that the serialized primitive can be deserialized - let deserialized: $ty = from_json(serialized_primitive).unwrap(); - assert_eq!(deserialized, input); - - // Verify that zero is not allowed - assert!(from_json::<$ty>("0").is_err()); - - // Verify that the msgpack encoding is the same as the primitive - let serialized = to_msgpack_vec(&input).unwrap(); - let serialized_primitive = to_msgpack_vec(&primitive).unwrap(); - prop_assert_eq!(serialized.as_slice(), serialized_primitive.as_slice()); - - // Verify that the serialized primitive can be deserialized - let deserialized: $ty = from_msgpack(&serialized_primitive).unwrap(); - prop_assert_eq!(deserialized, input); - } - } - }; - - ($($disc:ident $ty:ty),+$(,)?) => { + ($($ty:ty),+$(,)?) => { $( - test_integer!($disc $ty); + ::paste::paste! { + #[property_test] + fn [](input: $ty) { + let primitive = input.get(); + + // Verify that the serialization is the same as the primitive + let serialized = to_json_string(&input).unwrap(); + let serialized_primitive = to_json_string(&primitive).unwrap(); + prop_assert_eq!(serialized.as_str(), serialized_primitive.as_str()); + + // Verify that the serialized primitive can be deserialized + let deserialized: $ty = from_json(serialized_primitive).unwrap(); + assert_eq!(deserialized, input); + + // Verify that zero is not allowed + assert!(from_json::<$ty>("0").is_err()); + + // Verify that the msgpack encoding is the same as the primitive + let serialized = to_msgpack_vec(&input).unwrap(); + let serialized_primitive = to_msgpack_vec(&primitive).unwrap(); + prop_assert_eq!(serialized.as_slice(), serialized_primitive.as_slice()); + + // Verify that the serialized primitive can be deserialized + let deserialized: $ty = from_msgpack(&serialized_primitive).unwrap(); + prop_assert_eq!(deserialized, input); + } + } )+ }; } test_integer! { - unsigned NonZeroU8, - unsigned NonZeroU16, - unsigned NonZeroU32, - unsigned NonZeroU64, - unsigned NonZeroU128, - - signed NonZeroI8, - signed NonZeroI16, - signed NonZeroI32, - signed NonZeroI64, - signed NonZeroI128, + NonZeroU8, + NonZeroU16, + NonZeroU32, + NonZeroU64, + NonZeroU128, + } + + test_integer! { + NonZeroI8, + NonZeroI16, + NonZeroI32, + NonZeroI64, + NonZeroI128, } }