Skip to content

Commit

Permalink
Add regression tests for #252
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun authored and dralley committed Jan 8, 2023
1 parent 910e179 commit 9781b8d
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/serde-issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,78 @@ use quick_xml::de::from_str;
use quick_xml::se::to_string;
use serde::{Deserialize, Serialize};

/// Regression tests for https://github.com/tafia/quick-xml/issues/252.
mod issue252 {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn attributes() {
#[derive(Serialize, Debug, PartialEq)]
struct OptionalAttributes {
#[serde(rename = "@a")]
a: Option<&'static str>,

#[serde(rename = "@b")]
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<&'static str>,
}

assert_eq!(
to_string(&OptionalAttributes { a: None, b: None }).unwrap(),
r#"<OptionalAttributes a=""/>"#
);
assert_eq!(
to_string(&OptionalAttributes {
a: Some(""),
b: Some("")
})
.unwrap(),
r#"<OptionalAttributes a="" b=""/>"#
);
assert_eq!(
to_string(&OptionalAttributes {
a: Some("a"),
b: Some("b")
})
.unwrap(),
r#"<OptionalAttributes a="a" b="b"/>"#
);
}

#[test]
fn elements() {
#[derive(Serialize, Debug, PartialEq)]
struct OptionalElements {
a: Option<&'static str>,

#[serde(skip_serializing_if = "Option::is_none")]
b: Option<&'static str>,
}

assert_eq!(
to_string(&OptionalElements { a: None, b: None }).unwrap(),
r#"<OptionalElements><a/></OptionalElements>"#
);
assert_eq!(
to_string(&OptionalElements {
a: Some(""),
b: Some("")
})
.unwrap(),
r#"<OptionalElements><a/><b/></OptionalElements>"#
);
assert_eq!(
to_string(&OptionalElements {
a: Some("a"),
b: Some("b")
})
.unwrap(),
r#"<OptionalElements><a>a</a><b>b</b></OptionalElements>"#
);
}
}

/// Regression test for https://github.com/tafia/quick-xml/issues/537.
///
/// This test checks that special `xmlns:xxx` attributes uses full name of
Expand Down

0 comments on commit 9781b8d

Please sign in to comment.