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.
The examples below use the claims
crate for convenient assertions.
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::assertokeq; use serde::Serialize; use serde_assert::{ Serializer, Token, Tokens, };
let serializer = Serializer::builder().build();
assertokeq!(true.serialize(&serializer), Tokens(vec![Token::Bool(true)])); ```
A Deserializer
is constructed by providing Tokens
to be deserialized into a value.
``` use claims::assertokeq; use serde::Deserialize; use serde_assert::{ Deserializer, Token, Tokens, };
let mut deserializer = Deserializer::builder() .tokens(Tokens(vec![Token::Bool(true)])) .build();
assertokeq!(bool::deserialize(&mut deserializer), true); ```
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:
Serializer
and Deserializer
, allowing use of all parts of the serde
Serializer
and Deserializer
APIs, such as deserializing types that implement DeserializeSeed
.Serializer
s and Deserializer
s, allowing configuration of things like human-readability or whether the Deserializer
should interpret Tokens
as self-describing.Tokens
, including allowing testing of types whose serialized form can include items in arbitrary order, such as when serializing a HashSet
.This crate is guaranteed to compile on stable rustc 1.61.0
and up.
This project is licensed under either of
at your option.
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.