From 4383b7de88a29ab56ed8ff7f88b33eefbb49db95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 17 Sep 2022 22:17:26 +0200 Subject: [PATCH] component: add TestElement to have a custom Debug for Element MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/component.rs | 86 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/src/component.rs b/src/component.rs index ec86b58..2a9b30a 100644 --- a/src/component.rs +++ b/src/component.rs @@ -78,7 +78,7 @@ impl Component { } enum Expect { - Element(Element), + Element(TestElement), Iq(Box), Presence(Box), Message(Box), @@ -96,24 +96,86 @@ impl fmt::Debug for Expect { } } +#[derive(Clone, Eq, PartialEq)] +pub struct TestElement(pub Element); + +impl Deref for TestElement { + type Target = Element; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl fmt::Debug for TestElement { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", String::from(&self.0)) + } +} + +impl fmt::Display for TestElement { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", String::from(&self.0)) + } +} + +impl From<&TestElement> for String { + fn from(elem: &TestElement) -> Self { + format!("{}", elem) + } +} + +impl From for TestElement { + fn from(elem: Element) -> Self { + Self(elem) + } +} + +impl From for Element { + fn from(elem: TestElement) -> Self { + elem.0 + } +} + +impl From for TestElement { + fn from(elem: Iq) -> Self { + Self(Element::from(elem)) + } +} + +impl From for TestElement { + fn from(elem: Presence) -> Self { + Self(Element::from(elem)) + } +} + +impl From for TestElement { + fn from(elem: Message) -> Self { + Self(Element::from(elem)) + } +} + #[derive(Debug)] pub struct TestComponent { - in_buffer: VecDeque, - out_buffer: VecDeque, + in_buffer: VecDeque, + out_buffer: VecDeque, expect_buffer: VecDeque, } impl TestComponent { pub fn new(in_buffer: Vec) -> Self { TestComponent { - in_buffer: VecDeque::from(in_buffer), + in_buffer: VecDeque::from(in_buffer.into_iter() + .map(|el| TestElement(el)) + .collect::>() + ), out_buffer: VecDeque::new(), expect_buffer: VecDeque::new(), } } /// Adds elements to be expected, in the order they're being added - pub fn expect>(&mut self, el: E) { + pub fn expect>(&mut self, el: E) { self.expect_buffer.push_back(Expect::Element(el.into())) } @@ -141,9 +203,9 @@ impl TestComponent { (None, None) => break, (Some(out), Some(expected)) => match expected { Expect::Element(el) => assert_eq!(String::from(&el), String::from(&out)), - Expect::Iq(cb) => cb(Iq::try_from(out).unwrap()), - Expect::Message(cb) => cb(Message::try_from(out).unwrap()), - Expect::Presence(cb) => cb(Presence::try_from(out).unwrap()), + Expect::Iq(cb) => cb(Iq::try_from(out.0).unwrap()), + Expect::Message(cb) => cb(Message::try_from(out.0).unwrap()), + Expect::Presence(cb) => cb(Presence::try_from(out.0).unwrap()), }, (Some(out), None) => panic!("Missing matching expected element: {:?}", out), (None, Some(expected)) => match expected { @@ -156,7 +218,7 @@ impl TestComponent { } } - fn _send_stanza + Send>(&mut self, el: E) -> Result<(), Error> { + fn _send_stanza + Send>(&mut self, el: E) -> Result<(), Error> { Ok(self.out_buffer.push_back(el.into())) } } @@ -166,7 +228,7 @@ impl Stream for TestComponent { fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll> { while self.in_buffer.len() > 0 { - return Poll::Ready(self.in_buffer.pop_front()); + return Poll::Ready(self.in_buffer.pop_front().map(|el| el.0)); } Poll::Ready(None) @@ -176,13 +238,13 @@ impl Stream for TestComponent { #[async_trait] impl ComponentTrait for TestComponent { async fn send_stanza + Send>(&mut self, el: E) -> Result<(), Error> { - self._send_stanza(el) + self._send_stanza(el.into()) } } #[async_trait] impl ComponentTrait for &mut TestComponent { async fn send_stanza + Send>(&mut self, el: E) -> Result<(), Error> { - self._send_stanza(el) + self._send_stanza(el.into()) } }