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