Giter VIP home page Giter VIP logo

serde_assert's Introduction

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 using a sequence of Tokens representing a generic serialized state.

Usage

The examples below use the claims crate for convenient assertions.

Testing Serialization

The Serializer returns a sequence of 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,
};

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

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

Testing Deserialization

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

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

let mut deserializer = Deserializer::builder([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 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 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 sequences of Tokens 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 a HashSet.

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

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.

serde_assert's People

Contributors

anders429 avatar dependabot[bot] avatar orhun avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

orhun

serde_assert's Issues

`struct`s as sequences

When serializing, especially in compact formats, structs are sometimes serialized as sequences. Deserializing of most structs allows for visiting sequences as well as maps. It would make sense to add configurations to simulate this behavior in tests.

For serializing: Add a configuration setting for the serializer to serialize structs as sequences or structs.

For deserializing: Accept either a struct or a sequence token when deserializing structs.

Disallow equality comparisons between `[Token]` slices.

Allowing comparisons between arrays of tokens may result in unexpected behavior with #22. The equality comparison with Tokens is specialized to do correct comparison, but if the user compares two arrays somehow they may be confused by the comparison not being correct.

Remove need for `Tokens` newtype.

The boilerplate of wrapping all sequences of Tokens in a Tokens type is ugly. I think it can be removed by:

  1. Modifying Deserializer to take a generic token iterator.
  2. Have Serializer still output Tokens, but make it implement PartialEq<Vec<Token>> (and vise-versa).

For the second part, investigation can be done for whether the implementation can be for any generic iterator over tokens for more flexibility. The wrapper will still need to be present for the comparisons, because we can't define a PartialEq on just Vecs, but we can limit the need for the user to have to provide the Tokens everywhere.

Remove build script

The only thing the build script does is check whether 128-bit integers are supported. However, serde stopped supporting conditional compilation based on 128-bit integers with serde-rs/serde#2600, released in version 1.0.187. The macro used here technically still exists, but it is a no-op.

Since this crate doesn't support rustc versions older than 1.40 (and never has), it seems reasonable to remove this from the build script.

Rework `de::Builder::build()` to not panic

Since a call to tokens() is required, the Builder should be reworked to no longer be constructible without the tokens being present (i.e. make Deserializer::builder() take Tokens as a parameter, or something similar).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.