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

Introduce Config struct that holds parser configuration and implement #513 #677

Merged
merged 11 commits into from
Nov 5, 2023
133 changes: 132 additions & 1 deletion tests/reader-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//!
//! Please keep tests sorted (exceptions are allowed if options are tightly related).

use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::reader::Reader;

mod expand_empty_elements {
Expand Down Expand Up @@ -42,3 +42,134 @@ mod expand_empty_elements {
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}
}

const XML: &str = " \t\r\n\
<!doctype root \t\r\n> \t\r\n\
<root \t\r\n> \t\r\n\
<empty \t\r\n/> \t\r\n\
text \t\r\n\
<!-- comment \t\r\n--> \t\r\n\
<![CDATA[ \t\r\ncdata \t\r\n]]> \t\r\n\
<?pi \t\r\n?> \t\r\n\
</root> \t\r\n";

mod trim_text {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn false_() {
let mut reader = Reader::from_str(XML);
reader.trim_text(false);

assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::DocType(BytesText::new("root \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::Start(BytesStart::from_content("root \t\r\n", 4))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::Empty(BytesStart::from_content("empty \t\r\n", 5))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\ntext \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::Comment(BytesText::new(" comment \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::CData(BytesCData::new(" \t\r\ncdata \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::PI(BytesText::new("pi \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(
reader.read_event().unwrap(),
Event::End(BytesEnd::new("root"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new(" \t\r\n"))
);

assert_eq!(reader.read_event().unwrap(), Event::Eof);
}

#[test]
fn true_() {
let mut reader = Reader::from_str(XML);
reader.trim_text(true);

assert_eq!(
reader.read_event().unwrap(),
Event::DocType(BytesText::new("root \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Start(BytesStart::from_content("root \t\r\n", 4))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Empty(BytesStart::from_content("empty \t\r\n", 5))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Text(BytesText::new("text"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::Comment(BytesText::new(" comment \t\r\n"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any reason to support trimming the text values of "comments"? I cannot immediately think of a reason to do that, but perhaps one exists.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not, at least nobody request such feature. If such a request appears, we can add a separate option.

Generally speaking, I would delete the current trim options as they simply do not work correctly for text alternating with CDATA / comments / processing instructions, but I suppose that would break many users. I was thinking about renaming current Event into RawEvent and DeEvent to Event and give users stream of Events. The RawEvent then would be a low-level event which usually not needed by most users. That is very raw thoughts currently, so I decided to not do revolutional changes for now.

);
assert_eq!(
reader.read_event().unwrap(),
Event::CData(BytesCData::new(" \t\r\ncdata \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::PI(BytesText::new("pi \t\r\n"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::End(BytesEnd::new("root"))
);
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}
}
21 changes: 0 additions & 21 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ fn test_comment_starting_with_gt() {
}
}

#[test]
fn test_no_trim() {
let mut reader = Reader::from_str(" <tag> text </tag> ");

assert!(matches!(reader.read_event().unwrap(), Text(_)));
assert!(matches!(reader.read_event().unwrap(), Start(_)));
assert!(matches!(reader.read_event().unwrap(), Text(_)));
assert!(matches!(reader.read_event().unwrap(), End(_)));
assert!(matches!(reader.read_event().unwrap(), Text(_)));
}

#[test]
fn test_trim_end() {
let mut reader = Reader::from_str(" <tag> text </tag> ");
Expand All @@ -110,16 +99,6 @@ fn test_trim_end() {
assert!(matches!(reader.read_event().unwrap(), End(_)));
}

#[test]
fn test_trim() {
let mut reader = Reader::from_str(" <tag> text </tag> ");
reader.trim_text(true);

assert!(matches!(reader.read_event().unwrap(), Start(_)));
assert!(matches!(reader.read_event().unwrap(), Text(_)));
assert!(matches!(reader.read_event().unwrap(), End(_)));
}

#[test]
fn test_clone_reader() {
let mut reader = Reader::from_str("<tag>text</tag>");
Expand Down
11 changes: 0 additions & 11 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ fn test_xml_decl() {
}
}

#[test]
fn test_trim_test() {
let txt = "<a><b> </b></a>";
let mut r = Reader::from_str(txt);
r.trim_text(true);
next_eq!(r, Start, b"a", Start, b"b", End, b"b", End, b"a");

let mut r = Reader::from_str(txt);
next_eq!(r, Start, b"a", Start, b"b", Text, b" ", End, b"b", End, b"a");
}

#[test]
fn test_cdata() {
let mut r = Reader::from_str("<![CDATA[test]]>");
Expand Down