Skip to content

Commit 4d67ae4

Browse files
Mingundralley
authored andcommitted
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
1 parent 60f77c8 commit 4d67ae4

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

tests/async-tokio.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter;
22

33
use pretty_assertions::assert_eq;
4-
use quick_xml::events::Event::*;
4+
use quick_xml::events::{BytesStart, Event::*};
55
use quick_xml::name::QName;
66
use quick_xml::reader::Reader;
77

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

43+
/// This tests checks that read_to_end() correctly returns span even when
44+
/// text is trimmed from both sides
45+
mod read_to_end {
46+
use super::*;
47+
use pretty_assertions::assert_eq;
48+
49+
#[tokio::test]
50+
async fn text() {
51+
let mut r = Reader::from_str("<tag> text </tag>");
52+
// ^0 ^5 ^11
53+
r.config_mut().trim_text(true);
54+
55+
let mut buf = Vec::new();
56+
assert_eq!(
57+
r.read_event_into_async(&mut buf).await.unwrap(),
58+
Start(BytesStart::new("tag"))
59+
);
60+
assert_eq!(
61+
r.read_to_end_into_async(QName(b"tag"), &mut buf)
62+
.await
63+
.unwrap(),
64+
5..11
65+
);
66+
assert_eq!(r.read_event_into_async(&mut buf).await.unwrap(), Eof);
67+
}
68+
69+
#[tokio::test]
70+
async fn tag() {
71+
let mut r = Reader::from_str("<tag> <nested/> </tag>");
72+
// ^0 ^5 ^16
73+
r.config_mut().trim_text(true);
74+
75+
let mut buf = Vec::new();
76+
assert_eq!(
77+
r.read_event_into_async(&mut buf).await.unwrap(),
78+
Start(BytesStart::new("tag"))
79+
);
80+
assert_eq!(
81+
r.read_to_end_into_async(QName(b"tag"), &mut buf)
82+
.await
83+
.unwrap(),
84+
5..16
85+
);
86+
assert_eq!(r.read_event_into_async(&mut buf).await.unwrap(), Eof);
87+
}
88+
}
89+
4390
/// Regression test for https://github.com/tafia/quick-xml/issues/751
4491
///
4592
/// Actually, that error was not found in async reader, but we would to test it as well.

tests/reader.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::str::from_utf8;
22

33
use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event::*};
4+
use quick_xml::name::QName;
45
use quick_xml::reader::Reader;
56

67
use pretty_assertions::assert_eq;
@@ -265,3 +266,98 @@ mod double_dash {
265266
assert_eq!(r.read_event().unwrap(), End(BytesEnd::new("hello")));
266267
}
267268
}
269+
270+
/// This tests checks that read_to_end() correctly returns span even when
271+
/// text is trimmed from both sides
272+
mod read_to_end {
273+
use super::*;
274+
275+
mod borrowed {
276+
use super::*;
277+
use pretty_assertions::assert_eq;
278+
279+
#[test]
280+
fn text() {
281+
let mut r = Reader::from_str("<tag> text </tag>");
282+
// ^0 ^5 ^11
283+
r.config_mut().trim_text(true);
284+
285+
assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
286+
assert_eq!(r.read_to_end(QName(b"tag")).unwrap(), 5..11);
287+
assert_eq!(r.read_event().unwrap(), Eof);
288+
}
289+
290+
#[test]
291+
fn tag() {
292+
let mut r = Reader::from_str("<tag> <nested/> </tag>");
293+
// ^0 ^5 ^16
294+
r.config_mut().trim_text(true);
295+
296+
assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
297+
assert_eq!(r.read_to_end(QName(b"tag")).unwrap(), 5..16);
298+
assert_eq!(r.read_event().unwrap(), Eof);
299+
}
300+
}
301+
302+
mod buffered {
303+
use super::*;
304+
use pretty_assertions::assert_eq;
305+
306+
#[test]
307+
fn text() {
308+
let mut r = Reader::from_str("<tag> text </tag>");
309+
// ^0 ^5 ^11
310+
r.config_mut().trim_text(true);
311+
312+
let mut buf = Vec::new();
313+
assert_eq!(
314+
r.read_event_into(&mut buf).unwrap(),
315+
Start(BytesStart::new("tag"))
316+
);
317+
assert_eq!(r.read_to_end_into(QName(b"tag"), &mut buf).unwrap(), 5..11);
318+
assert_eq!(r.read_event_into(&mut buf).unwrap(), Eof);
319+
}
320+
321+
#[test]
322+
fn tag() {
323+
let mut r = Reader::from_str("<tag> <nested/> </tag>");
324+
// ^0 ^5 ^16
325+
r.config_mut().trim_text(true);
326+
327+
let mut buf = Vec::new();
328+
assert_eq!(
329+
r.read_event_into(&mut buf).unwrap(),
330+
Start(BytesStart::new("tag"))
331+
);
332+
assert_eq!(r.read_to_end_into(QName(b"tag"), &mut buf).unwrap(), 5..16);
333+
assert_eq!(r.read_event_into(&mut buf).unwrap(), Eof);
334+
}
335+
}
336+
}
337+
338+
/// This tests checks that read_text() correctly returns text even when
339+
/// text is trimmed from both sides
340+
mod read_text {
341+
use super::*;
342+
use pretty_assertions::assert_eq;
343+
344+
#[test]
345+
fn text() {
346+
let mut r = Reader::from_str("<tag> text </tag>");
347+
r.config_mut().trim_text(true);
348+
349+
assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
350+
assert_eq!(r.read_text(QName(b"tag")).unwrap(), " text ");
351+
assert_eq!(r.read_event().unwrap(), Eof);
352+
}
353+
354+
#[test]
355+
fn tag() {
356+
let mut r = Reader::from_str("<tag> <nested/> </tag>");
357+
r.config_mut().trim_text(true);
358+
359+
assert_eq!(r.read_event().unwrap(), Start(BytesStart::new("tag")));
360+
assert_eq!(r.read_text(QName(b"tag")).unwrap(), " <nested/> ");
361+
assert_eq!(r.read_event().unwrap(), Eof);
362+
}
363+
}

0 commit comments

Comments
 (0)