From 37098393be49e1b751aca200eff7487b53bd59d1 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 4 Sep 2022 10:24:32 +0200 Subject: [PATCH] Split tests into multiple files --- instant-xml/tests/all.rs | 520 ------------------------------ instant-xml/tests/de-direct.rs | 40 +++ instant-xml/tests/de-nested.rs | 59 ++++ instant-xml/tests/de-ns.rs | 167 ++++++++++ instant-xml/tests/entities.rs | 71 ++++ instant-xml/tests/scalar.rs | 77 +++++ instant-xml/tests/ser-child-ns.rs | 50 +++ instant-xml/tests/ser-named.rs | 32 ++ instant-xml/tests/ser-nested.rs | 54 ++++ instant-xml/tests/ser-unit.rs | 12 + 10 files changed, 562 insertions(+), 520 deletions(-) delete mode 100644 instant-xml/tests/all.rs create mode 100644 instant-xml/tests/de-direct.rs create mode 100644 instant-xml/tests/de-nested.rs create mode 100644 instant-xml/tests/de-ns.rs create mode 100644 instant-xml/tests/entities.rs create mode 100644 instant-xml/tests/scalar.rs create mode 100644 instant-xml/tests/ser-child-ns.rs create mode 100644 instant-xml/tests/ser-named.rs create mode 100644 instant-xml/tests/ser-nested.rs create mode 100644 instant-xml/tests/ser-unit.rs diff --git a/instant-xml/tests/all.rs b/instant-xml/tests/all.rs deleted file mode 100644 index 37a82f8..0000000 --- a/instant-xml/tests/all.rs +++ /dev/null @@ -1,520 +0,0 @@ -use std::borrow::Cow; - -use similar_asserts::assert_eq; - -use instant_xml::{Error, FromXml, ToXml}; - -#[derive(Debug, Eq, PartialEq, ToXml)] -struct Unit; - -#[test] -fn unit() { - assert_eq!(Unit.to_xml().unwrap(), ""); - //assert_eq!(Unit::from_xml("").unwrap(), Unit); -} - -#[derive(Debug, Eq, PartialEq, ToXml)] -#[xml(ns(bar = "BAZ", foo = "BAR"))] -struct StructWithNamedFields { - flag: bool, - #[xml(ns(bar))] - string: String, - #[xml(ns("typo"))] - number: i32, -} - -// Tests: -// - Empty default namespace -// - Prefix namespace -// - Direct namespace - -#[test] -fn struct_with_named_fields() { - assert_eq!( - StructWithNamedFields { - flag: true, - string: "test".to_string(), - number: 1, - } - .to_xml() - .unwrap(), - "truetest1" - ); -} - -#[derive(Debug, Eq, PartialEq, ToXml)] -#[xml(ns("URI", dar = "BAZ", internal = "INTERNAL"))] -struct Nested { - #[xml(ns(dar))] - flag_parent_prefix: bool, - #[xml(ns(internal))] - flag_internal_prefix: bool, -} - -#[derive(Debug, Eq, PartialEq, ToXml)] -#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] -struct StructWithCustomField { - #[xml(attribute)] - int_attribute: i32, - #[xml(ns("BAZ"))] - flag_direct_namespace_same_the_same_as_prefix: bool, - #[xml(ns(bar))] - flag_prefix: bool, - #[xml(ns("DIFFERENT"))] - flag_direct_namespace: bool, - test: Nested, -} - -// Tests: -// - The same direct namespace as the one from prefix -// - Attribute handling -// - Omitting redeclared child default namespace -// - Omitting redeclared child namespace with different prefix -// - Unique direct namespace -// - Child unique prefix -// - Child repeated prefix -// - Child default namespace the same as parent -#[test] -fn struct_with_custom_field() { - assert_eq!( - StructWithCustomField { - int_attribute: 42, - flag_direct_namespace_same_the_same_as_prefix: true, - flag_prefix: false, - flag_direct_namespace: true, - test: Nested { - flag_parent_prefix: true, - flag_internal_prefix: false, - }, - } - .to_xml() - .unwrap(), - "truefalsetruetruefalse" - ); -} - -#[derive(Debug, Eq, PartialEq, ToXml)] -#[xml(ns(dar = "BAZ", internal = "INTERNAL"))] -struct NestedDifferentNamespace { - #[xml(ns(dar))] - flag_parent_prefix: bool, - #[xml(ns(internal))] - flag_internal_prefix: bool, -} - -#[derive(Debug, Eq, PartialEq, ToXml)] -#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] -struct StructChildNamespaces { - different_child_namespace: NestedDifferentNamespace, - same_child_namespace: Nested, -} - -// Tests: -// - Different child namespace -// - The same child namespace -#[test] -fn struct_child_namespaces() { - assert_eq!( - StructChildNamespaces { - different_child_namespace: NestedDifferentNamespace { - flag_parent_prefix: true, - flag_internal_prefix: false, - }, - same_child_namespace: Nested { - flag_parent_prefix: true, - flag_internal_prefix: false, - }, - } - .to_xml() - .unwrap(), - "truefalsetruefalse" - ); -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI", bar = "BAZ"))] -struct NestedDe { - #[xml(ns(bar))] - flag: bool, -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] -struct StructWithCustomFieldFromXml { - #[xml(ns(bar))] - flag: bool, - #[xml(attribute)] - flag_attribute: bool, - test: NestedDe, -} - -#[test] -fn struct_with_custom_field_from_xml() { - assert_eq!( - StructWithCustomFieldFromXml::from_xml("falsetrue").unwrap(), - StructWithCustomFieldFromXml { - flag: false, - flag_attribute: true, - test: NestedDe { flag: true } - } - ); - // Different order - assert_eq!( - StructWithCustomFieldFromXml::from_xml("truefalse").unwrap(), - StructWithCustomFieldFromXml { - flag: false, - flag_attribute: true, - test: NestedDe { flag: true } - } - ); - - // Different prefixes then in definition - assert_eq!( - StructWithCustomFieldFromXml::from_xml("falsetrue").unwrap(), - StructWithCustomFieldFromXml { - flag: false, - flag_attribute: true, - test: NestedDe { flag: true } - } - ); - - assert_eq!( - NestedDe::from_xml( - "true" - ) - .unwrap(), - NestedDe { flag: true } - ); -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -struct NestedWrongNamespace { - flag: bool, -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI", bar = "BAZ"))] -struct StructWithCorrectNestedNamespace { - test: NestedDe, -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI", bar = "BAZ"))] -struct StructWithWrongNestedNamespace { - test: NestedWrongNamespace, -} - -#[test] -fn default_namespaces() { - // Default namespace not-nested - assert_eq!( - NestedDe::from_xml( - "true" - ) - .unwrap(), - NestedDe { flag: true } - ); - - // Default namespace not-nested - wrong namespace - assert_eq!( - NestedDe::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); - - // Correct child namespace - assert_eq!( - StructWithCorrectNestedNamespace::from_xml("true").unwrap(), - StructWithCorrectNestedNamespace { - test: NestedDe { flag: true } - } - ); - - // Correct child namespace - without child redefinition - assert_eq!( - StructWithCorrectNestedNamespace::from_xml("true").unwrap(), - StructWithCorrectNestedNamespace { - test: NestedDe { flag: true } - } - ); - - // Different child namespace - assert_eq!( - StructWithWrongNestedNamespace::from_xml("true").unwrap(), - StructWithWrongNestedNamespace { - test: NestedWrongNamespace { - flag: true - } - } - ); - - // Wrong child namespace - assert_eq!( - StructWithWrongNestedNamespace::from_xml("true").unwrap_err(), - Error::WrongNamespace - ); -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI", bar = "BAZ"))] -struct NestedOtherNamespace { - #[xml(ns(bar))] - flag: bool, -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI", bar = "BAZ"))] -struct StructOtherNamespace { - test: NestedOtherNamespace, -} - -#[test] -fn other_namespaces() { - // Other namespace not-nested - assert_eq!( - NestedOtherNamespace::from_xml( - "true" - ) - .unwrap(), - NestedOtherNamespace { flag: true } - ); - - // Other namespace not-nested - wrong defined namespace - assert_eq!( - NestedOtherNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); - - // Other namespace not-nested - wrong parser namespace - assert_eq!( - NestedOtherNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); - - // Other namespace not-nested - missing parser prefix - assert_eq!( - NestedOtherNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); - - // Correct child other namespace - assert_eq!( - StructOtherNamespace::from_xml( - "true" - ) - .unwrap(), - StructOtherNamespace { - test: NestedOtherNamespace { - flag: true, - } - } - ); - - // Correct child other namespace - without child redefinition - assert_eq!( - StructOtherNamespace::from_xml( - "true" - ) - .unwrap(), - StructOtherNamespace { - test: NestedOtherNamespace { - flag: true, - } - } - ); - - // Wrong child other namespace - without child redefinition - assert_eq!( - StructOtherNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); -} - -#[derive(Debug, Eq, PartialEq, FromXml)] -#[xml(ns("URI"))] -struct StructDirectNamespace { - #[xml(ns("BAZ"))] - flag: bool, -} - -#[test] -fn direct_namespaces() { - // Correct direct namespace - assert_eq!( - StructDirectNamespace::from_xml( - "true" - ) - .unwrap(), - StructDirectNamespace { flag: true } - ); - - // Wrong direct namespace - assert_eq!( - StructDirectNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); - - // Wrong direct namespace - missing namespace - assert_eq!( - StructDirectNamespace::from_xml( - "true" - ) - .unwrap_err(), - Error::WrongNamespace - ); -} - -#[derive(Debug, PartialEq, Eq, FromXml, ToXml)] -#[xml(ns("URI"))] -struct NestedLifetimes<'a> { - flag: bool, - str_type_a: &'a str, -} - -#[derive(Debug, PartialEq, FromXml, ToXml)] -#[xml(ns("URI"))] -struct StructDeserializerScalars<'a, 'b> { - bool_type: bool, - i8_type: i8, - u32_type: u32, - string_type: String, - str_type_a: &'a str, - str_type_b: &'b str, - char_type: char, - f32_type: f32, - nested: NestedLifetimes<'a>, - cow: Cow<'a, str>, - option: Option<&'a str>, -} - -#[test] -fn scalars() { - assert_eq!( - StructDeserializerScalars::from_xml( - "true142stringlifetime alifetime bc1.20trueasd123" - ) - .unwrap(), - StructDeserializerScalars{ - bool_type: true, - i8_type: 1, - u32_type: 42, - string_type: "string".to_string(), - str_type_a: "lifetime a", - str_type_b: "lifetime b", - char_type: 'c', - f32_type: 1.20, - nested: NestedLifetimes { - flag: true, - str_type_a: "asd" - }, - cow: Cow::from("123"), - option: None, - } - ); - - // Option none - assert_eq!( - StructDeserializerScalars::from_xml( - "true142stringlifetime alifetime bc1.2trueasd123" - ).unwrap(), - StructDeserializerScalars{ - bool_type: true, - i8_type: 1, - u32_type: 42, - string_type: "string".to_string(), - str_type_a: "lifetime a", - str_type_b: "lifetime b", - char_type: 'c', - f32_type: 1.20, - nested: NestedLifetimes { - flag: true, - str_type_a: "asd" - }, - cow: Cow::from("123"), - option: Some("asd"), - } - ); -} - -#[derive(Debug, PartialEq, Eq, FromXml, ToXml)] -#[xml(ns("URI"))] -struct StructSpecialEntities<'a> { - string: String, - str: &'a str, - cow: Cow<'a, str>, -} - -#[test] -fn escape_back() { - assert_eq!( - StructSpecialEntities::from_xml( - "<>&"'adsad"strstr&" - ) - .unwrap(), - StructSpecialEntities { - string: String::from("<>&\"'adsad\""), - str: "str", - cow: Cow::Owned("str&".to_string()), - } - ); - - // Wrong str char - assert_eq!( - StructSpecialEntities::from_xml( - "<>&"'adsad"str&" - ) - .unwrap_err(), - Error::Other("Unsupported char: str&".to_string()) - ); - - // Borrowed - let escape_back = StructSpecialEntities::from_xml( - "<>&"'adsad"strstr" - ) - .unwrap(); - - if let Cow::Owned(_) = escape_back.cow { - panic!("Should be Borrowed") - } - - // Owned - let escape_back = StructSpecialEntities::from_xml( - "<>&"'adsad"strstr&" - ) - .unwrap(); - - if let Cow::Borrowed(_) = escape_back.cow { - panic!("Should be Owned") - } -} - -#[test] -fn special_entities() { - assert_eq!( - StructSpecialEntities{ - string: "&\"<>\'aa".to_string(), - str: "&\"<>\'bb", - cow: Cow::from("&\"<>\'cc"), - } - .to_xml() - .unwrap(), - "&"<>'aa&"<>'bb&"<>'cc" - ); -} diff --git a/instant-xml/tests/de-direct.rs b/instant-xml/tests/de-direct.rs new file mode 100644 index 0000000..b952645 --- /dev/null +++ b/instant-xml/tests/de-direct.rs @@ -0,0 +1,40 @@ +use similar_asserts::assert_eq; + +use instant_xml::{Error, FromXml}; + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI"))] +struct StructDirectNamespace { + #[xml(ns("BAZ"))] + flag: bool, +} + +#[test] +fn direct_namespaces() { + // Correct direct namespace + assert_eq!( + StructDirectNamespace::from_xml( + "true" + ) + .unwrap(), + StructDirectNamespace { flag: true } + ); + + // Wrong direct namespace + assert_eq!( + StructDirectNamespace::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); + + // Wrong direct namespace - missing namespace + assert_eq!( + StructDirectNamespace::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); +} diff --git a/instant-xml/tests/de-nested.rs b/instant-xml/tests/de-nested.rs new file mode 100644 index 0000000..17e8f3a --- /dev/null +++ b/instant-xml/tests/de-nested.rs @@ -0,0 +1,59 @@ +use similar_asserts::assert_eq; + +use instant_xml::FromXml; + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct NestedDe { + #[xml(ns(bar))] + flag: bool, +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] +struct StructWithCustomFieldFromXml { + #[xml(ns(bar))] + flag: bool, + #[xml(attribute)] + flag_attribute: bool, + test: NestedDe, +} + +#[test] +fn struct_with_custom_field_from_xml() { + assert_eq!( + StructWithCustomFieldFromXml::from_xml("falsetrue").unwrap(), + StructWithCustomFieldFromXml { + flag: false, + flag_attribute: true, + test: NestedDe { flag: true } + } + ); + // Different order + assert_eq!( + StructWithCustomFieldFromXml::from_xml("truefalse").unwrap(), + StructWithCustomFieldFromXml { + flag: false, + flag_attribute: true, + test: NestedDe { flag: true } + } + ); + + // Different prefixes then in definition + assert_eq!( + StructWithCustomFieldFromXml::from_xml("falsetrue").unwrap(), + StructWithCustomFieldFromXml { + flag: false, + flag_attribute: true, + test: NestedDe { flag: true } + } + ); + + assert_eq!( + NestedDe::from_xml( + "true" + ) + .unwrap(), + NestedDe { flag: true } + ); +} diff --git a/instant-xml/tests/de-ns.rs b/instant-xml/tests/de-ns.rs new file mode 100644 index 0000000..a0c47ee --- /dev/null +++ b/instant-xml/tests/de-ns.rs @@ -0,0 +1,167 @@ +use similar_asserts::assert_eq; + +use instant_xml::{Error, FromXml}; + +#[derive(Debug, Eq, PartialEq, FromXml)] +struct NestedWrongNamespace { + flag: bool, +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct NestedDe { + #[xml(ns(bar))] + flag: bool, +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct StructWithCorrectNestedNamespace { + test: NestedDe, +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct StructWithWrongNestedNamespace { + test: NestedWrongNamespace, +} + +#[test] +fn default_namespaces() { + // Default namespace not-nested + assert_eq!( + NestedDe::from_xml( + "true" + ) + .unwrap(), + NestedDe { flag: true } + ); + + // Default namespace not-nested - wrong namespace + assert_eq!( + NestedDe::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); + + // Correct child namespace + assert_eq!( + StructWithCorrectNestedNamespace::from_xml("true").unwrap(), + StructWithCorrectNestedNamespace { + test: NestedDe { flag: true } + } + ); + + // Correct child namespace - without child redefinition + assert_eq!( + StructWithCorrectNestedNamespace::from_xml("true").unwrap(), + StructWithCorrectNestedNamespace { + test: NestedDe { flag: true } + } + ); + + // Different child namespace + assert_eq!( + StructWithWrongNestedNamespace::from_xml("true").unwrap(), + StructWithWrongNestedNamespace { + test: NestedWrongNamespace { + flag: true + } + } + ); + + // Wrong child namespace + assert_eq!( + StructWithWrongNestedNamespace::from_xml("true").unwrap_err(), + Error::WrongNamespace + ); +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct NestedOtherNamespace { + #[xml(ns(bar))] + flag: bool, +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct StructOtherNamespace { + test: NestedOtherNamespace, +} + +#[test] +fn other_namespaces() { + // Other namespace not-nested + assert_eq!( + NestedOtherNamespace::from_xml( + "true" + ) + .unwrap(), + NestedOtherNamespace { flag: true } + ); + + // Other namespace not-nested - wrong defined namespace + assert_eq!( + NestedOtherNamespace::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); + + // Other namespace not-nested - wrong parser namespace + assert_eq!( + NestedOtherNamespace::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); + + // Other namespace not-nested - missing parser prefix + assert_eq!( + NestedOtherNamespace::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); + + // Correct child other namespace + assert_eq!( + StructOtherNamespace::from_xml( + "true" + ) + .unwrap(), + StructOtherNamespace { + test: NestedOtherNamespace { + flag: true, + } + } + ); + + // Correct child other namespace - without child redefinition + assert_eq!( + StructOtherNamespace::from_xml( + "true" + ) + .unwrap(), + StructOtherNamespace { + test: NestedOtherNamespace { + flag: true, + } + } + ); + + // Wrong child other namespace - without child redefinition + assert_eq!( + StructOtherNamespace::from_xml( + "true" + ) + .unwrap_err(), + Error::WrongNamespace + ); +} diff --git a/instant-xml/tests/entities.rs b/instant-xml/tests/entities.rs new file mode 100644 index 0000000..12589e6 --- /dev/null +++ b/instant-xml/tests/entities.rs @@ -0,0 +1,71 @@ +use std::borrow::Cow; + +use similar_asserts::assert_eq; + +use instant_xml::{Error, FromXml, ToXml}; + +#[derive(Debug, PartialEq, Eq, FromXml, ToXml)] +#[xml(ns("URI"))] +struct StructSpecialEntities<'a> { + string: String, + str: &'a str, + cow: Cow<'a, str>, +} + +#[test] +fn escape_back() { + assert_eq!( + StructSpecialEntities::from_xml( + "<>&"'adsad"strstr&" + ) + .unwrap(), + StructSpecialEntities { + string: String::from("<>&\"'adsad\""), + str: "str", + cow: Cow::Owned("str&".to_string()), + } + ); + + // Wrong str char + assert_eq!( + StructSpecialEntities::from_xml( + "<>&"'adsad"str&" + ) + .unwrap_err(), + Error::Other("Unsupported char: str&".to_string()) + ); + + // Borrowed + let escape_back = StructSpecialEntities::from_xml( + "<>&"'adsad"strstr" + ) + .unwrap(); + + if let Cow::Owned(_) = escape_back.cow { + panic!("Should be Borrowed") + } + + // Owned + let escape_back = StructSpecialEntities::from_xml( + "<>&"'adsad"strstr&" + ) + .unwrap(); + + if let Cow::Borrowed(_) = escape_back.cow { + panic!("Should be Owned") + } +} + +#[test] +fn special_entities() { + assert_eq!( + StructSpecialEntities{ + string: "&\"<>\'aa".to_string(), + str: "&\"<>\'bb", + cow: Cow::from("&\"<>\'cc"), + } + .to_xml() + .unwrap(), + "&"<>'aa&"<>'bb&"<>'cc" + ); +} diff --git a/instant-xml/tests/scalar.rs b/instant-xml/tests/scalar.rs new file mode 100644 index 0000000..88d85e9 --- /dev/null +++ b/instant-xml/tests/scalar.rs @@ -0,0 +1,77 @@ +use std::borrow::Cow; + +use similar_asserts::assert_eq; + +use instant_xml::{FromXml, ToXml}; + +#[derive(Debug, PartialEq, Eq, FromXml, ToXml)] +#[xml(ns("URI"))] +struct NestedLifetimes<'a> { + flag: bool, + str_type_a: &'a str, +} + +#[derive(Debug, PartialEq, FromXml, ToXml)] +#[xml(ns("URI"))] +struct StructDeserializerScalars<'a, 'b> { + bool_type: bool, + i8_type: i8, + u32_type: u32, + string_type: String, + str_type_a: &'a str, + str_type_b: &'b str, + char_type: char, + f32_type: f32, + nested: NestedLifetimes<'a>, + cow: Cow<'a, str>, + option: Option<&'a str>, +} + +#[test] +fn scalars() { + assert_eq!( + StructDeserializerScalars::from_xml( + "true142stringlifetime alifetime bc1.20trueasd123" + ) + .unwrap(), + StructDeserializerScalars{ + bool_type: true, + i8_type: 1, + u32_type: 42, + string_type: "string".to_string(), + str_type_a: "lifetime a", + str_type_b: "lifetime b", + char_type: 'c', + f32_type: 1.20, + nested: NestedLifetimes { + flag: true, + str_type_a: "asd" + }, + cow: Cow::from("123"), + option: None, + } + ); + + // Option none + assert_eq!( + StructDeserializerScalars::from_xml( + "true142stringlifetime alifetime bc1.2trueasd123" + ).unwrap(), + StructDeserializerScalars{ + bool_type: true, + i8_type: 1, + u32_type: 42, + string_type: "string".to_string(), + str_type_a: "lifetime a", + str_type_b: "lifetime b", + char_type: 'c', + f32_type: 1.20, + nested: NestedLifetimes { + flag: true, + str_type_a: "asd" + }, + cow: Cow::from("123"), + option: Some("asd"), + } + ); +} diff --git a/instant-xml/tests/ser-child-ns.rs b/instant-xml/tests/ser-child-ns.rs new file mode 100644 index 0000000..4de3fb2 --- /dev/null +++ b/instant-xml/tests/ser-child-ns.rs @@ -0,0 +1,50 @@ +use similar_asserts::assert_eq; + +use instant_xml::ToXml; + +#[derive(Debug, Eq, PartialEq, ToXml)] +#[xml(ns(dar = "BAZ", internal = "INTERNAL"))] +struct NestedDifferentNamespace { + #[xml(ns(dar))] + flag_parent_prefix: bool, + #[xml(ns(internal))] + flag_internal_prefix: bool, +} + +#[derive(Debug, Eq, PartialEq, ToXml)] +#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] +struct StructChildNamespaces { + different_child_namespace: NestedDifferentNamespace, + same_child_namespace: Nested, +} + +#[derive(Debug, Eq, PartialEq, ToXml)] +#[xml(ns("URI", dar = "BAZ", internal = "INTERNAL"))] +struct Nested { + #[xml(ns(dar))] + flag_parent_prefix: bool, + #[xml(ns(internal))] + flag_internal_prefix: bool, +} + +// Tests: +// - Different child namespace +// - The same child namespace +#[test] +fn struct_child_namespaces() { + assert_eq!( + StructChildNamespaces { + different_child_namespace: NestedDifferentNamespace { + flag_parent_prefix: true, + flag_internal_prefix: false, + }, + same_child_namespace: Nested { + flag_parent_prefix: true, + flag_internal_prefix: false, + }, + } + .to_xml() + .unwrap(), + "truefalsetruefalse" + ); +} diff --git a/instant-xml/tests/ser-named.rs b/instant-xml/tests/ser-named.rs new file mode 100644 index 0000000..d556c79 --- /dev/null +++ b/instant-xml/tests/ser-named.rs @@ -0,0 +1,32 @@ +use similar_asserts::assert_eq; + +use instant_xml::ToXml; + +#[derive(Debug, Eq, PartialEq, ToXml)] +#[xml(ns(bar = "BAZ", foo = "BAR"))] +struct StructWithNamedFields { + flag: bool, + #[xml(ns(bar))] + string: String, + #[xml(ns("typo"))] + number: i32, +} + +// Tests: +// - Empty default namespace +// - Prefix namespace +// - Direct namespace + +#[test] +fn struct_with_named_fields() { + assert_eq!( + StructWithNamedFields { + flag: true, + string: "test".to_string(), + number: 1, + } + .to_xml() + .unwrap(), + "truetest1" + ); +} diff --git a/instant-xml/tests/ser-nested.rs b/instant-xml/tests/ser-nested.rs new file mode 100644 index 0000000..dc18b0a --- /dev/null +++ b/instant-xml/tests/ser-nested.rs @@ -0,0 +1,54 @@ +use similar_asserts::assert_eq; + +use instant_xml::ToXml; + +#[derive(Debug, Eq, PartialEq, ToXml)] +#[xml(ns("URI", dar = "BAZ", internal = "INTERNAL"))] +struct Nested { + #[xml(ns(dar))] + flag_parent_prefix: bool, + #[xml(ns(internal))] + flag_internal_prefix: bool, +} + +#[derive(Debug, Eq, PartialEq, ToXml)] +#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] +struct StructWithCustomField { + #[xml(attribute)] + int_attribute: i32, + #[xml(ns("BAZ"))] + flag_direct_namespace_same_the_same_as_prefix: bool, + #[xml(ns(bar))] + flag_prefix: bool, + #[xml(ns("DIFFERENT"))] + flag_direct_namespace: bool, + test: Nested, +} + +// Tests: +// - The same direct namespace as the one from prefix +// - Attribute handling +// - Omitting redeclared child default namespace +// - Omitting redeclared child namespace with different prefix +// - Unique direct namespace +// - Child unique prefix +// - Child repeated prefix +// - Child default namespace the same as parent +#[test] +fn struct_with_custom_field() { + assert_eq!( + StructWithCustomField { + int_attribute: 42, + flag_direct_namespace_same_the_same_as_prefix: true, + flag_prefix: false, + flag_direct_namespace: true, + test: Nested { + flag_parent_prefix: true, + flag_internal_prefix: false, + }, + } + .to_xml() + .unwrap(), + "truefalsetruetruefalse" + ); +} diff --git a/instant-xml/tests/ser-unit.rs b/instant-xml/tests/ser-unit.rs new file mode 100644 index 0000000..793937a --- /dev/null +++ b/instant-xml/tests/ser-unit.rs @@ -0,0 +1,12 @@ +use similar_asserts::assert_eq; + +use instant_xml::ToXml; + +#[derive(Debug, Eq, PartialEq, ToXml)] +struct Unit; + +#[test] +fn unit() { + assert_eq!(Unit.to_xml().unwrap(), ""); + //assert_eq!(Unit::from_xml("").unwrap(), Unit); +}