1# serde\_test   [![Build Status]][actions] [![Latest Version]][crates.io] 2 3[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/test/ci.yml?branch=master 4[actions]: https://github.com/serde-rs/test/actions?query=branch%3Amaster 5[Latest Version]: https://img.shields.io/crates/v/serde_test.svg 6[crates.io]: https://crates.io/crates/serde\_test 7 8This crate provides a convenient concise way to write unit tests for 9implementations of [`Serialize`] and [`Deserialize`]. 10 11[`Serialize`]: serde::ser::Serialize 12[`Deserialize`]: serde::de::Deserialize 13 14The `Serialize` impl for a value can be characterized by the sequence of 15[`Serializer`] calls that are made in the course of serializing the value, so 16`serde_test` provides a [`Token`] abstraction which corresponds roughly to 17`Serializer` method calls. There is an [`assert_ser_tokens`] function to test 18that a value serializes to a particular sequence of method calls, an 19[`assert_de_tokens`] function to test that a value can be deserialized from a 20particular sequence of method calls, and an [`assert_tokens`] function to test 21both directions. There are also functions to test expected failure conditions. 22 23[`Serializer`]: serde::ser::Serializer 24 25Here is an example from the [`linked-hash-map`] crate. 26 27[`linked-hash-map`]: https://github.com/contain-rs/linked-hash-map 28 29```rust 30use linked_hash_map::LinkedHashMap; 31use serde_test::{assert_tokens, Token}; 32 33#[test] 34fn test_ser_de_empty() { 35 let map = LinkedHashMap::<char, u32>::new(); 36 37 assert_tokens( 38 &map, 39 &[ 40 Token::Map { len: Some(0) }, 41 Token::MapEnd, 42 ], 43 ); 44} 45 46#[test] 47fn test_ser_de() { 48 let mut map = LinkedHashMap::new(); 49 map.insert('b', 20); 50 map.insert('a', 10); 51 map.insert('c', 30); 52 53 assert_tokens( 54 &map, 55 &[ 56 Token::Map { len: Some(3) }, 57 Token::Char('b'), 58 Token::I32(20), 59 Token::Char('a'), 60 Token::I32(10), 61 Token::Char('c'), 62 Token::I32(30), 63 Token::MapEnd, 64 ], 65 ); 66} 67``` 68 69<br> 70 71#### License 72 73<sup> 74Licensed under either of <a href="LICENSE-APACHE">Apache License, Version 752.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. 76</sup> 77 78<br> 79 80<sub> 81Unless you explicitly state otherwise, any contribution intentionally submitted 82for inclusion in this crate by you, as defined in the Apache-2.0 license, shall 83be dual licensed as above, without any additional terms or conditions. 84</sub> 85