| 1 |
#![cfg(feature = "serde")] |
| 2 |
|
| 3 |
// SPDX-FileCopyrightText: 2017 - 2022 Kamila Borowska <[email protected]> |
| 4 |
// SPDX-FileCopyrightText: 2022 Cass Fridkin <[email protected]> |
| 5 |
// |
| 6 |
// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 7 |
|
| 8 |
use enum_map::{enum_map, Enum, EnumMap}; |
| 9 |
use serde::{Deserialize, Serialize}; |
| 10 |
use serde_test::{assert_de_tokens_error, assert_tokens, Compact, Configure, Token}; |
| 11 |
|
| 12 |
#[derive(Debug, Enum, Deserialize, Serialize)] |
| 13 |
enum Example { |
| 14 |
A, |
| 15 |
B, |
| 16 |
} |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn serialization() { |
| 20 |
let map = enum_map! { Example::A => 5, Example::B => 10 }; |
| 21 |
assert_tokens( |
| 22 |
&map.readable(), |
| 23 |
&[ |
| 24 |
Token::Map { len: Some(2) }, |
| 25 |
Token::UnitVariant { |
| 26 |
name: "Example", |
| 27 |
variant: "A", |
| 28 |
}, |
| 29 |
Token::I32(5), |
| 30 |
Token::UnitVariant { |
| 31 |
name: "Example", |
| 32 |
variant: "B", |
| 33 |
}, |
| 34 |
Token::I32(10), |
| 35 |
Token::MapEnd, |
| 36 |
], |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn compact_serialization() { |
| 42 |
let map = enum_map! { Example::A => 5, Example::B => 10 }; |
| 43 |
assert_tokens( |
| 44 |
&map.compact(), |
| 45 |
&[ |
| 46 |
Token::Tuple { len: 2 }, |
| 47 |
Token::I32(5), |
| 48 |
Token::I32(10), |
| 49 |
Token::TupleEnd, |
| 50 |
], |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn invalid_compact_deserialization() { |
| 56 |
assert_de_tokens_error::<Compact<EnumMap<bool, bool>>>( |
| 57 |
&[Token::I32(4)], |
| 58 |
"invalid type: integer `4`, expected a sequence", |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn too_short_compact_deserialization() { |
| 64 |
assert_de_tokens_error::<Compact<EnumMap<bool, bool>>>( |
| 65 |
&[Token::Seq { len: None }, Token::Bool(true), Token::SeqEnd], |
| 66 |
"invalid length 1, expected a sequence with as many elements as there are variants", |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
const JSON: &str = r#"{"A":5,"B":10}"#; |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn json_serialization() { |
| 74 |
let map = enum_map! { Example::A => 5, Example::B => 10 }; |
| 75 |
assert_eq!(serde_json::to_string(&map).unwrap(), String::from(JSON)); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn json_deserialization() { |
| 80 |
let example: EnumMap<Example, i32> = serde_json::from_str(JSON).unwrap(); |
| 81 |
assert_eq!(example, enum_map! { Example::A => 5, Example::B => 10 }); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn json_invalid_deserialization() { |
| 86 |
let example: Result<EnumMap<Example, i32>, _> = serde_json::from_str(r"{}"); |
| 87 |
assert!(example.is_err()); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn json_invalid_type() { |
| 92 |
let example: Result<EnumMap<Example, i32>, _> = serde_json::from_str("4"); |
| 93 |
assert!(example.is_err()); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn json_invalid_key() { |
| 98 |
let example: Result<EnumMap<Example, i32>, _> = |
| 99 |
serde_json::from_str(r#"{"a": 5, "b": 10, "c": 6}"#); |
| 100 |
assert!(example.is_err()); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn bincode_serialization() { |
| 105 |
let example = enum_map! { false => 3u8, true => 4u8 }; |
| 106 |
let serialized = bincode::serialize(&example).unwrap(); |
| 107 |
assert_eq!(example, bincode::deserialize(&serialized).unwrap()); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn bincode_too_short_deserialization() { |
| 112 |
assert!( |
| 113 |
bincode::deserialize::<EnumMap<bool, bool>>(&bincode::serialize(&()).unwrap()).is_err() |
| 114 |
); |
| 115 |
} |