TestComponent: Split expect_with into _iq, _message, _presence

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-09-12 00:10:57 +02:00
parent d383aa8655
commit 43c31ed1c3
2 changed files with 40 additions and 24 deletions

View file

@ -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<dyn FnOnce(Element) + Send + 'static>),
Iq(Box<dyn FnOnce(Iq) + Send + 'static>),
Presence(Box<dyn FnOnce(Presence) + Send + 'static>),
Message(Box<dyn FnOnce(Message) + Send + 'static>),
}
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(<cb>)"),
Expect::Iq(_) => write!(f, "Iq(<cb>)"),
Expect::Message(_) => write!(f, "Message(<cb>)"),
Expect::Presence(_) => write!(f, "Presence(<cb>)"),
}
}
}
@ -113,9 +117,18 @@ impl TestComponent {
self.expect_buffer.push_back(Expect::Element(el.into()))
}
pub fn expect_with<F: FnOnce(Element) + Send + 'static>(&mut self, callback: F) {
pub fn expect_iq<F: FnOnce(Iq) + Send + 'static>(&mut self, callback: F) {
self.expect_buffer.push_back(Expect::Iq(Box::new(callback)))
}
pub fn expect_message<F: FnOnce(Message) + Send + 'static>(&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<F: FnOnce(Presence) + Send + 'static>(&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"),
},
}
}

View file

@ -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<BareJid, Room> = 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<BareJid, Room> = 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 <delay/> 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<BareJid, Room> = 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<BareJid, Room> = 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()))