Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regression tests for fixed issues #542

Merged
merged 7 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,48 @@
//!
//! Name each module / test as `issue<GH number>` and keep sorted by issue number

use std::sync::mpsc;

use quick_xml::events::{BytesStart, Event};
use quick_xml::name::QName;
use quick_xml::reader::Reader;
use quick_xml::Error;

/// Regression test for https://github.com/tafia/quick-xml/issues/115
#[test]
fn issue115() {
let mut r = Reader::from_str("<tag1 attr1='line 1\nline 2'></tag1>");
match r.read_event() {
Ok(Event::Start(e)) if e.name() == QName(b"tag1") => {
let v = e.attributes().map(|a| a.unwrap().value).collect::<Vec<_>>();
assert_eq!(v[0].clone().into_owned(), b"line 1\nline 2");
}
_ => (),
}
}

/// Regression test for https://github.com/tafia/quick-xml/issues/360
#[test]
fn issue360() {
let (tx, rx) = mpsc::channel::<Event>();

std::thread::spawn(move || {
let mut r = Reader::from_str("<tag1 attr1='line 1\nline 2'></tag1>");
loop {
let event = r.read_event().unwrap();
if event == Event::Eof {
tx.send(event).unwrap();
break;
} else {
tx.send(event).unwrap();
}
}
});
for event in rx.iter() {
println!("{:?}", event);
}
}

/// Regression test for https://github.com/tafia/quick-xml/issues/514
mod issue514 {
use super::*;
Expand Down
235 changes: 235 additions & 0 deletions tests/serde-issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,244 @@
//!
//! Name each module / test as `issue<GH number>` and keep sorted by issue number

use pretty_assertions::assert_eq;
use quick_xml::de::from_str;
use quick_xml::se::to_string;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// 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/343.
#[test]
fn issue343() {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Users {
users: HashMap<String, User>,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Max(u16);

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct User {
max: Max,
}

let xml = "<Users>\
<users>\
<roger>\
<max>10</max>\
</roger>\
</users>\
</Users>";
let users: Users = from_str(xml).unwrap();

assert_eq!(
users,
Users {
users: HashMap::from([("roger".to_string(), User { max: Max(10) })]),
}
);
assert_eq!(to_string(&users).unwrap(), xml);
}

/// Regression test for https://github.com/tafia/quick-xml/issues/349.
#[test]
fn issue349() {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Entity {
id: Id,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Id {
#[serde(rename = "$value")]
content: Enum,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
enum Enum {
A(String),
B(String),
}

assert_eq!(
from_str::<Entity>("<entity><id><a>Id</a></id></entity>").unwrap(),
Entity {
id: Id {
content: Enum::A("Id".to_string()),
}
}
);
}

/// Regression test for https://github.com/tafia/quick-xml/issues/429.
#[test]
fn issue429() {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
enum State {
A,
B,
C,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct StateOuter {
#[serde(rename = "$text")]
state: State,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct Root {
state: StateOuter,
}

assert_eq!(
from_str::<Root>("<root><state>B</state></root>").unwrap(),
Root {
state: StateOuter { state: State::B }
}
);

assert_eq!(
to_string(&Root {
state: StateOuter { state: State::B }
})
.unwrap(),
"<Root><state>B</state></Root>"
);
}

/// Regression test for https://github.com/tafia/quick-xml/issues/500.
#[test]
fn issue500() {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct TagOne {}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct TagTwo {}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
enum Tag {
TagOne(TagOne),
TagTwo(TagTwo),
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Root {
#[serde(rename = "$value", default)]
data: Vec<Tag>,
}

let data: Root = from_str(
"\
<root>\
<TagOne></TagOne>\
<TagTwo></TagTwo>\
<TagOne></TagOne>\
</root>\
",
)
.unwrap();

assert_eq!(
data,
Root {
data: vec![
Tag::TagOne(TagOne {}),
Tag::TagTwo(TagTwo {}),
Tag::TagOne(TagOne {}),
],
}
);

let data: Vec<Tag> = from_str(
"\
<TagOne></TagOne>\
<TagTwo></TagTwo>\
<TagOne></TagOne>\
",
)
.unwrap();

assert_eq!(
data,
vec![
Tag::TagOne(TagOne {}),
Tag::TagTwo(TagTwo {}),
Tag::TagOne(TagOne {}),
]
);
}

/// Regression test for https://github.com/tafia/quick-xml/issues/537.
///
Expand Down