diff --git a/tests/serde-se.rs b/tests/serde-se.rs index 5f4607b5..f7efd767 100644 --- a/tests/serde-se.rs +++ b/tests/serde-se.rs @@ -230,99 +230,49 @@ mod with_root { use super::*; use pretty_assertions::assert_eq; - #[test] - fn unit() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let data = Unit; - data.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), ""); - } - - #[test] - fn newtype() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let data = Newtype(true); - data.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), "true"); - } - - #[test] - fn tuple() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let data = (42.0, "answer"); - data.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - "42answer" - ); - } - - #[test] - fn tuple_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let data = Tuple(42.0, "answer"); - data.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - "42answer" - ); - } - - #[test] - fn struct_() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); + macro_rules! serialize_as { + ($name:ident: $data:expr => $expected:literal) => { + #[test] + fn $name() { + let mut buffer = Vec::new(); + let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - let node = Struct { - float: 42.0, - string: "answer", + $data.serialize(&mut ser).unwrap(); + assert_eq!(String::from_utf8(buffer).unwrap(), $expected); + } }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); } - #[test] - fn nested_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = NestedStruct { + serialize_as!(unit: + Unit + => ""); + serialize_as!(newtype: + Newtype(true) + => "true"); + serialize_as!(tuple: + (42.0, "answer") + => "42answer"); + serialize_as!(tuple_struct: + Tuple(42.0, "answer") + => "42answer"); + serialize_as!(struct_: + Struct { + float: 42.0, + string: "answer" + } + => r#""#); + serialize_as!(nested_struct: + NestedStruct { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn flatten_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = FlattenStruct { + } + => r#""#); + serialize_as!(flatten_struct: + FlattenStruct { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#"42answer"# - ); - } + } + => r#"42answer"#); mod enum_ { use super::*; @@ -331,352 +281,134 @@ mod with_root { use super::*; use pretty_assertions::assert_eq; - #[test] - fn unit() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::Unit; - node.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), ""); - } - - #[test] - fn primitive_unit() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::PrimitiveUnit; - node.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), "PrimitiveUnit"); - } - - #[test] - fn newtype() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::Newtype(true); - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - "true" - ); - } - - #[test] - fn struct_() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::Struct { + serialize_as!(unit: + ExternallyTagged::Unit + => ""); + serialize_as!(primitive_unit: + ExternallyTagged::PrimitiveUnit + => "PrimitiveUnit"); + serialize_as!(newtype: + ExternallyTagged::Newtype(true) + => "true"); + serialize_as!(tuple_struct: + ExternallyTagged::Tuple(42.0, "answer") + => "42answer"); + serialize_as!(struct_: + ExternallyTagged::Struct { float: 42.0, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn tuple_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::Tuple(42.0, "answer"); - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - "42answer" - ); - } - - #[test] - fn nested_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::Holder { + } + => r#""#); + serialize_as!(nested_struct: + ExternallyTagged::Holder { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn flatten_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = ExternallyTagged::Flatten { + } + => r#""#); + serialize_as!(flatten_struct: + ExternallyTagged::Flatten { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#"42answer"# - ); - } + } + => r#"42answer"#); } mod internally_tagged { use super::*; use pretty_assertions::assert_eq; - #[test] - fn unit() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = InternallyTagged::Unit; - node.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), r#""#); - } - - #[test] - fn newtype() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = InternallyTagged::Newtype(Nested { float: 4.2 }); - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn struct_() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = InternallyTagged::Struct { + serialize_as!(unit: + InternallyTagged::Unit + => r#""#); + serialize_as!(newtype: + InternallyTagged::Newtype(Nested { float: 4.2 }) + => r#""#); + serialize_as!(struct_: + InternallyTagged::Struct { float: 42.0, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn nested_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = InternallyTagged::Holder { + } + => r#""#); + serialize_as!(nested_struct: + InternallyTagged::Holder { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn flatten_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = InternallyTagged::Flatten { + } + => r#""#); + serialize_as!(flatten_struct: + InternallyTagged::Flatten { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#"Flatten42answer"# - ); - } + } + => r#"Flatten42answer"#); } mod adjacently_tagged { use super::*; use pretty_assertions::assert_eq; - #[test] - fn unit() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = AdjacentlyTagged::Unit; - node.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), r#""#); - } - - #[test] - fn newtype() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = AdjacentlyTagged::Newtype(true); - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn tuple_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = AdjacentlyTagged::Tuple(42.0, "answer"); - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#"42answer"# - ); - } - - #[test] - fn struct_() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = AdjacentlyTagged::Struct { + serialize_as!(unit: + AdjacentlyTagged::Unit + => r#""#); + serialize_as!(newtype: + AdjacentlyTagged::Newtype(true) + => r#""#); + serialize_as!(tuple_struct: + AdjacentlyTagged::Tuple(42.0, "answer") + => r#"42answer"#); + serialize_as!(struct_: + AdjacentlyTagged::Struct { float: 42.0, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn nested_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = AdjacentlyTagged::Holder { + } + => r#""#); + serialize_as!(nested_struct: + AdjacentlyTagged::Holder { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn flatten_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = AdjacentlyTagged::Flatten { + } + => r#""#); + serialize_as!(flatten_struct: + AdjacentlyTagged::Flatten { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#"42answer"# - ); - } + } + => r#"42answer"#); } mod untagged { use super::*; use pretty_assertions::assert_eq; - #[test] - fn unit() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = Untagged::Unit; - node.serialize(&mut ser).unwrap(); + serialize_as!(unit: + Untagged::Unit // Unit variant consists just from the tag, and because tags // are not written in untagged mode, nothing is written - assert_eq!(String::from_utf8(buffer).unwrap(), ""); - } - - #[test] - fn newtype() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = Untagged::Newtype(true); - node.serialize(&mut ser).unwrap(); - assert_eq!(String::from_utf8(buffer).unwrap(), "true"); - } - - #[test] - fn tuple_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = Untagged::Tuple(42.0, "answer"); - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - "42answer" - ); - } - - #[test] - fn struct_() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = Untagged::Struct { + => ""); + serialize_as!(newtype: + Untagged::Newtype(true) + => "true"); + serialize_as!(tuple_struct: + Untagged::Tuple(42.0, "answer") + => "42answer"); + serialize_as!(struct_: + Untagged::Struct { float: 42.0, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn nested_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = Untagged::Holder { + } + => r#""#); + serialize_as!(nested_struct: + Untagged::Holder { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#""# - ); - } - - #[test] - fn flatten_struct() { - let mut buffer = Vec::new(); - let mut ser = Serializer::with_root(Writer::new(&mut buffer), Some("root")); - - let node = Untagged::Flatten { + } + => r#""#); + serialize_as!(flatten_struct: + Untagged::Flatten { nested: Nested { float: 42.0 }, string: "answer", - }; - node.serialize(&mut ser).unwrap(); - assert_eq!( - String::from_utf8(buffer).unwrap(), - r#"42answer"# - ); - } + } + => r#"42answer"#); } } }