1 #![allow(
2 clippy::decimal_literal_representation,
3 clippy::unreadable_literal,
4 clippy::shadow_unrelated
5 )]
6
7 use indoc::indoc;
8 use serde_derive::{Deserialize, Serialize};
9 use serde_yaml::Value;
10 use std::collections::BTreeMap;
11 use std::f64;
12 use std::fmt::Debug;
13
test_serde<T>(thing: &T, yaml: &str) where T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug,14 fn test_serde<T>(thing: &T, yaml: &str)
15 where
16 T: serde::Serialize + serde::de::DeserializeOwned + PartialEq + Debug,
17 {
18 let serialized = serde_yaml::to_string(&thing).unwrap();
19 assert_eq!(yaml, serialized);
20
21 let value = serde_yaml::to_value(&thing).unwrap();
22 let serialized = serde_yaml::to_string(&value).unwrap();
23 assert_eq!(yaml, serialized);
24
25 let deserialized: T = serde_yaml::from_str(yaml).unwrap();
26 assert_eq!(*thing, deserialized);
27
28 let value: Value = serde_yaml::from_str(yaml).unwrap();
29 let deserialized: T = serde_yaml::from_value(value).unwrap();
30 assert_eq!(*thing, deserialized);
31
32 serde_yaml::from_str::<serde::de::IgnoredAny>(yaml).unwrap();
33 }
34
35 #[test]
test_default()36 fn test_default() {
37 assert_eq!(Value::default(), Value::Null);
38 }
39
40 #[test]
test_int()41 fn test_int() {
42 let thing = 256;
43 let yaml = indoc! {"
44 ---
45 256
46 "};
47 test_serde(&thing, yaml);
48 }
49
50 #[test]
test_int_max_u64()51 fn test_int_max_u64() {
52 let thing = ::std::u64::MAX;
53 let yaml = indoc! {"
54 ---
55 18446744073709551615
56 "};
57 test_serde(&thing, yaml);
58 }
59
60 #[test]
test_int_min_i64()61 fn test_int_min_i64() {
62 let thing = ::std::i64::MIN;
63 let yaml = indoc! {"
64 ---
65 -9223372036854775808
66 "};
67 test_serde(&thing, yaml);
68 }
69
70 #[test]
test_int_max_i64()71 fn test_int_max_i64() {
72 let thing = ::std::i64::MAX;
73 let yaml = indoc! {"
74 ---
75 9223372036854775807
76 "};
77 test_serde(&thing, yaml);
78 }
79
80 #[test]
test_i128_small()81 fn test_i128_small() {
82 let thing: i128 = -256;
83 let yaml = indoc! {"
84 ---
85 -256
86 "};
87 test_serde(&thing, yaml);
88 }
89
90 #[test]
test_u128_small()91 fn test_u128_small() {
92 let thing: u128 = 256;
93 let yaml = indoc! {"
94 ---
95 256
96 "};
97 test_serde(&thing, yaml);
98 }
99
100 #[test]
test_float()101 fn test_float() {
102 let thing = 25.6;
103 let yaml = indoc! {"
104 ---
105 25.6
106 "};
107 test_serde(&thing, yaml);
108
109 let thing = 25.;
110 let yaml = indoc! {"
111 ---
112 25.0
113 "};
114 test_serde(&thing, yaml);
115
116 let thing = f64::INFINITY;
117 let yaml = indoc! {"
118 ---
119 .inf
120 "};
121 test_serde(&thing, yaml);
122
123 let thing = f64::NEG_INFINITY;
124 let yaml = indoc! {"
125 ---
126 -.inf
127 "};
128 test_serde(&thing, yaml);
129
130 let float: f64 = serde_yaml::from_str(indoc! {"
131 ---
132 .nan
133 "})
134 .unwrap();
135 assert!(float.is_nan());
136 }
137
138 #[test]
test_float32()139 fn test_float32() {
140 let thing: f32 = 25.6;
141 let yaml = indoc! {"
142 ---
143 25.6
144 "};
145 test_serde(&thing, yaml);
146
147 let thing = f32::INFINITY;
148 let yaml = indoc! {"
149 ---
150 .inf
151 "};
152 test_serde(&thing, yaml);
153
154 let thing = f32::NEG_INFINITY;
155 let yaml = indoc! {"
156 ---
157 -.inf
158 "};
159 test_serde(&thing, yaml);
160
161 let single_float: f32 = serde_yaml::from_str(indoc! {"
162 ---
163 .nan
164 "})
165 .unwrap();
166 assert!(single_float.is_nan());
167
168 }
169
170 #[test]
test_vec()171 fn test_vec() {
172 let thing = vec![1, 2, 3];
173 let yaml = indoc! {"
174 ---
175 - 1
176 - 2
177 - 3
178 "};
179 test_serde(&thing, yaml);
180 }
181
182 #[test]
test_map()183 fn test_map() {
184 let mut thing = BTreeMap::new();
185 thing.insert(String::from("x"), 1);
186 thing.insert(String::from("y"), 2);
187 let yaml = indoc! {"
188 ---
189 x: 1
190 y: 2
191 "};
192 test_serde(&thing, yaml);
193 }
194
195 #[test]
test_basic_struct()196 fn test_basic_struct() {
197 #[derive(Serialize, Deserialize, PartialEq, Debug)]
198 struct Basic {
199 x: isize,
200 y: String,
201 z: bool,
202 }
203 let thing = Basic {
204 x: -4,
205 y: String::from("hi\tquoted"),
206 z: true,
207 };
208 let yaml = indoc! {r#"
209 ---
210 x: -4
211 y: "hi\tquoted"
212 z: true
213 "#};
214 test_serde(&thing, yaml);
215 }
216
217 #[test]
test_nested_vec()218 fn test_nested_vec() {
219 let thing = vec![vec![1, 2, 3], vec![4, 5, 6]];
220 let yaml = indoc! {"
221 ---
222 - - 1
223 - 2
224 - 3
225 - - 4
226 - 5
227 - 6
228 "};
229 test_serde(&thing, yaml);
230 }
231
232 #[test]
test_nested_struct()233 fn test_nested_struct() {
234 #[derive(Serialize, Deserialize, PartialEq, Debug)]
235 struct Outer {
236 inner: Inner,
237 }
238 #[derive(Serialize, Deserialize, PartialEq, Debug)]
239 struct Inner {
240 v: u16,
241 }
242 let thing = Outer {
243 inner: Inner { v: 512 },
244 };
245 let yaml = indoc! {"
246 ---
247 inner:
248 v: 512
249 "};
250 test_serde(&thing, yaml);
251 }
252
253 #[test]
test_option()254 fn test_option() {
255 let thing = vec![Some(1), None, Some(3)];
256 let yaml = indoc! {"
257 ---
258 - 1
259 - ~
260 - 3
261 "};
262 test_serde(&thing, yaml);
263 }
264
265 #[test]
test_unit()266 fn test_unit() {
267 let thing = vec![(), ()];
268 let yaml = indoc! {"
269 ---
270 - ~
271 - ~
272 "};
273 test_serde(&thing, yaml);
274 }
275
276 #[test]
test_unit_variant()277 fn test_unit_variant() {
278 #[derive(Serialize, Deserialize, PartialEq, Debug)]
279 enum Variant {
280 First,
281 Second,
282 }
283 let thing = Variant::First;
284 let yaml = indoc! {"
285 ---
286 First
287 "};
288 test_serde(&thing, yaml);
289 }
290
291 #[test]
test_newtype_struct()292 fn test_newtype_struct() {
293 #[derive(Serialize, Deserialize, PartialEq, Debug)]
294 struct OriginalType {
295 v: u16,
296 }
297 #[derive(Serialize, Deserialize, PartialEq, Debug)]
298 struct NewType(OriginalType);
299 let thing = NewType(OriginalType { v: 1 });
300 let yaml = indoc! {"
301 ---
302 v: 1
303 "};
304 test_serde(&thing, yaml);
305 }
306
307 #[test]
test_newtype_variant()308 fn test_newtype_variant() {
309 #[derive(Serialize, Deserialize, PartialEq, Debug)]
310 enum Variant {
311 Size(usize),
312 }
313 let thing = Variant::Size(127);
314 let yaml = indoc! {"
315 ---
316 Size: 127
317 "};
318 test_serde(&thing, yaml);
319 }
320
321 #[test]
test_tuple_variant()322 fn test_tuple_variant() {
323 #[derive(Serialize, Deserialize, PartialEq, Debug)]
324 enum Variant {
325 Rgb(u8, u8, u8),
326 }
327 let thing = Variant::Rgb(32, 64, 96);
328 let yaml = indoc! {"
329 ---
330 Rgb:
331 - 32
332 - 64
333 - 96
334 "};
335 test_serde(&thing, yaml);
336 }
337
338 #[test]
test_struct_variant()339 fn test_struct_variant() {
340 #[derive(Serialize, Deserialize, PartialEq, Debug)]
341 enum Variant {
342 Color { r: u8, g: u8, b: u8 },
343 }
344 let thing = Variant::Color {
345 r: 32,
346 g: 64,
347 b: 96,
348 };
349 let yaml = indoc! {"
350 ---
351 Color:
352 r: 32
353 g: 64
354 b: 96
355 "};
356 test_serde(&thing, yaml);
357 }
358
359 #[test]
test_value()360 fn test_value() {
361 use serde_yaml::{Mapping, Number};
362
363 #[derive(Serialize, Deserialize, PartialEq, Debug)]
364 pub struct GenericInstructions {
365 #[serde(rename = "type")]
366 pub typ: String,
367 pub config: Value,
368 }
369 let thing = GenericInstructions {
370 typ: "primary".to_string(),
371 config: Value::Sequence(vec![
372 Value::Null,
373 Value::Bool(true),
374 Value::Number(Number::from(65535)),
375 Value::Number(Number::from(0.54321)),
376 Value::String("s".into()),
377 Value::Mapping(Mapping::new()),
378 ]),
379 };
380 let yaml = indoc! {"
381 ---
382 type: primary
383 config:
384 - ~
385 - true
386 - 65535
387 - 0.54321
388 - s
389 - {}
390 "};
391 test_serde(&thing, yaml);
392 }
393
394 #[test]
test_mapping()395 fn test_mapping() {
396 use serde_yaml::Mapping;
397 #[derive(Serialize, Deserialize, PartialEq, Debug)]
398 struct Data {
399 pub substructure: Mapping,
400 }
401
402 let mut thing = Data {
403 substructure: Mapping::new(),
404 };
405 thing.substructure.insert(
406 Value::String("a".to_owned()),
407 Value::String("foo".to_owned()),
408 );
409 thing.substructure.insert(
410 Value::String("b".to_owned()),
411 Value::String("bar".to_owned()),
412 );
413
414 let yaml = indoc! {"
415 ---
416 substructure:
417 a: foo
418 b: bar
419 "};
420
421 test_serde(&thing, yaml);
422 }
423