Added tests for deserialization

This commit is contained in:
TheSchemm 2022-05-05 08:24:57 -05:00
parent 2f8e87cd88
commit b27659b8dc

View File

@ -149,45 +149,81 @@ fn attribute_enum() {
#[test] #[test]
fn attribute_enum2() { fn attribute_enum2() {
#[derive(YaSerialize)] #[derive(Debug, PartialEq, YaSerialize, YaDeserialize)]
#[yaserde(rename = "child1")] #[yaserde(rename = "child1")]
struct Child1 { struct Child1 {
#[yaserde(attribute, rename = "val")] #[yaserde(attribute, rename = "val")]
pub val: String, pub val: String,
} }
#[derive(YaSerialize)] impl Default for Child1 {
fn default() -> Child1 {
Child1{val: "hello world".into()}
}
}
#[derive(Debug, PartialEq, YaSerialize, YaDeserialize)]
#[yaserde(rename = "child2")] #[yaserde(rename = "child2")]
struct Child2 { struct Child2 {
#[yaserde(attribute)] #[yaserde(attribute)]
pub num: u8, pub num: u8,
} }
#[derive(YaSerialize)] impl Default for Child2 {
fn default() -> Child2 {
Child2{num: 0}
}
}
#[derive(Debug, PartialEq, YaSerialize, YaDeserialize)]
#[yaserde(flatten)] #[yaserde(flatten)]
enum Base { enum Base {
#[yaserde(flatten)] #[yaserde(flatten, rename="child1")]
C1(Child1), C1(Child1),
#[yaserde(flatten, rename="child2")]
C2(Child2),
}
impl Default for Base {
fn default() -> Base {
Base::C1(Child1{val: "hello world".into()})
}
} }
let content = r#"<child1 val="hello world" />"#; let content = r#"<child1 val="hello world" />"#;
let model = Base::C1(Child1 { let model = Base::C1(Child1 {
val: "hello world".into(), val: "hello world".into(),
}); });
serialize_and_validate!(model, content);
#[derive(YaSerialize)] serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, Base);
let content = r#"<child2 num="7" />"#;
let model = Base::C2(Child2{ num: 7 });
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, Base);
#[derive(Debug, PartialEq, YaSerialize, YaDeserialize)]
#[yaserde(rename = "base")] #[yaserde(rename = "base")]
enum Base2 { enum Base2 {
#[yaserde(flatten)] #[yaserde(flatten)]
C1(Child1), C1(Child1),
} }
impl Default for Base2 {
fn default() -> Base2 {
Base2::C1(Child1{val: "hello world".into()})
}
}
let content = r#"<base><child1 val="hello world" /></base>"#; let content = r#"<base><child1 val="hello world" /></base>"#;
let model = Base2::C1(Child1 { let model = Base2::C1(Child1 {
val: "hello world".into(), val: "hello world".into(),
}); });
serialize_and_validate!(model, content); serialize_and_validate!(model, content);
println!("{:?}", yaserde::de::from_str::<Base2>(content));
deserialize_and_validate!(content, model, Base2);
} }
#[test] #[test]