Skip to content

Commit

Permalink
Add test for ignored_any
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Aug 22, 2021
1 parent d683c37 commit b3c1595
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/de/mod.rs
Expand Up @@ -452,10 +452,18 @@ impl<'de, 'a, R: BorrowingReader<'de>> de::Deserializer<'de> for &'a mut Deseria
self.deserialize_string(visitor)
}

/// Always call `visitor.visit_unit()` because returned value ignored in any case.
///
/// This method consumes any single [event][Event] except the [`Start`][Event::Start]
/// event. in which case all events up to corresponding [`End`][Event::End] event will
/// be consumed.
///
/// This method returns error if current event is [`End`][Event::End] or [`Eof`][Event::Eof]
fn deserialize_ignored_any<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
match self.next()? {
Event::Start(e) => self.read_to_end(e.name())?,
Event::End(_) => return Err(DeError::End),
Event::Eof => return Err(DeError::Eof),
_ => (),
}
visitor.visit_unit()
Expand Down Expand Up @@ -570,6 +578,7 @@ impl<'de> BorrowingReader<'de> for SliceReader<'de> {
#[cfg(test)]
mod tests {
use super::*;
use serde::de::IgnoredAny;
use serde::Deserialize;

/// Deserialize an instance of type T from a string of XML text.
Expand Down Expand Up @@ -955,6 +964,22 @@ mod tests {
assert_eq!(item, Item);
}

/// Tests calling `deserialize_ignored_any`
#[test]
fn ignored_any() {
let err = from_str::<IgnoredAny>("");
match err {
Err(DeError::Eof) => {}
other => panic!("Expected `Eof`, found {:?}", other),
}

from_str::<IgnoredAny>(r#"<empty/>"#).unwrap();
from_str::<IgnoredAny>(r#"<with-attributes key="value"/>"#).unwrap();
from_str::<IgnoredAny>(r#"<nested>text</nested>"#).unwrap();
from_str::<IgnoredAny>(r#"<nested><![CDATA[cdata]]></nested>"#).unwrap();
from_str::<IgnoredAny>(r#"<nested><nested/></nested>"#).unwrap();
}

mod unit {
use super::*;

Expand Down

0 comments on commit b3c1595

Please sign in to comment.