Skip to content

Commit

Permalink
BytesCData: Convert cdata_escaping test to doc test
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Nov 17, 2024
1 parent 99cc72f commit 28ead21
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
/// assert_eq!(cdata.len(), 1);
/// assert_eq!(cdata[0].as_ref(), b"");
///
/// let content = "Certain tokens like ]]> can be difficult and <invalid>";
/// let cdata = BytesCData::escaped(content).collect::<Vec<_>>();
/// 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 <invalid>");
///
/// let content = "foo]]>bar]]>baz]]>quux";
/// let cdata = BytesCData::escaped(content).collect::<Vec<_>>();
/// 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("]]>");
Expand Down Expand Up @@ -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()))
}
Expand Down Expand Up @@ -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::<Vec<_>>();
assert_eq!(cdata.len(), 1);
assert_eq!(cdata[0].as_ref(), b"");

let content = "Certain tokens like ]]> can be difficult and <invalid>";
let cdata = BytesCData::escaped(content).collect::<Vec<_>>();
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 <invalid>");

let content = "foo]]>bar]]>baz]]>quux";
let cdata = BytesCData::escaped(content).collect::<Vec<_>>();
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");
}
}

0 comments on commit 28ead21

Please sign in to comment.