start to restructure unit tests

This commit is contained in:
Marc-Antoine Arnaud 2020-04-20 22:23:12 +02:00
parent 1c9ee67ace
commit 0fd06404da
16 changed files with 574 additions and 823 deletions

View File

@ -137,3 +137,76 @@ fn default_visitor() {
test_type!(visit_u64, "Unexpected u64 \"\"");
test_type!(visit_str, "Unexpected str \"\"");
}
#[doc(hidden)]
mod testing {
#[macro_export]
macro_rules! test_for_type {
($type:ty, $value:expr, $content:expr) => {{
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(root = "data")]
pub struct Data {
item: $type,
}
let model = Data { item: $value };
let content = if let Some(str_value) = $content {
String::from("<data><item>") + str_value + "</item></data>"
} else {
String::from("<data />")
};
serialize_and_validate!(model, content);
deserialize_and_validate!(&content, model, Data);
}};
}
#[macro_export]
macro_rules! test_for_attribute_type {
($type: ty, $value: expr, $content: expr) => {{
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(root = "data")]
pub struct Data {
#[yaserde(attribute)]
item: $type,
}
let model = Data { item: $value };
let content = if let Some(str_value) = $content {
"<data item=\"".to_string() + str_value + "\" />"
} else {
"<data />".to_string()
};
serialize_and_validate!(model, content);
deserialize_and_validate!(&content, model, Data);
}};
}
#[macro_export]
macro_rules! deserialize_and_validate {
($content: expr, $model: expr, $struct: tt) => {
let loaded: Result<$struct, String> = yaserde::de::from_str($content);
assert_eq!(loaded, Ok($model));
};
}
#[macro_export]
macro_rules! serialize_and_validate {
($model: expr, $content: expr) => {
let data: Result<String, String> = yaserde::ser::to_string(&$model);
let content = String::from(r#"<?xml version="1.0" encoding="utf-8"?>"#) + &$content;
assert_eq!(
data,
Ok(
String::from(content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
);
};
}
}

View File

@ -1,93 +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_default_field_string() {
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_string")]
background: String,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
background: "my_default_value".to_string(),
}
);
}
#[test]
fn de_default_field_boolean() {
fn default_boolean() -> bool {
true
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_boolean")]
background: bool,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(content, XmlStruct, XmlStruct { background: true });
}
#[test]
fn de_default_field_number() {
fn default_number() -> u8 {
6
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_number")]
background: u8,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(content, XmlStruct, XmlStruct { background: 6 });
}
#[test]
fn de_default_attribute_string() {
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, default = "default_string")]
background: String,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
background: "my_default_value".to_string(),
}
);
}

View File

@ -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 {
($type: ty, $value: expr, $content: expr) => {{
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
item: Option<$type>,
}
let model = Data { item: $value };
let source = if let Some(content) = $content {
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data><item>")
+ content
+ "</item></data>"
} else {
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data />")
};
let loaded: Result<Data, String> = from_str(&source);
assert_eq!(loaded, Ok(model));
}};
}
macro_rules! convert_and_validate_for_attribute {
($type: ty, $value: expr, $content: expr) => {{
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
#[yaserde(attribute)]
item: Option<$type>,
}
let model = Data { item: $value };
let source = if let Some(content) = $content {
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data item=\"")
+ content
+ "\"></data>"
} else {
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data />")
};
let loaded: Result<Data, String> = from_str(&source);
assert_eq!(loaded, Ok(model));
}};
}
#[test]
fn de_option() {
convert_and_validate!(String, Some("test".to_string()), Some("test"));
convert_and_validate!(String, None, None);
convert_and_validate!(bool, Some(true), Some("true"));
convert_and_validate!(bool, None, None);
convert_and_validate!(u8, Some(12 as u8), Some("12"));
convert_and_validate!(u8, None, None);
convert_and_validate!(i8, Some(12 as i8), Some("12"));
convert_and_validate!(i8, Some(-12 as i8), Some("-12"));
convert_and_validate!(i8, None, None);
convert_and_validate!(u16, Some(12 as u16), Some("12"));
convert_and_validate!(u16, None, None);
convert_and_validate!(i16, Some(12 as i16), Some("12"));
convert_and_validate!(i16, Some(-12 as i16), Some("-12"));
convert_and_validate!(i16, None, None);
convert_and_validate!(u32, Some(12 as u32), Some("12"));
convert_and_validate!(u32, None, None);
convert_and_validate!(i32, Some(12 as i32), Some("12"));
convert_and_validate!(i32, Some(-12 as i32), Some("-12"));
convert_and_validate!(i32, None, None);
convert_and_validate!(u64, Some(12 as u64), Some("12"));
convert_and_validate!(u64, None, None);
convert_and_validate!(i64, Some(12 as i64), Some("12"));
convert_and_validate!(i64, Some(-12 as i64), Some("-12"));
convert_and_validate!(i64, None, None);
convert_and_validate!(f32, Some(-12.5_f32 as f32), Some("-12.5"));
convert_and_validate!(f32, None, None);
convert_and_validate!(f64, Some(-12.5 as f64), Some("-12.5"));
convert_and_validate!(f64, None, None);
convert_and_validate_for_attribute!(String, Some("test".to_string()), Some("test"));
convert_and_validate_for_attribute!(String, None, None);
convert_and_validate_for_attribute!(bool, Some(true), Some("true"));
convert_and_validate_for_attribute!(bool, None, None);
convert_and_validate_for_attribute!(u8, Some(12 as u8), Some("12"));
convert_and_validate_for_attribute!(u8, None, None);
convert_and_validate_for_attribute!(i8, Some(12 as i8), Some("12"));
convert_and_validate_for_attribute!(i8, Some(-12 as i8), Some("-12"));
convert_and_validate_for_attribute!(i8, None, None);
convert_and_validate_for_attribute!(u16, Some(12 as u16), Some("12"));
convert_and_validate_for_attribute!(u16, None, None);
convert_and_validate_for_attribute!(i16, Some(12 as i16), Some("12"));
convert_and_validate_for_attribute!(i16, Some(-12 as i16), Some("-12"));
convert_and_validate_for_attribute!(i16, None, None);
convert_and_validate_for_attribute!(u32, Some(12 as u32), Some("12"));
convert_and_validate_for_attribute!(u32, None, None);
convert_and_validate_for_attribute!(i32, Some(12 as i32), Some("12"));
convert_and_validate_for_attribute!(i32, Some(-12 as i32), Some("-12"));
convert_and_validate_for_attribute!(i32, None, None);
convert_and_validate_for_attribute!(u64, Some(12 as u64), Some("12"));
convert_and_validate_for_attribute!(u64, None, None);
convert_and_validate_for_attribute!(i64, Some(12 as i64), Some("12"));
convert_and_validate_for_attribute!(i64, Some(-12 as i64), Some("-12"));
convert_and_validate_for_attribute!(i64, None, None);
convert_and_validate_for_attribute!(f32, Some(-12.5 as f32), Some("-12.5"));
convert_and_validate_for_attribute!(f32, None, None);
convert_and_validate_for_attribute!(f64, Some(-12.5 as f64), Some("-12.5"));
convert_and_validate_for_attribute!(f64, None, None);
}
#[test]
fn de_option_struct() {
#[derive(YaDeserialize, Debug, PartialEq)]
struct Test {
field: SubTest,
}
#[derive(YaDeserialize, Debug, PartialEq)]
struct SubTest {
content: Option<String>,
}
impl Default for SubTest {
fn default() -> Self {
SubTest { content: None }
}
}
convert_and_validate!(
Test,
Some(Test {
field: SubTest {
content: Some("value".to_string())
}
}),
Some("<field><content>value</content></field>")
);
convert_and_validate!(Test, None, None);
}

View File

@ -1,81 +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 {
($type:ty, $value:expr, $content:expr) => {{
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
item: $type,
}
let model = Data { item: $value };
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data><item>")
+ $content
+ "</item></data>";
let loaded: Result<Data, String> = from_str(&content);
assert_eq!(loaded, Ok(model));
}};
}
macro_rules! convert_and_validate_for_attribute {
($type:ty, $value:expr, $content:expr) => {{
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
#[yaserde(attribute)]
item: $type,
}
let model = Data { item: $value };
let content =
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data item=\"") + $content + "\" />";
let loaded: Result<Data, String> = from_str(&content);
assert_eq!(loaded, Ok(model));
}};
}
#[test]
fn de_type() {
convert_and_validate!(String, "test".to_string(), "test");
convert_and_validate!(bool, true, "true");
convert_and_validate!(u8, 12 as u8, "12");
convert_and_validate!(i8, 12 as i8, "12");
convert_and_validate!(i8, -12 as i8, "-12");
convert_and_validate!(u16, 12 as u16, "12");
convert_and_validate!(i16, 12 as i16, "12");
convert_and_validate!(i16, -12 as i16, "-12");
convert_and_validate!(u32, 12 as u32, "12");
convert_and_validate!(i32, 12 as i32, "12");
convert_and_validate!(i32, -12 as i32, "-12");
convert_and_validate!(u64, 12 as u64, "12");
convert_and_validate!(i64, 12 as i64, "12");
convert_and_validate!(i64, -12 as i64, "-12");
convert_and_validate!(f32, -12.5_f32 as f32, "-12.5");
convert_and_validate!(f64, -12.5 as f64, "-12.5");
convert_and_validate_for_attribute!(String, "test".to_string(), "test");
convert_and_validate_for_attribute!(bool, true, "true");
convert_and_validate_for_attribute!(u8, 12 as u8, "12");
convert_and_validate_for_attribute!(i8, 12 as i8, "12");
convert_and_validate_for_attribute!(i8, -12 as i8, "-12");
convert_and_validate_for_attribute!(u16, 12 as u16, "12");
convert_and_validate_for_attribute!(i16, 12 as i16, "12");
convert_and_validate_for_attribute!(i16, -12 as i16, "-12");
convert_and_validate_for_attribute!(u32, 12 as u32, "12");
convert_and_validate_for_attribute!(i32, 12 as i32, "12");
convert_and_validate_for_attribute!(i32, -12 as i32, "-12");
convert_and_validate_for_attribute!(u64, 12 as u64, "12");
convert_and_validate_for_attribute!(i64, 12 as i64, "12");
convert_and_validate_for_attribute!(i64, -12 as i64, "-12");
convert_and_validate_for_attribute!(f32, -12.5 as f32, "-12.5");
convert_and_validate_for_attribute!(f64, -12.5 as f64, "-12.5");
}

113
yaserde/tests/default.rs Normal file
View File

@ -0,0 +1,113 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::{Read, Write};
use yaserde::{YaDeserialize, YaSerialize};
#[test]
fn default_field_string() {
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_string")]
background: String,
}
let model = XmlStruct {
background: "my_default_value".to_string(),
};
let content = "<base />";
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(),
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn default_field_boolean() {
fn default_boolean() -> bool {
true
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_boolean")]
background: bool,
}
let content = "<base />";
let model = XmlStruct { background: true };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = "<base><background>false</background></base>";
let model = XmlStruct { background: false };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn default_field_number() {
fn default_number() -> u8 {
6
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_number")]
background: u8,
}
let content = "<base />";
let model = XmlStruct { background: 6 };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = "<base><background>4</background></base>";
let model = XmlStruct { background: 4 };
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}
#[test]
fn de_default_attribute_string() {
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, default = "default_string")]
background: String,
}
let content = "<base />";
let model = XmlStruct {
background: "my_default_value".to_string(),
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
let content = r#"<base background="black" />"#;
let model = XmlStruct {
background: "black".to_string(),
};
serialize_and_validate!(model, content);
deserialize_and_validate!(content, model, XmlStruct);
}

145
yaserde/tests/flatten.rs Normal file
View File

@ -0,0 +1,145 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::{Read, Write};
use yaserde::{YaDeserialize, YaSerialize};
#[test]
fn basic_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 root_flatten_struct() {
#[derive(YaDeserialize, 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);
deserialize_and_validate!(content, model, Content);
}
#[test]
fn 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);
}

103
yaserde/tests/option.rs Normal file
View File

@ -0,0 +1,103 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::{Read, Write};
use yaserde::{YaDeserialize, YaSerialize};
#[test]
fn basic_option_types() {
test_for_type!(Option::<String>, Some("test".to_string()), Some("test"));
test_for_type!(Option::<String>, None, None);
test_for_type!(Option::<bool>, Some(true), Some("true"));
test_for_type!(Option::<bool>, None, None);
test_for_type!(Option::<u8>, Some(12 as u8), Some("12"));
test_for_type!(Option::<u8>, None, None);
test_for_type!(Option::<i8>, Some(12 as i8), Some("12"));
test_for_type!(Option::<i8>, Some(-12 as i8), Some("-12"));
test_for_type!(Option::<i8>, None, None);
test_for_type!(Option::<u16>, Some(12 as u16), Some("12"));
test_for_type!(Option::<u16>, None, None);
test_for_type!(Option::<i16>, Some(12 as i16), Some("12"));
test_for_type!(Option::<i16>, Some(-12 as i16), Some("-12"));
test_for_type!(Option::<i16>, None, None);
test_for_type!(Option::<u32>, Some(12 as u32), Some("12"));
test_for_type!(Option::<u32>, None, None);
test_for_type!(Option::<i32>, Some(12 as i32), Some("12"));
test_for_type!(Option::<i32>, Some(-12 as i32), Some("-12"));
test_for_type!(Option::<i32>, None, None);
test_for_type!(Option::<u64>, Some(12 as u64), Some("12"));
test_for_type!(Option::<u64>, None, None);
test_for_type!(Option::<i64>, Some(12 as i64), Some("12"));
test_for_type!(Option::<i64>, Some(-12 as i64), Some("-12"));
test_for_type!(Option::<i64>, None, None);
test_for_type!(Option::<f32>, Some(-12.5 as f32), Some("-12.5"));
test_for_type!(Option::<f32>, None, None);
test_for_type!(Option::<f64>, Some(-12.5 as f64), Some("-12.5"));
test_for_type!(Option::<f64>, None, None);
// test_for_type!(Option::<Vec::<u8>>, None, None);
// test_for_type!(Option::<Vec::<u8>>, Some(vec![0]), Some("0"));
// test_for_type!(Option::<Vec::<String>>, None, None);
// test_for_type!(Option::<Vec::<String>>, Some(vec!["test".to_string()]), Some("test"));
test_for_attribute_type!(Option::<String>, Some("test".to_string()), Some("test"));
test_for_attribute_type!(Option::<String>, None, None);
test_for_attribute_type!(Option::<bool>, Some(true), Some("true"));
test_for_attribute_type!(Option::<bool>, None, None);
test_for_attribute_type!(Option::<u8>, Some(12 as u8), Some("12"));
test_for_attribute_type!(Option::<u8>, None, None);
test_for_attribute_type!(Option::<i8>, Some(12 as i8), Some("12"));
test_for_attribute_type!(Option::<i8>, Some(-12 as i8), Some("-12"));
test_for_attribute_type!(Option::<i8>, None, None);
test_for_attribute_type!(Option::<u16>, Some(12 as u16), Some("12"));
test_for_attribute_type!(Option::<u16>, None, None);
test_for_attribute_type!(Option::<i16>, Some(12 as i16), Some("12"));
test_for_attribute_type!(Option::<i16>, Some(-12 as i16), Some("-12"));
test_for_attribute_type!(Option::<i16>, None, None);
test_for_attribute_type!(Option::<u32>, Some(12 as u32), Some("12"));
test_for_attribute_type!(Option::<u32>, None, None);
test_for_attribute_type!(Option::<i32>, Some(12 as i32), Some("12"));
test_for_attribute_type!(Option::<i32>, Some(-12 as i32), Some("-12"));
test_for_attribute_type!(Option::<i32>, None, None);
test_for_attribute_type!(Option::<u64>, Some(12 as u64), Some("12"));
test_for_attribute_type!(Option::<u64>, None, None);
test_for_attribute_type!(Option::<i64>, Some(12 as i64), Some("12"));
test_for_attribute_type!(Option::<i64>, Some(-12 as i64), Some("-12"));
test_for_attribute_type!(Option::<i64>, None, None);
test_for_attribute_type!(Option::<f32>, Some(-12.5 as f32), Some("-12.5"));
test_for_attribute_type!(Option::<f32>, None, None);
test_for_attribute_type!(Option::<f64>, Some(-12.5 as f64), Some("-12.5"));
test_for_attribute_type!(Option::<f64>, None, None);
}
#[test]
fn option_struct() {
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
struct Test {
field: SubTest,
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
struct SubTest {
content: Option<String>,
}
impl Default for SubTest {
fn default() -> Self {
SubTest { content: None }
}
}
test_for_type!(
Option::<Test>,
Some(Test {
field: SubTest {
content: Some("value".to_string())
}
}),
Some("<field><content>value</content></field>")
);
test_for_type!(Option::<Test>, None, None);
}

View File

@ -1,103 +0,0 @@
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($model:expr, $content:expr) => {
let data: Result<String, String> = to_string(&$model);
assert_eq!(data, Ok(String::from($content)));
};
}
#[test]
fn ser_default_field_string() {
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_string")]
background: String,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(
XmlStruct {
background: "my_default_value".to_string(),
},
content
);
let content =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><base><background>my_value</background></base>";
convert_and_validate!(
XmlStruct {
background: "my_value".to_string(),
},
content
);
}
#[test]
fn ser_default_field_boolean() {
fn default_boolean() -> bool {
true
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_boolean")]
background: bool,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(XmlStruct { background: true }, content);
let content =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><base><background>false</background></base>";
convert_and_validate!(XmlStruct { background: false }, content);
}
#[test]
fn ser_default_field_number() {
fn default_number() -> u8 {
6
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(default = "default_number")]
background: u8,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(XmlStruct { background: 6 }, content);
}
#[test]
fn ser_default_attribute_string() {
fn default_string() -> String {
"my_default_value".to_string()
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, default = "default_string")]
background: String,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(
XmlStruct {
background: "my_default_value".to_string(),
},
content
);
}

View File

@ -1,25 +1,11 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($model: expr, $content: expr) => {
let data: Result<String, String> = to_string(&$model);
assert_eq!(
data,
Ok(
String::from($content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
);
};
}
#[test]
fn ser_enum() {
#[derive(YaSerialize, PartialEq, Debug)]
@ -77,8 +63,8 @@ fn ser_enum() {
color: Color::Black,
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color>Black</color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color>Black</color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Color::Custom {
@ -103,7 +89,7 @@ fn ser_enum() {
},
};
let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"
<base>
<color><enabled>true</enabled>
<u8_value>8</u8_value>
@ -123,7 +109,7 @@ fn ser_enum() {
</color>
</base>"#;
convert_and_validate!(model, content);
serialize_and_validate!(model, content);
}
#[test]
@ -144,8 +130,8 @@ fn ser_attribute_enum() {
let model = XmlStruct { color: Color::Pink };
let content = r#"<?xml version="1.0" encoding="utf-8"?><base color="pink" />"#;
convert_and_validate!(model, content);
let content = r#"<base color="pink" />"#;
serialize_and_validate!(model, content);
}
#[test]
@ -190,73 +176,72 @@ fn ser_unnamed_enum() {
color: Enum::Field(String::from("some_text")),
};
let content =
r#"<?xml version="1.0" encoding="utf-8"?><base><color><Field>some_text</Field></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><Field>some_text</Field></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::FullPath(String::from("some_text")),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><FullPath>some_text</FullPath></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><FullPath>some_text</FullPath></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::Integer(56),
};
let content =
r#"<?xml version="1.0" encoding="utf-8"?><base><color><Integer>56</Integer></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><Integer>56</Integer></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::UserStruct(OtherStruct { fi: 24, se: 42 }),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><UserStruct><fi>24</fi><se>42</se></UserStruct></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><UserStruct><fi>24</fi><se>42</se></UserStruct></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::OptionString(Some(String::from("some_text"))),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><OptionString>some_text</OptionString></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><OptionString>some_text</OptionString></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::OptionString(None),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color /></base>"#;
convert_and_validate!(model, content);
let content = "<base><color /></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::OptionUserStruct(Some(OtherStruct { fi: 12, se: 23 })),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><OptionUserStruct><fi>12</fi><se>23</se></OptionUserStruct></color></base>"#;
convert_and_validate!(model, content);
let content =
"<base><color><OptionUserStruct><fi>12</fi><se>23</se></OptionUserStruct></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::OptionUserStruct(None),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color /></base>"#;
convert_and_validate!(model, content);
let content = "<base><color /></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::Strings(vec![String::from("abc"), String::from("def")]),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><Strings>abc</Strings><Strings>def</Strings></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><Strings>abc</Strings><Strings>def</Strings></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::Ints(vec![23, 45]),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><Ints>23</Ints><Ints>45</Ints></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><Ints>23</Ints><Ints>45</Ints></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::Structs(vec![
@ -265,21 +250,20 @@ fn ser_unnamed_enum() {
]),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><Structs><fi>12</fi><se>23</se></Structs><Structs><fi>34</fi><se>45</se></Structs></color></base>"#;
convert_and_validate!(model, content);
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);
let model = XmlStruct {
color: Enum::ToRename(87),
};
let content =
r#"<?xml version="1.0" encoding="utf-8"?><base><color><renamed>87</renamed></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><renamed>87</renamed></color></base>";
serialize_and_validate!(model, content);
let model = XmlStruct {
color: Enum::ToRenameDots(84),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><base><color><renamed.with.dots>84</renamed.with.dots></color></base>"#;
convert_and_validate!(model, content);
let content = "<base><color><renamed.with.dots>84</renamed.with.dots></color></base>";
serialize_and_validate!(model, content);
}

View File

@ -1,25 +1,11 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($model: expr, $content: expr) => {
let data: Result<String, String> = to_string(&$model);
assert_eq!(
data,
Ok(
String::from($content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
);
};
}
#[test]
fn ser_flatten() {
#[derive(Default, PartialEq, Debug, YaSerialize)]
@ -87,7 +73,6 @@ fn ser_flatten() {
};
let content = r#"
<?xml version="1.0" encoding="utf-8"?>
<DateTime>
<year>2020</year>
<month>1</month>
@ -101,7 +86,7 @@ fn ser_flatten() {
<holidays>Polar Bear Swim Day</holidays>
</DateTime>"#;
convert_and_validate!(model, content);
serialize_and_validate!(model, content);
}
#[test]
@ -117,8 +102,8 @@ fn ser_root_flatten_struct() {
binary_data: "binary".to_string(),
string_data: "string".to_string(),
};
let content = r#"<?xml version="1.0" encoding="utf-8"?><binary_data>binary</binary_data><string_data>string</string_data>"#;
convert_and_validate!(model, content);
let content = "<binary_data>binary</binary_data><string_data>string</string_data>";
serialize_and_validate!(model, content);
}
#[test]
@ -143,14 +128,12 @@ fn ser_root_flatten_enum() {
let model = Content::Binary(Binary {
binary_data: "binary".to_string(),
});
let content =
r#"<?xml version="1.0" encoding="utf-8"?><Binary><binary_data>binary</binary_data></Binary>"#;
convert_and_validate!(model, content);
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 =
r#"<?xml version="1.0" encoding="utf-8"?><Data><string_data>string</string_data></Data>"#;
convert_and_validate!(model, content);
let content = "<Data><string_data>string</string_data></Data>";
serialize_and_validate!(model, content);
}

View File

@ -1,25 +1,11 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($model: expr, $content: expr) => {
let data: Result<String, String> = to_string(&$model);
assert_eq!(
data,
Ok(
String::from($content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
);
};
}
#[test]
fn ser_struct_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
@ -37,8 +23,9 @@ fn ser_struct_namespace() {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ns:root xmlns:ns=\"http://www.sample.com/ns/domain\"><ns:item>something</ns:item></ns:root>";
convert_and_validate!(model, content);
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]
@ -58,13 +45,13 @@ fn ser_enum_namespace() {
let model = 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!(model, content);
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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ns:root xmlns:ns=\"http://www.sample.com/ns/domain\"><ns:ItemWithField>Value</ns:ItemWithField></ns:root>";
convert_and_validate!(model, content);
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]
@ -87,8 +74,8 @@ fn ser_struct_multi_namespace() {
item_2: "something 2".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><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>";
convert_and_validate!(model, content);
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]
@ -107,11 +94,11 @@ fn ser_enum_multi_namespace() {
}
let model1 = XmlStruct::Item1;
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root xmlns:ns1=\"http://www.sample.com/ns/domain1\" xmlns:ns2=\"http://www.sample.com/ns/domain2\">ns1:Item1</root>";
convert_and_validate!(model1, content);
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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root xmlns:ns1=\"http://www.sample.com/ns/domain1\" xmlns:ns2=\"http://www.sample.com/ns/domain2\">ns2:Item2</root>";
convert_and_validate!(model2, content);
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]
@ -134,8 +121,8 @@ fn ser_struct_attribute_namespace() {
item_2: "something 2".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><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>";
convert_and_validate!(model, content);
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]
@ -154,8 +141,8 @@ fn ser_struct_default_namespace() {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";
convert_and_validate!(model, content);
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]
@ -175,8 +162,8 @@ fn ser_struct_default_namespace_via_attribute() {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";
convert_and_validate!(model, content);
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]
@ -198,8 +185,8 @@ fn ser_struct_default_namespace_via_attribute_with_prefix() {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";
convert_and_validate!(model, content);
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]
@ -219,12 +206,11 @@ fn ser_struct_namespace_nested() {
nested: A,
}
convert_and_validate!(
serialize_and_validate!(
B {
nested: A { alpha: 32 }
},
r#"
<?xml version="1.0" encoding="utf-8"?>
<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>

View File

@ -1,147 +0,0 @@
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($type: ty, $value: expr, $content: expr) => {{
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
item: Option<$type>,
}
let model = Data { item: $value };
let data: Result<String, String> = to_string(&model);
let content = if let Some(str_value) = $content {
"<data><item>".to_string() + str_value + "</item></data>"
} else {
"<data />".to_string()
};
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>") + &content;
assert_eq!(data, Ok(content));
}};
}
macro_rules! convert_and_validate_as_attribute {
($type: ty, $value: expr, $content: expr) => {{
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
#[yaserde(attribute)]
item: Option<$type>,
}
let model = Data { item: $value };
let data: Result<String, String> = to_string(&model);
let content = if let Some(str_value) = $content {
"<data item=\"".to_string() + str_value + "\" />"
} else {
"<data />".to_string()
};
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>") + &content;
assert_eq!(data, Ok(content));
}};
}
#[test]
fn ser_option() {
convert_and_validate!(String, Some("test".to_string()), Some("test"));
convert_and_validate!(String, None, None);
convert_and_validate!(bool, Some(true), Some("true"));
convert_and_validate!(bool, None, None);
convert_and_validate!(u8, Some(12 as u8), Some("12"));
convert_and_validate!(u8, None, None);
convert_and_validate!(i8, Some(12 as i8), Some("12"));
convert_and_validate!(i8, Some(-12 as i8), Some("-12"));
convert_and_validate!(i8, None, None);
convert_and_validate!(u16, Some(12 as u16), Some("12"));
convert_and_validate!(u16, None, None);
convert_and_validate!(i16, Some(12 as i16), Some("12"));
convert_and_validate!(i16, Some(-12 as i16), Some("-12"));
convert_and_validate!(i16, None, None);
convert_and_validate!(u32, Some(12 as u32), Some("12"));
convert_and_validate!(u32, None, None);
convert_and_validate!(i32, Some(12 as i32), Some("12"));
convert_and_validate!(i32, Some(-12 as i32), Some("-12"));
convert_and_validate!(i32, None, None);
convert_and_validate!(u64, Some(12 as u64), Some("12"));
convert_and_validate!(u64, None, None);
convert_and_validate!(i64, Some(12 as i64), Some("12"));
convert_and_validate!(i64, Some(-12 as i64), Some("-12"));
convert_and_validate!(i64, None, None);
convert_and_validate!(f32, Some(-12.5 as f32), Some("-12.5"));
convert_and_validate!(f32, None, None);
convert_and_validate!(f64, Some(-12.5 as f64), Some("-12.5"));
convert_and_validate!(f64, None, None);
convert_and_validate!(Vec<u8>, None, None);
convert_and_validate!(Vec<u8>, Some(vec![0]), Some("0"));
convert_and_validate!(Vec<String>, None, None);
convert_and_validate!(Vec<String>, Some(vec!["test".to_string()]), Some("test"));
convert_and_validate_as_attribute!(String, Some("test".to_string()), Some("test"));
convert_and_validate_as_attribute!(String, None, None);
convert_and_validate_as_attribute!(bool, Some(true), Some("true"));
convert_and_validate_as_attribute!(bool, None, None);
convert_and_validate_as_attribute!(u8, Some(12 as u8), Some("12"));
convert_and_validate_as_attribute!(u8, None, None);
convert_and_validate_as_attribute!(i8, Some(12 as i8), Some("12"));
convert_and_validate_as_attribute!(i8, Some(-12 as i8), Some("-12"));
convert_and_validate_as_attribute!(i8, None, None);
convert_and_validate_as_attribute!(u16, Some(12 as u16), Some("12"));
convert_and_validate_as_attribute!(u16, None, None);
convert_and_validate_as_attribute!(i16, Some(12 as i16), Some("12"));
convert_and_validate_as_attribute!(i16, Some(-12 as i16), Some("-12"));
convert_and_validate_as_attribute!(i16, None, None);
convert_and_validate_as_attribute!(u32, Some(12 as u32), Some("12"));
convert_and_validate_as_attribute!(u32, None, None);
convert_and_validate_as_attribute!(i32, Some(12 as i32), Some("12"));
convert_and_validate_as_attribute!(i32, Some(-12 as i32), Some("-12"));
convert_and_validate_as_attribute!(i32, None, None);
convert_and_validate_as_attribute!(u64, Some(12 as u64), Some("12"));
convert_and_validate_as_attribute!(u64, None, None);
convert_and_validate_as_attribute!(i64, Some(12 as i64), Some("12"));
convert_and_validate_as_attribute!(i64, Some(-12 as i64), Some("-12"));
convert_and_validate_as_attribute!(i64, None, None);
convert_and_validate_as_attribute!(f32, Some(-12.5 as f32), Some("-12.5"));
convert_and_validate_as_attribute!(f32, None, None);
convert_and_validate_as_attribute!(f64, Some(-12.5 as f64), Some("-12.5"));
convert_and_validate_as_attribute!(f64, None, None);
}
#[test]
fn de_option_struct() {
#[derive(YaSerialize, Debug, PartialEq)]
struct Test {
field: SubTest,
}
#[derive(YaSerialize, Debug, PartialEq)]
struct SubTest {
content: Option<String>,
}
impl Default for SubTest {
fn default() -> Self {
SubTest { content: None }
}
}
convert_and_validate!(
Test,
Some(Test {
field: SubTest {
content: Some("value".to_string())
}
}),
Some("<field><content>value</content></field>")
);
convert_and_validate!(Test, None, None);
}

View File

@ -1,25 +1,11 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($model: expr, $content: expr) => {
let data: Result<String, String> = to_string(&$model);
assert_eq!(
data,
Ok(
String::from($content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
);
};
}
#[test]
fn ser_skip_serializing_if_for_struct() {
#[derive(YaSerialize, PartialEq, Debug)]
@ -60,6 +46,6 @@ fn ser_skip_serializing_if_for_struct() {
option_string_item: Some("something".to_string()),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base />";
convert_and_validate!(model, content);
let content = "<base />";
serialize_and_validate!(model, content);
}

View File

@ -1,83 +0,0 @@
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($type: ty, $value: expr, $content: expr) => {{
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
item: $type,
}
let model = Data { item: $value };
let data: Result<String, String> = to_string(&model);
let content = if $content == "" {
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data />")
} else {
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data><item>")
+ $content
+ "</item></data>"
};
assert_eq!(data, Ok(content));
}};
}
macro_rules! convert_and_validate_as_attribute {
($type: ty, $value: expr, $content: expr) => {{
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "data")]
pub struct Data {
#[yaserde(attribute)]
item: $type,
}
let model = Data { item: $value };
let data: Result<String, String> = to_string(&model);
let content =
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data item=\"") + $content + "\" />";
assert_eq!(data, Ok(content));
}};
}
#[test]
fn ser_type() {
convert_and_validate!(String, "test".to_string(), "test");
convert_and_validate!(bool, true, "true");
convert_and_validate!(u8, 12 as u8, "12");
convert_and_validate!(i8, 12 as i8, "12");
convert_and_validate!(i8, -12 as i8, "-12");
convert_and_validate!(u16, 12 as u16, "12");
convert_and_validate!(i16, 12 as i16, "12");
convert_and_validate!(i16, -12 as i16, "-12");
convert_and_validate!(u32, 12 as u32, "12");
convert_and_validate!(i32, 12 as i32, "12");
convert_and_validate!(i32, -12 as i32, "-12");
convert_and_validate!(u64, 12 as u64, "12");
convert_and_validate!(i64, 12 as i64, "12");
convert_and_validate!(i64, -12 as i64, "-12");
convert_and_validate!(f32, -12.5 as f32, "-12.5");
convert_and_validate!(f64, -12.5 as f64, "-12.5");
convert_and_validate!(Vec<String>, vec![], "");
convert_and_validate!(Vec<String>, vec!["test".to_string()], "test");
convert_and_validate_as_attribute!(String, "test".to_string(), "test");
convert_and_validate_as_attribute!(bool, true, "true");
convert_and_validate_as_attribute!(u8, 12 as u8, "12");
convert_and_validate_as_attribute!(i8, 12 as i8, "12");
convert_and_validate_as_attribute!(i8, -12 as i8, "-12");
convert_and_validate_as_attribute!(u16, 12 as u16, "12");
convert_and_validate_as_attribute!(i16, 12 as i16, "12");
convert_and_validate_as_attribute!(i16, -12 as i16, "-12");
convert_and_validate_as_attribute!(u32, 12 as u32, "12");
convert_and_validate_as_attribute!(i32, 12 as i32, "12");
convert_and_validate_as_attribute!(i32, -12 as i32, "-12");
convert_and_validate_as_attribute!(u64, 12 as u64, "12");
convert_and_validate_as_attribute!(i64, 12 as i64, "12");
convert_and_validate_as_attribute!(i64, -12 as i64, "-12");
convert_and_validate_as_attribute!(f32, -12.5 as f32, "-12.5");
convert_and_validate_as_attribute!(f64, -12.5 as f64, "-12.5");
}

View File

@ -1,25 +1,11 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::ser::to_string;
use yaserde::YaSerialize;
macro_rules! convert_and_validate {
($model: expr, $content: expr) => {
let data: Result<String, String> = to_string(&$model);
assert_eq!(
data,
Ok(
String::from($content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
);
};
}
#[test]
fn ser_basic() {
#[derive(YaSerialize, PartialEq, Debug)]
@ -32,8 +18,8 @@ fn ser_basic() {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><item>something</item></base>";
convert_and_validate!(model, content);
let content = "<base><item>something</item></base>";
serialize_and_validate!(model, content);
}
#[test]
@ -48,8 +34,8 @@ fn ser_list_of_items() {
items: vec!["something1".to_string(), "something2".to_string()],
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><items>something1</items><items>something2</items></base>";
convert_and_validate!(model, content);
let content = "<base><items>something1</items><items>something2</items></base>";
serialize_and_validate!(model, content);
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root = "base")]
@ -74,8 +60,9 @@ fn ser_list_of_items() {
],
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><items><field>something1</field></items><items><field>something2</field></items></base>";
convert_and_validate!(model2, content);
let content =
"<base><items><field>something1</field></items><items><field>something2</field></items></base>";
serialize_and_validate!(model2, content);
}
#[test]
@ -117,8 +104,8 @@ fn ser_attributes() {
},
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base item=\"something\"><sub subitem=\"sub-something\" /></base>";
convert_and_validate!(model, content);
let content = r#"<base item="something"><sub subitem="sub-something" /></base>"#;
serialize_and_validate!(model, content);
}
#[test]
@ -158,24 +145,20 @@ fn ser_attributes_complex() {
}
}
convert_and_validate!(
serialize_and_validate!(
Struct {
attr_option_string: None,
attr_option_enum: None,
},
r#"
<?xml version="1.0" encoding="utf-8"?>
<Struct />
"#
"<Struct />"
);
convert_and_validate!(
serialize_and_validate!(
Struct {
attr_option_string: Some("some value".to_string()),
attr_option_enum: Some(other_mod::AttrEnum::Variant2),
},
r#"
<?xml version="1.0" encoding="utf-8"?>
<Struct attr_option_string="some value" attr_option_enum="variant 2" />
"#
);
@ -224,8 +207,8 @@ fn ser_rename() {
version: "2.0.2".into(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base Item=\"something\"><sub sub_item=\"sub_something\" /><maj.min.bug>2.0.2</maj.min.bug></base>";
convert_and_validate!(model, content);
let content = r#"<base Item="something"><sub sub_item="sub_something" /><maj.min.bug>2.0.2</maj.min.bug></base>"#;
serialize_and_validate!(model, content);
}
#[test]
@ -273,8 +256,8 @@ fn ser_text_content_with_attributes() {
},
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base Item=\"something\"><sub sub_item=\"sub_something\">text_content</sub></base>";
convert_and_validate!(model, content);
let content = r#"<base Item="something"><sub sub_item="sub_something">text_content</sub></base>"#;
serialize_and_validate!(model, content);
}
#[test]
@ -289,8 +272,8 @@ fn ser_name_issue_21() {
name: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><name>something</name></base>";
convert_and_validate!(model, content);
let content = "<base><name>something</name></base>";
serialize_and_validate!(model, content);
}
#[test]
@ -326,6 +309,6 @@ fn ser_custom() {
month: 1,
day: Day { value: 5 },
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Date><Year>2020</Year><Month>1</Month><DoubleDay>10</DoubleDay></Date>";
convert_and_validate!(model, content);
let content = "<Date><Year>2020</Year><Month>1</Month><DoubleDay>10</DoubleDay></Date>";
serialize_and_validate!(model, content);
}

46
yaserde/tests/types.rs Normal file
View File

@ -0,0 +1,46 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::{Read, Write};
use yaserde::{YaDeserialize, YaSerialize};
#[test]
fn ser_type() {
test_for_type!(String, "test".to_string(), Some("test"));
test_for_type!(bool, true, Some("true"));
test_for_type!(u8, 12 as u8, Some("12"));
test_for_type!(i8, 12 as i8, Some("12"));
test_for_type!(i8, -12 as i8, Some("-12"));
test_for_type!(u16, 12 as u16, Some("12"));
test_for_type!(i16, 12 as i16, Some("12"));
test_for_type!(i16, -12 as i16, Some("-12"));
test_for_type!(u32, 12 as u32, Some("12"));
test_for_type!(i32, 12 as i32, Some("12"));
test_for_type!(i32, -12 as i32, Some("-12"));
test_for_type!(u64, 12 as u64, Some("12"));
test_for_type!(i64, 12 as i64, Some("12"));
test_for_type!(i64, -12 as i64, Some("-12"));
test_for_type!(f32, -12.5 as f32, Some("-12.5"));
test_for_type!(f64, -12.5 as f64, Some("-12.5"));
test_for_type!(Vec::<String>, vec![], None);
test_for_type!(Vec::<String>, vec!["test".to_string()], Some("test"));
test_for_attribute_type!(String, "test".to_string(), Some("test"));
test_for_attribute_type!(bool, true, Some("true"));
test_for_attribute_type!(u8, 12 as u8, Some("12"));
test_for_attribute_type!(i8, 12 as i8, Some("12"));
test_for_attribute_type!(i8, -12 as i8, Some("-12"));
test_for_attribute_type!(u16, 12 as u16, Some("12"));
test_for_attribute_type!(i16, 12 as i16, Some("12"));
test_for_attribute_type!(i16, -12 as i16, Some("-12"));
test_for_attribute_type!(u32, 12 as u32, Some("12"));
test_for_attribute_type!(i32, 12 as i32, Some("12"));
test_for_attribute_type!(i32, -12 as i32, Some("-12"));
test_for_attribute_type!(u64, 12 as u64, Some("12"));
test_for_attribute_type!(i64, 12 as i64, Some("12"));
test_for_attribute_type!(i64, -12 as i64, Some("-12"));
test_for_attribute_type!(f32, -12.5 as f32, Some("-12.5"));
test_for_attribute_type!(f64, -12.5 as f64, Some("-12.5"));
}