Expect: add description alongside callback

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-09-18 16:21:51 +02:00
parent ee4ea6dd9a
commit 1b5e80529b
2 changed files with 55 additions and 57 deletions

View file

@ -78,10 +78,14 @@ impl Component {
}
enum Expect {
/// Simple Element
Element(TestElement),
Iq(Box<dyn FnOnce(Iq) + Send + 'static>),
Presence(Box<dyn FnOnce(Presence) + Send + 'static>),
Message(Box<dyn FnOnce(Message) + Send + 'static>),
/// Callback taking an Iq, with a description alongside
Iq(Box<dyn FnOnce(Iq) + Send + 'static>, String),
/// Callback taking a Presence, with a description alongside
Presence(Box<dyn FnOnce(Presence) + Send + 'static>, String),
/// Callback taking a Message, with a description alongside
Message(Box<dyn FnOnce(Message) + Send + 'static>, String),
}
impl fmt::Debug for Expect {
@ -89,9 +93,9 @@ impl fmt::Debug for Expect {
write!(f, "Expect::")?;
match self {
Expect::Element(el) => write!(f, "Element({:?})", String::from(el)),
Expect::Iq(_) => write!(f, "Iq(<cb>)"),
Expect::Message(_) => write!(f, "Message(<cb>)"),
Expect::Presence(_) => write!(f, "Presence(<cb>)"),
Expect::Iq(_, desc) => write!(f, "Iq(<cb>, {})", desc),
Expect::Message(_, desc) => write!(f, "Message(<cb>, {})", desc),
Expect::Presence(_, desc) => write!(f, "Presence(<cb>, {})", desc),
}
}
}
@ -181,18 +185,31 @@ impl TestComponent {
self.expect_buffer.push_back(Expect::Element(el.into()))
}
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_iq<F: FnOnce(Iq) + Send + 'static, S: Into<String>>(
&mut self,
callback: F,
desc: S,
) {
self.expect_buffer
.push_back(Expect::Iq(Box::new(callback), desc.into()))
}
pub fn expect_message<F: FnOnce(Message) + Send + 'static>(&mut self, callback: F) {
pub fn expect_message<F: FnOnce(Message) + Send + 'static, S: Into<String>>(
&mut self,
callback: F,
desc: S,
) {
self.expect_buffer
.push_back(Expect::Message(Box::new(callback)))
.push_back(Expect::Message(Box::new(callback), desc.into()))
}
pub fn expect_presence<F: FnOnce(Presence) + Send + 'static>(&mut self, callback: F) {
pub fn expect_presence<F: FnOnce(Presence) + Send + 'static, S: Into<String>>(
&mut self,
callback: F,
desc: S,
) {
self.expect_buffer
.push_back(Expect::Presence(Box::new(callback)))
.push_back(Expect::Presence(Box::new(callback), desc.into()))
}
/// Asserts expected output and actual output are the same
@ -205,16 +222,16 @@ 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.0).unwrap()),
Expect::Message(cb) => cb(Message::try_from(out.0).unwrap()),
Expect::Presence(cb) => cb(Presence::try_from(out.0).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 {
Expect::Element(el) => panic!("Missing matching sent element: {:?}", el),
Expect::Iq(_) => panic!("Missing matching sent iq"),
Expect::Message(_) => panic!("Missing matching sent message"),
Expect::Presence(_) => panic!("Missing matching sent presence"),
Expect::Iq(_, desc) => panic!("Missing iq: {}", desc),
Expect::Message(_, desc) => panic!("Missing message: {}", desc),
Expect::Presence(_, desc) => panic!("Missing presence: {}", desc),
},
}
}

View file

@ -134,7 +134,7 @@ async fn test_join_presence_empty_room() {
String::from(&expected),
String::from(&Into::<Element>::into(out))
);
});
}, "Room subject to participant1");
handle_stanza(&mut component, &mut rooms).await.unwrap();
component.assert();
@ -174,10 +174,8 @@ async fn test_join_presence_nick_already_assigned() {
let mut component = TestComponent::new(vec![join1, join2]);
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
// Ignore self-presence for first participant
component.expect_presence(|_| ());
// Ignore message subject for first participant
component.expect_message(|_| ());
component.expect_presence(|_| (), "Self-presence for first participant");
component.expect_message(|_| (), "Subject for first participant");
component.expect(
Presence::new(PresenceType::Error)
@ -224,10 +222,8 @@ async fn test_join_presence_existing_room() {
let mut component = TestComponent::new(vec![join1, join2]);
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
// Ignore self-presence for first participant
component.expect_presence(|_| ());
// Ignore message subject for first participant
component.expect_message(|_| ());
component.expect_presence(|_| (), "Self-presence for participant1");
component.expect_message(|_| (), "Subject for participant1");
// Participant1 presence for participant2
component.expect(
@ -265,7 +261,6 @@ async fn test_join_presence_existing_room() {
.into()]),
);
// Subject for participant2
component.expect_message(|el| {
let mut subjects = BTreeMap::new();
subjects.insert(String::from("en"), MessageSubject::from_str("").unwrap());
@ -292,7 +287,7 @@ async fn test_join_presence_existing_room() {
String::from(&expected),
String::from(&Into::<Element>::into(out))
);
});
}, "Subject for participant2");
handle_stanza(&mut component, &mut rooms).await.unwrap();
component.assert();
@ -326,18 +321,12 @@ async fn test_presence_resync() {
let mut component = TestComponent::new(vec![join1.clone(), join2, join1]);
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
// Self-presence for participant1
component.expect_presence(|_| ());
// Subject for participant1
component.expect_message(|_| ());
// Participant1 presence for participant2
component.expect_presence(|_| ());
// Participant2 presence for participant1
component.expect_presence(|_| ());
// Self-presence for participant2
component.expect_presence(|_| ());
// Subject for participant2
component.expect_message(|_| ());
component.expect_presence(|_| (), "Self-presence for participant1");
component.expect_message(|_| (), "Subject for participant1");
component.expect_presence(|_| (), "Participant1 presence for participant2");
component.expect_presence(|_| (), "Participant2 presence for participant1");
component.expect_presence(|_| (), "Self-presence for participant2");
component.expect_message(|_| (), "Subject for participant2");
// Resync: Participant2 presence for participant1
component.expect(
@ -413,10 +402,8 @@ async fn test_leave_last_participant() {
let mut component = TestComponent::new(vec![join1, leave1]);
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
// Ignore self-presence for participant1
component.expect_presence(|_| ());
// Ignore subject message for participant1
component.expect_message(|_| ());
component.expect_presence(|_| (), "Self-presence for participant1");
component.expect_message(|_| (), "Subject for participant1");
component.expect(
Presence::new(PresenceType::Unavailable)
@ -464,18 +451,12 @@ async fn test_leave_room_not_last() {
let mut component = TestComponent::new(vec![join1, join2, leave2]);
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
// Ignore self-presence for participant1
component.expect_presence(|_| ());
// Ignore subject message for participant1
component.expect_message(|_| ());
// Ignore participant1 presence for participant2
component.expect_presence(|_| ());
// Ignore self-presence for participant2
component.expect_presence(|_| ());
// Ignore participant2 presence for participant1
component.expect_presence(|_| ());
// Ignore subject message for participant2
component.expect_message(|_| ());
component.expect_presence(|_| (), "Self-presence for participant1");
component.expect_message(|_| (), "Subject message for participant1");
component.expect_presence(|_| (), "Participant1 presence for participant2");
component.expect_presence(|_| (), "Self-presence for participant2");
component.expect_presence(|_| (), "Participant2 presence for participant1");
component.expect_message(|_| (), "Subject message for participant2");
component.expect(
Presence::new(PresenceType::Unavailable)