#[macro_use] extern crate yaserde; #[macro_use] extern crate yaserde_derive; fn init() { let _ = env_logger::builder().is_test(true).try_init(); } #[test] fn basic_option_types() { init(); 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_u8), Some("12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_i8), Some("12")); test_for_type!(Option::, Some(-12_i8), Some("-12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_u16), Some("12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_i16), Some("12")); test_for_type!(Option::, Some(-12_i16), Some("-12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_u32), Some("12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_i32), Some("12")); test_for_type!(Option::, Some(-12_i32), Some("-12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_u64), Some("12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(12_i64), Some("12")); test_for_type!(Option::, Some(-12_i64), Some("-12")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(-12.5_f32), Some("-12.5")); test_for_type!(Option::, None, None); test_for_type!(Option::, Some(-12.5_f64), Some("-12.5")); test_for_type!(Option::, None, None); // TODO // 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_u8), Some("12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_i8), Some("12")); test_for_attribute_type!(Option::, Some(-12_i8), Some("-12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_u16), Some("12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_i16), Some("12")); test_for_attribute_type!(Option::, Some(-12_i16), Some("-12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_u32), Some("12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_i32), Some("12")); test_for_attribute_type!(Option::, Some(-12_i32), Some("-12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_u64), Some("12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(12_i64), Some("12")); test_for_attribute_type!(Option::, Some(-12_i64), Some("-12")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(-12.5_f32), Some("-12.5")); test_for_attribute_type!(Option::, None, None); test_for_attribute_type!(Option::, Some(-12.5_f64), Some("-12.5")); test_for_attribute_type!(Option::, None, None); } #[test] fn option_struct() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] struct Test { field: SubTest, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] struct SubTest { content: Option, } test_for_type!( Option::, Some(Test { field: SubTest { content: Some("value".to_string()) } }), Some("value") ); test_for_type!(Option::, None, None); } #[test] fn option_bool_no_crash_on_bad_input() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] struct Test { field: SubTest, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] struct SubTest { #[yaserde(attribute = true)] content: Option, } let content = "/ = yaserde::de::from_str(content); assert!(result.is_err()); } #[cfg(test)] mod tests { use super::*; use yaserde::de::from_str; #[derive(Debug, YaDeserialize, YaSerialize)] pub struct Car { #[yaserde(rename = "CarColor")] color: String, #[yaserde(rename = "CarBrand")] brand: String, } #[derive(Debug, YaDeserialize, YaSerialize)] pub struct Person { #[yaserde(flatten = true)] pub car: Option, } #[test] fn deserialize_without_car() { let person = r#" brown 25 true false "#; let person: Person = from_str(person).unwrap(); let expected_person = Person { car: None }; assert_eq!(person.car.is_none(), expected_person.car.is_none()); } }