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

Fix incorrect end position in read_to_end and read_text #773

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for read_to_end and read_text
failures (3):
  async-tokio (1):
    read_to_end::tag
  reader (2):
    read_to_end::borrowed::tag
    read_to_end::buffered::tag
Mingun committed Jun 27, 2024
commit 2de63052c747e210c60eaeba2603e8f14a39dd23
49 changes: 48 additions & 1 deletion tests/async-tokio.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::iter;

use pretty_assertions::assert_eq;
use quick_xml::events::Event::*;
use quick_xml::events::{BytesStart, Event::*};
use quick_xml::name::QName;
use quick_xml::reader::Reader;

@@ -40,6 +40,53 @@ async fn test_sample() {
assert_eq!((count, reads), (1247, 5245));
}

/// This tests checks that read_to_end() correctly returns span even when
/// text is trimmed from both sides
mod read_to_end {
use super::*;
use pretty_assertions::assert_eq;

#[tokio::test]
async fn text() {
let mut r = Reader::from_str("<tag> text </tag>");
// ^0 ^5 ^11
r.config_mut().trim_text(true);

let mut buf = Vec::new();
assert_eq!(
r.read_event_into_async(&mut buf).await.unwrap(),
Start(BytesStart::new("tag"))
);
assert_eq!(
r.read_to_end_into_async(QName(b"tag"), &mut buf)
.await
.unwrap(),
5..11
);
assert_eq!(r.read_event_into_async(&mut buf).await.unwrap(), Eof);
}

#[tokio::test]
async fn tag() {
let mut r = Reader::from_str("<tag> <nested/> </tag>");
// ^0 ^5 ^16
r.config_mut().trim_text(true);

let mut buf = Vec::new();
assert_eq!(
r.read_event_into_async(&mut buf).await.unwrap(),
Start(BytesStart::new("tag"))
);
assert_eq!(
r.read_to_end_into_async(QName(b"tag"), &mut buf)
.await
.unwrap(),
5..16
);
assert_eq!(r.read_event_into_async(&mut buf).await.unwrap(), Eof);
}
}

/// Regression test for https://github.com/tafia/quick-xml/issues/751
///
/// Actually, that error was not found in async reader, but we would to test it as well.
96 changes: 96 additions & 0 deletions tests/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::str::from_utf8;

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

use pretty_assertions::assert_eq;
@@ -265,3 +266,98 @@ mod double_dash {
assert_eq!(r.read_event().unwrap(), End(BytesEnd::new("hello")));
}
}

/// This tests checks that read_to_end() correctly returns span even when
/// text is trimmed from both sides
mod read_to_end {
use super::*;

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

#[test]
fn text() {
let mut r = Reader::from_str("<tag> text </tag>");
// ^0 ^5 ^11
r.config_mut().trim_text(true);

assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
assert_eq!(r.read_to_end(QName(b"tag")).unwrap(), 5..11);
assert_eq!(r.read_event().unwrap(), Eof);
}

#[test]
fn tag() {
let mut r = Reader::from_str("<tag> <nested/> </tag>");
// ^0 ^5 ^16
r.config_mut().trim_text(true);

assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
assert_eq!(r.read_to_end(QName(b"tag")).unwrap(), 5..16);
assert_eq!(r.read_event().unwrap(), Eof);
}
}

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

#[test]
fn text() {
let mut r = Reader::from_str("<tag> text </tag>");
// ^0 ^5 ^11
r.config_mut().trim_text(true);

let mut buf = Vec::new();
assert_eq!(
r.read_event_into(&mut buf).unwrap(),
Start(BytesStart::new("tag"))
);
assert_eq!(r.read_to_end_into(QName(b"tag"), &mut buf).unwrap(), 5..11);
assert_eq!(r.read_event_into(&mut buf).unwrap(), Eof);
}

#[test]
fn tag() {
let mut r = Reader::from_str("<tag> <nested/> </tag>");
// ^0 ^5 ^16
r.config_mut().trim_text(true);

let mut buf = Vec::new();
assert_eq!(
r.read_event_into(&mut buf).unwrap(),
Start(BytesStart::new("tag"))
);
assert_eq!(r.read_to_end_into(QName(b"tag"), &mut buf).unwrap(), 5..16);
assert_eq!(r.read_event_into(&mut buf).unwrap(), Eof);
}
}
}

/// This tests checks that read_text() correctly returns text even when
/// text is trimmed from both sides
mod read_text {
use super::*;
use pretty_assertions::assert_eq;

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

assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
assert_eq!(r.read_text(QName(b"tag")).unwrap(), " text ");
assert_eq!(r.read_event().unwrap(), Eof);
}

#[test]
fn tag() {
let mut r = Reader::from_str("<tag> <nested/> </tag>");
r.config_mut().trim_text(true);

assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
assert_eq!(r.read_text(QName(b"tag")).unwrap(), " <nested/> ");
assert_eq!(r.read_event().unwrap(), Eof);
}
}