| 1 |
//! Test Cases |
| 2 |
|
| 3 |
extern crate alloc; |
| 4 |
|
| 5 |
mod utils; |
| 6 |
|
| 7 |
use crate::utils::is_equal; |
| 8 |
use alloc::collections::BTreeMap; |
| 9 |
use expect_test::expect; |
| 10 |
use serde::{Deserialize, Serialize}; |
| 11 |
use serde_with::with_suffix; |
| 12 |
use std::collections::HashMap; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_flatten_with_suffix() { |
| 16 |
#[derive(Serialize, Deserialize, PartialEq, Debug)] |
| 17 |
struct Match { |
| 18 |
#[serde(flatten, with = "suffix_player1")] |
| 19 |
player1: Player, |
| 20 |
#[serde(flatten, with = "suffix_player2")] |
| 21 |
player2: Option<Player>, |
| 22 |
#[serde(flatten, with = "suffix_player3")] |
| 23 |
player3: Option<Player>, |
| 24 |
#[serde(flatten, with = "suffix_tag")] |
| 25 |
tags: HashMap<String, String>, |
| 26 |
} |
| 27 |
|
| 28 |
#[derive(Serialize, Deserialize, PartialEq, Debug)] |
| 29 |
struct Player { |
| 30 |
name: String, |
| 31 |
votes: u64, |
| 32 |
} |
| 33 |
|
| 34 |
with_suffix!(suffix_player1 "_player1"); |
| 35 |
with_suffix!(suffix_player2 "_player2"); |
| 36 |
with_suffix!(suffix_player3 "_player3"); |
| 37 |
with_suffix!(suffix_tag "_tag"); |
| 38 |
|
| 39 |
let m = Match { |
| 40 |
player1: Player { |
| 41 |
name: "name1".to_owned(), |
| 42 |
votes: 1, |
| 43 |
}, |
| 44 |
player2: Some(Player { |
| 45 |
name: "name2".to_owned(), |
| 46 |
votes: 2, |
| 47 |
}), |
| 48 |
player3: None, |
| 49 |
tags: HashMap::from_iter(vec![("t".to_owned(), "T".to_owned())]), |
| 50 |
}; |
| 51 |
|
| 52 |
is_equal( |
| 53 |
m, |
| 54 |
expect![[r#" |
| 55 |
{ |
| 56 |
"name_player1": "name1", |
| 57 |
"votes_player1": 1, |
| 58 |
"name_player2": "name2", |
| 59 |
"votes_player2": 2, |
| 60 |
"t_tag": "T" |
| 61 |
}"#]], |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
fn test_plain_with_suffix() { |
| 67 |
#[derive(Serialize, Deserialize, PartialEq, Debug)] |
| 68 |
struct Match { |
| 69 |
#[serde(with = "suffix_player1")] |
| 70 |
player1: Player, |
| 71 |
#[serde(with = "suffix_player2")] |
| 72 |
player2: Option<Player>, |
| 73 |
#[serde(with = "suffix_player3")] |
| 74 |
player3: Option<Player>, |
| 75 |
#[serde(with = "suffix_tag")] |
| 76 |
tags: HashMap<String, String>, |
| 77 |
} |
| 78 |
|
| 79 |
#[derive(Serialize, Deserialize, PartialEq, Debug)] |
| 80 |
struct Player { |
| 81 |
name: String, |
| 82 |
votes: u64, |
| 83 |
} |
| 84 |
|
| 85 |
with_suffix!(suffix_player1 "_player1"); |
| 86 |
with_suffix!(suffix_player2 "_player2"); |
| 87 |
with_suffix!(suffix_player3 "_player3"); |
| 88 |
with_suffix!(suffix_tag "_tag"); |
| 89 |
|
| 90 |
let m = Match { |
| 91 |
player1: Player { |
| 92 |
name: "name1".to_owned(), |
| 93 |
votes: 1, |
| 94 |
}, |
| 95 |
player2: Some(Player { |
| 96 |
name: "name2".to_owned(), |
| 97 |
votes: 2, |
| 98 |
}), |
| 99 |
player3: None, |
| 100 |
tags: HashMap::from_iter(vec![("t".to_owned(), "T".to_owned())]), |
| 101 |
}; |
| 102 |
|
| 103 |
is_equal( |
| 104 |
m, |
| 105 |
expect![[r#" |
| 106 |
{ |
| 107 |
"player1": { |
| 108 |
"name_player1": "name1", |
| 109 |
"votes_player1": 1 |
| 110 |
}, |
| 111 |
"player2": { |
| 112 |
"name_player2": "name2", |
| 113 |
"votes_player2": 2 |
| 114 |
}, |
| 115 |
"player3": null, |
| 116 |
"tags": { |
| 117 |
"t_tag": "T" |
| 118 |
} |
| 119 |
}"#]], |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/// Ensure that `with_suffix` works for unit type enum variants. |
| 124 |
#[test] |
| 125 |
fn test_enum_unit_variant_with_suffix() { |
| 126 |
#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize, Ord, PartialOrd)] |
| 127 |
enum Foo { |
| 128 |
One, |
| 129 |
Two, |
| 130 |
Three, |
| 131 |
} |
| 132 |
|
| 133 |
#[derive(Hash, PartialEq, Eq, Debug, Serialize, Deserialize, Ord, PartialOrd)] |
| 134 |
struct Data { |
| 135 |
stuff: String, |
| 136 |
|
| 137 |
#[serde(flatten, with = "foo")] |
| 138 |
foo: BTreeMap<Foo, i32>, |
| 139 |
} |
| 140 |
with_suffix!(foo "_foo"); |
| 141 |
|
| 142 |
let d = Data { |
| 143 |
stuff: "Stuff".to_owned(), |
| 144 |
foo: BTreeMap::from_iter(vec![(Foo::One, 1), (Foo::Two, 2), (Foo::Three, 3)]), |
| 145 |
}; |
| 146 |
|
| 147 |
is_equal( |
| 148 |
d, |
| 149 |
expect![[r#" |
| 150 |
{ |
| 151 |
"stuff": "Stuff", |
| 152 |
"One_foo": 1, |
| 153 |
"Two_foo": 2, |
| 154 |
"Three_foo": 3 |
| 155 |
}"#]], |
| 156 |
); |
| 157 |
} |