From 02a928a47c43cb0144e5acad2cf0442a5a1faaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Sat, 10 Aug 2024 08:56:19 +0200 Subject: [PATCH] xso: add some tests --- xso/src/asxml.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/xso/src/asxml.rs b/xso/src/asxml.rs index b7bee08c..21eee74c 100644 --- a/xso/src/asxml.rs +++ b/xso/src/asxml.rs @@ -67,3 +67,65 @@ impl AsXml for Box { Ok(BoxAsXml(Box::new(T::as_xml_iter(&self)?))) } } + +#[cfg(test)] +mod tests { + use super::*; + + use std::borrow::Cow; + + #[test] + fn option_as_xml_terminates_immediately_for_none() { + let mut iter = OptionAsXml::>(None); + match iter.next() { + None => (), + other => panic!("unexpected item: {:?}", other), + } + } + + #[test] + fn option_as_xml_passes_values_from_inner_some() { + let inner = vec![ + Ok(Item::Text(Cow::Borrowed("hello world"))), + Ok(Item::ElementFoot), + ]; + let mut iter = OptionAsXml(Some(inner.into_iter())); + match iter.next() { + Some(Ok(Item::Text(text))) => { + assert_eq!(text, "hello world"); + } + other => panic!("unexpected item: {:?}", other), + } + match iter.next() { + Some(Ok(Item::ElementFoot)) => (), + other => panic!("unexpected item: {:?}", other), + } + match iter.next() { + None => (), + other => panic!("unexpected item: {:?}", other), + } + } + + #[test] + fn box_as_xml_passes_values_from_inner() { + let inner = vec![ + Ok(Item::Text(Cow::Borrowed("hello world"))), + Ok(Item::ElementFoot), + ]; + let mut iter = BoxAsXml(Box::new(inner.into_iter())); + match iter.next() { + Some(Ok(Item::Text(text))) => { + assert_eq!(text, "hello world"); + } + other => panic!("unexpected item: {:?}", other), + } + match iter.next() { + Some(Ok(Item::ElementFoot)) => (), + other => panic!("unexpected item: {:?}", other), + } + match iter.next() { + None => (), + other => panic!("unexpected item: {:?}", other), + } + } +}