diff --git a/src/component.rs b/src/component.rs index 248d563..ec86b58 100644 --- a/src/component.rs +++ b/src/component.rs @@ -17,7 +17,7 @@ use async_trait::async_trait; use futures::{task::Poll, Stream}; use log::debug; use tokio_xmpp::Component as TokioXMPPComponent; -use xmpp_parsers::Element; +use xmpp_parsers::{iq::Iq, message::Message, presence::Presence, Element}; // Testable interface #[async_trait] @@ -79,7 +79,9 @@ impl Component { enum Expect { Element(Element), - Callback(Box), + Iq(Box), + Presence(Box), + Message(Box), } impl fmt::Debug for Expect { @@ -87,7 +89,9 @@ impl fmt::Debug for Expect { write!(f, "Expect::")?; match self { Expect::Element(el) => write!(f, "Element({:?})", String::from(el)), - Expect::Callback(_) => write!(f, "Callback()"), + Expect::Iq(_) => write!(f, "Iq()"), + Expect::Message(_) => write!(f, "Message()"), + Expect::Presence(_) => write!(f, "Presence()"), } } } @@ -113,9 +117,18 @@ impl TestComponent { self.expect_buffer.push_back(Expect::Element(el.into())) } - pub fn expect_with(&mut self, callback: F) { + pub fn expect_iq(&mut self, callback: F) { + self.expect_buffer.push_back(Expect::Iq(Box::new(callback))) + } + + pub fn expect_message(&mut self, callback: F) { self.expect_buffer - .push_back(Expect::Callback(Box::new(callback))) + .push_back(Expect::Message(Box::new(callback))) + } + + pub fn expect_presence(&mut self, callback: F) { + self.expect_buffer + .push_back(Expect::Presence(Box::new(callback))) } /// Asserts expected output and actual output are the same @@ -128,12 +141,16 @@ impl TestComponent { (None, None) => break, (Some(out), Some(expected)) => match expected { Expect::Element(el) => assert_eq!(String::from(&el), String::from(&out)), - Expect::Callback(cb) => cb(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()), }, (Some(out), None) => panic!("Missing matching expected element: {:?}", out), (None, Some(expected)) => match expected { Expect::Element(el) => panic!("Missing matching sent element: {:?}", el), - Expect::Callback(_) => panic!("Missing matching sent element"), + Expect::Iq(_) => panic!("Missing matching sent iq"), + Expect::Message(_) => panic!("Missing matching sent message"), + Expect::Presence(_) => panic!("Missing matching sent presence"), }, } } diff --git a/src/tests.rs b/src/tests.rs index dc518c7..6e9ce61 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -106,7 +106,7 @@ async fn test_join_presence_empty_room() { .into()]), ); - component.expect_with(move |el| { + component.expect_message(move |mut out| { let roomjid = COMPONENT_JID.clone().with_node("room"); let mut subjects = BTreeMap::new(); @@ -124,7 +124,6 @@ async fn test_join_presence_empty_room() { } .into(); - let mut out = Message::try_from(el).unwrap(); assert_eq!(out.payloads.len(), 1); let delay = Delay::try_from(out.payloads[0].clone()).unwrap(); // Ensure delay is issued from the room JID. Ignore stamp as it'll always vary. @@ -176,9 +175,9 @@ async fn test_join_presence_nick_already_assigned() { let mut rooms: HashMap = HashMap::new(); // Ignore self-presence for first participant - component.expect_with(|_| ()); + component.expect_presence(|_| ()); // Ignore message subject for first participant - component.expect_with(|_| ()); + component.expect_message(|_| ()); component.expect( Presence::new(PresenceType::Error) @@ -226,9 +225,9 @@ async fn test_join_presence_existing_room() { let mut rooms: HashMap = HashMap::new(); // Ignore self-presence for first participant - component.expect_with(|_| ()); + component.expect_presence(|_| ()); // Ignore message subject for first participant - component.expect_with(|_| ()); + component.expect_message(|_| ()); // Occupant presences for participant2 component.expect( @@ -253,7 +252,7 @@ async fn test_join_presence_existing_room() { ); // Subject for participant2 - component.expect_with(|el| { + component.expect_message(|el| { let mut subjects = BTreeMap::new(); subjects.insert(String::from("en"), MessageSubject::from_str("").unwrap()); let expected: Element = Message { @@ -266,7 +265,8 @@ async fn test_join_presence_existing_room() { subjects, thread: None, payloads: Vec::new(), - }.into(); + } + .into(); let mut out = Message::try_from(el).unwrap(); // Check that is present. Don't check content as it's checked in a test earlier @@ -330,9 +330,9 @@ async fn test_leave_last_participant() { let mut rooms: HashMap = HashMap::new(); // Ignore self-presence for participant1 - component.expect_with(|_| ()); + component.expect_presence(|_| ()); // Ignore subject message for participant1 - component.expect_with(|_| ()); + component.expect_message(|_| ()); component.expect(Presence::new(PresenceType::Unavailable) .with_from(Jid::Full(participant1.clone())) @@ -374,21 +374,20 @@ async fn test_leave_room_not_last() { .with_to(Jid::Full(participant2.clone())) .into(); - // Room still has occupants after leave let mut component = TestComponent::new(vec![join1, join2, leave2]); let mut rooms: HashMap = HashMap::new(); // Ignore self-presence for participant1 - component.expect_with(|_| ()); + component.expect_presence(|_| ()); // Ignore subject message for participant1 - component.expect_with(|_| ()); - // Ignore participant1 occupant presence for participant2 - component.expect_with(|_| ()); + component.expect_message(|_| ()); + // Ignore participant1 presence for participant2 + component.expect_presence(|_| ()); // Ignore self-presence for participant2 - component.expect_with(|_| ()); + component.expect_presence(|_| ()); // Ignore subject message for participant2 - component.expect_with(|_| ()); + component.expect_message(|_| ()); component.expect(Presence::new(PresenceType::Unavailable) .with_from(Jid::Full(participant2.clone()))