From cf4e1d4e5502aa6115178ec7b0ad56982ea51c82 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 3 Sep 2022 14:01:33 +0200 Subject: [PATCH] wip --- instant-xml/tests/all.rs | 509 ++++++++++++++++++++++++++++++ instant-xml/tests/de-ns.rs | 4 +- instant-xml/tests/ser-child-ns.rs | 5 +- instant-xml/tests/ser-named.rs | 2 +- instant-xml/tests/ser-nested.rs | 8 +- 5 files changed, 514 insertions(+), 14 deletions(-) create mode 100644 instant-xml/tests/all.rs diff --git a/instant-xml/tests/all.rs b/instant-xml/tests/all.rs new file mode 100644 index 0000000..2a1b78e --- /dev/null +++ b/instant-xml/tests/all.rs @@ -0,0 +1,509 @@ +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("BAZ"))] + 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(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("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_direct_namespace: true, + test: Nested { + flag_internal_prefix: false, + }, + } + .to_xml() + .unwrap(), + "truetruefalse" + ); +} + +#[derive(Debug, Eq, PartialEq, ToXml)] +struct NestedDifferentNamespace { + #[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_internal_prefix: false, + }, + same_child_namespace: Nested { + flag_internal_prefix: false, + }, + } + .to_xml() + .unwrap(), + "falsefalse" + ); +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ"))] +struct NestedDe { + #[xml(ns("BAZ"))] + flag: bool, +} + +#[derive(Debug, Eq, PartialEq, FromXml)] +#[xml(ns("URI", bar = "BAZ", foo = "BAR"))] +struct StructWithCustomFieldFromXml { + #[xml(ns("BAZ"))] + 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("BAZ"))] + 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-ns.rs b/instant-xml/tests/de-ns.rs index d0e931b..2caec63 100644 --- a/instant-xml/tests/de-ns.rs +++ b/instant-xml/tests/de-ns.rs @@ -10,7 +10,7 @@ struct NestedWrongNamespace { #[derive(Debug, Eq, PartialEq, FromXml)] #[xml(ns("URI", bar = "BAZ"))] struct NestedDe { - #[xml(ns(bar))] + #[xml(ns("BAZ"))] flag: bool, } @@ -78,7 +78,7 @@ fn default_namespaces() { #[derive(Debug, Eq, PartialEq, FromXml)] #[xml(ns("URI", bar = "BAZ"))] struct NestedOtherNamespace { - #[xml(ns(bar))] + #[xml(ns("BAZ"))] flag: bool, } diff --git a/instant-xml/tests/ser-child-ns.rs b/instant-xml/tests/ser-child-ns.rs index 7948d19..8e83398 100644 --- a/instant-xml/tests/ser-child-ns.rs +++ b/instant-xml/tests/ser-child-ns.rs @@ -5,8 +5,6 @@ use instant_xml::{to_string, 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, } @@ -35,7 +33,6 @@ fn struct_child_namespaces() { assert_eq!( to_string(&StructChildNamespaces { different_child_namespace: NestedDifferentNamespace { - flag_parent_prefix: true, flag_internal_prefix: false, }, same_child_namespace: Nested { @@ -44,6 +41,6 @@ fn struct_child_namespaces() { }, }) .unwrap(), - "truefalsetruefalse" + "falsetruefalse" ); } diff --git a/instant-xml/tests/ser-named.rs b/instant-xml/tests/ser-named.rs index f8083de..cd25283 100644 --- a/instant-xml/tests/ser-named.rs +++ b/instant-xml/tests/ser-named.rs @@ -6,7 +6,7 @@ use instant_xml::{to_string, ToXml}; #[xml(ns(bar = "BAZ", foo = "BAR"))] struct StructWithNamedFields { flag: bool, - #[xml(ns(bar))] + #[xml(ns("BAZ"))] string: String, #[xml(ns("typo"))] number: i32, diff --git a/instant-xml/tests/ser-nested.rs b/instant-xml/tests/ser-nested.rs index 415d84b..bd8ae56 100644 --- a/instant-xml/tests/ser-nested.rs +++ b/instant-xml/tests/ser-nested.rs @@ -5,8 +5,6 @@ use instant_xml::{to_string, 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, } @@ -18,8 +16,6 @@ struct StructWithCustomField { 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, @@ -40,14 +36,12 @@ fn struct_with_custom_field() { to_string(&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, }, }) .unwrap(), - "truefalsetruetruefalse" + "truetruefalse" ); }