serde_assert
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 using a sequence of Token
s representing a generic serialized state.
Usage
The examples below use the claims
crate for convenient assertions.
Testing Serialization
The Serializer
returns a sequence of Token
s representing the serialization of a value. The returned Token
s can be checked to be equal to an expected value.
use assert_ok_eq;
use Serialize;
use ;
let serializer = builder.build;
assert_ok_eq!;
Testing Deserialization
A Deserializer
is constructed by providing a sequence of Token
s to be deserialized into a value.
use assert_ok_eq;
use Deserialize;
use ;
let mut deserializer = builder.build;
assert_ok_eq!;
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 functions, 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
andDeserializer
, allowing use of all parts of theserde
Serializer
andDeserializer
APIs, such as deserializing types that implementDeserializeSeed
. - Customization of
Serializer
s andDeserializer
s, allowing configuration of things like human-readability, whether theDeserializer
should interpret sequences ofToken
s as self-describing, and zero-copy deserialization. - Sophisticated comparison of serialized
Token
sequences, including allowing testing of types whose serialized form can include items in arbitrary order, such as when serializing aHashSet
.
Minimum Supported Rust Version
This crate is guaranteed to compile on stable rustc 1.56.0
and up.
License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.