Skip to content

Commit

Permalink
Add tests for read_to_end and read_text
Browse files Browse the repository at this point in the history
failures (3):
  async-tokio (1):
    read_to_end::tag
  reader (2):
    read_to_end::borrowed::tag
    read_to_end::buffered::tag
  • Loading branch information
Mingun authored and dralley committed Jun 27, 2024
1 parent 60f77c8 commit 4d67ae4
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
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;

Expand Down Expand Up @@ -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.
Expand Down
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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4d67ae4

Please sign in to comment.