Debian Codegraph @

third_party/rust/serde_with_macros/tests/apply.rs

raw
1 //! Test Cases
2
3 #![allow(dead_code)]
4
5 use expect_test::expect;
6 use serde_with_macros::apply;
7 use std::collections::BTreeMap;
8
9 /// Fields `a` and `c` match, as each has a fully specified pattern.
10 #[test]
11 fn test_apply_fully_specified() {
12 #[apply(
13 crate="serde_with_macros",
14
15 Option<String> => #[serde(skip)],
16 BTreeMap<String, String> => #[serde(skip)],
17 )]
18 #[derive(Default, serde::Serialize)]
19 struct FooBar<'a> {
20 a: Option<String>,
21 b: Option<i32>,
22 c: BTreeMap<String, String>,
23 d: BTreeMap<String, i32>,
24 e: BTreeMap<i32, String>,
25 f: &'a str,
26 g: &'static str,
27
28 #[serde_with(skip_apply)]
29 skip: Option<String>,
30 }
31
32 expect![[r#"
33 {
34 "b": null,
35 "d": {},
36 "e": {},
37 "f": "",
38 "g": "",
39 "skip": null
40 }"#]]
41 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
42 }
43
44 /// All fields match as `_` matches any type.
45 ///
46 /// The `skip` field is ignored because of the `#[serde_with(skip_apply)]` attribute.
47 #[test]
48 fn test_apply_all() {
49 #[apply(
50 crate="serde_with_macros",
51
52 _ => #[serde(skip)],
53 )]
54 #[derive(Default, serde::Serialize)]
55 struct FooBar<'a> {
56 a: Option<String>,
57 b: Option<i32>,
58 c: BTreeMap<String, String>,
59 d: BTreeMap<String, i32>,
60 e: BTreeMap<i32, String>,
61 f: &'a str,
62 g: &'static str,
63
64 #[serde_with(skip_apply)]
65 skip: Option<String>,
66 }
67
68 expect![[r#"
69 {
70 "skip": null
71 }"#]]
72 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
73 }
74
75 /// Fields `a` and `b` match, since both are variants of `Option`.
76 ///
77 /// No generic in the pattern allows matching with any number of generics on the fields.
78 /// The `skip` field is ignored because of the `#[serde_with(skip_apply)]` attribute.
79 #[test]
80 fn test_apply_partial_no_generic() {
81 #[apply(
82 crate="serde_with_macros",
83
84 Option => #[serde(skip)],
85 )]
86 #[derive(Default, serde::Serialize)]
87 struct FooBar<'a> {
88 a: Option<String>,
89 b: Option<i32>,
90 c: BTreeMap<String, String>,
91 d: BTreeMap<String, i32>,
92 e: BTreeMap<i32, String>,
93 f: &'a str,
94 g: &'static str,
95
96 #[serde_with(skip_apply)]
97 skip: Option<String>,
98 }
99
100 expect![[r#"
101 {
102 "c": {},
103 "d": {},
104 "e": {},
105 "f": "",
106 "g": "",
107 "skip": null
108 }"#]]
109 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
110 }
111
112 /// Fields `c` and `d` match, as both have a `String` key and `_` matches any type.
113 #[test]
114 fn test_apply_partial_generic() {
115 #[apply(
116 crate="serde_with_macros",
117
118 BTreeMap<String, _> => #[serde(skip)],
119 )]
120 #[derive(Default, serde::Serialize)]
121 struct FooBar<'a> {
122 a: Option<String>,
123 b: Option<i32>,
124 c: BTreeMap<String, String>,
125 d: BTreeMap<String, i32>,
126 e: BTreeMap<i32, String>,
127 f: &'a str,
128 g: &'static str,
129
130 #[serde_with(skip_apply)]
131 skip: Option<String>,
132 }
133
134 expect![[r#"
135 {
136 "a": null,
137 "b": null,
138 "e": {},
139 "f": "",
140 "g": "",
141 "skip": null
142 }"#]]
143 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
144 }
145
146 /// Fields `f` and `g` match, since no lifetime matches any reference.
147 #[test]
148 fn test_apply_no_lifetime() {
149 #[apply(
150 crate="serde_with_macros",
151
152 &str => #[serde(skip)],
153 )]
154 #[derive(Default, serde::Serialize)]
155 struct FooBar<'a> {
156 a: Option<String>,
157 b: Option<i32>,
158 c: BTreeMap<String, String>,
159 d: BTreeMap<String, i32>,
160 e: BTreeMap<i32, String>,
161 f: &'a str,
162 g: &'static str,
163
164 #[serde_with(skip_apply)]
165 skip: Option<String>,
166 }
167
168 expect![[r#"
169 {
170 "a": null,
171 "b": null,
172 "c": {},
173 "d": {},
174 "e": {},
175 "skip": null
176 }"#]]
177 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
178 }
179
180 /// Field `f` matches as the lifetime is identical and `mut` is ignored.
181 #[test]
182 fn test_apply_lifetime() {
183 #[apply(
184 crate="serde_with_macros",
185
186 &'a mut str => #[serde(skip)],
187 )]
188 #[derive(Default, serde::Serialize)]
189 struct FooBar<'a> {
190 a: Option<String>,
191 b: Option<i32>,
192 c: BTreeMap<String, String>,
193 d: BTreeMap<String, i32>,
194 e: BTreeMap<i32, String>,
195 f: &'a str,
196 g: &'static str,
197
198 #[serde_with(skip_apply)]
199 skip: Option<String>,
200 }
201
202 expect![[r#"
203 {
204 "a": null,
205 "b": null,
206 "c": {},
207 "d": {},
208 "e": {},
209 "g": "",
210 "skip": null
211 }"#]]
212 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
213 }
214
215 /// No field matches as the explicit lifetimes are different
216 #[test]
217 fn test_apply_mismatched_lifetime() {
218 #[apply(
219 crate="serde_with_macros",
220
221 &'b str => #[serde(skip)],
222 )]
223 #[derive(Default, serde::Serialize)]
224 struct FooBar<'a> {
225 a: Option<String>,
226 b: Option<i32>,
227 c: BTreeMap<String, String>,
228 d: BTreeMap<String, i32>,
229 e: BTreeMap<i32, String>,
230 f: &'a str,
231 g: &'static str,
232
233 #[serde_with(skip_apply)]
234 skip: Option<String>,
235 }
236
237 expect![[r#"
238 {
239 "a": null,
240 "b": null,
241 "c": {},
242 "d": {},
243 "e": {},
244 "f": "",
245 "g": "",
246 "skip": null
247 }"#]]
248 .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
249 }