Skip to main content

serde_test

Attribute Macro serde_test 

Source
#[serde_test]
Expand description

This macro is used to generate round-trip serialization tests.

By appending serde_test to a struct or enum definition, you automatically derive serialization tests that employ Serde for round-trip testing. The procedure in the generated tests is:

  1. Instantiate the type being tested
  2. Serialize the instance, ensuring the operation’s success
  3. Deserialize the serialized data, comparing the resulting instance with the original one

The type being tested must meet the following requirements:

  • Implementations of Debug and PartialEq traits
  • Implementation of Arbitrary trait
  • Implementations of Serialize and DeserializeOwned traits

When using the binary_serde annotation, the type must also implement the Serializable and Deserializable traits from miden-crypto (which provide to_bytes() and read_from_bytes() methods).

§Configuration Attributes

The macro supports configuration attributes to control test generation:

AttributeTypeDefaultPurposeFeatures Required
serde_testbooltrueGenerate standard Serde (JSON) round-trip testsarbitrary, serde, test
binary_serdeboolfalseGenerate binary serialization round-trip testsarbitrary, test
types(...)-noneSpecify type parameters for generics-

§Usage Examples

Default (Serde tests only):

#[serde_test]
#[derive(Debug, PartialEq, Arbitrary, Serialize, Deserialize)]
struct Simple {
    value: u64,
}

Binary serialization tests only:

#[serde_test(binary_serde(true), serde_test(false))]
#[derive(Debug, PartialEq, Arbitrary)]
struct BinaryTest {
    data: [u8; 32],
}

Both test types:

#[serde_test(binary_serde(true))]
#[derive(Debug, PartialEq, Arbitrary, Serialize, Deserialize)]
struct DualTest {
    name: u32,
    value: u64,
}

Generic types:

#[serde_test(types(u64, "Vec<u64>"), types(u32, bool))]
#[derive(Debug, PartialEq, Arbitrary, Serialize, Deserialize)]
struct Generic<T1, T2> {
    t1: T1,
    t2: T2,
}

§Generated Test Names

  • Serde tests: test_serde_roundtrip_{struct_name}_{index}
  • Binary tests: test_binary_serde_roundtrip_{struct_name}_{index}