From 28ead211485e7d8d21b87a6c7ab58effbe37cb4a Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sun, 17 Nov 2024 16:05:16 +0100 Subject: [PATCH] BytesCData: Convert `cdata_escaping` test to doc test --- src/events/mod.rs | 48 ++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/events/mod.rs b/src/events/mod.rs index 24142510..24e66f2d 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -709,6 +709,30 @@ impl<'a> BytesCData<'a> { } /// Creates an iterator of `BytesCData` from a string. + /// + /// # Examples + /// + /// ``` + /// # use quick_xml::events::BytesCData; + /// let content = ""; + /// let cdata = BytesCData::escaped(content).collect::>(); + /// assert_eq!(cdata.len(), 1); + /// assert_eq!(cdata[0].as_ref(), b""); + /// + /// let content = "Certain tokens like ]]> can be difficult and "; + /// let cdata = BytesCData::escaped(content).collect::>(); + /// assert_eq!(cdata.len(), 2); + /// assert_eq!(cdata[0].as_ref(), b"Certain tokens like ]]"); + /// assert_eq!(cdata[1].as_ref(), b"> can be difficult and "); + /// + /// let content = "foo]]>bar]]>baz]]>quux"; + /// let cdata = BytesCData::escaped(content).collect::>(); + /// assert_eq!(cdata.len(), 4); + /// assert_eq!(cdata[0].as_ref(), b"foo]]"); + /// assert_eq!(cdata[1].as_ref(), b">bar]]"); + /// assert_eq!(cdata[2].as_ref(), b">baz]]"); + /// assert_eq!(cdata[3].as_ref(), b">quux"); + /// ``` #[inline] pub fn escaped(content: &'a str) -> CDataIterator<'a> { let iter = content.match_indices("]]>"); @@ -883,7 +907,7 @@ impl<'a> Iterator for CDataIterator<'a> { // mark the iterator as finished. (false, None) => { self.finished = true; - + let slice = &self.content[self.start..]; Some(BytesCData::wrap(slice.as_bytes(), Decoder::utf8())) } @@ -1503,26 +1527,4 @@ mod test { assert_eq!(b.len(), 4); assert_eq!(b.name(), QName(b"test")); } - - #[test] - fn cdata_escaping() { - let content = ""; - let cdata = BytesCData::escaped(content).collect::>(); - assert_eq!(cdata.len(), 1); - assert_eq!(cdata[0].as_ref(), b""); - - let content = "Certain tokens like ]]> can be difficult and "; - let cdata = BytesCData::escaped(content).collect::>(); - assert_eq!(cdata.len(), 2); - assert_eq!(cdata[0].as_ref(), b"Certain tokens like ]]"); - assert_eq!(cdata[1].as_ref(), b"> can be difficult and "); - - let content = "foo]]>bar]]>baz]]>quux"; - let cdata = BytesCData::escaped(content).collect::>(); - assert_eq!(cdata.len(), 4); - assert_eq!(cdata[0].as_ref(), b"foo]]"); - assert_eq!(cdata[1].as_ref(), b">bar]]"); - assert_eq!(cdata[2].as_ref(), b">baz]]"); - assert_eq!(cdata[3].as_ref(), b">quux"); - } }