#serde #testing #serialization #deserialization #test

no-std serde_assert

Testing library for serde Serialize and Deserialize implementations

5 releases (breaking)

0.5.0 May 17, 2023
0.4.0 Apr 6, 2023
0.3.0 Apr 6, 2023
0.2.0 Jan 17, 2023
0.1.0 Jan 14, 2023

#288 in Encoding

Download history 6/week @ 2023-02-11 41/week @ 2023-02-18 7/week @ 2023-02-25 4/week @ 2023-03-04 89/week @ 2023-03-11 1047/week @ 2023-03-18 608/week @ 2023-03-25 903/week @ 2023-04-01 273/week @ 2023-04-08 656/week @ 2023-04-15 243/week @ 2023-04-22 112/week @ 2023-04-29 18/week @ 2023-05-06 95/week @ 2023-05-13 22/week @ 2023-05-20 98/week @ 2023-05-27

233 downloads per month
Used in brood

MIT/Apache

290KB
7K SLoC

serde_assert

GitHub Workflow Status codecov.io crates.io docs.rs MSRV License

Testing library for serde Serialize and Deserialize implementations.

This library provides a Serializer and Deserializer to be used in writing unit tests to assert the behavior of manual Serialize and Deserialize implementations, respectively. The implementation behavior can be verified by using Tokens representing an arbitrary serialized state.

Usage

The examples below use the claims crate for convenient assertions.

Testing Serialization

The Serializer returns Tokens representing the serialization of a value. The returned Tokens can be checked to be equal to an expected value.

use claims::assert_ok_eq;
use serde::Serialize;
use serde_assert::{
    Serializer,
    Token,
    Tokens,
};

let serializer = Serializer::builder().build();

assert_ok_eq!(true.serialize(&serializer), Tokens(vec![Token::Bool(true)]));

Testing Deserialization

A Deserializer is constructed by providing Tokens to be deserialized into a value.

use claims::assert_ok_eq;
use serde::Deserialize;
use serde_assert::{
    Deserializer,
    Token,
    Tokens,
};

let mut deserializer = Deserializer::builder()
    .tokens(Tokens(vec![Token::Bool(true)]))
    .build();

assert_ok_eq!(bool::deserialize(&mut deserializer), true);

Comparison with serde_test

This crate provides more flexibility than serde_test at the expense of more verbosity. While serde_test provides a small API of simple assertion macros, this crate will require you to call serialize() and deserialize() and assert yourself that the results are as expected.

While some users may find that the smaller API of serde_test is sufficient for their use-case, others will find that the flexibility of this crate makes testing more complicated Serailize and Deserialize implementations easier. Among other things, this crate's API provides these advantages:

  • Direct access to the Serializer and Deserializer, allowing use of all parts of the serde Serializer and Deserializer APIs, such as deserializing types that implement DeserializeSeed.
  • Customization of Serializers and Deserializers, allowing configuration of things like human-readability, whether the Deserializer should interpret Tokens as self-describing, and zero-copy deserialization.
  • Sophisticated comparison of serialized Tokens, including allowing testing of types whose serialized form can include items in arbitrary order, such as when serializing a HashSet.

Minimum Supported Rust Version

This crate is guaranteed to compile on stable rustc 1.61.0 and up.

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1MB
~17K SLoC