From 4d67ae4b70e561455f39cb29e0b5984bc803b837 Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 27 Jun 2024 02:05:39 +0500 Subject: [PATCH] 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 --- tests/async-tokio.rs | 49 +++++++++++++++++++++- tests/reader.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/tests/async-tokio.rs b/tests/async-tokio.rs index 95437d1c..3c267d90 100644 --- a/tests/async-tokio.rs +++ b/tests/async-tokio.rs @@ -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(" text "); + // ^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(" "); + // ^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. diff --git a/tests/reader.rs b/tests/reader.rs index 683edd77..4921cd87 100644 --- a/tests/reader.rs +++ b/tests/reader.rs @@ -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(" text "); + // ^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(" "); + // ^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(" text "); + // ^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(" "); + // ^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(" text "); + 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(" "); + 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(), " "); + assert_eq!(r.read_event().unwrap(), Eof); + } +}