diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index 8796731..bc27947 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -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("") + str_value + "" + } else { + String::from("") + }; + + 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 { + "" + } else { + "".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 = yaserde::ser::to_string(&$model); + + let content = String::from(r#""#) + &$content; + assert_eq!( + data, + Ok( + String::from(content) + .split("\n") + .map(|s| s.trim()) + .collect::() + ) + ); + }; + } +} diff --git a/yaserde/tests/de_default.rs b/yaserde/tests/de_default.rs deleted file mode 100644 index bb0c406..0000000 --- a/yaserde/tests/de_default.rs +++ /dev/null @@ -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 = ""; - 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 = ""; - 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 = ""; - 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 = ""; - convert_and_validate!( - content, - XmlStruct, - XmlStruct { - background: "my_default_value".to_string(), - } - ); -} diff --git a/yaserde/tests/de_option.rs b/yaserde/tests/de_option.rs deleted file mode 100644 index a986ce4..0000000 --- a/yaserde/tests/de_option.rs +++ /dev/null @@ -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("") - + content - + "" - } else { - String::from("") - }; - - let loaded: Result = 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("" - } else { - String::from("") - }; - - let loaded: Result = 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, - } - - impl Default for SubTest { - fn default() -> Self { - SubTest { content: None } - } - } - - convert_and_validate!( - Test, - Some(Test { - field: SubTest { - content: Some("value".to_string()) - } - }), - Some("value") - ); - convert_and_validate!(Test, None, None); -} diff --git a/yaserde/tests/de_type.rs b/yaserde/tests/de_type.rs deleted file mode 100644 index b8775da..0000000 --- a/yaserde/tests/de_type.rs +++ /dev/null @@ -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("") - + $content - + ""; - - let loaded: Result = 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(""; - - let loaded: Result = 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"); -} diff --git a/yaserde/tests/default.rs b/yaserde/tests/default.rs new file mode 100644 index 0000000..23be05f --- /dev/null +++ b/yaserde/tests/default.rs @@ -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 = ""; + 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() { + fn default_boolean() -> bool { + true + } + + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde(root = "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() { + fn default_number() -> u8 { + 6 + } + + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + #[yaserde(root = "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 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 = ""; + 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); +} diff --git a/yaserde/tests/flatten.rs b/yaserde/tests/flatten.rs new file mode 100644 index 0000000..33e0293 --- /dev/null +++ b/yaserde/tests/flatten.rs @@ -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, + } + + #[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), + #[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#" + + 2020 + 1 + 1 + 1 + 21 + 1 + + New Year's Day + Novy God Day + Polar Bear Swim Day + "#; + + 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 = "binarystring"; + + 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"; + serialize_and_validate!(model, content); + + let model = Content::Data(Data { + string_data: "string".to_string(), + }); + + let content = "string"; + serialize_and_validate!(model, content); +} diff --git a/yaserde/tests/option.rs b/yaserde/tests/option.rs new file mode 100644 index 0000000..9609796 --- /dev/null +++ b/yaserde/tests/option.rs @@ -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::, Some("test".to_string()), Some("test")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(true), Some("true")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as u8), Some("12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as i8), Some("12")); + test_for_type!(Option::, Some(-12 as i8), Some("-12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as u16), Some("12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as i16), Some("12")); + test_for_type!(Option::, Some(-12 as i16), Some("-12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as u32), Some("12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as i32), Some("12")); + test_for_type!(Option::, Some(-12 as i32), Some("-12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as u64), Some("12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(12 as i64), Some("12")); + test_for_type!(Option::, Some(-12 as i64), Some("-12")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(-12.5 as f32), Some("-12.5")); + test_for_type!(Option::, None, None); + test_for_type!(Option::, Some(-12.5 as f64), Some("-12.5")); + test_for_type!(Option::, None, None); + + // test_for_type!(Option::>, None, None); + // test_for_type!(Option::>, Some(vec![0]), Some("0")); + // test_for_type!(Option::>, None, None); + // test_for_type!(Option::>, Some(vec!["test".to_string()]), Some("test")); + + test_for_attribute_type!(Option::, Some("test".to_string()), Some("test")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(true), Some("true")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as u8), Some("12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as i8), Some("12")); + test_for_attribute_type!(Option::, Some(-12 as i8), Some("-12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as u16), Some("12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as i16), Some("12")); + test_for_attribute_type!(Option::, Some(-12 as i16), Some("-12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as u32), Some("12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as i32), Some("12")); + test_for_attribute_type!(Option::, Some(-12 as i32), Some("-12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as u64), Some("12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(12 as i64), Some("12")); + test_for_attribute_type!(Option::, Some(-12 as i64), Some("-12")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(-12.5 as f32), Some("-12.5")); + test_for_attribute_type!(Option::, None, None); + test_for_attribute_type!(Option::, Some(-12.5 as f64), Some("-12.5")); + test_for_attribute_type!(Option::, None, None); +} + +#[test] +fn option_struct() { + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + struct Test { + field: SubTest, + } + + #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] + struct SubTest { + content: Option, + } + + impl Default for SubTest { + fn default() -> Self { + SubTest { content: None } + } + } + + test_for_type!( + Option::, + Some(Test { + field: SubTest { + content: Some("value".to_string()) + } + }), + Some("value") + ); + test_for_type!(Option::, None, None); +} diff --git a/yaserde/tests/ser_default.rs b/yaserde/tests/ser_default.rs deleted file mode 100644 index b461a7e..0000000 --- a/yaserde/tests/ser_default.rs +++ /dev/null @@ -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 = 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 = ""; - convert_and_validate!( - XmlStruct { - background: "my_default_value".to_string(), - }, - content - ); - let content = - "my_value"; - 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 = ""; - convert_and_validate!(XmlStruct { background: true }, content); - - let content = - "false"; - 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 = ""; - 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 = ""; - convert_and_validate!( - XmlStruct { - background: "my_default_value".to_string(), - }, - content - ); -} diff --git a/yaserde/tests/ser_enum.rs b/yaserde/tests/ser_enum.rs index c5c38e3..4de471d 100644 --- a/yaserde/tests/ser_enum.rs +++ b/yaserde/tests/ser_enum.rs @@ -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 = to_string(&$model); - assert_eq!( - data, - Ok( - String::from($content) - .split("\n") - .map(|s| s.trim()) - .collect::() - ) - ); - }; -} - #[test] fn ser_enum() { #[derive(YaSerialize, PartialEq, Debug)] @@ -77,8 +63,8 @@ fn ser_enum() { color: Color::Black, }; - let content = r#"Black"#; - convert_and_validate!(model, content); + let content = "Black"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Color::Custom { @@ -103,7 +89,7 @@ fn ser_enum() { }, }; - let content = r#" + let content = r#" true 8 @@ -123,7 +109,7 @@ fn ser_enum() { "#; - 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#""#; - convert_and_validate!(model, content); + let content = r#""#; + serialize_and_validate!(model, content); } #[test] @@ -190,73 +176,72 @@ fn ser_unnamed_enum() { color: Enum::Field(String::from("some_text")), }; - let content = - r#"some_text"#; - convert_and_validate!(model, content); + let content = "some_text"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::FullPath(String::from("some_text")), }; - let content = r#"some_text"#; - convert_and_validate!(model, content); + let content = "some_text"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::Integer(56), }; - let content = - r#"56"#; - convert_and_validate!(model, content); + let content = "56"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::UserStruct(OtherStruct { fi: 24, se: 42 }), }; - let content = r#"2442"#; - convert_and_validate!(model, content); + let content = "2442"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::OptionString(Some(String::from("some_text"))), }; - let content = r#"some_text"#; - convert_and_validate!(model, content); + let content = "some_text"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::OptionString(None), }; - let content = r#""#; - convert_and_validate!(model, content); + let content = ""; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::OptionUserStruct(Some(OtherStruct { fi: 12, se: 23 })), }; - let content = r#"1223"#; - convert_and_validate!(model, content); + let content = + "1223"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::OptionUserStruct(None), }; - let content = r#""#; - convert_and_validate!(model, content); + let content = ""; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::Strings(vec![String::from("abc"), String::from("def")]), }; - let content = r#"abcdef"#; - convert_and_validate!(model, content); + let content = "abcdef"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::Ints(vec![23, 45]), }; - let content = r#"2345"#; - convert_and_validate!(model, content); + let content = "2345"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::Structs(vec![ @@ -265,21 +250,20 @@ fn ser_unnamed_enum() { ]), }; - let content = r#"12233445"#; - convert_and_validate!(model, content); + let content = "12233445"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::ToRename(87), }; - let content = - r#"87"#; - convert_and_validate!(model, content); + let content = "87"; + serialize_and_validate!(model, content); let model = XmlStruct { color: Enum::ToRenameDots(84), }; - let content = r#"84"#; - convert_and_validate!(model, content); + let content = "84"; + serialize_and_validate!(model, content); } diff --git a/yaserde/tests/ser_flatten.rs b/yaserde/tests/ser_flatten.rs index 0127ed1..b4c04ec 100644 --- a/yaserde/tests/ser_flatten.rs +++ b/yaserde/tests/ser_flatten.rs @@ -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 = to_string(&$model); - assert_eq!( - data, - Ok( - String::from($content) - .split("\n") - .map(|s| s.trim()) - .collect::() - ) - ); - }; -} - #[test] fn ser_flatten() { #[derive(Default, PartialEq, Debug, YaSerialize)] @@ -87,7 +73,6 @@ fn ser_flatten() { }; let content = r#" - 2020 1 @@ -101,7 +86,7 @@ fn ser_flatten() { Polar Bear Swim Day "#; - 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#"binarystring"#; - convert_and_validate!(model, content); + let content = "binarystring"; + 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#"binary"#; - convert_and_validate!(model, content); + let content = "binary"; + serialize_and_validate!(model, content); let model = Content::Data(Data { string_data: "string".to_string(), }); - let content = - r#"string"#; - convert_and_validate!(model, content); + let content = "string"; + serialize_and_validate!(model, content); } diff --git a/yaserde/tests/ser_namespace.rs b/yaserde/tests/ser_namespace.rs index 5af32e8..906735b 100644 --- a/yaserde/tests/ser_namespace.rs +++ b/yaserde/tests/ser_namespace.rs @@ -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 = to_string(&$model); - assert_eq!( - data, - Ok( - String::from($content) - .split("\n") - .map(|s| s.trim()) - .collect::() - ) - ); - }; -} - #[test] fn ser_struct_namespace() { #[derive(YaSerialize, PartialEq, Debug)] @@ -37,8 +23,9 @@ fn ser_struct_namespace() { item: "something".to_string(), }; - let content = "something"; - convert_and_validate!(model, content); + let content = + r#"something"#; + serialize_and_validate!(model, content); } #[test] @@ -58,13 +45,13 @@ fn ser_enum_namespace() { let model = XmlStruct::Item; - let content = "ns:Item"; - convert_and_validate!(model, content); + let content = r#"ns:Item"#; + serialize_and_validate!(model, content); let model = XmlStruct::ItemWithField("Value".to_string()); - let content = "Value"; - convert_and_validate!(model, content); + let content = r#"Value"#; + serialize_and_validate!(model, content); } #[test] @@ -87,8 +74,8 @@ fn ser_struct_multi_namespace() { item_2: "something 2".to_string(), }; - let content = "something 1something 2"; - convert_and_validate!(model, content); + let content = r#"something 1something 2"#; + serialize_and_validate!(model, content); } #[test] @@ -107,11 +94,11 @@ fn ser_enum_multi_namespace() { } let model1 = XmlStruct::Item1; - let content = "ns1:Item1"; - convert_and_validate!(model1, content); + let content = r#"ns1:Item1"#; + serialize_and_validate!(model1, content); let model2 = XmlStruct::Item2; - let content = "ns2:Item2"; - convert_and_validate!(model2, content); + let content = r#"ns2:Item2"#; + serialize_and_validate!(model2, content); } #[test] @@ -134,8 +121,8 @@ fn ser_struct_attribute_namespace() { item_2: "something 2".to_string(), }; - let content = "something 1"; - convert_and_validate!(model, content); + let content = r#"something 1"#; + serialize_and_validate!(model, content); } #[test] @@ -154,8 +141,8 @@ fn ser_struct_default_namespace() { item: "something".to_string(), }; - let content = "something"; - convert_and_validate!(model, content); + let content = r#"something"#; + serialize_and_validate!(model, content); } #[test] @@ -175,8 +162,8 @@ fn ser_struct_default_namespace_via_attribute() { item: "something".to_string(), }; - let content = "something"; - convert_and_validate!(model, content); + let content = r#"something"#; + 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 = "something"; - convert_and_validate!(model, content); + let content = r#"something"#; + 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#" - 32 diff --git a/yaserde/tests/ser_option.rs b/yaserde/tests/ser_option.rs deleted file mode 100644 index 26f1c08..0000000 --- a/yaserde/tests/ser_option.rs +++ /dev/null @@ -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 = to_string(&model); - - let content = if let Some(str_value) = $content { - "".to_string() + str_value + "" - } else { - "".to_string() - }; - - let content = String::from("") + &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 = to_string(&model); - - let content = if let Some(str_value) = $content { - "" - } else { - "".to_string() - }; - - let content = String::from("") + &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, None, None); - convert_and_validate!(Vec, Some(vec![0]), Some("0")); - convert_and_validate!(Vec, None, None); - convert_and_validate!(Vec, 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, - } - - impl Default for SubTest { - fn default() -> Self { - SubTest { content: None } - } - } - - convert_and_validate!( - Test, - Some(Test { - field: SubTest { - content: Some("value".to_string()) - } - }), - Some("value") - ); - convert_and_validate!(Test, None, None); -} diff --git a/yaserde/tests/ser_skip.rs b/yaserde/tests/ser_skip.rs index 53c6be0..05cb74b 100644 --- a/yaserde/tests/ser_skip.rs +++ b/yaserde/tests/ser_skip.rs @@ -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 = to_string(&$model); - assert_eq!( - data, - Ok( - String::from($content) - .split("\n") - .map(|s| s.trim()) - .collect::() - ) - ); - }; -} - #[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 = ""; - convert_and_validate!(model, content); + let content = ""; + serialize_and_validate!(model, content); } diff --git a/yaserde/tests/ser_type.rs b/yaserde/tests/ser_type.rs deleted file mode 100644 index 1506cc2..0000000 --- a/yaserde/tests/ser_type.rs +++ /dev/null @@ -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 = to_string(&model); - let content = if $content == "" { - String::from("") - } else { - String::from("") - + $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: $type, - } - let model = Data { item: $value }; - - let data: Result = to_string(&model); - let content = - String::from(""; - 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, vec![], ""); - convert_and_validate!(Vec, 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"); -} diff --git a/yaserde/tests/serializer.rs b/yaserde/tests/serializer.rs index c3ff11b..2814f81 100644 --- a/yaserde/tests/serializer.rs +++ b/yaserde/tests/serializer.rs @@ -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 = to_string(&$model); - assert_eq!( - data, - Ok( - String::from($content) - .split("\n") - .map(|s| s.trim()) - .collect::() - ) - ); - }; -} - #[test] fn ser_basic() { #[derive(YaSerialize, PartialEq, Debug)] @@ -32,8 +18,8 @@ fn ser_basic() { item: "something".to_string(), }; - let content = "something"; - convert_and_validate!(model, content); + let content = "something"; + 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 = "something1something2"; - convert_and_validate!(model, content); + let content = "something1something2"; + serialize_and_validate!(model, content); #[derive(YaSerialize, PartialEq, Debug)] #[yaserde(root = "base")] @@ -74,8 +60,9 @@ fn ser_list_of_items() { ], }; - let content = "something1something2"; - convert_and_validate!(model2, content); + let content = + "something1something2"; + serialize_and_validate!(model2, content); } #[test] @@ -117,8 +104,8 @@ fn ser_attributes() { }, }; - let content = ""; - convert_and_validate!(model, content); + let content = r#""#; + 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#" - - - "# + "" ); - convert_and_validate!( + serialize_and_validate!( Struct { attr_option_string: Some("some value".to_string()), attr_option_enum: Some(other_mod::AttrEnum::Variant2), }, r#" - "# ); @@ -224,8 +207,8 @@ fn ser_rename() { version: "2.0.2".into(), }; - let content = "2.0.2"; - convert_and_validate!(model, content); + let content = r#"2.0.2"#; + serialize_and_validate!(model, content); } #[test] @@ -273,8 +256,8 @@ fn ser_text_content_with_attributes() { }, }; - let content = "text_content"; - convert_and_validate!(model, content); + let content = r#"text_content"#; + serialize_and_validate!(model, content); } #[test] @@ -289,8 +272,8 @@ fn ser_name_issue_21() { name: "something".to_string(), }; - let content = "something"; - convert_and_validate!(model, content); + let content = "something"; + serialize_and_validate!(model, content); } #[test] @@ -326,6 +309,6 @@ fn ser_custom() { month: 1, day: Day { value: 5 }, }; - let content = "2020110"; - convert_and_validate!(model, content); + let content = "2020110"; + serialize_and_validate!(model, content); } diff --git a/yaserde/tests/types.rs b/yaserde/tests/types.rs new file mode 100644 index 0000000..de5e2d4 --- /dev/null +++ b/yaserde/tests/types.rs @@ -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::, vec![], None); + test_for_type!(Vec::, 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")); +}