diff --git a/yaserde/tests/de_flatten.rs b/yaserde/tests/de_flatten.rs deleted file mode 100644 index d385430..0000000 --- a/yaserde/tests/de_flatten.rs +++ /dev/null @@ -1,166 +0,0 @@ -#[macro_use] -extern crate yaserde_derive; - -use std::io::Read; -use yaserde::de::from_str; -use yaserde::YaDeserialize; - -macro_rules! convert_and_validate { - ($content: expr, $struct: tt, $model: expr) => { - let loaded: Result<$struct, String> = from_str($content); - assert_eq!(loaded, Ok($model)); - }; -} - -#[test] -fn de_root_flatten_struct() { - #[derive(Default, PartialEq, Debug, YaDeserialize)] - #[yaserde(flatten)] - struct Content { - binary_data: String, - string_data: String, - } - - let content = r#" - - binary - string - "#; - - convert_and_validate!( - content, - Content, - Content { - binary_data: "binary".to_string(), - string_data: "string".to_string(), - } - ); -} - -#[test] -fn de_root_flatten_enum() { - #[derive(PartialEq, Debug, YaDeserialize)] - #[yaserde(flatten)] - pub enum Content { - Binary(Binary), - Data(Data), - Unknown, - } - - impl Default for Content { - fn default() -> Self { - Content::Unknown - } - } - - #[derive(Default, PartialEq, Debug, YaDeserialize)] - pub struct Binary { - binary_data: String, - } - - #[derive(Default, PartialEq, Debug, YaDeserialize)] - pub struct Data { - string_data: String, - } - - let content = r#" - - - binary - - "#; - - convert_and_validate!( - content, - Content, - Content::Binary(Binary { - binary_data: "binary".to_string(), - }) - ); -} - -#[test] -fn de_flatten() { - #[derive(Default, PartialEq, Debug, YaDeserialize)] - struct DateTime { - #[yaserde(flatten)] - date: Date, - time: String, - #[yaserde(flatten)] - kind: DateKind, - } - - #[derive(Default, PartialEq, Debug, YaDeserialize)] - struct Date { - year: i32, - month: i32, - day: i32, - #[yaserde(flatten)] - extra: Extra, - #[yaserde(flatten)] - optional_extra: Option, - } - - #[derive(Default, PartialEq, Debug, YaDeserialize)] - pub struct Extra { - week: i32, - century: i32, - } - - #[derive(Default, PartialEq, Debug, YaDeserialize)] - pub struct OptionalExtra { - lunar_day: i32, - } - - #[derive(PartialEq, Debug, YaDeserialize)] - pub enum DateKind { - #[yaserde(rename = "holidays")] - Holidays(Vec), - #[yaserde(rename = "working")] - Working, - } - - impl Default for DateKind { - fn default() -> Self { - DateKind::Working - } - }; - - let content = r#" - - - 2020 - 1 - 1 - 1 - 21 - 1 - - New Year's Day - Novy God Day - Polar Bear Swim Day - - "#; - convert_and_validate!( - content, - DateTime, - DateTime { - date: Date { - year: 2020, - month: 1, - day: 1, - extra: Extra { - week: 1, - century: 21, - }, - optional_extra: Some(OptionalExtra { lunar_day: 1 }), - }, - time: "10:40:03".to_string(), - kind: DateKind::Holidays(vec![ - "New Year's Day".into(), - "Novy God Day".into(), - "Polar Bear Swim Day".into() - ]) - } - ); -} diff --git a/yaserde/tests/de_namespace.rs b/yaserde/tests/de_namespace.rs deleted file mode 100644 index dd3d94c..0000000 --- a/yaserde/tests/de_namespace.rs +++ /dev/null @@ -1,144 +0,0 @@ -#[macro_use] -extern crate yaserde_derive; - -use std::io::Read; -use yaserde::de::from_str; -use yaserde::YaDeserialize; - -macro_rules! convert_and_validate { - ($content: expr, $struct: tt, $model: expr) => { - let loaded: Result<$struct, String> = from_str($content); - assert_eq!(loaded, Ok($model)); - }; -} - -#[test] -fn de_struct_namespace() { - #[derive(YaDeserialize, PartialEq, Debug)] - #[yaserde( - root = "book", - prefix = "ns", - namespace = "ns: http://www.sample.com/ns/domain", - namespace = "ns2: http://www.sample.com/ns/domain_2" - )] - pub struct Book { - #[yaserde(prefix = "ns")] - author: String, - #[yaserde(prefix = "ns2")] - title: String, - } - - let content = r#" - - Antoine de Saint-Exupéry - Little prince - - "#; - convert_and_validate!( - content, - Book, - Book { - author: String::from("Antoine de Saint-Exupéry"), - title: String::from("Little prince"), - } - ); - - let content = r#" - - Antoine de Saint-Exupéry - Little prince - - "#; - convert_and_validate!( - content, - Book, - Book { - author: String::from("Antoine de Saint-Exupéry"), - title: String::from("Little prince"), - } - ); - - let content = r#" - - Antoine de Saint-Exupéry - Little prince - - "#; - convert_and_validate!( - content, - Book, - Book { - author: String::from("Antoine de Saint-Exupéry"), - title: String::from("Little prince"), - } - ); - - let content = r#" - - Antoine de Saint-Exupéry - Little prince - "#; - let loaded: Result = from_str(content); - assert_eq!( - loaded, - Err("bad namespace for book, found http://www.sample.com/ns/domain2".to_string()) - ); -} - -#[test] -fn de_struct_namespace_nested() { - #[derive(YaDeserialize, Default, PartialEq, Debug)] - #[yaserde(prefix = "nsa", namespace = "nsa: http://www.sample.com/ns/a")] - struct A { - #[yaserde(prefix = "nsa")] - alpha: i32, - } - - #[derive(YaDeserialize, Default, PartialEq, Debug)] - #[yaserde(prefix = "nsb", namespace = "nsb: http://www.sample.com/ns/b")] - struct B { - // Note that name `nested` resides in `nsb` though it has a type from `nsa` - #[yaserde(prefix = "nsb")] - nested: A, - } - - convert_and_validate!( - r#" - - - - 32 - - - "#, - B, - B { - nested: A { alpha: 32 } - } - ); -} - -#[test] -fn de_enum_namespace() { - #[derive(YaDeserialize, PartialEq, Debug)] - #[yaserde( - root = "root", - prefix = "ns", - namespace = "ns: http://www.sample.com/ns/domain" - )] - pub enum XmlStruct { - #[yaserde(prefix = "ns")] - Item, - } - - impl Default for XmlStruct { - fn default() -> XmlStruct { - XmlStruct::Item - } - } - - let content = "ns:Item"; - convert_and_validate!(content, XmlStruct, XmlStruct::Item); -} diff --git a/yaserde/tests/default.rs b/yaserde/tests/default.rs index 23be05f..38fb151 100644 --- a/yaserde/tests/default.rs +++ b/yaserde/tests/default.rs @@ -27,11 +27,10 @@ fn default_field_string() { serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); - let content = "my_value"; let model = XmlStruct { - background: "my_value".to_string(), - }; + background: "my_value".to_string(), + }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } @@ -85,7 +84,7 @@ fn default_field_number() { } #[test] -fn de_default_attribute_string() { +fn default_attribute_string() { fn default_string() -> String { "my_default_value".to_string() } diff --git a/yaserde/tests/ser_enum.rs b/yaserde/tests/enum.rs similarity index 76% rename from yaserde/tests/ser_enum.rs rename to yaserde/tests/enum.rs index 4de471d..ab96c42 100644 --- a/yaserde/tests/ser_enum.rs +++ b/yaserde/tests/enum.rs @@ -3,18 +3,18 @@ extern crate yaserde; #[macro_use] extern crate yaserde_derive; -use std::io::Write; -use yaserde::YaSerialize; +use std::io::{Read, Write}; +use yaserde::{YaDeserialize, YaSerialize}; #[test] -fn ser_enum() { - #[derive(YaSerialize, PartialEq, Debug)] +fn basic_enum() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(root = "base")] pub struct XmlStruct { color: Color, } - #[derive(YaSerialize, PartialEq, Debug)] + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(root = "color")] pub enum Color { White, @@ -46,25 +46,32 @@ fn ser_enum() { assert_eq!(Color::default(), Color::White); - #[derive(YaSerialize, PartialEq, Debug)] + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] pub struct RGBColor { red: String, green: String, blue: String, } - #[derive(YaSerialize, PartialEq, Debug)] + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] pub enum Alpha { Transparent, Opaque, } + impl Default for Alpha { + fn default() -> Alpha { + Alpha::Transparent + } + } + let model = XmlStruct { color: Color::Black, }; let content = "Black"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Color::Custom { @@ -110,45 +117,54 @@ fn ser_enum() { "#; serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); } #[test] -fn ser_attribute_enum() { - #[derive(YaSerialize, PartialEq, Debug)] +fn attribute_enum() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(root = "base")] pub struct XmlStruct { #[yaserde(attribute)] color: Color, } - #[derive(YaSerialize, PartialEq, Debug)] + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(root = "color")] pub enum Color { #[yaserde(rename = "pink")] Pink, } + impl Default for Color { + fn default() -> Color { + Color::Pink + } + } + let model = XmlStruct { color: Color::Pink }; let content = r#""#; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); } #[test] -fn ser_unnamed_enum() { - #[derive(YaSerialize, PartialEq, Debug)] +fn unnamed_enum() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(root = "base")] pub struct XmlStruct { color: Enum, } - #[derive(YaSerialize, PartialEq, Debug, Default)] + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] pub struct OtherStruct { fi: i32, se: i32, } - #[derive(YaSerialize, PartialEq, Debug)] + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] pub enum Enum { Simple, Field(String), @@ -178,6 +194,7 @@ fn ser_unnamed_enum() { let content = "some_text"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::FullPath(String::from("some_text")), @@ -185,6 +202,7 @@ fn ser_unnamed_enum() { let content = "some_text"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Integer(56), @@ -192,6 +210,7 @@ fn ser_unnamed_enum() { let content = "56"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::UserStruct(OtherStruct { fi: 24, se: 42 }), @@ -199,6 +218,7 @@ fn ser_unnamed_enum() { let content = "2442"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionString(Some(String::from("some_text"))), @@ -206,6 +226,7 @@ fn ser_unnamed_enum() { let content = "some_text"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionString(None), @@ -213,6 +234,8 @@ fn ser_unnamed_enum() { let content = ""; serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionUserStruct(Some(OtherStruct { fi: 12, se: 23 })), @@ -221,6 +244,7 @@ fn ser_unnamed_enum() { let content = "1223"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionUserStruct(None), @@ -228,6 +252,8 @@ fn ser_unnamed_enum() { let content = ""; serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Strings(vec![String::from("abc"), String::from("def")]), @@ -235,6 +261,7 @@ fn ser_unnamed_enum() { let content = "abcdef"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Ints(vec![23, 45]), @@ -242,6 +269,7 @@ fn ser_unnamed_enum() { let content = "2345"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Structs(vec![ @@ -252,6 +280,7 @@ fn ser_unnamed_enum() { let content = "12233445"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::ToRename(87), @@ -259,6 +288,7 @@ fn ser_unnamed_enum() { let content = "87"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::ToRenameDots(84), @@ -266,4 +296,5 @@ fn ser_unnamed_enum() { let content = "84"; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); } diff --git a/yaserde/tests/flatten.rs b/yaserde/tests/flatten.rs index 33e0293..71274d6 100644 --- a/yaserde/tests/flatten.rs +++ b/yaserde/tests/flatten.rs @@ -9,7 +9,7 @@ use yaserde::{YaDeserialize, YaSerialize}; #[test] fn basic_flatten() { - #[derive(Default, PartialEq, Debug, YaSerialize)] + #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)] struct DateTime { #[yaserde(flatten)] date: Date, @@ -18,7 +18,7 @@ fn basic_flatten() { kind: DateKind, } - #[derive(Default, PartialEq, Debug, YaSerialize)] + #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)] struct Date { year: i32, month: i32, @@ -29,18 +29,18 @@ fn basic_flatten() { optional_extra: Option, } - #[derive(Default, PartialEq, Debug, YaSerialize)] + #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)] pub struct Extra { week: i32, century: i32, } - #[derive(Default, PartialEq, Debug, YaSerialize)] + #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)] pub struct OptionalExtra { lunar_day: i32, } - #[derive(PartialEq, Debug, YaSerialize)] + #[derive(PartialEq, Debug, YaDeserialize, YaSerialize)] pub enum DateKind { #[yaserde(rename = "holidays")] Holidays(Vec), @@ -88,6 +88,7 @@ fn basic_flatten() { "#; serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, DateTime); } #[test] diff --git a/yaserde/tests/namespace.rs b/yaserde/tests/namespace.rs new file mode 100644 index 0000000..4025e90 --- /dev/null +++ b/yaserde/tests/namespace.rs @@ -0,0 +1,442 @@ +#[macro_use] +extern crate yaserde; +#[macro_use] +extern crate yaserde_derive; + +use std::io::{Read, Write}; +use yaserde::{YaDeserialize, YaSerialize}; + +#[test] +fn struct_simple_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "book", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain" + )] + pub struct Book { + #[yaserde(prefix = "ns")] + author: String, + #[yaserde(prefix = "ns")] + title: String, + } + + let content = r#" + + Antoine de Saint-Exupéry + Little prince + + "#; + + let model = Book { + author: String::from("Antoine de Saint-Exupéry"), + title: String::from("Little prince"), + }; + + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, Book); +} + +#[test] +fn struct_multiple_namespaces() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "book", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain", + namespace = "ns2: http://www.sample.com/ns/domain_2" + )] + pub struct Book { + #[yaserde(prefix = "ns")] + author: String, + #[yaserde(prefix = "ns2")] + title: String, + } + + let content = r#" + + Antoine de Saint-Exupéry + Little prince + + "#; + + let model = Book { + author: String::from("Antoine de Saint-Exupéry"), + title: String::from("Little prince"), + }; + + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, Book); +} + +#[test] +fn struct_partial_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "book", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain" + )] + pub struct Book { + author: String, + #[yaserde(prefix = "ns")] + title: String, + } + + let content = r#" + + Antoine de Saint-Exupéry + Little prince + + "#; + + let model = Book { + author: String::from("Antoine de Saint-Exupéry"), + title: String::from("Little prince"), + }; + + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, Book); +} + +#[test] +fn struct_sub_namespace_definition() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "book", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain", + namespace = "ns2: http://www.sample.com/ns/domain_2" + )] + pub struct Book { + #[yaserde(prefix = "ns")] + author: String, + #[yaserde(prefix = "ns2", namespace = "ns2: http://www.sample.com/ns/domain_2")] + title: String, + } + + let content = r#" + + Antoine de Saint-Exupéry + Little prince + + "#; + + let model = Book { + author: String::from("Antoine de Saint-Exupéry"), + title: String::from("Little prince"), + }; + + // TODO support namespace for attribute to specify local namespace + // serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, Book); +} + +#[test] +fn struct_namespace_nested() { + #[derive(Debug, Default, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde(prefix = "nsa", namespace = "nsa: http://www.sample.com/ns/a")] + struct A { + #[yaserde(prefix = "nsa")] + alpha: i32, + } + + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde(prefix = "nsb", namespace = "nsb: http://www.sample.com/ns/b")] + struct B { + // Note that name `nested` resides in `nsb` though it has a type from `nsa` + #[yaserde(prefix = "nsb")] + nested: A, + } + + let content = r#" + + + 32 + + + "#; + + let model = B { + nested: A { alpha: 32 }, + }; + + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, B); +} + +#[test] +fn struct_namespace_nested_defined_at_root() { + #[derive(Debug, Default, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde(prefix = "nsa", namespace = "nsa: http://www.sample.com/ns/a")] + struct A { + #[yaserde(prefix = "nsa")] + alpha: i32, + } + + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + prefix = "nsb", + namespace = "nsb: http://www.sample.com/ns/b" + namespace = "nsa: http://www.sample.com/ns/a" + )] + struct B { + // Note that name `nested` resides in `nsb` though it has a type from `nsa` + #[yaserde(prefix = "nsb")] + nested: A, + } + + let content = r#" + + + 32 + + + "#; + + let model = B { + nested: A { alpha: 32 }, + }; + + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, B); +} + +#[test] +fn struct_attribute_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "root", + namespace = "ns1: http://www.sample.com/ns/domain1", + namespace = "ns2: http://www.sample.com/ns/domain2" + )] + pub struct XmlStruct { + #[yaserde(prefix = "ns1")] + item_1: String, + #[yaserde(attribute, prefix = "ns2")] + item_2: String, + } + + let model = XmlStruct { + item_1: "something 1".to_string(), + item_2: "something 2".to_string(), + }; + + let content = r#" + + something 1 + + "#; + + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn struct_implicit_default_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "tt", + namespace = "http://www.w3.org/ns/ttml", + namespace = "ttm: http://www.w3.org/ns/ttml#metadata" + )] + pub struct XmlStruct { + item: String, + } + + let model = XmlStruct { + item: "something".to_string(), + }; + + let content = r#"something"#; + serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn struct_explicit_default_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "tt", + default_namespace = "ttml", + namespace = "ttml: http://www.w3.org/ns/ttml", + namespace = "ttm: http://www.w3.org/ns/ttml#metadata" + )] + pub struct XmlStruct { + item: String, + } + + let model = XmlStruct { + item: "something".to_string(), + }; + + let content = r#"something"#; + serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn struct_default_namespace_via_attribute_with_prefix() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "tt", + prefix = "TTML", + default_namespace = "TTML", + namespace = "TTML: http://www.w3.org/ns/ttml", + namespace = "ttm: http://www.w3.org/ns/ttml#metadata" + )] + pub struct XmlStruct { + #[yaserde(prefix = "TTML")] + item: String, + } + + let model = XmlStruct { + item: "something".to_string(), + }; + + let content = r#"something"#; + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn enum_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "root", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain" + )] + pub enum XmlStruct { + #[yaserde(prefix = "ns")] + Item, + } + + impl Default for XmlStruct { + fn default() -> XmlStruct { + XmlStruct::Item + } + } + + let content = r#" + + ns:Item + + "#; + + let model = XmlStruct::Item; + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn enum_multi_namespaces() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "root", + namespace = "ns1: http://www.sample.com/ns/domain1", + namespace = "ns2: http://www.sample.com/ns/domain2" + )] + pub enum XmlStruct { + #[yaserde(prefix = "ns1")] + Item1, + #[yaserde(prefix = "ns2")] + Item2, + } + + impl Default for XmlStruct { + fn default() -> XmlStruct { + XmlStruct::Item1 + } + } + + let model = XmlStruct::Item1; + let content = r#" + + ns1:Item1 + + "#; + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); + + let model = XmlStruct::Item2; + let content = r#" + + ns2:Item2 + + "#; + serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn enum_attribute_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "root", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain" + )] + pub enum XmlStruct { + #[yaserde(prefix = "ns")] + Item, + #[yaserde(prefix = "ns")] + ItemWithField(String), + } + + impl Default for XmlStruct { + fn default() -> XmlStruct { + XmlStruct::Item + } + } + + let content = r#" + + ns:Item + + "#; + + let model = XmlStruct::Item; + serialize_and_validate!(model, content); + deserialize_and_validate!(content, model, XmlStruct); + + let model = XmlStruct::ItemWithField("Value".to_string()); + + let content = r#"Value"#; + serialize_and_validate!(model, content); + // TODO + // deserialize_and_validate!(content, model, XmlStruct); +} + +#[test] +fn struct_bad_namespace() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde( + root = "book", + prefix = "ns", + namespace = "ns: http://www.sample.com/ns/domain", + namespace = "ns2: http://www.sample.com/ns/domain_2" + )] + pub struct Book { + #[yaserde(prefix = "ns")] + author: String, + #[yaserde(prefix = "ns2", namespace = "ns2: http://www.sample.com/ns/domain_2")] + title: String, + } + + let content = r#" + + Antoine de Saint-Exupéry + Little prince + + "#; + + let loaded: Result = yaserde::de::from_str(content); + assert_eq!( + loaded, + Err("bad namespace for book, found http://www.sample.com/ns/domain2".to_string()) + ); +} diff --git a/yaserde/tests/ser_flatten.rs b/yaserde/tests/ser_flatten.rs deleted file mode 100644 index b4c04ec..0000000 --- a/yaserde/tests/ser_flatten.rs +++ /dev/null @@ -1,139 +0,0 @@ -#[macro_use] -extern crate yaserde; -#[macro_use] -extern crate yaserde_derive; - -use std::io::Write; -use yaserde::YaSerialize; - -#[test] -fn ser_flatten() { - #[derive(Default, PartialEq, Debug, YaSerialize)] - struct DateTime { - #[yaserde(flatten)] - date: Date, - time: String, - #[yaserde(flatten)] - kind: DateKind, - } - - #[derive(Default, PartialEq, Debug, YaSerialize)] - struct Date { - year: i32, - month: i32, - day: i32, - #[yaserde(flatten)] - extra: Extra, - #[yaserde(flatten)] - optional_extra: Option, - } - - #[derive(Default, PartialEq, Debug, YaSerialize)] - pub struct Extra { - week: i32, - century: i32, - } - - #[derive(Default, PartialEq, Debug, YaSerialize)] - pub struct OptionalExtra { - lunar_day: i32, - } - - #[derive(PartialEq, Debug, YaSerialize)] - pub enum DateKind { - #[yaserde(rename = "holidays")] - Holidays(Vec), - #[yaserde(rename = "working")] - Working, - } - - impl Default for DateKind { - fn default() -> Self { - DateKind::Working - } - }; - - let model = DateTime { - date: Date { - year: 2020, - month: 1, - day: 1, - extra: Extra { - week: 1, - century: 21, - }, - optional_extra: Some(OptionalExtra { lunar_day: 1 }), - }, - time: "10:40:03".to_string(), - kind: DateKind::Holidays(vec![ - "New Year's Day".into(), - "Novy God Day".into(), - "Polar Bear Swim Day".into(), - ]), - }; - - let content = r#" - - 2020 - 1 - 1 - 1 - 21 - 1 - - New Year's Day - Novy God Day - Polar Bear Swim Day - "#; - - serialize_and_validate!(model, content); -} - -#[test] -fn ser_root_flatten_struct() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde(flatten)] - pub struct Content { - binary_data: String, - string_data: String, - } - - let model = Content { - binary_data: "binary".to_string(), - string_data: "string".to_string(), - }; - let content = "binarystring"; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_root_flatten_enum() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde(flatten)] - pub enum Content { - Binary(Binary), - Data(Data), - } - - #[derive(YaSerialize, PartialEq, Debug)] - pub struct Binary { - binary_data: String, - } - - #[derive(YaSerialize, PartialEq, Debug)] - pub struct Data { - string_data: String, - } - - let model = Content::Binary(Binary { - binary_data: "binary".to_string(), - }); - let content = "binary"; - serialize_and_validate!(model, content); - - let model = Content::Data(Data { - string_data: "string".to_string(), - }); - let content = "string"; - serialize_and_validate!(model, content); -} diff --git a/yaserde/tests/ser_namespace.rs b/yaserde/tests/ser_namespace.rs deleted file mode 100644 index 906735b..0000000 --- a/yaserde/tests/ser_namespace.rs +++ /dev/null @@ -1,221 +0,0 @@ -#[macro_use] -extern crate yaserde; -#[macro_use] -extern crate yaserde_derive; - -use std::io::Write; -use yaserde::YaSerialize; - -#[test] -fn ser_struct_namespace() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "root", - prefix = "ns", - namespace = "ns: http://www.sample.com/ns/domain" - )] - pub struct XmlStruct { - #[yaserde(prefix = "ns")] - item: String, - } - - let model = XmlStruct { - item: "something".to_string(), - }; - - let content = - r#"something"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_enum_namespace() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "root", - prefix = "ns", - namespace = "ns: http://www.sample.com/ns/domain" - )] - pub enum XmlStruct { - #[yaserde(prefix = "ns")] - Item, - #[yaserde(prefix = "ns")] - ItemWithField(String), - } - - let model = XmlStruct::Item; - - let content = r#"ns:Item"#; - serialize_and_validate!(model, content); - - let model = XmlStruct::ItemWithField("Value".to_string()); - - let content = r#"Value"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_struct_multi_namespace() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "root", - namespace = "ns1: http://www.sample.com/ns/domain1", - namespace = "ns2: http://www.sample.com/ns/domain2" - )] - pub struct XmlStruct { - #[yaserde(prefix = "ns1")] - item_1: String, - #[yaserde(prefix = "ns2")] - item_2: String, - } - - let model = XmlStruct { - item_1: "something 1".to_string(), - item_2: "something 2".to_string(), - }; - - let content = r#"something 1something 2"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_enum_multi_namespace() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "root", - namespace = "ns1: http://www.sample.com/ns/domain1", - namespace = "ns2: http://www.sample.com/ns/domain2" - )] - pub enum XmlStruct { - #[yaserde(prefix = "ns1")] - Item1, - #[yaserde(prefix = "ns2")] - Item2, - } - - let model1 = XmlStruct::Item1; - let content = r#"ns1:Item1"#; - serialize_and_validate!(model1, content); - let model2 = XmlStruct::Item2; - let content = r#"ns2:Item2"#; - serialize_and_validate!(model2, content); -} - -#[test] -fn ser_struct_attribute_namespace() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "root", - namespace = "ns1: http://www.sample.com/ns/domain1", - namespace = "ns2: http://www.sample.com/ns/domain2" - )] - pub struct XmlStruct { - #[yaserde(prefix = "ns1")] - item_1: String, - #[yaserde(attribute, prefix = "ns2")] - item_2: String, - } - - let model = XmlStruct { - item_1: "something 1".to_string(), - item_2: "something 2".to_string(), - }; - - let content = r#"something 1"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_struct_default_namespace() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "tt", - namespace = "http://www.w3.org/ns/ttml", - namespace = "ttm: http://www.w3.org/ns/ttml#metadata" - )] - pub struct XmlStruct { - item: String, - } - - let model = XmlStruct { - item: "something".to_string(), - }; - - let content = r#"something"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_struct_default_namespace_via_attribute() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "tt", - default_namespace = "ttml", - namespace = "ttml: http://www.w3.org/ns/ttml", - namespace = "ttm: http://www.w3.org/ns/ttml#metadata" - )] - pub struct XmlStruct { - item: String, - } - - let model = XmlStruct { - item: "something".to_string(), - }; - - let content = r#"something"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_struct_default_namespace_via_attribute_with_prefix() { - #[derive(YaSerialize, PartialEq, Debug)] - #[yaserde( - root = "tt", - prefix = "TTML", - default_namespace = "TTML", - namespace = "TTML: http://www.w3.org/ns/ttml", - namespace = "ttm: http://www.w3.org/ns/ttml#metadata" - )] - pub struct XmlStruct { - #[yaserde(prefix = "TTML")] - item: String, - } - - let model = XmlStruct { - item: "something".to_string(), - }; - - let content = r#"something"#; - serialize_and_validate!(model, content); -} - -#[test] -fn ser_struct_namespace_nested() { - #[derive(YaSerialize, Default, PartialEq, Debug)] - #[yaserde(prefix = "nsa", namespace = "nsa: http://www.sample.com/ns/a")] - struct A { - #[yaserde(prefix = "nsa")] - alpha: i32, - } - - #[derive(YaSerialize, Default, PartialEq, Debug)] - #[yaserde(prefix = "nsb", namespace = "nsb: http://www.sample.com/ns/b")] - struct B { - // Note that name `nested` resides in `nsb` though it has a type from `nsa` - #[yaserde(prefix = "nsb")] - nested: A, - } - - serialize_and_validate!( - B { - nested: A { alpha: 32 } - }, - r#" - - - 32 - - - "# - ); -} diff --git a/yaserde/tests/ser_skip.rs b/yaserde/tests/skip_if.rs similarity index 96% rename from yaserde/tests/ser_skip.rs rename to yaserde/tests/skip_if.rs index 05cb74b..ad6f9de 100644 --- a/yaserde/tests/ser_skip.rs +++ b/yaserde/tests/skip_if.rs @@ -7,7 +7,7 @@ use std::io::Write; use yaserde::YaSerialize; #[test] -fn ser_skip_serializing_if_for_struct() { +fn skip_serializing_if_for_struct() { #[derive(YaSerialize, PartialEq, Debug)] #[yaserde(root = "base")] pub struct XmlStruct {