From 65b9003ee73189d629941badaa22a6305d8dbe03 Mon Sep 17 00:00:00 2001 From: RedIODev Date: Tue, 1 Oct 2024 16:42:23 +0500 Subject: [PATCH] Change expectations about indentation - never indent text productions That means, never indent "$text" fields and do not indent "$value" fields that produces string without surrounding tags failures (208): se::content::tests::with_indent::enum_with_text_field::char_amp se::content::tests::with_indent::enum_with_text_field::char_apos se::content::tests::with_indent::enum_with_text_field::char_gt se::content::tests::with_indent::enum_with_text_field::char_lt se::content::tests::with_indent::enum_with_text_field::char_non_escaped se::content::tests::with_indent::enum_with_text_field::char_quot se::content::tests::with_indent::enum_with_text_field::char_space se::content::tests::with_indent::enum_with_text_field::enum_unit se::content::tests::with_indent::enum_with_text_field::enum_unit_escaped se::content::tests::with_indent::enum_with_text_field::f32_ se::content::tests::with_indent::enum_with_text_field::f64_ se::content::tests::with_indent::enum_with_text_field::false_ se::content::tests::with_indent::enum_with_text_field::i128_ se::content::tests::with_indent::enum_with_text_field::i16_ se::content::tests::with_indent::enum_with_text_field::i32_ se::content::tests::with_indent::enum_with_text_field::i64_ se::content::tests::with_indent::enum_with_text_field::i8_ se::content::tests::with_indent::enum_with_text_field::isize_ se::content::tests::with_indent::enum_with_text_field::newtype se::content::tests::with_indent::enum_with_text_field::option_none se::content::tests::with_indent::enum_with_text_field::option_some se::content::tests::with_indent::enum_with_text_field::option_some_empty_str se::content::tests::with_indent::enum_with_text_field::seq se::content::tests::with_indent::enum_with_text_field::seq_empty se::content::tests::with_indent::enum_with_text_field::str_escaped se::content::tests::with_indent::enum_with_text_field::str_non_escaped se::content::tests::with_indent::enum_with_text_field::true_ se::content::tests::with_indent::enum_with_text_field::tuple se::content::tests::with_indent::enum_with_text_field::tuple_struct se::content::tests::with_indent::enum_with_text_field::u128_ se::content::tests::with_indent::enum_with_text_field::u16_ se::content::tests::with_indent::enum_with_text_field::u32_ se::content::tests::with_indent::enum_with_text_field::u64_ se::content::tests::with_indent::enum_with_text_field::u8_ se::content::tests::with_indent::enum_with_text_field::unit se::content::tests::with_indent::enum_with_text_field::unit_struct se::content::tests::with_indent::enum_with_text_field::unit_struct_escaped se::content::tests::with_indent::enum_with_text_field::usize_ se::content::tests::with_indent::enum_with_value_field::char_amp se::content::tests::with_indent::enum_with_value_field::char_apos se::content::tests::with_indent::enum_with_value_field::char_gt se::content::tests::with_indent::enum_with_value_field::char_lt se::content::tests::with_indent::enum_with_value_field::char_non_escaped se::content::tests::with_indent::enum_with_value_field::char_quot se::content::tests::with_indent::enum_with_value_field::char_space se::content::tests::with_indent::enum_with_value_field::f32_ se::content::tests::with_indent::enum_with_value_field::f64_ se::content::tests::with_indent::enum_with_value_field::false_ se::content::tests::with_indent::enum_with_value_field::i128_ se::content::tests::with_indent::enum_with_value_field::i16_ se::content::tests::with_indent::enum_with_value_field::i32_ se::content::tests::with_indent::enum_with_value_field::i64_ se::content::tests::with_indent::enum_with_value_field::i8_ se::content::tests::with_indent::enum_with_value_field::isize_ se::content::tests::with_indent::enum_with_value_field::newtype se::content::tests::with_indent::enum_with_value_field::option_none se::content::tests::with_indent::enum_with_value_field::option_some se::content::tests::with_indent::enum_with_value_field::option_some_empty_str se::content::tests::with_indent::enum_with_value_field::seq se::content::tests::with_indent::enum_with_value_field::seq_empty se::content::tests::with_indent::enum_with_value_field::str_escaped se::content::tests::with_indent::enum_with_value_field::str_non_escaped se::content::tests::with_indent::enum_with_value_field::true_ se::content::tests::with_indent::enum_with_value_field::tuple se::content::tests::with_indent::enum_with_value_field::tuple_struct se::content::tests::with_indent::enum_with_value_field::u128_ se::content::tests::with_indent::enum_with_value_field::u16_ se::content::tests::with_indent::enum_with_value_field::u32_ se::content::tests::with_indent::enum_with_value_field::u64_ se::content::tests::with_indent::enum_with_value_field::u8_ se::content::tests::with_indent::enum_with_value_field::usize_ se::content::tests::with_indent::seq se::content::tests::with_indent::text_field::enum_struct se::content::tests::with_indent::tuple se::content::tests::with_indent::tuple_struct se::element::tests::with_indent::text_field::map::char_amp se::element::tests::with_indent::text_field::map::char_apos se::element::tests::with_indent::text_field::map::char_gt se::element::tests::with_indent::text_field::map::char_lt se::element::tests::with_indent::text_field::map::char_non_escaped se::element::tests::with_indent::text_field::map::char_quot se::element::tests::with_indent::text_field::map::char_space se::element::tests::with_indent::text_field::map::enum_unit se::element::tests::with_indent::text_field::map::enum_unit_escaped se::element::tests::with_indent::text_field::map::f32_ se::element::tests::with_indent::text_field::map::f64_ se::element::tests::with_indent::text_field::map::false_ se::element::tests::with_indent::text_field::map::i128_ se::element::tests::with_indent::text_field::map::i16_ se::element::tests::with_indent::text_field::map::i32_ se::element::tests::with_indent::text_field::map::i64_ se::element::tests::with_indent::text_field::map::i8_ se::element::tests::with_indent::text_field::map::isize_ se::element::tests::with_indent::text_field::map::newtype se::element::tests::with_indent::text_field::map::option_some se::element::tests::with_indent::text_field::map::seq se::element::tests::with_indent::text_field::map::str_escaped se::element::tests::with_indent::text_field::map::str_non_escaped se::element::tests::with_indent::text_field::map::true_ se::element::tests::with_indent::text_field::map::tuple se::element::tests::with_indent::text_field::map::tuple_struct se::element::tests::with_indent::text_field::map::u128_ se::element::tests::with_indent::text_field::map::u16_ se::element::tests::with_indent::text_field::map::u32_ se::element::tests::with_indent::text_field::map::u64_ se::element::tests::with_indent::text_field::map::u8_ se::element::tests::with_indent::text_field::map::usize_ se::element::tests::with_indent::text_field::struct_::char_amp se::element::tests::with_indent::text_field::struct_::char_apos se::element::tests::with_indent::text_field::struct_::char_gt se::element::tests::with_indent::text_field::struct_::char_lt se::element::tests::with_indent::text_field::struct_::char_non_escaped se::element::tests::with_indent::text_field::struct_::char_quot se::element::tests::with_indent::text_field::struct_::char_space se::element::tests::with_indent::text_field::struct_::enum_unit se::element::tests::with_indent::text_field::struct_::enum_unit_escaped se::element::tests::with_indent::text_field::struct_::f32_ se::element::tests::with_indent::text_field::struct_::f64_ se::element::tests::with_indent::text_field::struct_::false_ se::element::tests::with_indent::text_field::struct_::i128_ se::element::tests::with_indent::text_field::struct_::i16_ se::element::tests::with_indent::text_field::struct_::i32_ se::element::tests::with_indent::text_field::struct_::i64_ se::element::tests::with_indent::text_field::struct_::i8_ se::element::tests::with_indent::text_field::struct_::isize_ se::element::tests::with_indent::text_field::struct_::newtype se::element::tests::with_indent::text_field::struct_::option_none se::element::tests::with_indent::text_field::struct_::option_some se::element::tests::with_indent::text_field::struct_::option_some_empty_str se::element::tests::with_indent::text_field::struct_::seq se::element::tests::with_indent::text_field::struct_::seq_empty se::element::tests::with_indent::text_field::struct_::str_escaped se::element::tests::with_indent::text_field::struct_::str_non_escaped se::element::tests::with_indent::text_field::struct_::true_ se::element::tests::with_indent::text_field::struct_::tuple se::element::tests::with_indent::text_field::struct_::tuple_struct se::element::tests::with_indent::text_field::struct_::u128_ se::element::tests::with_indent::text_field::struct_::u16_ se::element::tests::with_indent::text_field::struct_::u32_ se::element::tests::with_indent::text_field::struct_::u64_ se::element::tests::with_indent::text_field::struct_::u8_ se::element::tests::with_indent::text_field::struct_::unit se::element::tests::with_indent::text_field::struct_::unit_struct se::element::tests::with_indent::text_field::struct_::unit_struct_escaped se::element::tests::with_indent::text_field::struct_::usize_ se::element::tests::with_indent::value_field::map::char_amp se::element::tests::with_indent::value_field::map::char_apos se::element::tests::with_indent::value_field::map::char_gt se::element::tests::with_indent::value_field::map::char_lt se::element::tests::with_indent::value_field::map::char_non_escaped se::element::tests::with_indent::value_field::map::char_quot se::element::tests::with_indent::value_field::map::char_space se::element::tests::with_indent::value_field::map::f32_ se::element::tests::with_indent::value_field::map::f64_ se::element::tests::with_indent::value_field::map::false_ se::element::tests::with_indent::value_field::map::i128_ se::element::tests::with_indent::value_field::map::i16_ se::element::tests::with_indent::value_field::map::i32_ se::element::tests::with_indent::value_field::map::i64_ se::element::tests::with_indent::value_field::map::i8_ se::element::tests::with_indent::value_field::map::isize_ se::element::tests::with_indent::value_field::map::newtype se::element::tests::with_indent::value_field::map::option_some se::element::tests::with_indent::value_field::map::seq se::element::tests::with_indent::value_field::map::str_escaped se::element::tests::with_indent::value_field::map::str_non_escaped se::element::tests::with_indent::value_field::map::true_ se::element::tests::with_indent::value_field::map::tuple se::element::tests::with_indent::value_field::map::tuple_struct se::element::tests::with_indent::value_field::map::u128_ se::element::tests::with_indent::value_field::map::u16_ se::element::tests::with_indent::value_field::map::u32_ se::element::tests::with_indent::value_field::map::u64_ se::element::tests::with_indent::value_field::map::u8_ se::element::tests::with_indent::value_field::map::usize_ se::element::tests::with_indent::value_field::struct_::char_amp se::element::tests::with_indent::value_field::struct_::char_apos se::element::tests::with_indent::value_field::struct_::char_gt se::element::tests::with_indent::value_field::struct_::char_lt se::element::tests::with_indent::value_field::struct_::char_non_escaped se::element::tests::with_indent::value_field::struct_::char_quot se::element::tests::with_indent::value_field::struct_::char_space se::element::tests::with_indent::value_field::struct_::f32_ se::element::tests::with_indent::value_field::struct_::f64_ se::element::tests::with_indent::value_field::struct_::false_ se::element::tests::with_indent::value_field::struct_::i128_ se::element::tests::with_indent::value_field::struct_::i16_ se::element::tests::with_indent::value_field::struct_::i32_ se::element::tests::with_indent::value_field::struct_::i64_ se::element::tests::with_indent::value_field::struct_::i8_ se::element::tests::with_indent::value_field::struct_::isize_ se::element::tests::with_indent::value_field::struct_::newtype se::element::tests::with_indent::value_field::struct_::option_none se::element::tests::with_indent::value_field::struct_::option_some se::element::tests::with_indent::value_field::struct_::option_some_empty_str se::element::tests::with_indent::value_field::struct_::seq se::element::tests::with_indent::value_field::struct_::seq_empty se::element::tests::with_indent::value_field::struct_::str_escaped se::element::tests::with_indent::value_field::struct_::str_non_escaped se::element::tests::with_indent::value_field::struct_::true_ se::element::tests::with_indent::value_field::struct_::tuple se::element::tests::with_indent::value_field::struct_::tuple_struct se::element::tests::with_indent::value_field::struct_::u128_ se::element::tests::with_indent::value_field::struct_::u16_ se::element::tests::with_indent::value_field::struct_::u32_ se::element::tests::with_indent::value_field::struct_::u64_ se::element::tests::with_indent::value_field::struct_::u8_ se::element::tests::with_indent::value_field::struct_::usize_ --- src/se/content.rs | 109 ++++++++++-------------------- src/se/element.rs | 131 ++++++++++++++---------------------- tests/serde-se.rs | 20 ++---- tests/writer-indentation.rs | 4 +- 4 files changed, 90 insertions(+), 174 deletions(-) diff --git a/src/se/content.rs b/src/se/content.rs index 30c7414f..e814f5b9 100644 --- a/src/se/content.rs +++ b/src/se/content.rs @@ -651,8 +651,6 @@ pub(super) mod tests { serialize_as!(char_amp: '&' => "&", SensitiveText); serialize_as!(char_apos: '\'' => "'", SensitiveText); serialize_as!(char_quot: '"' => """, SensitiveText); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content serialize_as!(char_space: ' ' => " ", SensitiveText); serialize_as!(str_non_escaped: "non-escaped string" => "non-escaped string", SensitiveText); @@ -781,8 +779,6 @@ pub(super) mod tests { text!(char_amp: '&' => "&"); text!(char_apos: '\'' => "'"); text!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content text!(char_space: ' ' => " "); text!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -908,8 +904,6 @@ pub(super) mod tests { value!(char_amp: '&' => "&"); value!(char_apos: '\'' => "'"); value!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content value!(char_space: ' ' => " "); value!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1094,8 +1088,6 @@ pub(super) mod tests { serialize_as!(char_amp: '&' => "&", SensitiveText); serialize_as!(char_apos: '\'' => "'", SensitiveText); serialize_as!(char_quot: '"' => """, SensitiveText); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content serialize_as!(char_space: ' ' => " ", SensitiveText); serialize_as!(str_non_escaped: "non-escaped string" => "non-escaped string", SensitiveText); @@ -1119,19 +1111,14 @@ pub(super) mod tests { serialize_as!(newtype: Newtype(42) => "42", Text); serialize_as!(enum_newtype: Enum::Newtype(42) => "42"); - // Note that sequences of primitives serialized without delimiters other that indent! - serialize_as!(seq: vec![1, 2, 3] - => "1\n\ - 2\n\ - 3", Text); + // Note that sequences of primitives serialized without delimiters! + serialize_as!(seq: vec![1, 2, 3] => "123", Text); serialize_as!(seq_empty: Vec::::new() => "", SensitiveNothing); serialize_as!(tuple: ("<\"&'>", "with\t\r\n spaces", 3usize) - => "<"&'>\n\ - with\t\r\n spaces\n\ + => "<"&'>\ + with\t\r\n spaces\ 3", Text); - serialize_as!(tuple_struct: Tuple("first", 42) - => "first\n\ - 42", Text); + serialize_as!(tuple_struct: Tuple("first", 42) => "first42", Text); serialize_as!(enum_tuple: Enum::Tuple("first", 42) => "first\n\ 42"); @@ -1170,9 +1157,7 @@ pub(super) mod tests { after: "answer", } => "\n \ - answer\n \ - 42 42\n \ - answer\n\ + answer42 42answer\n\ "); } @@ -1182,18 +1167,6 @@ pub(super) mod tests { use pretty_assertions::assert_eq; macro_rules! text { - ($name:ident: $data:expr) => { - serialize_as!($name: - SpecialEnum::Text { - before: "answer", - content: $data, - after: "answer", - } - => "\n \ - answer\n \ - answer\n\ - "); - }; ($name:ident: $data:expr => $expected:literal) => { serialize_as!($name: SpecialEnum::Text { @@ -1202,9 +1175,9 @@ pub(super) mod tests { after: "answer", } => concat!( - "\n answer\n ", + "\n answer", $expected, - "\n answer\n", + "answer\n", )); }; } @@ -1238,8 +1211,6 @@ pub(super) mod tests { text!(char_amp: '&' => "&"); text!(char_apos: '\'' => "'"); text!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content text!(char_space: ' ' => " "); text!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1253,13 +1224,13 @@ pub(super) mod tests { } => Unsupported("`serialize_bytes` not supported yet")); - text!(option_none: Option::<&str>::None); + text!(option_none: Option::<&str>::None => ""); text!(option_some: Some("non-escaped string") => "non-escaped string"); - text!(option_some_empty_str: Some("")); + text!(option_some_empty_str: Some("") => ""); - text!(unit: ()); - text!(unit_struct: Unit); - text!(unit_struct_escaped: UnitEscaped); + text!(unit: () => ""); + text!(unit_struct: Unit => ""); + text!(unit_struct_escaped: UnitEscaped => ""); text!(enum_unit: Enum::Unit => "Unit"); text!(enum_unit_escaped: Enum::UnitEscaped => "<"&'>"); @@ -1276,7 +1247,7 @@ pub(super) mod tests { // Sequences are serialized separated by spaces, all spaces inside are escaped text!(seq: vec![1, 2, 3] => "1 2 3"); - text!(seq_empty: Vec::::new()); + text!(seq_empty: Vec::::new() => ""); text!(tuple: ("<\"&'>", "with\t\n\r spaces", 3usize) => "<"&'> \ with spaces \ @@ -1321,18 +1292,6 @@ pub(super) mod tests { use pretty_assertions::assert_eq; macro_rules! value { - ($name:ident: $data:expr) => { - serialize_as!($name: - SpecialEnum::Value { - before: "answer", - content: $data, - after: "answer", - } - => "\n \ - answer\n \ - answer\n\ - "); - }; ($name:ident: $data:expr => $expected:literal) => { serialize_as!($name: SpecialEnum::Value { @@ -1341,9 +1300,9 @@ pub(super) mod tests { after: "answer", } => concat!( - "\n answer\n ", + "\n answer", $expected, - "\n answer\n", + "answer\n", )); }; } @@ -1377,8 +1336,6 @@ pub(super) mod tests { value!(char_amp: '&' => "&"); value!(char_apos: '\'' => "'"); value!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content value!(char_space: ' ' => " "); value!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1392,15 +1349,15 @@ pub(super) mod tests { } => Unsupported("`serialize_bytes` not supported yet")); - value!(option_none: Option::<&str>::None); + value!(option_none: Option::<&str>::None => ""); value!(option_some: Some("non-escaped string") => "non-escaped string"); - value!(option_some_empty_str: Some("")); + value!(option_some_empty_str: Some("") => ""); - value!(unit: ()); - value!(unit_struct: Unit); - value!(unit_struct_escaped: UnitEscaped); + value!(unit: () => "\n "); + value!(unit_struct: Unit => "\n "); + value!(unit_struct_escaped: UnitEscaped => "\n "); - value!(enum_unit: Enum::Unit => ""); + value!(enum_unit: Enum::Unit => "\n \n "); err!(enum_unit_escaped: SpecialEnum::Value { before: "answer", @@ -1410,19 +1367,20 @@ pub(super) mod tests { => Unsupported("character `<` is not allowed at the start of an XML name `<\"&'>`")); value!(newtype: Newtype(42) => "42"); - value!(enum_newtype: Enum::Newtype(42) => "42"); + value!(enum_newtype: Enum::Newtype(42) => "\n 42\n "); // Note that sequences of primitives serialized without delimiters! - value!(seq: vec![1, 2, 3] => "1\n 2\n 3"); - value!(seq_empty: Vec::::new()); + value!(seq: vec![1, 2, 3] => "123"); + value!(seq_empty: Vec::::new() => ""); value!(tuple: ("<\"&'>", "with\t\n\r spaces", 3usize) - => "<"&'>\n \ - with\t\n\r spaces\n \ + => "<"&'>\ + with\t\n\r spaces\ 3"); - value!(tuple_struct: Tuple("first", 42) => "first\n 42"); + value!(tuple_struct: Tuple("first", 42) => "first42"); value!(enum_tuple: Enum::Tuple("first", 42) - => "first\n \ - 42"); + => "\n \ + first\n \ + 42\n "); // We cannot wrap map or struct in any container and should not // flatten it, so it is impossible to serialize maps and structs @@ -1442,11 +1400,12 @@ pub(super) mod tests { => Unsupported("serialization of struct `Struct` is not supported in `$value` field")); value!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) } - => "\n \ + => "\n \ + \n \ answer\n \ 42\n \ 42\n \ - "); + \n "); } mod attributes { diff --git a/src/se/element.rs b/src/se/element.rs index 48a93707..10a62389 100644 --- a/src/se/element.rs +++ b/src/se/element.rs @@ -800,8 +800,6 @@ mod tests { text!(char_amp: '&' => "&"); text!(char_apos: '\'' => "'"); text!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content text!(char_space: ' ' => " "); text!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -927,8 +925,6 @@ mod tests { text!(char_amp: '&' => "&"); text!(char_apos: '\'' => "'"); text!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content text!(char_space: ' ' => " "); text!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1058,8 +1054,6 @@ mod tests { value!(char_amp: '&' => "&"); value!(char_apos: '\'' => "'"); value!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content value!(char_space: ' ' => " "); value!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1164,8 +1158,6 @@ mod tests { value!(char_amp: '&' => "&"); value!(char_apos: '\'' => "'"); value!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content value!(char_space: ' ' => " "); value!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1316,7 +1308,8 @@ mod tests { use crate::writer::Indentation; use pretty_assertions::assert_eq; - /// Checks that given `$data` successfully serialized as `$expected` + /// Checks that given `$data` successfully serialized as `$expected`. + /// Writes `$data` using [`ElementSerializer`] with indent of two spaces. macro_rules! serialize_as { ($name:ident: $data:expr => $expected:expr) => { #[test] @@ -1402,8 +1395,6 @@ mod tests { serialize_as!(char_amp: '&' => "&"); serialize_as!(char_apos: '\'' => "'"); serialize_as!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content serialize_as!(char_space: ' ' => " "); serialize_as!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1468,13 +1459,15 @@ mod tests { macro_rules! text { ($name:ident: $data:expr) => { serialize_as!($name: + // Serialization started from ElementSerializer::serialize_map BTreeMap::from([("$text", $data)]) => ""); }; ($name:ident: $data:expr => $expected:literal) => { serialize_as!($name: + // Serialization started from ElementSerializer::serialize_map BTreeMap::from([("$text", $data)]) - => concat!("\n ", $expected,"\n")); + => concat!("", $expected,"")); }; } @@ -1507,8 +1500,6 @@ mod tests { text!(char_amp: '&' => "&"); text!(char_apos: '\'' => "'"); text!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content text!(char_space: ' ' => " "); text!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1590,29 +1581,18 @@ mod tests { use pretty_assertions::assert_eq; macro_rules! text { - ($name:ident: $data:expr) => { - serialize_as!($name: - Text { - before: "answer", - content: $data, - after: "answer", - } - => "\n \ - answer\n \ - answer\n\ - "); - }; ($name:ident: $data:expr => $expected:literal) => { serialize_as!($name: + // Serialization started from ElementSerializer::serialize_struct Text { before: "answer", content: $data, after: "answer", } => concat!( - "\n answer\n ", + "\n answer", $expected, - "\n answer\n", + "answer\n", )); }; } @@ -1646,8 +1626,6 @@ mod tests { text!(char_amp: '&' => "&"); text!(char_apos: '\'' => "'"); text!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content text!(char_space: ' ' => " "); text!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1661,13 +1639,13 @@ mod tests { } => Unsupported("`serialize_bytes` not supported yet")); - text!(option_none: Option::<&str>::None); + text!(option_none: Option::<&str>::None => ""); text!(option_some: Some("non-escaped string") => "non-escaped string"); - text!(option_some_empty_str: Some("")); + text!(option_some_empty_str: Some("") => ""); - text!(unit: ()); - text!(unit_struct: Unit); - text!(unit_struct_escaped: UnitEscaped); + text!(unit: () => ""); + text!(unit_struct: Unit => ""); + text!(unit_struct_escaped: UnitEscaped => ""); text!(enum_unit: Enum::Unit => "Unit"); text!(enum_unit_escaped: Enum::UnitEscaped => "<"&'>"); @@ -1684,7 +1662,7 @@ mod tests { // Sequences are serialized separated by spaces, all spaces inside are escaped text!(seq: vec![1, 2, 3] => "1 2 3"); - text!(seq_empty: Vec::::new()); + text!(seq_empty: Vec::::new() => ""); text!(tuple: ("<\"&'>", "with\t\n\r spaces", 3usize) => "<"&'> \ with spaces \ @@ -1738,13 +1716,15 @@ mod tests { macro_rules! value { ($name:ident: $data:expr) => { serialize_as!($name: + // Serialization started from ElementSerializer::serialize_map BTreeMap::from([("$value", $data)]) => ""); }; ($name:ident: $data:expr => $expected:literal) => { serialize_as!($name: + // Serialization started from ElementSerializer::serialize_map BTreeMap::from([("$value", $data)]) - => concat!("\n ", $expected,"\n")); + => concat!("", $expected,"")); }; } @@ -1777,8 +1757,6 @@ mod tests { value!(char_amp: '&' => "&"); value!(char_apos: '\'' => "'"); value!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content value!(char_space: ' ' => " "); value!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1796,24 +1774,25 @@ mod tests { value!(unit_struct: Unit); value!(unit_struct_escaped: UnitEscaped); - value!(enum_unit: Enum::Unit => ""); + value!(enum_unit: Enum::Unit => "\n \n"); err!(enum_unit_escaped: BTreeMap::from([("$value", Enum::UnitEscaped)]) => Unsupported("character `<` is not allowed at the start of an XML name `<\"&'>`")); value!(newtype: Newtype(42) => "42"); - value!(enum_newtype: Enum::Newtype(42) => "42"); + value!(enum_newtype: Enum::Newtype(42) => "\n 42\n"); - value!(seq: vec![1, 2, 3] => "1\n 2\n 3"); + value!(seq: vec![1, 2, 3] => "123"); value!(seq_empty: Vec::::new()); value!(tuple: ("<\"&'>", "with\t\n\r spaces", 3usize) - => "<"&'>\n \ - with\t\n\r spaces\n \ + => "<"&'>\ + with\t\n\r spaces\ 3"); - value!(tuple_struct: Tuple("first", 42) => "first\n 42"); + value!(tuple_struct: Tuple("first", 42) => "first42"); value!(enum_tuple: Enum::Tuple("first", 42) - => "first\n \ - 42"); + => "\n \ + first\n \ + 42\n"); // We cannot wrap map or struct in any container and should not // flatten it, so it is impossible to serialize maps and structs @@ -1825,11 +1804,12 @@ mod tests { => Unsupported("serialization of struct `Struct` is not supported in `$value` field")); value!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) } - => "\n \ + => "\n \ + \n \ answer\n \ 42\n \ 42\n \ - "); + \n"); } /// `$value` field inside a struct @@ -1838,29 +1818,18 @@ mod tests { use pretty_assertions::assert_eq; macro_rules! value { - ($name:ident: $data:expr) => { - serialize_as!($name: - Value { - before: "answer", - content: $data, - after: "answer", - } - => "\n \ - answer\n \ - answer\n\ - "); - }; ($name:ident: $data:expr => $expected:literal) => { serialize_as!($name: + // Serialization started from ElementSerializer::serialize_struct Value { before: "answer", content: $data, after: "answer", } => concat!( - "\n answer\n ", + "\n answer", $expected, - "\n answer\n", + "answer\n", )); }; } @@ -1894,8 +1863,6 @@ mod tests { value!(char_amp: '&' => "&"); value!(char_apos: '\'' => "'"); value!(char_quot: '"' => """); - //TODO: add a setting to escape leading/trailing spaces, in order to - // pretty-print does not change the content value!(char_space: ' ' => " "); value!(str_non_escaped: "non-escaped string" => "non-escaped string"); @@ -1909,15 +1876,15 @@ mod tests { } => Unsupported("`serialize_bytes` not supported yet")); - value!(option_none: Option::<&str>::None); + value!(option_none: Option::<&str>::None => ""); value!(option_some: Some("non-escaped string") => "non-escaped string"); - value!(option_some_empty_str: Some("")); + value!(option_some_empty_str: Some("") => ""); - value!(unit: ()); - value!(unit_struct: Unit); - value!(unit_struct_escaped: UnitEscaped); + value!(unit: () => "\n "); + value!(unit_struct: Unit => "\n "); + value!(unit_struct_escaped: UnitEscaped => "\n "); - value!(enum_unit: Enum::Unit => ""); + value!(enum_unit: Enum::Unit => "\n \n "); err!(enum_unit_escaped: Value { before: "answer", @@ -1927,19 +1894,20 @@ mod tests { => Unsupported("character `<` is not allowed at the start of an XML name `<\"&'>`")); value!(newtype: Newtype(42) => "42"); - value!(enum_newtype: Enum::Newtype(42) => "42"); + value!(enum_newtype: Enum::Newtype(42) => "\n 42\n "); // Note that sequences of primitives serialized without delimiters! - value!(seq: vec![1, 2, 3] => "1\n 2\n 3"); - value!(seq_empty: Vec::::new()); + value!(seq: vec![1, 2, 3] => "123"); + value!(seq_empty: Vec::::new() => ""); value!(tuple: ("<\"&'>", "with\t\n\r spaces", 3usize) - => "<"&'>\n \ - with\t\n\r spaces\n \ + => "<"&'>\ + with\t\n\r spaces\ 3"); - value!(tuple_struct: Tuple("first", 42) => "first\n 42"); + value!(tuple_struct: Tuple("first", 42) => "first42"); value!(enum_tuple: Enum::Tuple("first", 42) - => "first\n \ - 42"); + => "\n \ + first\n \ + 42\n "); // We cannot wrap map or struct in any container and should not // flatten it, so it is impossible to serialize maps and structs @@ -1959,11 +1927,12 @@ mod tests { => Unsupported("serialization of struct `Struct` is not supported in `$value` field")); value!(enum_struct: Enum::Struct { key: "answer", val: (42, 42) } - => "\n \ + => "\n \ + \n \ answer\n \ 42\n \ 42\n \ - "); + \n "); } } diff --git a/tests/serde-se.rs b/tests/serde-se.rs index fc560628..ec73b4ee 100644 --- a/tests/serde-se.rs +++ b/tests/serde-se.rs @@ -1380,9 +1380,7 @@ mod without_root { float: 42.0, string: "answer" } - => "\n \ - 42\n \ - answer\n\ + => "42answer\n\ "); mod enum_ { @@ -1439,9 +1437,7 @@ mod without_root { float: 42.0, string: "answer" } - => "\n \ - 42\n \ - answer\n\ + => "42answer\n\ "); /// Test serialization of the specially named variant `$text` @@ -1544,9 +1540,7 @@ mod without_root { string: "answer" } => "\n \ - Text\n \ - 42\n \ - answer\n\ + Text42answer\n\ "); } @@ -1624,9 +1618,7 @@ mod without_root { } => "\n \ Text\n \ - \n \ - 42\n \ - answer\n \ + 42answer\n \ \n\ "); } @@ -1676,9 +1668,7 @@ mod without_root { float: 42.0, string: "answer" } - => "\n \ - 42\n \ - answer\n\ + => "42answer\n\ "); } } diff --git a/tests/writer-indentation.rs b/tests/writer-indentation.rs index f96f6063..baabd967 100644 --- a/tests/writer-indentation.rs +++ b/tests/writer-indentation.rs @@ -222,9 +222,7 @@ fn serializable() { 43 first element - second element - text - foo + second elementtextfoo "# );