Skip to content

Commit 01650ee

Browse files
committed
Change expected enum representation in tests according to new policy
New representation policy is compatible with serde>=1.0.181 and also more correct and consistent with general mapping rules. General rules of new policy: - In `$value` field the representation is always the same as top-level representation; - In `$text` field the representation is always the same as in normal field, but surrounding tags with field name are removed; - In normal field the representation is always contains a tag with field name. The tables below summarizes the representation policy. For examples in fields only content at place of `...` is shown: ``` <Container>...</Container> ``` The struct which contains field looks like: ``` struct Container { field: Enum, #[serde(rename = "$text")] text: Enum, #[serde(rename = "$value")] value: Enum, } ``` Normal enum variant ------------------- ### Old (before this commit) representation |Kind |Top-level, in normal and `$value` fields |In `$text` field| |-------|-----------------------------------------|----------------| |Unit |`<Unit/>` |_(empty)_ | |Newtype|`<Newtype>42</Newtype>` |`42` | |Tuple |`<Tuple>42</Tuple><Tuple>answer</Tuple>` |`42 answer` | |Struct |`<Struct><q>42</q><a>answer</a></Struct>`|Err(Unsupported)| ### New (since this commit) |Kind |Top-level and in `$value` field |In normal field |In `$text` field| |-------|-----------------------------------------|---------------------|----------------| |Unit |`<Unit/>` |`<field>Unit</field>`|`Unit` | |Newtype|`<Newtype>42</Newtype>` |Err(Unsupported) |Err(Unsupported)| |Tuple |`<Tuple>42</Tuple><Tuple>answer</Tuple>` |Err(Unsupported) |Err(Unsupported)| |Struct |`<Struct><q>42</q><a>answer</a></Struct>`|Err(Unsupported) |Err(Unsupported)| `$text` enum variant -------------------- `<field>` in top-level serialization is the name of top-level element, serialization is impossible if it is not defined in the serializer. ### Old (before this commit) representation |Kind |In `$value` field |In normal field |In `$text` field| |-------|-------------------------------|--------------------------|----------------| |Unit |_(empty)_ |`<field/>` |_(empty)_ | |Newtype|`42` |`<field>42</field>` |`42` | |Tuple |`42 answer` |`<field>42 answer</field>`|`42 answer` | |Struct |Err(Unsupported) |Err(Unsupported) |Err(Unsupported)| ### New (since this commit) |Kind |Top-level and in `$value` field|In normal field |In `$text` field| |-------|-------------------------------|--------------------------|----------------| |Unit |_(empty)_ |`<field/>` |_(empty)_ | |Newtype|`42` |Err(Unsupported) |Err(Unsupported)| |Tuple |`42 answer` |Err(Unsupported) |Err(Unsupported)| |Struct |Err(Unsupported) |Err(Unsupported) |Err(Unsupported)| failures (28): --lib (14) se::element::tests::expand_empty_elements::enum_unit se::element::tests::expand_empty_elements::enum_unit_escaped se::element::tests::with_indent::attributes::enum_ se::element::tests::with_indent::enum_newtype se::element::tests::with_indent::enum_struct se::element::tests::with_indent::enum_tuple se::element::tests::with_indent::enum_unit se::element::tests::with_indent::enum_unit_escaped se::element::tests::without_indent::attributes::enum_ se::element::tests::without_indent::enum_newtype se::element::tests::without_indent::enum_struct se::element::tests::without_indent::enum_tuple se::element::tests::without_indent::enum_unit se::element::tests::without_indent::enum_unit_escaped serde-se (14): without_root::enum_::externally_tagged::normal_field2::empty_struct without_root::enum_::externally_tagged::normal_field2::newtype without_root::enum_::externally_tagged::normal_field2::struct_ without_root::enum_::externally_tagged::normal_field2::tuple without_root::enum_::externally_tagged::normal_field2::unit without_root::enum_::externally_tagged::normal_field::empty_struct without_root::enum_::externally_tagged::normal_field::newtype without_root::enum_::externally_tagged::normal_field::struct_ without_root::enum_::externally_tagged::normal_field::tuple without_root::enum_::externally_tagged::normal_field::unit without_root::enum_::externally_tagged::text_variant::normal_field::newtype without_root::enum_::externally_tagged::text_variant::normal_field::tuple without_root::enum_::externally_tagged::text_variant::normal_field::unit without_root::enum_::externally_tagged::text_variant::text_field::unit
1 parent 3b21d28 commit 01650ee

File tree

2 files changed

+111
-224
lines changed

2 files changed

+111
-224
lines changed

src/se/element.rs

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -670,12 +670,12 @@ mod tests {
670670
serialize_as!(unit_struct: Unit => "<root/>");
671671
serialize_as!(unit_struct_escaped: UnitEscaped => "<root/>");
672672

673-
serialize_as!(enum_unit: Enum::Unit => "<Unit/>");
674-
err!(enum_unit_escaped: Enum::UnitEscaped
675-
=> Unsupported("character `<` is not allowed at the start of an XML name `<\"&'>`"));
673+
serialize_as!(enum_unit: Enum::Unit => "<root>Unit</root>");
674+
serialize_as!(enum_unit_escaped: Enum::UnitEscaped => "<root>&lt;&quot;&amp;&apos;&gt;</root>");
676675

677676
serialize_as!(newtype: Newtype(42) => "<root>42</root>");
678-
serialize_as!(enum_newtype: Enum::Newtype(42) => "<Newtype>42</Newtype>");
677+
err!(enum_newtype: Enum::Newtype(42)
678+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype`"));
679679

680680
serialize_as!(seq: vec![1, 2, 3]
681681
=> "<root>1</root>\
@@ -689,9 +689,8 @@ mod tests {
689689
serialize_as!(tuple_struct: Tuple("first", 42)
690690
=> "<root>first</root>\
691691
<root>42</root>");
692-
serialize_as!(enum_tuple: Enum::Tuple("first", 42)
693-
=> "<Tuple>first</Tuple>\
694-
<Tuple>42</Tuple>");
692+
err!(enum_tuple: Enum::Tuple("first", 42)
693+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple`"));
695694

696695
serialize_as!(map: BTreeMap::from([("_1", 2), ("_3", 4)])
697696
=> "<root>\
@@ -704,12 +703,8 @@ mod tests {
704703
<val>42</val>\
705704
<val>42</val>\
706705
</root>");
707-
serialize_as!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) }
708-
=> "<Struct>\
709-
<key>answer</key>\
710-
<val>42</val>\
711-
<val>42</val>\
712-
</Struct>");
706+
err!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) }
707+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct`"));
713708

714709
/// Special field name `$text` should be serialized as text content.
715710
/// Sequences serialized as an `xs:list` content
@@ -1216,12 +1211,8 @@ mod tests {
12161211
serialize_as!(struct_after: AttributesAfter { key: "answer", val: 42 }
12171212
=> r#"<root val="42"><key>answer</key></root>"#);
12181213

1219-
serialize_as!(enum_: Enum::Attributes { key: "answer", val: (42, 42) }
1220-
=> r#"<Attributes key="answer" val="42 42"/>"#);
1221-
serialize_as!(enum_before: Enum::AttributesBefore { key: "answer", val: 42 }
1222-
=> r#"<AttributesBefore key="answer"><val>42</val></AttributesBefore>"#);
1223-
serialize_as!(enum_after: Enum::AttributesAfter { key: "answer", val: 42 }
1224-
=> r#"<AttributesAfter val="42"><key>answer</key></AttributesAfter>"#);
1214+
err!(enum_: Enum::Attributes { key: "answer", val: (42, 42) }
1215+
=> Unsupported("cannot serialize enum struct variant `Enum::Attributes`"));
12251216

12261217
/// Test for https://github.com/tafia/quick-xml/issues/252
12271218
mod optional {
@@ -1385,12 +1376,12 @@ mod tests {
13851376
serialize_as!(unit_struct: Unit => "<root/>");
13861377
serialize_as!(unit_struct_escaped: UnitEscaped => "<root/>");
13871378

1388-
serialize_as!(enum_unit: Enum::Unit => "<Unit/>");
1389-
err!(enum_unit_escaped: Enum::UnitEscaped
1390-
=> Unsupported("character `<` is not allowed at the start of an XML name `<\"&'>`"));
1379+
serialize_as!(enum_unit: Enum::Unit => "<root>Unit</root>");
1380+
serialize_as!(enum_unit_escaped: Enum::UnitEscaped => "<root>&lt;&quot;&amp;&apos;&gt;</root>");
13911381

13921382
serialize_as!(newtype: Newtype(42) => "<root>42</root>");
1393-
serialize_as!(enum_newtype: Enum::Newtype(42) => "<Newtype>42</Newtype>");
1383+
err!(enum_newtype: Enum::Newtype(42)
1384+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype`"));
13941385

13951386
serialize_as!(seq: vec![1, 2, 3]
13961387
=> "<root>1</root>\n\
@@ -1404,9 +1395,8 @@ mod tests {
14041395
serialize_as!(tuple_struct: Tuple("first", 42)
14051396
=> "<root>first</root>\n\
14061397
<root>42</root>");
1407-
serialize_as!(enum_tuple: Enum::Tuple("first", 42)
1408-
=> "<Tuple>first</Tuple>\n\
1409-
<Tuple>42</Tuple>");
1398+
err!(enum_tuple: Enum::Tuple("first", 42)
1399+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple`"));
14101400

14111401
serialize_as!(map: BTreeMap::from([("_1", 2), ("_3", 4)])
14121402
=> "<root>\n \
@@ -1419,12 +1409,8 @@ mod tests {
14191409
<val>42</val>\n \
14201410
<val>42</val>\n\
14211411
</root>");
1422-
serialize_as!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) }
1423-
=> "<Struct>\n \
1424-
<key>answer</key>\n \
1425-
<val>42</val>\n \
1426-
<val>42</val>\n\
1427-
</Struct>");
1412+
err!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) }
1413+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct`"));
14281414

14291415
/// Special field name `$text` should be serialized as text content.
14301416
/// Sequences serialized as an `xs:list` content
@@ -1960,16 +1946,8 @@ mod tests {
19601946
<key>answer</key>\n\
19611947
</root>");
19621948

1963-
serialize_as!(enum_: Enum::Attributes { key: "answer", val: (42, 42) }
1964-
=> r#"<Attributes key="answer" val="42 42"/>"#);
1965-
serialize_as!(enum_before: Enum::AttributesBefore { key: "answer", val: 42 }
1966-
=> "<AttributesBefore key=\"answer\">\n \
1967-
<val>42</val>\n\
1968-
</AttributesBefore>");
1969-
serialize_as!(enum_after: Enum::AttributesAfter { key: "answer", val: 42 }
1970-
=> "<AttributesAfter val=\"42\">\n \
1971-
<key>answer</key>\n\
1972-
</AttributesAfter>");
1949+
err!(enum_: Enum::Attributes { key: "answer", val: (42, 42) }
1950+
=> Unsupported("cannot serialize enum struct variant `Enum::Attributes`"));
19731951

19741952
/// Test for https://github.com/tafia/quick-xml/issues/252
19751953
mod optional {
@@ -2052,48 +2030,14 @@ mod tests {
20522030
};
20532031
}
20542032

2055-
/// Checks that attempt to serialize given `$data` results to a
2056-
/// serialization error `$kind` with `$reason`
2057-
macro_rules! err {
2058-
($name:ident: $data:expr => $kind:ident($reason:literal)) => {
2059-
#[test]
2060-
fn $name() {
2061-
let mut buffer = String::new();
2062-
let ser = ElementSerializer {
2063-
ser: ContentSerializer {
2064-
writer: &mut buffer,
2065-
level: QuoteLevel::Full,
2066-
indent: Indent::None,
2067-
write_indent: false,
2068-
expand_empty_elements: false,
2069-
},
2070-
key: XmlName("root"),
2071-
};
2072-
2073-
match $data.serialize(ser).unwrap_err() {
2074-
DeError::$kind(e) => assert_eq!(e, $reason),
2075-
e => panic!(
2076-
"Expected `{}({})`, found `{:?}`",
2077-
stringify!($kind),
2078-
$reason,
2079-
e
2080-
),
2081-
}
2082-
// We can write something before fail
2083-
// assert_eq!(buffer, "");
2084-
}
2085-
};
2086-
}
2087-
20882033
serialize_as!(option_some_empty: Some("") => "<root></root>");
20892034
serialize_as!(option_some_empty_str: Some("") => "<root></root>");
20902035

20912036
serialize_as!(unit: () => "<root></root>");
20922037
serialize_as!(unit_struct: Unit => "<root></root>");
20932038
serialize_as!(unit_struct_escaped: UnitEscaped => "<root></root>");
20942039

2095-
serialize_as!(enum_unit: Enum::Unit => "<Unit></Unit>");
2096-
err!(enum_unit_escaped: Enum::UnitEscaped
2097-
=> Unsupported("character `<` is not allowed at the start of an XML name `<\"&'>`"));
2040+
serialize_as!(enum_unit: Enum::Unit => "<root>Unit</root>");
2041+
serialize_as!(enum_unit_escaped: Enum::UnitEscaped => "<root>&lt;&quot;&amp;&apos;&gt;</root>");
20982042
}
20992043
}

0 commit comments

Comments
 (0)