support Option type value

This commit is contained in:
Marc-Antoine Arnaud
2018-05-29 16:20:18 +02:00
parent ef37615458
commit 266b77428b
8 changed files with 714 additions and 47 deletions

116
yaserde/tests/der_option.rs Normal file
View File

@@ -0,0 +1,116 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Read;
use yaserde::YaDeserialize;
use yaserde::de::from_str;
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);
}

View File

@@ -48,6 +48,7 @@ macro_rules! convert_and_validate_for_attribute {
#[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");
@@ -64,6 +65,7 @@ fn de_type() {
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");

116
yaserde/tests/se_option.rs Normal file
View File

@@ -0,0 +1,116 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
use std::io::Write;
use yaserde::YaSerialize;
use yaserde::ser::to_string;
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_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);
}

View File

@@ -44,6 +44,7 @@ macro_rules! convert_and_validate_as_attribute {
#[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");
@@ -60,6 +61,7 @@ fn ser_type() {
convert_and_validate!(f32, -12.5 as f32, "-12.5");
convert_and_validate!(f64, -12.5 as f64, "-12.5");
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");