diff --git a/src/component.rs b/src/component.rs index dfd6804..d02ff75 100644 --- a/src/component.rs +++ b/src/component.rs @@ -6,19 +6,24 @@ use crate::error::Error; +#[cfg(test)] use std::collections::VecDeque; +#[cfg(test)] use std::fmt; use std::marker::Send; use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::task::Context; +#[cfg(test)] use std::thread; use async_trait::async_trait; use futures::{task::Poll, Stream}; use log::debug; use tokio_xmpp::Component as TokioXMPPComponent; -use xmpp_parsers::{iq::Iq, message::Message, presence::Presence, Element}; +use xmpp_parsers::Element; +#[cfg(test)] +use xmpp_parsers::{iq::Iq, message::Message, presence::Presence}; // Testable interface #[async_trait] @@ -78,6 +83,7 @@ impl Component { } } +#[cfg(test)] enum Expect { /// Simple Element Element(TestElement), @@ -89,6 +95,7 @@ enum Expect { Message(Box, String), } +#[cfg(test)] impl fmt::Debug for Expect { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Expect::")?; @@ -101,9 +108,11 @@ impl fmt::Debug for Expect { } } +#[cfg(test)] #[derive(Clone, Eq, PartialEq)] pub struct TestElement(pub Element); +#[cfg(test)] impl Deref for TestElement { type Target = Element; @@ -112,60 +121,70 @@ impl Deref for TestElement { } } +#[cfg(test)] impl fmt::Debug for TestElement { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", String::from(&self.0)) } } +#[cfg(test)] impl fmt::Display for TestElement { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", String::from(&self.0)) } } +#[cfg(test)] impl From<&TestElement> for String { fn from(elem: &TestElement) -> Self { format!("{}", elem) } } +#[cfg(test)] impl From for TestElement { fn from(elem: Element) -> Self { Self(elem) } } +#[cfg(test)] impl From for Element { fn from(elem: TestElement) -> Self { elem.0 } } +#[cfg(test)] impl From for TestElement { fn from(elem: Iq) -> Self { Self(Element::from(elem)) } } +#[cfg(test)] impl From for TestElement { fn from(elem: Presence) -> Self { Self(Element::from(elem)) } } +#[cfg(test)] impl From for TestElement { fn from(elem: Message) -> Self { Self(Element::from(elem)) } } +#[cfg(test)] #[derive(Debug)] pub struct TestComponent { in_buffer: VecDeque, expect_buffer: VecDeque, } +#[cfg(test)] impl TestComponent { pub fn new(in_buffer: Vec) -> Self { TestComponent { @@ -247,6 +266,7 @@ impl TestComponent { } } +#[cfg(test)] impl Stream for TestComponent { type Item = Element; @@ -259,6 +279,7 @@ impl Stream for TestComponent { } } +#[cfg(test)] impl Drop for TestComponent { fn drop(&mut self) { // Don't assert if we're already panicking. Rustc displays a huge backtrace when "panicked @@ -274,6 +295,7 @@ impl Drop for TestComponent { } } +#[cfg(test)] #[async_trait] impl ComponentTrait for TestComponent { async fn send_stanza + Send>(&mut self, el: E) -> Result<(), Error> { @@ -281,6 +303,7 @@ impl ComponentTrait for TestComponent { } } +#[cfg(test)] #[async_trait] impl ComponentTrait for &mut TestComponent { async fn send_stanza + Send>(&mut self, el: E) -> Result<(), Error> {