restructure unit tests to check serialization and deserialisation on same features
This commit is contained in:
parent
0fd06404da
commit
ccf353ce6b
@ -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#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<binary_data>binary</binary_data>
|
||||
<string_data>string</string_data>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Binary>
|
||||
<binary_data>binary</binary_data>
|
||||
</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<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[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<String>),
|
||||
#[yaserde(rename = "working")]
|
||||
Working,
|
||||
}
|
||||
|
||||
impl Default for DateKind {
|
||||
fn default() -> Self {
|
||||
DateKind::Working
|
||||
}
|
||||
};
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<DateTime>
|
||||
<year>2020</year>
|
||||
<month>1</month>
|
||||
<day>1</day>
|
||||
<week>1</week>
|
||||
<century>21</century>
|
||||
<lunar_day>1</lunar_day>
|
||||
<time>10:40:03</time>
|
||||
<holidays>New Year's Day</holidays>
|
||||
<holidays>Novy God Day</holidays>
|
||||
<holidays>Polar Bear Swim Day</holidays>
|
||||
</DateTime>
|
||||
"#;
|
||||
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()
|
||||
])
|
||||
}
|
||||
);
|
||||
}
|
@ -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#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain" xmlns:ns2="http://www.sample.com/ns/domain_2">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns2:title>Little prince</ns2:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
convert_and_validate!(
|
||||
content,
|
||||
Book,
|
||||
Book {
|
||||
author: String::from("Antoine de Saint-Exupéry"),
|
||||
title: String::from("Little prince"),
|
||||
}
|
||||
);
|
||||
|
||||
let content = r#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns2:title xmlns:ns2="http://www.sample.com/ns/domain_2">Little prince</ns2:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
convert_and_validate!(
|
||||
content,
|
||||
Book,
|
||||
Book {
|
||||
author: String::from("Antoine de Saint-Exupéry"),
|
||||
title: String::from("Little prince"),
|
||||
}
|
||||
);
|
||||
|
||||
let content = r#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<book xmlns="http://www.sample.com/ns/domain">
|
||||
<author>Antoine de Saint-Exupéry</author>
|
||||
<ns2:title xmlns:ns2="http://www.sample.com/ns/domain_2">Little prince</ns2:title>
|
||||
</book>
|
||||
"#;
|
||||
convert_and_validate!(
|
||||
content,
|
||||
Book,
|
||||
Book {
|
||||
author: String::from("Antoine de Saint-Exupéry"),
|
||||
title: String::from("Little prince"),
|
||||
}
|
||||
);
|
||||
|
||||
let content = r#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain2">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns:title>Little prince</ns:title>
|
||||
</ns:book>"#;
|
||||
let loaded: Result<Book, String> = 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#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<nsb:B
|
||||
xmlns:nsa="http://www.sample.com/ns/a"
|
||||
xmlns:nsb="http://www.sample.com/ns/b">
|
||||
<nsb:nested>
|
||||
<nsa:alpha>32</nsa:alpha>
|
||||
</nsb:nested>
|
||||
</nsb:B>
|
||||
"#,
|
||||
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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ns:root xmlns:ns=\"http://www.sample.com/ns/domain\">ns:Item</ns:root>";
|
||||
convert_and_validate!(content, XmlStruct, XmlStruct::Item);
|
||||
}
|
@ -27,11 +27,10 @@ fn default_field_string() {
|
||||
serialize_and_validate!(model, content);
|
||||
deserialize_and_validate!(content, model, XmlStruct);
|
||||
|
||||
|
||||
let content = "<base><background>my_value</background></base>";
|
||||
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()
|
||||
}
|
||||
|
@ -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 = "<base><color>Black</color></base>";
|
||||
serialize_and_validate!(model, content);
|
||||
deserialize_and_validate!(content, model, XmlStruct);
|
||||
|
||||
let model = XmlStruct {
|
||||
color: Color::Custom {
|
||||
@ -110,45 +117,54 @@ fn ser_enum() {
|
||||
</base>"#;
|
||||
|
||||
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#"<base color="pink" />"#;
|
||||
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 = "<base><color><Field>some_text</Field></color></base>";
|
||||
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 = "<base><color><FullPath>some_text</FullPath></color></base>";
|
||||
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 = "<base><color><Integer>56</Integer></color></base>";
|
||||
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 = "<base><color><UserStruct><fi>24</fi><se>42</se></UserStruct></color></base>";
|
||||
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 = "<base><color><OptionString>some_text</OptionString></color></base>";
|
||||
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 = "<base><color /></base>";
|
||||
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 =
|
||||
"<base><color><OptionUserStruct><fi>12</fi><se>23</se></OptionUserStruct></color></base>";
|
||||
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 = "<base><color /></base>";
|
||||
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 = "<base><color><Strings>abc</Strings><Strings>def</Strings></color></base>";
|
||||
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 = "<base><color><Ints>23</Ints><Ints>45</Ints></color></base>";
|
||||
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 = "<base><color><Structs><fi>12</fi><se>23</se></Structs><Structs><fi>34</fi><se>45</se></Structs></color></base>";
|
||||
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 = "<base><color><renamed>87</renamed></color></base>";
|
||||
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 = "<base><color><renamed.with.dots>84</renamed.with.dots></color></base>";
|
||||
serialize_and_validate!(model, content);
|
||||
deserialize_and_validate!(content, model, XmlStruct);
|
||||
}
|
@ -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<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[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<String>),
|
||||
@ -88,6 +88,7 @@ fn basic_flatten() {
|
||||
</DateTime>"#;
|
||||
|
||||
serialize_and_validate!(model, content);
|
||||
deserialize_and_validate!(content, model, DateTime);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
442
yaserde/tests/namespace.rs
Normal file
442
yaserde/tests/namespace.rs
Normal file
@ -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#"
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns:title>Little prince</ns:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain" xmlns:ns2="http://www.sample.com/ns/domain_2">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns2:title>Little prince</ns2:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain">
|
||||
<author>Antoine de Saint-Exupéry</author>
|
||||
<ns:title>Little prince</ns:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns2:title xmlns:ns2="http://www.sample.com/ns/domain_2">Little prince</ns2:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<nsb:B xmlns:nsb="http://www.sample.com/ns/b">
|
||||
<nsb:nested xmlns:nsa="http://www.sample.com/ns/a">
|
||||
<nsa:alpha>32</nsa:alpha>
|
||||
</nsb:nested>
|
||||
</nsb:B>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<nsb:B xmlns:nsa="http://www.sample.com/ns/a" xmlns:nsb="http://www.sample.com/ns/b">
|
||||
<nsb:nested>
|
||||
<nsa:alpha>32</nsa:alpha>
|
||||
</nsb:nested>
|
||||
</nsb:B>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2" ns2:item_2="something 2">
|
||||
<ns1:item_1>something 1</ns1:item_1>
|
||||
</root>
|
||||
"#;
|
||||
|
||||
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#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata"><item>something</item></tt>"#;
|
||||
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#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata"><item>something</item></tt>"#;
|
||||
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#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata"><item>something</item></tt>"#;
|
||||
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:root xmlns:ns="http://www.sample.com/ns/domain">
|
||||
ns:Item
|
||||
</ns:root>
|
||||
"#;
|
||||
|
||||
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#"
|
||||
<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2">
|
||||
ns1:Item1
|
||||
</root>
|
||||
"#;
|
||||
serialize_and_validate!(model, content);
|
||||
deserialize_and_validate!(content, model, XmlStruct);
|
||||
|
||||
let model = XmlStruct::Item2;
|
||||
let content = r#"
|
||||
<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2">
|
||||
ns2:Item2
|
||||
</root>
|
||||
"#;
|
||||
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:root xmlns:ns="http://www.sample.com/ns/domain">
|
||||
ns:Item
|
||||
</ns:root>
|
||||
"#;
|
||||
|
||||
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#"<ns:root xmlns:ns="http://www.sample.com/ns/domain"><ns:ItemWithField>Value</ns:ItemWithField></ns:root>"#;
|
||||
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#"
|
||||
<ns:book xmlns:ns="http://www.sample.com/ns/domain2">
|
||||
<ns:author>Antoine de Saint-Exupéry</ns:author>
|
||||
<ns:title>Little prince</ns:title>
|
||||
</ns:book>
|
||||
"#;
|
||||
|
||||
let loaded: Result<Book, String> = yaserde::de::from_str(content);
|
||||
assert_eq!(
|
||||
loaded,
|
||||
Err("bad namespace for book, found http://www.sample.com/ns/domain2".to_string())
|
||||
);
|
||||
}
|
@ -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<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[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<String>),
|
||||
#[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#"
|
||||
<DateTime>
|
||||
<year>2020</year>
|
||||
<month>1</month>
|
||||
<day>1</day>
|
||||
<week>1</week>
|
||||
<century>21</century>
|
||||
<lunar_day>1</lunar_day>
|
||||
<time>10:40:03</time>
|
||||
<holidays>New Year's Day</holidays>
|
||||
<holidays>Novy God Day</holidays>
|
||||
<holidays>Polar Bear Swim Day</holidays>
|
||||
</DateTime>"#;
|
||||
|
||||
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 = "<binary_data>binary</binary_data><string_data>string</string_data>";
|
||||
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><binary_data>binary</binary_data></Binary>";
|
||||
serialize_and_validate!(model, content);
|
||||
|
||||
let model = Content::Data(Data {
|
||||
string_data: "string".to_string(),
|
||||
});
|
||||
let content = "<Data><string_data>string</string_data></Data>";
|
||||
serialize_and_validate!(model, content);
|
||||
}
|
@ -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#"<ns:root xmlns:ns="http://www.sample.com/ns/domain"><ns:item>something</ns:item></ns:root>"#;
|
||||
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:root xmlns:ns="http://www.sample.com/ns/domain">ns:Item</ns:root>"#;
|
||||
serialize_and_validate!(model, content);
|
||||
|
||||
let model = XmlStruct::ItemWithField("Value".to_string());
|
||||
|
||||
let content = r#"<ns:root xmlns:ns="http://www.sample.com/ns/domain"><ns:ItemWithField>Value</ns:ItemWithField></ns:root>"#;
|
||||
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#"<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2"><ns1:item_1>something 1</ns1:item_1><ns2:item_2>something 2</ns2:item_2></root>"#;
|
||||
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#"<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2">ns1:Item1</root>"#;
|
||||
serialize_and_validate!(model1, content);
|
||||
let model2 = XmlStruct::Item2;
|
||||
let content = r#"<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2">ns2:Item2</root>"#;
|
||||
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#"<root xmlns:ns1="http://www.sample.com/ns/domain1" xmlns:ns2="http://www.sample.com/ns/domain2" ns2:item_2="something 2"><ns1:item_1>something 1</ns1:item_1></root>"#;
|
||||
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#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata"><item>something</item></tt>"#;
|
||||
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#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata"><item>something</item></tt>"#;
|
||||
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#"<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata"><item>something</item></tt>"#;
|
||||
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#"
|
||||
<nsb:B xmlns:nsb="http://www.sample.com/ns/b">
|
||||
<nsb:nested xmlns:nsa="http://www.sample.com/ns/a">
|
||||
<nsa:alpha>32</nsa:alpha>
|
||||
</nsb:nested>
|
||||
</nsb:B>
|
||||
"#
|
||||
);
|
||||
}
|
@ -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 {
|
Loading…
Reference in New Issue
Block a user