2024-06-21 15:53:37 +00:00
|
|
|
// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
unsafe_code,
|
|
|
|
unused_variables,
|
|
|
|
unused_mut,
|
|
|
|
dead_code
|
|
|
|
)]
|
|
|
|
|
|
|
|
mod helpers {
|
|
|
|
// we isolate the helpers into a module, because we do not want to have
|
|
|
|
// them in scope below.
|
|
|
|
// this is to ensure that the macros do not have hidden dependencies on
|
|
|
|
// any specific names being imported.
|
|
|
|
use minidom::Element;
|
2024-07-09 15:01:42 +00:00
|
|
|
use xso::{error::FromElementError, transform, try_from_element, AsXml, FromXml};
|
2024-06-21 15:53:37 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
pub(super) fn roundtrip_full<T: AsXml + FromXml + PartialEq + std::fmt::Debug + Clone>(
|
2024-06-21 15:53:37 +00:00
|
|
|
s: &str,
|
|
|
|
) {
|
|
|
|
let initial: Element = s.parse().unwrap();
|
|
|
|
let structural: T = match try_from_element(initial.clone()) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => panic!("failed to parse from {:?}: {}", s, e),
|
|
|
|
};
|
|
|
|
let recovered =
|
|
|
|
transform(structural.clone()).expect("roundtrip did not produce an element");
|
|
|
|
assert_eq!(initial, recovered);
|
|
|
|
let structural2: T = match try_from_element(recovered) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => panic!("failed to parse from serialisation of {:?}: {}", s, e),
|
|
|
|
};
|
|
|
|
assert_eq!(structural, structural2);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn parse_str<T: FromXml>(s: &str) -> Result<T, FromElementError> {
|
|
|
|
let initial: Element = s.parse().unwrap();
|
|
|
|
try_from_element(initial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use self::helpers::{parse_str, roundtrip_full};
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
use xso::{AsXml, FromXml};
|
2024-06-21 15:53:37 +00:00
|
|
|
|
|
|
|
// these are adverserial local names in order to trigger any issues with
|
|
|
|
// unqualified names in the macro expansions.
|
|
|
|
#[allow(dead_code, non_snake_case)]
|
|
|
|
fn Err() {}
|
|
|
|
#[allow(dead_code, non_snake_case)]
|
|
|
|
fn Ok() {}
|
|
|
|
#[allow(dead_code, non_snake_case)]
|
|
|
|
fn Some() {}
|
|
|
|
#[allow(dead_code, non_snake_case)]
|
|
|
|
fn None() {}
|
|
|
|
#[allow(dead_code)]
|
|
|
|
type Option = ((),);
|
|
|
|
#[allow(dead_code)]
|
|
|
|
type Result = ((),);
|
|
|
|
|
|
|
|
static NS1: &str = "urn:example:ns1";
|
2024-06-24 12:43:15 +00:00
|
|
|
static NS2: &str = "urn:example:ns2";
|
2024-06-21 15:53:37 +00:00
|
|
|
|
2024-06-24 12:43:15 +00:00
|
|
|
static FOO_NAME: &::xso::exports::rxml::strings::NcNameStr = {
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
unsafe {
|
|
|
|
::xso::exports::rxml::strings::NcNameStr::from_str_unchecked("foo")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static BAR_NAME: &::xso::exports::rxml::strings::NcNameStr = {
|
2024-06-24 12:41:43 +00:00
|
|
|
#[allow(unsafe_code)]
|
|
|
|
unsafe {
|
|
|
|
::xso::exports::rxml::strings::NcNameStr::from_str_unchecked("bar")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-21 15:53:37 +00:00
|
|
|
#[xml(namespace = NS1, name = "foo")]
|
|
|
|
struct Empty;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<Empty>("<foo xmlns='urn:example:ns1'/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_name_mismatch() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<Empty>("<bar xmlns='urn:example:ns1'/>") {
|
|
|
|
Err(xso::error::FromElementError::Mismatch(..)) => (),
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_namespace_mismatch() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<Empty>("<foo xmlns='urn:example:ns2'/>") {
|
|
|
|
Err(xso::error::FromElementError::Mismatch(..)) => (),
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_unexpected_attribute() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<Empty>("<foo xmlns='urn:example:ns1' fnord='bar'/>") {
|
|
|
|
Err(xso::error::FromElementError::Invalid(xso::error::Error::Other(e))) => {
|
2024-06-22 13:35:56 +00:00
|
|
|
assert_eq!(e, "Unknown attribute in Empty element.");
|
2024-06-21 15:53:37 +00:00
|
|
|
}
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_unexpected_child() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<Empty>("<foo xmlns='urn:example:ns1'><coucou/></foo>") {
|
|
|
|
Err(xso::error::FromElementError::Invalid(xso::error::Error::Other(e))) => {
|
2024-06-22 13:35:56 +00:00
|
|
|
assert_eq!(e, "Unknown child in Empty element.");
|
2024-06-21 15:53:37 +00:00
|
|
|
}
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_qname_check_has_precedence_over_attr_check() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<Empty>("<bar xmlns='urn:example:ns1' fnord='bar'/>") {
|
|
|
|
Err(xso::error::FromElementError::Mismatch(..)) => (),
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
2024-06-22 07:25:42 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-24 12:43:15 +00:00
|
|
|
#[xml(namespace = NS1, name = BAR_NAME)]
|
2024-06-22 07:25:42 +00:00
|
|
|
struct NamePath;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn name_path_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<NamePath>("<bar xmlns='urn:example:ns1'/>");
|
|
|
|
}
|
2024-06-22 07:30:45 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-22 07:30:45 +00:00
|
|
|
#[xml(namespace = "urn:example:ns2", name = "baz")]
|
|
|
|
struct NamespaceLit;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn namespace_lit_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<NamespaceLit>("<baz xmlns='urn:example:ns2'/>");
|
|
|
|
}
|
2024-06-23 07:06:32 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-23 07:06:32 +00:00
|
|
|
#[xml(namespace = NS1, name = "attr")]
|
|
|
|
struct RequiredAttribute {
|
|
|
|
#[xml(attribute)]
|
|
|
|
foo: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_attribute_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<RequiredAttribute>("<attr xmlns='urn:example:ns1' foo='bar'/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_attribute_positive() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
let data = parse_str::<RequiredAttribute>("<attr xmlns='urn:example:ns1' foo='bar'/>").unwrap();
|
|
|
|
assert_eq!(data.foo, "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_attribute_missing() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<RequiredAttribute>("<attr xmlns='urn:example:ns1'/>") {
|
|
|
|
Err(::xso::error::FromElementError::Invalid(::xso::error::Error::Other(e)))
|
|
|
|
if e.contains("Required attribute field") && e.contains("missing") =>
|
|
|
|
{
|
|
|
|
()
|
|
|
|
}
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
2024-06-23 08:09:59 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-23 08:09:59 +00:00
|
|
|
#[xml(namespace = NS1, name = "attr")]
|
|
|
|
struct RenamedAttribute {
|
|
|
|
#[xml(attribute = "a1")]
|
|
|
|
foo: String,
|
2024-06-24 12:43:15 +00:00
|
|
|
#[xml(attribute = BAR_NAME)]
|
2024-06-24 12:41:43 +00:00
|
|
|
bar: String,
|
2024-06-23 08:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn renamed_attribute_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
2024-06-24 12:41:43 +00:00
|
|
|
roundtrip_full::<RenamedAttribute>("<attr xmlns='urn:example:ns1' a1='bar' bar='baz'/>");
|
2024-06-23 08:09:59 +00:00
|
|
|
}
|
2024-06-24 05:28:04 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-24 05:28:04 +00:00
|
|
|
#[xml(namespace = NS1, name = "attr")]
|
|
|
|
struct NamespacedAttribute {
|
2024-06-24 12:43:15 +00:00
|
|
|
#[xml(attribute(namespace = "urn:example:ns1", name = FOO_NAME))]
|
|
|
|
foo_1: String,
|
|
|
|
#[xml(attribute(namespace = NS2, name = "foo"))]
|
|
|
|
foo_2: String,
|
|
|
|
#[xml(attribute(namespace = NS1, name = BAR_NAME))]
|
|
|
|
bar_1: String,
|
|
|
|
#[xml(attribute(namespace = "urn:example:ns2", name = "bar"))]
|
|
|
|
bar_2: String,
|
2024-06-24 05:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn namespaced_attribute_roundtrip_a() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<NamespacedAttribute>(
|
|
|
|
"<attr xmlns='urn:example:ns1'
|
2024-06-24 12:43:15 +00:00
|
|
|
xmlns:tns0='urn:example:ns1' tns0:foo='a1' tns0:bar='a3'
|
|
|
|
xmlns:tns1='urn:example:ns2' tns1:foo='a2' tns1:bar='a4'/>",
|
2024-06-24 05:28:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn namespaced_attribute_roundtrip_b() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<NamespacedAttribute>(
|
|
|
|
"<tns0:attr
|
2024-06-24 12:43:15 +00:00
|
|
|
xmlns:tns0='urn:example:ns1' tns0:foo='a1' tns0:bar='a3'
|
|
|
|
xmlns:tns1='urn:example:ns2' tns1:foo='a2' tns1:bar='a4'/>",
|
2024-06-24 05:28:04 +00:00
|
|
|
);
|
|
|
|
}
|
2024-06-24 05:43:51 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-24 05:43:51 +00:00
|
|
|
#[xml(namespace = NS1, name = "attr")]
|
|
|
|
struct PrefixedAttribute {
|
|
|
|
#[xml(attribute = "xml:lang")]
|
|
|
|
lang: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prefixed_attribute_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<PrefixedAttribute>("<attr xmlns='urn:example:ns1' xml:lang='foo'/>");
|
|
|
|
}
|
2024-06-25 15:37:03 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-25 15:37:03 +00:00
|
|
|
#[xml(namespace = NS1, name = "attr")]
|
|
|
|
struct RequiredNonStringAttribute {
|
|
|
|
#[xml(attribute)]
|
|
|
|
foo: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn required_non_string_attribute_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<RequiredNonStringAttribute>("<attr xmlns='urn:example:ns1' foo='-16'/>");
|
|
|
|
}
|
2024-06-26 13:56:43 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 13:56:43 +00:00
|
|
|
#[xml(namespace = NS1, name = "attr")]
|
|
|
|
struct DefaultAttribute {
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
foo: std::option::Option<String>,
|
|
|
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
bar: std::option::Option<u16>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_attribute_roundtrip_aa() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<DefaultAttribute>("<attr xmlns='urn:example:ns1'/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_attribute_roundtrip_pa() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<DefaultAttribute>("<attr xmlns='urn:example:ns1' foo='xyz'/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_attribute_roundtrip_ap() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<DefaultAttribute>("<attr xmlns='urn:example:ns1' bar='16'/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_attribute_roundtrip_pp() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<DefaultAttribute>("<attr xmlns='urn:example:ns1' foo='xyz' bar='16'/>");
|
|
|
|
}
|
2024-06-26 15:54:36 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 15:54:36 +00:00
|
|
|
#[xml(namespace = NS1, name = "text")]
|
|
|
|
struct TextString {
|
|
|
|
#[xml(text)]
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn text_string_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<TextString>("<text xmlns='urn:example:ns1'>hello world!</text>");
|
|
|
|
}
|
|
|
|
|
2024-07-01 05:40:28 +00:00
|
|
|
#[test]
|
|
|
|
fn text_string_positive_preserves_whitespace() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
let el = parse_str::<TextString>("<text xmlns='urn:example:ns1'> \t\n</text>").unwrap();
|
|
|
|
assert_eq!(el.text, " \t\n");
|
|
|
|
}
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 15:54:36 +00:00
|
|
|
#[xml(namespace = NS1, name = "text")]
|
|
|
|
struct TextNonString {
|
|
|
|
#[xml(text)]
|
|
|
|
text: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn text_non_string_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<TextNonString>("<text xmlns='urn:example:ns1'>123456</text>");
|
|
|
|
}
|
2024-07-01 05:40:28 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-07-01 05:40:28 +00:00
|
|
|
#[xml(namespace = NS1, name = "elem")]
|
|
|
|
struct IgnoresWhitespaceWithoutTextConsumer;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ignores_whitespace_without_text_consumer_positive() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
let _ = parse_str::<IgnoresWhitespaceWithoutTextConsumer>(
|
|
|
|
"<elem xmlns='urn:example:ns1'> \t\r\n</elem>",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-07-01 05:40:28 +00:00
|
|
|
#[xml(namespace = NS1, name = "elem")]
|
|
|
|
struct FailsTextWithoutTextConsumer;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fails_text_without_text_consumer_positive() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
match parse_str::<FailsTextWithoutTextConsumer>("<elem xmlns='urn:example:ns1'> quak </elem>")
|
|
|
|
{
|
|
|
|
Err(::xso::error::FromElementError::Invalid(::xso::error::Error::Other(e)))
|
|
|
|
if e.contains("Unexpected text") =>
|
|
|
|
{
|
|
|
|
()
|
|
|
|
}
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 16:26:13 +00:00
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 16:26:13 +00:00
|
|
|
#[xml(namespace = NS1, name = "text")]
|
|
|
|
struct TextWithCodec {
|
|
|
|
#[xml(text(codec = xso::text::EmptyAsNone))]
|
|
|
|
text: std::option::Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn text_with_codec_roundtrip_empty() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<TextWithCodec>("<text xmlns='urn:example:ns1'/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn text_with_codec_roundtrip_non_empty() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<TextWithCodec>("<text xmlns='urn:example:ns1'>hello</text>");
|
|
|
|
}
|
2024-06-29 13:22:52 +00:00
|
|
|
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
|
|
|
#[xml(namespace = NS1, name = "parent")]
|
|
|
|
struct Parent {
|
|
|
|
#[xml(child)]
|
|
|
|
child: RequiredAttribute,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parent_roundtrip() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
roundtrip_full::<Parent>("<parent xmlns='urn:example:ns1'><attr foo='hello world!'/></parent>")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parent_positive() {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::{
|
|
|
|
option::Option::{None, Some},
|
|
|
|
result::Result::{Err, Ok},
|
|
|
|
};
|
|
|
|
let v =
|
|
|
|
parse_str::<Parent>("<parent xmlns='urn:example:ns1'><attr foo='hello world!'/></parent>")
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(v.child.foo, "hello world!");
|
|
|
|
}
|