@@ -6,6 +6,78 @@ use quick_xml::de::from_str;
66use quick_xml:: se:: to_string;
77use serde:: { Deserialize , Serialize } ;
88
9+ /// Regression tests for https://github.com/tafia/quick-xml/issues/252.
10+ mod issue252 {
11+ use super :: * ;
12+ use pretty_assertions:: assert_eq;
13+
14+ #[ test]
15+ fn attributes ( ) {
16+ #[ derive( Serialize , Debug , PartialEq ) ]
17+ struct OptionalAttributes {
18+ #[ serde( rename = "@a" ) ]
19+ a : Option < & ' static str > ,
20+
21+ #[ serde( rename = "@b" ) ]
22+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
23+ b : Option < & ' static str > ,
24+ }
25+
26+ assert_eq ! (
27+ to_string( & OptionalAttributes { a: None , b: None } ) . unwrap( ) ,
28+ r#"<OptionalAttributes a=""/>"#
29+ ) ;
30+ assert_eq ! (
31+ to_string( & OptionalAttributes {
32+ a: Some ( "" ) ,
33+ b: Some ( "" )
34+ } )
35+ . unwrap( ) ,
36+ r#"<OptionalAttributes a="" b=""/>"#
37+ ) ;
38+ assert_eq ! (
39+ to_string( & OptionalAttributes {
40+ a: Some ( "a" ) ,
41+ b: Some ( "b" )
42+ } )
43+ . unwrap( ) ,
44+ r#"<OptionalAttributes a="a" b="b"/>"#
45+ ) ;
46+ }
47+
48+ #[ test]
49+ fn elements ( ) {
50+ #[ derive( Serialize , Debug , PartialEq ) ]
51+ struct OptionalElements {
52+ a : Option < & ' static str > ,
53+
54+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
55+ b : Option < & ' static str > ,
56+ }
57+
58+ assert_eq ! (
59+ to_string( & OptionalElements { a: None , b: None } ) . unwrap( ) ,
60+ r#"<OptionalElements><a/></OptionalElements>"#
61+ ) ;
62+ assert_eq ! (
63+ to_string( & OptionalElements {
64+ a: Some ( "" ) ,
65+ b: Some ( "" )
66+ } )
67+ . unwrap( ) ,
68+ r#"<OptionalElements><a/><b/></OptionalElements>"#
69+ ) ;
70+ assert_eq ! (
71+ to_string( & OptionalElements {
72+ a: Some ( "a" ) ,
73+ b: Some ( "b" )
74+ } )
75+ . unwrap( ) ,
76+ r#"<OptionalElements><a>a</a><b>b</b></OptionalElements>"#
77+ ) ;
78+ }
79+ }
80+
981/// Regression test for https://github.com/tafia/quick-xml/issues/537.
1082///
1183/// This test checks that special `xmlns:xxx` attributes uses full name of
0 commit comments