@@ -179,34 +179,51 @@ mod trivial {
179179
180180 macro_rules! in_struct {
181181 // string, char and bool have some specifics in some tests
182- ( string: $type: ty = $value: expr, $expected: expr ) => {
182+ ( string: $type: ty = $value: expr, $expected: literal ) => {
183183 in_struct!(
184- string: $type = $value, $expected;
184+ string: $type = $value, $expected. into ( ) , concat! ( " \n \t " , $expected , " \n \t " ) . into ( ) ;
185185
186+ /// Try to deserialize type from a root tag without data or with only spaces
186187 #[ test]
187188 fn wrapped_empty( ) {
188189 let item: $type = from_str( "<root/>" ) . unwrap( ) ;
189190 let expected: $type = "" . into( ) ;
190191 assert_eq!( item, expected) ;
192+
193+ let item: $type = from_str( "<root> \r \n \t </root>" ) . unwrap( ) ;
194+ // \r\n normalized to \n
195+ let expected: $type = " \n \t " . into( ) ;
196+ assert_eq!( item, expected) ;
191197 }
192198
193199 #[ test]
194200 fn field_empty( ) {
195201 let item: Field <$type> = from_str( "<root><value/></root>" ) . unwrap( ) ;
196202 assert_eq!( item, Field { value: "" . into( ) } ) ;
203+
204+ let item: Field <$type> = from_str( "<root><value> \r \n \t </value></root>" ) . unwrap( ) ;
205+ // \r\n normalized to \n
206+ assert_eq!( item, Field { value: " \n \t " . into( ) } ) ;
197207 }
198208 ) ;
199209 } ;
200210 ( char_: $type: ty = $value: expr, $expected: expr) => {
201211 in_struct!(
202- char_: $type = $value, $expected;
212+ char_: $type = $value, $expected, $expected ;
203213
214+ /// Try to deserialize type from a root tag without data or with only spaces
204215 #[ test]
205216 fn wrapped_empty( ) {
206217 match from_str:: <$type>( "<root/>" ) {
207218 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid value: string "", expected a character"# ) ,
208219 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
209220 }
221+
222+ match from_str:: <$type>( "<root> \r \n \t </root>" ) {
223+ // \r\n normalized to \n
224+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid value: string " \n\t", expected a character"# ) ,
225+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
226+ }
210227 }
211228
212229 #[ test]
@@ -215,19 +232,32 @@ mod trivial {
215232 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid value: string "", expected a character"# ) ,
216233 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
217234 }
235+
236+ match from_str:: <Field <$type>>( "<root><value> \r \n \t </value></root>" ) {
237+ // \r\n normalized to \n
238+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid value: string " \n\t", expected a character"# ) ,
239+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
240+ }
218241 }
219242 ) ;
220243 } ;
221244 ( $name: ident: bool = $value: expr, $expected: expr) => {
222245 in_struct!(
223- $name: bool = $value, $expected;
246+ $name: bool = $value, $expected, $expected ;
224247
248+ /// Try to deserialize type from a root tag without data or with only spaces
225249 #[ test]
226250 fn wrapped_empty( ) {
227251 match from_str:: <bool >( "<root/>" ) {
228252 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid type: string "", expected a boolean"# ) ,
229253 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
230254 }
255+
256+ match from_str:: <bool >( "<root> \r \n \t </root>" ) {
257+ // \r\n normalized to \n
258+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid type: string " \n\t", expected a boolean"# ) ,
259+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
260+ }
231261 }
232262
233263 #[ test]
@@ -236,19 +266,32 @@ mod trivial {
236266 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid type: string "", expected a boolean"# ) ,
237267 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
238268 }
269+
270+ match from_str:: <Field <bool >>( "<root><value> \r \n \t </value></root>" ) {
271+ // \r\n normalized to \n
272+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, r#"invalid type: string " \n\t", expected a boolean"# ) ,
273+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
274+ }
239275 }
240276 ) ;
241277 } ;
242278 ( $name: ident: $type: ty = $value: expr, $expected: expr) => {
243279 in_struct!(
244- $name: $type = $value, $expected;
280+ $name: $type = $value, $expected, $expected ;
245281
282+ /// Try to deserialize type from a root tag without data or with only spaces
246283 #[ test]
247284 fn wrapped_empty( ) {
248285 match from_str:: <$type>( "<root/>" ) {
249286 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, concat!( r#"invalid type: string "", expected "# , stringify!( $type) ) ) ,
250287 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
251288 }
289+
290+ match from_str:: <$type>( "<root> \r \n \t </root>" ) {
291+ // \r\n normalized to \n
292+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, concat!( r#"invalid type: string " \n\t", expected "# , stringify!( $type) ) ) ,
293+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
294+ }
252295 }
253296
254297 #[ test]
@@ -257,23 +300,36 @@ mod trivial {
257300 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, concat!( r#"invalid type: string "", expected "# , stringify!( $type) ) ) ,
258301 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
259302 }
303+
304+ match from_str:: <Field <$type>>( "<root><value> \r \n \t </value></root>" ) {
305+ // \r\n normalized to \n
306+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, concat!( r#"invalid type: string " \n\t", expected "# , stringify!( $type) ) ) ,
307+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
308+ }
260309 }
261310 ) ;
262311 } ;
263- ( $name: ident: $type: ty = $value: expr, $expected: expr; $( $specific_tests: item) * ) => {
312+ ( $name: ident: $type: ty = $value: expr, $expected: expr, $expected_with_indent : expr ; $( $specific_tests: item) * ) => {
264313 mod $name {
265314 use super :: * ;
266315 use pretty_assertions:: assert_eq;
267316
268317 $( $specific_tests) *
269318
319+ /// Try to deserialize type wrapped in a tag, optionally surround with spaces
270320 #[ test]
271321 fn wrapped( ) {
272322 let item: $type = from_str( & format!( "<root>{}</root>" , $value) ) . unwrap( ) ;
273323 let expected: $type = $expected;
274324 assert_eq!( item, expected) ;
325+
326+ let item: $type =
327+ from_str( & format!( "<root> \r \n \t {} \r \n \t </root>" , $value) ) . unwrap( ) ;
328+ let expected: $type = $expected_with_indent;
329+ assert_eq!( item, expected) ;
275330 }
276331
332+ /// Try to deserialize type wrapped in two tags
277333 #[ test]
278334 fn nested( ) {
279335 match from_str:: <$type>( & format!( "<root><nested>{}</nested></root>" , $value) ) {
@@ -284,8 +340,21 @@ mod trivial {
284340 x
285341 ) ,
286342 }
343+
344+ match from_str:: <$type>( & format!(
345+ "<root> \r \n \t <nested>{}</nested> \r \n \t </root>" ,
346+ $value
347+ ) ) {
348+ // Expected unexpected start element `<nested>`
349+ Err ( DeError :: UnexpectedStart ( tag) ) => assert_eq!( tag, b"nested" ) ,
350+ x => panic!(
351+ r#"Expected `Err(UnexpectedStart("nested"))`, but got `{:?}`"# ,
352+ x
353+ ) ,
354+ }
287355 }
288356
357+ /// Try to deserialize type wrapped in a tag with another tag after content
289358 #[ test]
290359 fn tag_after( ) {
291360 match from_str:: <$type>( & format!( "<root>{}<something-else/></root>" , $value) ) {
@@ -296,8 +365,21 @@ mod trivial {
296365 x
297366 ) ,
298367 }
368+
369+ match from_str:: <$type>( & format!(
370+ "<root> \r \n \t {} \r \n \t <something-else/></root>" ,
371+ $value
372+ ) ) {
373+ // Expected unexpected start element `<something-else>`
374+ Err ( DeError :: UnexpectedStart ( tag) ) => assert_eq!( tag, b"something-else" ) ,
375+ x => panic!(
376+ r#"Expected `Err(UnexpectedStart("something-else"))`, but got `{:?}`"# ,
377+ x
378+ ) ,
379+ }
299380 }
300381
382+ /// Try to deserialize type wrapped in a tag with another tag before content
301383 #[ test]
302384 fn tag_before( ) {
303385 match from_str:: <$type>( & format!( "<root><something-else/>{}</root>" , $value) ) {
@@ -308,13 +390,30 @@ mod trivial {
308390 x
309391 ) ,
310392 }
393+
394+ match from_str:: <$type>( & format!(
395+ "<root><something-else/> \r \n \t {} \r \n \t </root>" ,
396+ $value
397+ ) ) {
398+ // Expected unexpected start element `<something-else>`
399+ Err ( DeError :: UnexpectedStart ( tag) ) => assert_eq!( tag, b"something-else" ) ,
400+ x => panic!(
401+ r#"Expected `Err(UnexpectedStart("something-else"))`, but got `{:?}`"# ,
402+ x
403+ ) ,
404+ }
311405 }
312406
407+ /// Try to deserialize type which is a field of a struct
313408 #[ test]
314409 fn field( ) {
315410 let item: Field <$type> =
316411 from_str( & format!( "<root><value>{}</value></root>" , $value) ) . unwrap( ) ;
317412 assert_eq!( item, Field { value: $expected } ) ;
413+
414+ let item: Field <$type> =
415+ from_str( & format!( "<root><value> \r \n \t {} \r \n \t </value></root>" , $value) ) . unwrap( ) ;
416+ assert_eq!( item, Field { value: $expected_with_indent } ) ;
318417 }
319418
320419 #[ test]
@@ -330,6 +429,18 @@ mod trivial {
330429 x
331430 ) ,
332431 }
432+
433+ match from_str:: <Field <$type>>( & format!(
434+ "<root><value> \r \n \t <nested>{}</nested> \r \n \t </value></root>" ,
435+ $value
436+ ) ) {
437+ // Expected unexpected start element `<nested>`
438+ Err ( DeError :: UnexpectedStart ( tag) ) => assert_eq!( tag, b"nested" ) ,
439+ x => panic!(
440+ r#"Expected `Err(UnexpectedStart("nested"))`, but got `{:?}`"# ,
441+ x
442+ ) ,
443+ }
333444 }
334445
335446 #[ test]
@@ -345,6 +456,18 @@ mod trivial {
345456 x
346457 ) ,
347458 }
459+
460+ match from_str:: <Field <$type>>( & format!(
461+ "<root><value> \r \n \t {} \r \n \t <something-else/></value></root>" ,
462+ $value
463+ ) ) {
464+ // Expected unexpected start element `<something-else>`
465+ Err ( DeError :: UnexpectedStart ( tag) ) => assert_eq!( tag, b"something-else" ) ,
466+ x => panic!(
467+ r#"Expected `Err(UnexpectedStart("something-else"))`, but got `{:?}`"# ,
468+ x
469+ ) ,
470+ }
348471 }
349472
350473 #[ test]
@@ -360,6 +483,18 @@ mod trivial {
360483 x
361484 ) ,
362485 }
486+
487+ match from_str:: <Field <$type>>( & format!(
488+ "<root><value><something-else/> \r \n \t {} \r \n \t </value></root>" ,
489+ $value
490+ ) ) {
491+ // Expected unexpected start element `<something-else>`
492+ Err ( DeError :: UnexpectedStart ( tag) ) => assert_eq!( tag, b"something-else" ) ,
493+ x => panic!(
494+ r#"Expected `Err(UnexpectedStart("something-else"))`, but got `{:?}`"# ,
495+ x
496+ ) ,
497+ }
363498 }
364499
365500 #[ test]
@@ -368,6 +503,11 @@ mod trivial {
368503 Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, "missing field `$text`" ) ,
369504 x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
370505 }
506+
507+ match from_str:: <Trivial <$type>>( "<root> \r \n \t </root>" ) {
508+ Err ( DeError :: Custom ( msg) ) => assert_eq!( msg, "missing field `$text`" ) ,
509+ x => panic!( "Expected `Err(Custom(_))`, but got `{:?}`" , x) ,
510+ }
371511 }
372512
373513 /// Tests deserialization from top-level tag content: `<root>...content...</root>`
@@ -376,6 +516,10 @@ mod trivial {
376516 let item: Trivial <$type> =
377517 from_str( & format!( "<root>{}</root>" , $value) ) . unwrap( ) ;
378518 assert_eq!( item, Trivial { value: $expected } ) ;
519+
520+ let item: Trivial <$type> =
521+ from_str( & format!( "<root> \r \n \t {} \r \n \t </root>" , $value) ) . unwrap( ) ;
522+ assert_eq!( item, Trivial { value: $expected_with_indent } ) ;
379523 }
380524
381525 #[ test]
@@ -391,6 +535,18 @@ mod trivial {
391535 x
392536 ) ,
393537 }
538+
539+ match from_str:: <Trivial <$type>>( & format!(
540+ "<root> \r \n \t <nested>{}</nested> \r \n \t </root>" ,
541+ $value
542+ ) ) {
543+ // Expected unexpected start element `<nested>`
544+ Err ( DeError :: Custom ( reason) ) => assert_eq!( reason, "missing field `$text`" ) ,
545+ x => panic!(
546+ r#"Expected `Err(Custom("missing field `$text`"))`, but got `{:?}`"# ,
547+ x
548+ ) ,
549+ }
394550 }
395551
396552 #[ test]
@@ -402,13 +558,21 @@ mod trivial {
402558 let item: Trivial <$type> =
403559 from_str( & format!( "<root>{}<something-else/></root>" , $value) ) . unwrap( ) ;
404560 assert_eq!( item, Trivial { value: $expected } ) ;
561+
562+ let item: Trivial <$type> =
563+ from_str( & format!( "<root> \r \n \t {} \r \n \t <something-else/></root>" , $value) ) . unwrap( ) ;
564+ assert_eq!( item, Trivial { value: $expected_with_indent } ) ;
405565 }
406566
407567 #[ test]
408568 fn text_tag_before( ) {
409569 let item: Trivial <$type> =
410570 from_str( & format!( "<root><something-else/>{}</root>" , $value) ) . unwrap( ) ;
411571 assert_eq!( item, Trivial { value: $expected } ) ;
572+
573+ let item: Trivial <$type> =
574+ from_str( & format!( "<root><something-else/> \r \n \t {} \r \n \t </root>" , $value) ) . unwrap( ) ;
575+ assert_eq!( item, Trivial { value: $expected_with_indent } ) ;
412576 }
413577 }
414578 } ;
@@ -461,7 +625,7 @@ mod trivial {
461625 in_struct ! ( true_: bool = "true" , true ) ;
462626 in_struct ! ( char_: char = "r" , 'r' ) ;
463627
464- in_struct ! ( string: String = "escaped string" , "escaped string" . into ( ) ) ;
628+ in_struct ! ( string: String = "escaped string" , "escaped string" ) ;
465629
466630 /// XML does not able to store binary data
467631 #[ test]
@@ -524,7 +688,7 @@ mod trivial {
524688 in_struct ! ( char_: char = "<![CDATA[r]]>" , 'r' ) ;
525689
526690 // Escape sequences does not processed inside CDATA section
527- in_struct ! ( string: String = "<![CDATA[escaped string]]>" , "escaped string" . into ( ) ) ;
691+ in_struct ! ( string: String = "<![CDATA[escaped string]]>" , "escaped string" ) ;
528692
529693 /// XML does not able to store binary data
530694 #[ test]
0 commit comments