#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
#[test]
fn default_field_string() {
init();
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_string")]
background: String,
}
let model = XmlStruct {
background: "my_default_value".to_string(),
};
let content = "";
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = "my_value";
let model = XmlStruct {
background: "my_value".to_string(),
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn default_field_boolean() {
init();
fn default_boolean() -> bool {
true
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_boolean")]
background: bool,
}
let content = "";
let model = XmlStruct { background: true };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = "false";
let model = XmlStruct { background: false };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn default_field_number() {
init();
fn default_number() -> u8 {
6
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_number")]
background: u8,
}
let content = "";
let model = XmlStruct { background: 6 };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = "4";
let model = XmlStruct { background: 4 };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn default_attribute_string() {
init();
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(attribute = true, default = "default_string")]
background: String,
}
let content = "";
let model = XmlStruct {
background: "my_default_value".to_string(),
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = r#""#;
let model = XmlStruct {
background: "black".to_string(),
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn module_inclusion() {
init();
mod module {
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "module")]
pub struct Module {
#[yaserde(attribute = true)]
pub color: String,
}
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
background: module::Module,
}
let content = r#""#;
let model = XmlStruct {
background: module::Module {
color: "blue".to_string(),
},
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}