component: add TestElement to have a custom Debug for Element

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-09-17 22:17:26 +02:00
parent f2c8e74ef4
commit 4383b7de88

View file

@ -78,7 +78,7 @@ impl Component {
}
enum Expect {
Element(Element),
Element(TestElement),
Iq(Box<dyn FnOnce(Iq) + Send + 'static>),
Presence(Box<dyn FnOnce(Presence) + Send + 'static>),
Message(Box<dyn FnOnce(Message) + Send + 'static>),
@ -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<Element> for TestElement {
fn from(elem: Element) -> Self {
Self(elem)
}
}
impl From<TestElement> for Element {
fn from(elem: TestElement) -> Self {
elem.0
}
}
impl From<Iq> for TestElement {
fn from(elem: Iq) -> Self {
Self(Element::from(elem))
}
}
impl From<Presence> for TestElement {
fn from(elem: Presence) -> Self {
Self(Element::from(elem))
}
}
impl From<Message> for TestElement {
fn from(elem: Message) -> Self {
Self(Element::from(elem))
}
}
#[derive(Debug)]
pub struct TestComponent {
in_buffer: VecDeque<Element>,
out_buffer: VecDeque<Element>,
in_buffer: VecDeque<TestElement>,
out_buffer: VecDeque<TestElement>,
expect_buffer: VecDeque<Expect>,
}
impl TestComponent {
pub fn new(in_buffer: Vec<Element>) -> Self {
TestComponent {
in_buffer: VecDeque::from(in_buffer),
in_buffer: VecDeque::from(in_buffer.into_iter()
.map(|el| TestElement(el))
.collect::<Vec<_>>()
),
out_buffer: VecDeque::new(),
expect_buffer: VecDeque::new(),
}
}
/// Adds elements to be expected, in the order they're being added
pub fn expect<E: Into<Element>>(&mut self, el: E) {
pub fn expect<E: Into<TestElement>>(&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<E: Into<Element> + Send>(&mut self, el: E) -> Result<(), Error> {
fn _send_stanza<E: Into<TestElement> + 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<Option<Self::Item>> {
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<E: Into<Element> + 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<E: Into<Element> + Send>(&mut self, el: E) -> Result<(), Error> {
self._send_stanza(el)
self._send_stanza(el.into())
}
}