component: guard with #[cfg(test)] what's only used in tests
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
b2da2495a1
commit
0252aece38
1 changed files with 24 additions and 1 deletions
|
@ -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<dyn FnOnce(Message) + Send + 'static>, 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<Element> for TestElement {
|
||||
fn from(elem: Element) -> Self {
|
||||
Self(elem)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl From<TestElement> for Element {
|
||||
fn from(elem: TestElement) -> Self {
|
||||
elem.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl From<Iq> for TestElement {
|
||||
fn from(elem: Iq) -> Self {
|
||||
Self(Element::from(elem))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl From<Presence> for TestElement {
|
||||
fn from(elem: Presence) -> Self {
|
||||
Self(Element::from(elem))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl From<Message> for TestElement {
|
||||
fn from(elem: Message) -> Self {
|
||||
Self(Element::from(elem))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[derive(Debug)]
|
||||
pub struct TestComponent {
|
||||
in_buffer: VecDeque<TestElement>,
|
||||
expect_buffer: VecDeque<Expect>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl TestComponent {
|
||||
pub fn new(in_buffer: Vec<Element>) -> 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<E: Into<Element> + 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<E: Into<Element> + Send>(&mut self, el: E) -> Result<(), Error> {
|
||||
|
|
Loading…
Reference in a new issue