Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split serde-de tests into several files and add new serde-se tests #657

Merged
merged 5 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ name = "serde-de"
required-features = ["serialize"]
path = "tests/serde-de.rs"

[[test]]
name = "serde-de-enum"
required-features = ["serialize"]
path = "tests/serde-de-enum.rs"

[[test]]
name = "serde-de-seq"
required-features = ["serialize"]
path = "tests/serde-de-seq.rs"

[[test]]
name = "serde-se"
required-features = ["serialize"]
Expand Down
72 changes: 40 additions & 32 deletions src/se/element.rs

Large diffs are not rendered by default.

44 changes: 20 additions & 24 deletions src/se/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,15 @@ impl<W: Write> Serializer for QNameSerializer<W> {
/// a valid XML name, serialization of unit returns `Err(Unsupported)`
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(DeError::Unsupported(
"unit type `()` cannot be serialized as an XML tag name".into(),
"cannot serialize unit type `()` as an XML tag name".into(),
))
}

/// Because unit struct can be represented only by empty string which is not
/// a valid XML name, serialization of unit struct returns `Err(Unsupported)`
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
Err(DeError::Unsupported(
format!(
"unit struct `{}` cannot be serialized as an XML tag name",
name
)
.into(),
format!("cannot serialize unit struct `{}` as an XML tag name", name).into(),
))
}

Expand All @@ -73,7 +69,7 @@ impl<W: Write> Serializer for QNameSerializer<W> {
) -> Result<Self::Ok, DeError> {
Err(DeError::Unsupported(
format!(
"enum newtype variant `{}::{}` cannot be serialized as an XML tag name",
"cannot serialize enum newtype variant `{}::{}` as an XML tag name",
name, variant
)
.into(),
Expand All @@ -82,13 +78,13 @@ impl<W: Write> Serializer for QNameSerializer<W> {

fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(DeError::Unsupported(
"sequence cannot be serialized as an XML tag name".into(),
"cannot serialize sequence as an XML tag name".into(),
))
}

fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(DeError::Unsupported(
"tuple cannot be serialized as an XML tag name".into(),
"cannot serialize tuple as an XML tag name".into(),
))
}

Expand All @@ -99,7 +95,7 @@ impl<W: Write> Serializer for QNameSerializer<W> {
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(DeError::Unsupported(
format!(
"tuple struct `{}` cannot be serialized as an XML tag name",
"cannot serialize tuple struct `{}` as an XML tag name",
name
)
.into(),
Expand All @@ -115,7 +111,7 @@ impl<W: Write> Serializer for QNameSerializer<W> {
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(DeError::Unsupported(
format!(
"enum tuple variant `{}::{}` cannot be serialized as an XML tag name",
"cannot serialize enum tuple variant `{}::{}` as an XML tag name",
name, variant
)
.into(),
Expand All @@ -124,7 +120,7 @@ impl<W: Write> Serializer for QNameSerializer<W> {

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(DeError::Unsupported(
"map cannot be serialized as an XML tag name".into(),
"cannot serialize map as an XML tag name".into(),
))
}

Expand All @@ -134,7 +130,7 @@ impl<W: Write> Serializer for QNameSerializer<W> {
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Err(DeError::Unsupported(
format!("struct `{}` cannot be serialized as an XML tag name", name).into(),
format!("cannot serialize struct `{}` as an XML tag name", name).into(),
))
}

Expand All @@ -147,7 +143,7 @@ impl<W: Write> Serializer for QNameSerializer<W> {
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(DeError::Unsupported(
format!(
"enum struct variant `{}::{}` cannot be serialized as an XML tag name",
"cannot serialize enum struct variant `{}::{}` as an XML tag name",
name, variant
)
.into(),
Expand Down Expand Up @@ -276,30 +272,30 @@ mod tests {
serialize_as!(option_some: Some("non-escaped-string") => "non-escaped-string");

err!(unit: ()
=> Unsupported("unit type `()` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize unit type `()` as an XML tag name"));
err!(unit_struct: Unit
=> Unsupported("unit struct `Unit` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize unit struct `Unit` as an XML tag name"));

serialize_as!(enum_unit: Enum::Unit => "Unit");
serialize_as!(enum_unit_escaped: Enum::UnitEscaped => "<\"&'>");

serialize_as!(newtype: Newtype(true) => "true");
err!(enum_newtype: Enum::Newtype(false)
=> Unsupported("enum newtype variant `Enum::Newtype` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an XML tag name"));

err!(seq: vec![1, 2, 3]
=> Unsupported("sequence cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize sequence as an XML tag name"));
err!(tuple: ("<\"&'>", "with\t\r\n spaces", 3usize)
=> Unsupported("tuple cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize tuple as an XML tag name"));
err!(tuple_struct: Tuple("first", 42)
=> Unsupported("tuple struct `Tuple` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize tuple struct `Tuple` as an XML tag name"));
err!(enum_tuple: Enum::Tuple("first", 42)
=> Unsupported("enum tuple variant `Enum::Tuple` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an XML tag name"));

err!(map: BTreeMap::from([("_1", 2), ("_3", 4)])
=> Unsupported("map cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize map as an XML tag name"));
err!(struct_: Struct { key: "answer", val: 42 }
=> Unsupported("struct `Struct` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize struct `Struct` as an XML tag name"));
err!(enum_struct: Enum::Struct { key: "answer", val: 42 }
=> Unsupported("enum struct variant `Enum::Struct` cannot be serialized as an XML tag name"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an XML tag name"));
}
64 changes: 30 additions & 34 deletions src/se/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
/// does not differ, so serialization of unit returns `Err(Unsupported)`
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(DeError::Unsupported(
"unit type `()` cannot be serialized as an `xs:list` item".into(),
"cannot serialize unit type `()` as an `xs:list` item".into(),
))
}

Expand All @@ -215,7 +215,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
Err(DeError::Unsupported(
format!(
"unit struct `{}` cannot be serialized as an `xs:list` item",
"cannot serialize unit struct `{}` as an `xs:list` item",
name
)
.into(),
Expand All @@ -233,7 +233,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
) -> Result<Self::Ok, DeError> {
Err(DeError::Unsupported(
format!(
"enum newtype variant `{}::{}` cannot be serialized as an `xs:list` item",
"cannot serialize enum newtype variant `{}::{}` as an `xs:list` item",
name, variant
)
.into(),
Expand All @@ -242,13 +242,13 @@ impl<W: Write> Serializer for AtomicSerializer<W> {

fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(DeError::Unsupported(
"sequence cannot be serialized as an `xs:list` item".into(),
"cannot serialize sequence as an `xs:list` item".into(),
))
}

fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(DeError::Unsupported(
"tuple cannot be serialized as an `xs:list` item".into(),
"cannot serialize tuple as an `xs:list` item".into(),
))
}

Expand All @@ -259,7 +259,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(DeError::Unsupported(
format!(
"tuple struct `{}` cannot be serialized as an `xs:list` item",
"cannot serialize tuple struct `{}` as an `xs:list` item",
name
)
.into(),
Expand All @@ -275,7 +275,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(DeError::Unsupported(
format!(
"enum tuple variant `{}::{}` cannot be serialized as an `xs:list` item",
"cannot serialize enum tuple variant `{}::{}` as an `xs:list` item",
name, variant
)
.into(),
Expand All @@ -284,7 +284,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(DeError::Unsupported(
"map cannot be serialized as an `xs:list` item".into(),
"cannot serialize map as an `xs:list` item".into(),
))
}

Expand All @@ -294,11 +294,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Err(DeError::Unsupported(
format!(
"struct `{}` cannot be serialized as an `xs:list` item",
name
)
.into(),
format!("cannot serialize struct `{}` as an `xs:list` item", name).into(),
))
}

Expand All @@ -311,7 +307,7 @@ impl<W: Write> Serializer for AtomicSerializer<W> {
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(DeError::Unsupported(
format!(
"enum struct variant `{}::{}` cannot be serialized as an `xs:list` item",
"cannot serialize enum struct variant `{}::{}` as an `xs:list` item",
name, variant
)
.into(),
Expand Down Expand Up @@ -389,7 +385,7 @@ impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> {
_value: &T,
) -> Result<Self::Ok, DeError> {
Err(DeError::Unsupported(
format!("enum newtype variant `{}::{}` cannot be serialized as an attribute or text content value", name, variant).into(),
format!("cannot serialize enum newtype variant `{}::{}` as an attribute or text content value", name, variant).into(),
))
}

Expand Down Expand Up @@ -426,13 +422,13 @@ impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> {
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(DeError::Unsupported(
format!("enum tuple variant `{}::{}` cannot be serialized as an attribute or text content value", name, variant).into(),
format!("cannot serialize enum tuple variant `{}::{}` as an attribute or text content value", name, variant).into(),
))
}

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(DeError::Unsupported(
"map cannot be serialized as an attribute or text content value".into(),
"cannot serialize map as an attribute or text content value".into(),
))
}

Expand All @@ -443,7 +439,7 @@ impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> {
) -> Result<Self::SerializeStruct, Self::Error> {
Err(DeError::Unsupported(
format!(
"struct `{}` cannot be serialized as an attribute or text content value",
"cannot serialize struct `{}` as an attribute or text content value",
name
)
.into(),
Expand All @@ -458,7 +454,7 @@ impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> {
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(DeError::Unsupported(
format!("enum struct variant `{}::{}` cannot be serialized as an attribute or text content value", name, variant).into(),
format!("cannot serialize enum struct variant `{}::{}` as an attribute or text content value", name, variant).into(),
))
}
}
Expand Down Expand Up @@ -912,32 +908,32 @@ mod tests {
serialize_as!(option_some: Some("non-escaped-string") => "non-escaped-string");

err!(unit: ()
=> Unsupported("unit type `()` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize unit type `()` as an `xs:list` item"));
err!(unit_struct: Unit
=> Unsupported("unit struct `Unit` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize unit struct `Unit` as an `xs:list` item"));

serialize_as!(enum_unit: Enum::Unit => "Unit");
serialize_as!(enum_unit_escaped: Enum::UnitEscaped => "&lt;&quot;&amp;&apos;&gt;");

serialize_as!(newtype: Newtype(42) => "42");
err!(enum_newtype: Enum::Newtype(42)
=> Unsupported("enum newtype variant `Enum::Newtype` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an `xs:list` item"));

err!(seq: vec![1, 2, 3]
=> Unsupported("sequence cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize sequence as an `xs:list` item"));
err!(tuple: ("<\"&'>", "with\t\n\r spaces", 3usize)
=> Unsupported("tuple cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize tuple as an `xs:list` item"));
err!(tuple_struct: Tuple("first", 42)
=> Unsupported("tuple struct `Tuple` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize tuple struct `Tuple` as an `xs:list` item"));
err!(enum_tuple: Enum::Tuple("first", 42)
=> Unsupported("enum tuple variant `Enum::Tuple` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an `xs:list` item"));

err!(map: BTreeMap::from([(1, 2), (3, 4)])
=> Unsupported("map cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize map as an `xs:list` item"));
err!(struct_: Struct { key: "answer", val: 42 }
=> Unsupported("struct `Struct` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize struct `Struct` as an `xs:list` item"));
err!(enum_struct: Enum::Struct { key: "answer", val: 42 }
=> Unsupported("enum struct variant `Enum::Struct` cannot be serialized as an `xs:list` item"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an `xs:list` item"));
}

mod simple_type {
Expand Down Expand Up @@ -1037,7 +1033,7 @@ mod tests {

serialize_as!(newtype: Newtype(42) => "42");
err!(enum_newtype: Enum::Newtype(42)
=> Unsupported("enum newtype variant `Enum::Newtype` cannot be serialized as an attribute or text content value"));
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));

serialize_as!(seq: vec![1, 2, 3] => "1 2 3");
serialize_as!(seq_empty: Vec::<usize>::new() => "");
Expand All @@ -1047,13 +1043,13 @@ mod tests {
=> "&lt;&quot;&amp;&apos;&gt; with&#9;&#10;&#13;&#32;spaces 3");
serialize_as!(tuple_struct: Tuple("first", 42) => "first 42");
err!(enum_tuple: Enum::Tuple("first", 42)
=> Unsupported("enum tuple variant `Enum::Tuple` cannot be serialized as an attribute or text content value"));
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));

err!(map: BTreeMap::from([(1, 2), (3, 4)])
=> Unsupported("map cannot be serialized as an attribute or text content value"));
=> Unsupported("cannot serialize map as an attribute or text content value"));
err!(struct_: Struct { key: "answer", val: 42 }
=> Unsupported("struct `Struct` cannot be serialized as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
err!(enum_struct: Enum::Struct { key: "answer", val: 42 }
=> Unsupported("enum struct variant `Enum::Struct` cannot be serialized as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
}
}
27 changes: 27 additions & 0 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Utility functions for integration tests

use quick_xml::de::Deserializer;
use quick_xml::DeError;
use serde::Deserialize;

/// Deserialize an instance of type T from a string of XML text.
/// If deserialization was succeeded checks that all XML events was consumed
pub fn from_str<'de, T>(source: &'de str) -> Result<T, DeError>
where
T: Deserialize<'de>,
{
// Log XML that we try to deserialize to see it in the failed tests output
dbg!(source);
let mut de = Deserializer::from_str(source);
let result = T::deserialize(&mut de);

// If type was deserialized, the whole XML document should be consumed
if let Ok(_) = result {
match <()>::deserialize(&mut de) {
Err(DeError::UnexpectedEof) => (),
e => panic!("Expected end `UnexpectedEof`, but got {:?}", e),
}
}

result
}
Loading
Loading