pub use rstest::{fixture, rstest}; use simple_logger::SimpleLogger; use std::fmt::Debug; #[fixture] fn logger() { let _ = SimpleLogger::new().init(); } mod de { use super::*; use serde::Deserialize; use serde_xml_rs::from_str; #[rstest] #[case::string("This is a String", "This is a String".to_string())] #[case::string("", "".to_string())] #[case::string(" ", "".to_string())] #[case::string("<boom/>", "".to_string())] #[case::string("", "♫".to_string())] #[case::string("", "♫".to_string())] #[case::string("]]>♫", "♫♫".to_string())] #[case::i64("0", 0i64)] #[case::i64("-2", -2i64)] #[case::i64("-1234", -1234i64)] #[case::i64(" -1234 ", -1234i64)] #[case::u64("0", 0u64)] #[case::u64("1234", 1234u64)] #[case::u64(" 1234 ", 1234u64)] #[case::bool("true", true)] #[case::bool("false", false)] #[case::unit("", ())] #[case::f64("3.0", 3.0f64)] #[case::f64("3.1", 3.1f64)] #[case::f64("-1.2", -1.2f64)] #[case::f64("0.4", 0.4f64)] #[case::f64("0.4e5", 0.4e5f64)] #[case::f64("0.4e15", 0.4e15f64)] #[case::f64_precision_troubles("0.4e-01", 0.4e-01f64)] #[case::f64(" 0.4e-01 ", 0.4e-01f64)] #[case::option("", Some("".to_string()))] #[case::option("", Some("".to_string()))] #[case::option(" ", Some("".to_string()))] #[case::option("42", Some("42".to_string()))] fn element_ok(_logger: (), #[case] document: &str, #[case] expected: T) where T: Deserialize<'de> + Debug + PartialEq, { let actual: T = from_str(document).unwrap(); assert_eq!(actual, expected); } #[rstest] #[case("verum", Some(true))] fn element_ko(_logger: (), #[case] document: &str, #[case] _type: Option) where T: Deserialize<'de> + Debug + PartialEq, { let actual: Result = from_str(document); assert!(actual.is_err()); } #[derive(PartialEq, Debug, Deserialize)] struct DummyAttribute { foo: T, } #[rstest] #[case(r#""#, DummyAttribute { foo: true })] #[case(r#""#, DummyAttribute { foo: false })] #[case(r#""#, DummyAttribute { foo: true })] #[case(r#""#, DummyAttribute { foo: false })] fn attribute_ok(_logger: (), #[case] document: &str, #[case] expected: T) where T: Deserialize<'de> + Debug + PartialEq, { let actual: T = from_str(document).unwrap(); assert_eq!(actual, expected); } } mod ser { use super::*; use serde::{self, Serialize}; use serde_xml_rs::to_string; #[derive(Serialize, Debug)] #[serde(rename = "bla")] struct Dummy { #[serde(rename = "$value")] value: T, } #[rstest] #[case::string("This is a String", "This is a String".to_string())] #[case::string("", "".to_string())] #[case::string("<boom/>", "".to_string())] #[case::string("", "♫".to_string())] #[case::string("♫<cookies/>♫", "♫♫".to_string())] #[case::i64("0", 0i64)] #[case::i64("-2", -2i64)] #[case::i64("-1234", -1234i64)] #[case::u64("0", 0u64)] #[case::u64("1234", 1234u64)] #[case::bool("true", true)] #[case::bool("false", false)] #[case::unit("", ())] #[case::f64("3", 3.0f64)] #[case::f64("3.1", 3.1f64)] #[case::f64("-1.2", -1.2f64)] #[case::f64("0.4", 0.4f64)] #[case::f64("40000", 0.4e5f64)] #[case::f64("400000000000000", 0.4e15f64)] #[case::f64_precision_troubles("0.04", 0.4e-01f64)] #[case::option("", Some("".to_string()))] #[case::option("42", Some("42".to_string()))] fn element_ok(_logger: (), #[case] expected: &str, #[case] value: T) where T: Serialize + Debug, { let actual = to_string(&Dummy { value }).unwrap(); assert_eq!( actual, format!(r#"{}"#, expected) ); } #[derive(Serialize, Debug)] #[serde(rename = "bla")] struct DummyAttribute { #[serde(rename = "@value")] value: T, } #[rstest] #[case::string(r#""#, "".to_string())] #[case::bool(r#""#, true)] #[case::bool(r#""#, false)] fn attribute_ok(_logger: (), #[case] expected: &str, #[case] value: T) where T: Serialize + Debug, { let actual = to_string(&DummyAttribute { value }).unwrap(); assert_eq!( actual, format!(r#"{}"#, expected) ); } }