Skip to content

Commit e052a46

Browse files
committed
Add tests for tafia#469
failures (8): reader::async_tokio::test::small_buffers::cdata1 reader::async_tokio::test::small_buffers::cdata2 reader::async_tokio::test::small_buffers::comment1 reader::async_tokio::test::small_buffers::comment2 reader::buffered_reader::test::small_buffers::cdata1 reader::buffered_reader::test::small_buffers::cdata2 reader::buffered_reader::test::small_buffers::comment1 reader::buffered_reader::test::small_buffers::comment2
1 parent f8b292b commit e052a46

File tree

3 files changed

+163
-3
lines changed

3 files changed

+163
-3
lines changed

src/reader/async_tokio.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<R: AsyncBufRead + Unpin> NsReader<R> {
377377
#[cfg(test)]
378378
mod test {
379379
use super::TokioAdapter;
380-
use crate::reader::test::check;
380+
use crate::reader::test::{check, small_buffers};
381381

382382
check!(
383383
#[tokio::test]
@@ -387,4 +387,10 @@ mod test {
387387
&mut Vec::new(),
388388
async, await
389389
);
390+
391+
small_buffers!(
392+
#[tokio::test]
393+
read_event_into_async: tokio::io::BufReader<_>,
394+
async, await
395+
);
390396
}

src/reader/buffered_reader.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl Reader<BufReader<File>> {
406406

407407
#[cfg(test)]
408408
mod test {
409-
use crate::reader::test::check;
409+
use crate::reader::test::{check, small_buffers};
410410
use crate::reader::XmlSource;
411411

412412
/// Default buffer constructor just pass the byte array from the test
@@ -422,6 +422,11 @@ mod test {
422422
&mut Vec::new()
423423
);
424424

425+
small_buffers!(
426+
#[test]
427+
read_event_into: std::io::BufReader<_>
428+
);
429+
425430
#[cfg(feature = "encoding")]
426431
mod encoding {
427432
use crate::events::Event;

src/reader/mod.rs

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,8 +1751,157 @@ mod test {
17511751
};
17521752
}
17531753

1754-
// Export a macro for the child modules:
1754+
/// Tests for https://github.com/tafia/quick-xml/issues/469
1755+
macro_rules! small_buffers {
1756+
(
1757+
#[$test:meta]
1758+
$read_event:ident: $BufReader:ty
1759+
$(, $async:ident, $await:ident)?
1760+
) => {
1761+
mod small_buffers {
1762+
use crate::events::{BytesCData, BytesDecl, BytesStart, BytesText, Event};
1763+
use crate::reader::Reader;
1764+
use pretty_assertions::assert_eq;
1765+
1766+
#[$test]
1767+
$($async)? fn decl() {
1768+
let xml = "<?xml ?>";
1769+
// ^^^^^^^ data that fit into buffer
1770+
let size = xml.match_indices("?>").next().unwrap().0 + 1;
1771+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1772+
let mut reader = Reader::from_reader(br);
1773+
let mut buf = Vec::new();
1774+
1775+
assert_eq!(
1776+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1777+
Event::Decl(BytesDecl::from_start(BytesStart::from_content("xml ", 3)))
1778+
);
1779+
assert_eq!(
1780+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1781+
Event::Eof
1782+
);
1783+
}
1784+
1785+
#[$test]
1786+
$($async)? fn pi() {
1787+
let xml = "<?pi?>";
1788+
// ^^^^^ data that fit into buffer
1789+
let size = xml.match_indices("?>").next().unwrap().0 + 1;
1790+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1791+
let mut reader = Reader::from_reader(br);
1792+
let mut buf = Vec::new();
1793+
1794+
assert_eq!(
1795+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1796+
Event::PI(BytesText::new("pi"))
1797+
);
1798+
assert_eq!(
1799+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1800+
Event::Eof
1801+
);
1802+
}
1803+
1804+
#[$test]
1805+
$($async)? fn empty() {
1806+
let xml = "<empty/>";
1807+
// ^^^^^^^ data that fit into buffer
1808+
let size = xml.match_indices("/>").next().unwrap().0 + 1;
1809+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1810+
let mut reader = Reader::from_reader(br);
1811+
let mut buf = Vec::new();
1812+
1813+
assert_eq!(
1814+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1815+
Event::Empty(BytesStart::new("empty"))
1816+
);
1817+
assert_eq!(
1818+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1819+
Event::Eof
1820+
);
1821+
}
1822+
1823+
#[$test]
1824+
$($async)? fn cdata1() {
1825+
let xml = "<![CDATA[cdata]]>";
1826+
// ^^^^^^^^^^^^^^^ data that fit into buffer
1827+
let size = xml.match_indices("]]>").next().unwrap().0 + 1;
1828+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1829+
let mut reader = Reader::from_reader(br);
1830+
let mut buf = Vec::new();
1831+
1832+
assert_eq!(
1833+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1834+
Event::CData(BytesCData::new("cdata"))
1835+
);
1836+
assert_eq!(
1837+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1838+
Event::Eof
1839+
);
1840+
}
1841+
1842+
#[$test]
1843+
$($async)? fn cdata2() {
1844+
let xml = "<![CDATA[cdata]]>";
1845+
// ^^^^^^^^^^^^^^^^ data that fit into buffer
1846+
let size = xml.match_indices("]]>").next().unwrap().0 + 2;
1847+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1848+
let mut reader = Reader::from_reader(br);
1849+
let mut buf = Vec::new();
1850+
1851+
assert_eq!(
1852+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1853+
Event::CData(BytesCData::new("cdata"))
1854+
);
1855+
assert_eq!(
1856+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1857+
Event::Eof
1858+
);
1859+
}
1860+
1861+
#[$test]
1862+
$($async)? fn comment1() {
1863+
let xml = "<!--comment-->";
1864+
// ^^^^^^^^^^^^ data that fit into buffer
1865+
let size = xml.match_indices("-->").next().unwrap().0 + 1;
1866+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1867+
let mut reader = Reader::from_reader(br);
1868+
let mut buf = Vec::new();
1869+
1870+
assert_eq!(
1871+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1872+
Event::Comment(BytesText::new("comment"))
1873+
);
1874+
assert_eq!(
1875+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1876+
Event::Eof
1877+
);
1878+
}
1879+
1880+
#[$test]
1881+
$($async)? fn comment2() {
1882+
let xml = "<!--comment-->";
1883+
// ^^^^^^^^^^^^^ data that fit into buffer
1884+
let size = xml.match_indices("-->").next().unwrap().0 + 2;
1885+
let br = <$BufReader>::with_capacity(size, xml.as_bytes());
1886+
let mut reader = Reader::from_reader(br);
1887+
let mut buf = Vec::new();
1888+
1889+
assert_eq!(
1890+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1891+
Event::Comment(BytesText::new("comment"))
1892+
);
1893+
assert_eq!(
1894+
reader.$read_event(&mut buf) $(.$await)? .unwrap(),
1895+
Event::Eof
1896+
);
1897+
}
1898+
}
1899+
};
1900+
}
1901+
1902+
// Export macros for the child modules:
17551903
// - buffered_reader
17561904
// - slice_reader
17571905
pub(super) use check;
1906+
pub(super) use small_buffers;
17581907
}

0 commit comments

Comments
 (0)