1 use serde_derive::Deserialize;
2 use std::fmt::Debug;
3 use unindent::unindent;
4
test_error<T>(yaml: &str, expected: &str) where T: serde::de::DeserializeOwned + Debug,5 fn test_error<T>(yaml: &str, expected: &str)
6 where
7 T: serde::de::DeserializeOwned + Debug,
8 {
9 let result = serde_yaml::from_str::<T>(yaml);
10 assert_eq!(expected, format!("{}", result.unwrap_err()));
11 }
12
13 #[test]
test_incorrect_type()14 fn test_incorrect_type() {
15 let yaml = unindent(
16 "
17 ---
18 str",
19 );
20 let expected = "invalid type: string \"str\", expected i16 at line 2 column 1";
21 test_error::<i16>(&yaml, expected);
22 }
23
24 #[test]
test_incorrect_nested_type()25 fn test_incorrect_nested_type() {
26 #[derive(Deserialize, Debug)]
27 struct A {
28 b: Vec<B>,
29 }
30 #[derive(Deserialize, Debug)]
31 enum B {
32 C(C),
33 }
34 #[derive(Deserialize, Debug)]
35 struct C {
36 d: bool,
37 }
38 let yaml = unindent(
39 "
40 ---
41 b:
42 - C:
43 d: fase",
44 );
45 let expected =
46 "b[0].C.d: invalid type: string \"fase\", expected a boolean at line 4 column 10";
47 test_error::<A>(&yaml, expected);
48 }
49
50 #[test]
test_empty()51 fn test_empty() {
52 let expected = "EOF while parsing a value";
53 test_error::<String>("", expected);
54 }
55
56 #[test]
test_missing_field()57 fn test_missing_field() {
58 #[derive(Deserialize, Debug)]
59 struct Basic {
60 v: bool,
61 w: bool,
62 }
63 let yaml = unindent(
64 "
65 ---
66 v: true",
67 );
68 let expected = "missing field `w` at line 2 column 2";
69 test_error::<Basic>(&yaml, expected);
70 }
71
72 #[test]
test_unknown_anchor()73 fn test_unknown_anchor() {
74 let yaml = unindent(
75 "
76 ---
77 *some",
78 );
79 let expected = "while parsing node, found unknown anchor at line 2 column 1";
80 test_error::<String>(&yaml, expected);
81 }
82
83 #[test]
test_ignored_unknown_anchor()84 fn test_ignored_unknown_anchor() {
85 #[derive(Deserialize, Debug)]
86 struct Wrapper {
87 c: (),
88 }
89 let yaml = unindent(
90 "
91 ---
92 b: [*a]
93 c: ~",
94 );
95 let expected = "while parsing node, found unknown anchor at line 2 column 5";
96 test_error::<Wrapper>(&yaml, expected);
97 }
98
99 #[test]
test_two_documents()100 fn test_two_documents() {
101 let yaml = unindent(
102 "
103 ---
104 0
105 ---
106 1",
107 );
108 let expected = "deserializing from YAML containing more than one document is not supported";
109 test_error::<usize>(&yaml, expected);
110 }
111
112 #[test]
test_variant_map_wrong_size()113 fn test_variant_map_wrong_size() {
114 #[derive(Deserialize, Debug)]
115 enum E {
116 V(usize),
117 }
118 let yaml = unindent(
119 r#"
120 ---
121 "V": 16
122 "other": 32"#,
123 );
124 let expected = "invalid length 2, expected map containing 1 entry";
125 test_error::<E>(&yaml, expected);
126 }
127
128 #[test]
test_variant_not_a_map()129 fn test_variant_not_a_map() {
130 #[derive(Deserialize, Debug)]
131 enum E {
132 V(usize),
133 }
134 let yaml = unindent(
135 r#"
136 ---
137 - "V""#,
138 );
139 let expected = "invalid type: sequence, expected string or singleton map at line 2 column 1";
140 test_error::<E>(&yaml, expected);
141 }
142
143 #[test]
test_variant_not_string()144 fn test_variant_not_string() {
145 #[derive(Deserialize, Debug)]
146 enum E {
147 V(bool),
148 }
149 let yaml = unindent(
150 r#"
151 ---
152 {}: true"#,
153 );
154 let expected = "invalid type: map, expected variant of enum `E` at line 2 column 1";
155 test_error::<E>(&yaml, expected);
156 }
157
158 #[test]
test_bad_bool()159 fn test_bad_bool() {
160 let yaml = unindent(
161 "
162 ---
163 !!bool str",
164 );
165 let expected = "invalid value: string \"str\", expected a boolean at line 2 column 8";
166 test_error::<bool>(&yaml, expected);
167 }
168
169 #[test]
test_bad_int()170 fn test_bad_int() {
171 let yaml = unindent(
172 "
173 ---
174 !!int str",
175 );
176 let expected = "invalid value: string \"str\", expected an integer at line 2 column 7";
177 test_error::<i64>(&yaml, expected);
178 }
179
180 #[test]
test_bad_float()181 fn test_bad_float() {
182 let yaml = unindent(
183 "
184 ---
185 !!float str",
186 );
187 let expected = "invalid value: string \"str\", expected a float at line 2 column 9";
188 test_error::<f64>(&yaml, expected);
189 }
190
191 #[test]
test_bad_null()192 fn test_bad_null() {
193 let yaml = unindent(
194 "
195 ---
196 !!null str",
197 );
198 let expected = "invalid value: string \"str\", expected null at line 2 column 8";
199 test_error::<()>(&yaml, expected);
200 }
201
202 #[test]
test_short_tuple()203 fn test_short_tuple() {
204 let yaml = unindent(
205 "
206 ---
207 [0, 0]",
208 );
209 let expected = "invalid length 2, expected a tuple of size 3 at line 2 column 1";
210 test_error::<(u8, u8, u8)>(&yaml, expected);
211 }
212
213 #[test]
test_long_tuple()214 fn test_long_tuple() {
215 let yaml = unindent(
216 "
217 ---
218 [0, 0, 0]",
219 );
220 let expected = "invalid length 3, expected sequence of 2 elements at line 2 column 1";
221 test_error::<(u8, u8)>(&yaml, expected);
222 }
223
224 #[test]
test_no_location()225 fn test_no_location() {
226 let invalid_utf8: Result<serde_yaml::Value, serde_yaml::Error> =
227 serde_yaml::from_slice(b"\x80\xae");
228
229 let utf8_location = invalid_utf8.unwrap_err().location();
230
231 assert_eq!(utf8_location.is_none(), true);
232 }
233
234 #[test]
test_invalid_scalar_type()235 fn test_invalid_scalar_type() {
236 #[derive(Deserialize, Debug)]
237 struct S {
238 x: [(); 1],
239 }
240
241 let yaml = "x:\n";
242 let expected = "x: invalid type: unit value, expected an array of length 1 at line 2 column 1";
243 test_error::<S>(yaml, expected);
244 }
245
246 #[test]
test_infinite_recursion_objects()247 fn test_infinite_recursion_objects() {
248 #[derive(Deserialize, Debug)]
249 struct S {
250 x: Option<Box<S>>,
251 }
252
253 let yaml = "&a {x: *a}";
254 let expected = "recursion limit exceeded";
255 test_error::<S>(yaml, expected);
256 }
257
258 #[test]
test_infinite_recursion_arrays()259 fn test_infinite_recursion_arrays() {
260 #[derive(Deserialize, Debug)]
261 struct S {
262 x: Option<Box<S>>,
263 }
264
265 let yaml = "&a [*a]";
266 let expected = "recursion limit exceeded";
267 test_error::<S>(yaml, expected);
268 }
269
270 #[test]
test_finite_recursion_objects()271 fn test_finite_recursion_objects() {
272 #[derive(Deserialize, Debug)]
273 struct S {
274 x: Option<Box<S>>,
275 }
276
277 let yaml = "{x:".repeat(1_000) + &"}".repeat(1_000);
278 let expected = "recursion limit exceeded at line 1 column 766";
279 test_error::<i32>(&yaml, expected);
280 }
281
282 #[test]
test_finite_recursion_arrays()283 fn test_finite_recursion_arrays() {
284 #[derive(Deserialize, Debug)]
285 struct S {
286 x: Option<Box<S>>,
287 }
288
289 let yaml = "[".repeat(1_000) + &"]".repeat(1_000);
290 let expected = "recursion limit exceeded at line 1 column 256";
291 test_error::<S>(&yaml, expected);
292 }
293