Skip to content

Commit

Permalink
Only use proptests
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Sep 23, 2024
1 parent 3ccdb76 commit 6cbf188
Showing 1 changed file with 41 additions and 79 deletions.
120 changes: 41 additions & 79 deletions packages/std/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<test_ $ty:snake:lower _encoding>]() {
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 [<test_ $ty:snake:lower _encoding>](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 [<test_ $ty:snake:lower _encoding>](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,
}
}

0 comments on commit 6cbf188

Please sign in to comment.