Expect: add description alongside callback
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
562aadb488
commit
e7d31e41dc
2 changed files with 60 additions and 60 deletions
|
@ -78,10 +78,14 @@ impl Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Expect {
|
enum Expect {
|
||||||
|
/// Simple Element
|
||||||
Element(TestElement),
|
Element(TestElement),
|
||||||
Iq(Box<dyn FnOnce(Iq) + Send + 'static>),
|
/// Callback taking an Iq, with a description alongside
|
||||||
Presence(Box<dyn FnOnce(Presence) + Send + 'static>),
|
Iq(Box<dyn FnOnce(Iq) + Send + 'static>, String),
|
||||||
Message(Box<dyn FnOnce(Message) + Send + 'static>),
|
/// 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 {
|
impl fmt::Debug for Expect {
|
||||||
|
@ -89,9 +93,9 @@ impl fmt::Debug for Expect {
|
||||||
write!(f, "Expect::")?;
|
write!(f, "Expect::")?;
|
||||||
match self {
|
match self {
|
||||||
Expect::Element(el) => write!(f, "Element({:?})", String::from(el)),
|
Expect::Element(el) => write!(f, "Element({:?})", String::from(el)),
|
||||||
Expect::Iq(_) => write!(f, "Iq(<cb>)"),
|
Expect::Iq(_, desc) => write!(f, "Iq(<cb>, {})", desc),
|
||||||
Expect::Message(_) => write!(f, "Message(<cb>)"),
|
Expect::Message(_, desc) => write!(f, "Message(<cb>, {})", desc),
|
||||||
Expect::Presence(_) => write!(f, "Presence(<cb>)"),
|
Expect::Presence(_, desc) => write!(f, "Presence(<cb>, {})", desc),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,9 +169,11 @@ pub struct TestComponent {
|
||||||
impl TestComponent {
|
impl TestComponent {
|
||||||
pub fn new(in_buffer: Vec<Element>) -> Self {
|
pub fn new(in_buffer: Vec<Element>) -> Self {
|
||||||
TestComponent {
|
TestComponent {
|
||||||
in_buffer: VecDeque::from(in_buffer.into_iter()
|
in_buffer: VecDeque::from(
|
||||||
.map(|el| TestElement(el))
|
in_buffer
|
||||||
.collect::<Vec<_>>()
|
.into_iter()
|
||||||
|
.map(|el| TestElement(el))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
),
|
),
|
||||||
out_buffer: VecDeque::new(),
|
out_buffer: VecDeque::new(),
|
||||||
expect_buffer: VecDeque::new(),
|
expect_buffer: VecDeque::new(),
|
||||||
|
@ -179,18 +185,31 @@ impl TestComponent {
|
||||||
self.expect_buffer.push_back(Expect::Element(el.into()))
|
self.expect_buffer.push_back(Expect::Element(el.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expect_iq<F: FnOnce(Iq) + Send + 'static>(&mut self, callback: F) {
|
pub fn expect_iq<F: FnOnce(Iq) + Send + 'static, S: Into<String>>(
|
||||||
self.expect_buffer.push_back(Expect::Iq(Box::new(callback)))
|
&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
|
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
|
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
|
/// Asserts expected output and actual output are the same
|
||||||
|
@ -203,16 +222,16 @@ impl TestComponent {
|
||||||
(None, None) => break,
|
(None, None) => break,
|
||||||
(Some(out), Some(expected)) => match expected {
|
(Some(out), Some(expected)) => match expected {
|
||||||
Expect::Element(el) => assert_eq!(String::from(&el), String::from(&out)),
|
Expect::Element(el) => assert_eq!(String::from(&el), String::from(&out)),
|
||||||
Expect::Iq(cb) => cb(Iq::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::Message(cb, _) => cb(Message::try_from(out.0).unwrap()),
|
||||||
Expect::Presence(cb) => cb(Presence::try_from(out.0).unwrap()),
|
Expect::Presence(cb, _) => cb(Presence::try_from(out.0).unwrap()),
|
||||||
},
|
},
|
||||||
(Some(out), None) => panic!("Missing matching expected element: {:?}", out),
|
(Some(out), None) => panic!("Missing matching expected element: {:?}", out),
|
||||||
(None, Some(expected)) => match expected {
|
(None, Some(expected)) => match expected {
|
||||||
Expect::Element(el) => panic!("Missing matching sent element: {:?}", el),
|
Expect::Element(el) => panic!("Missing matching sent element: {:?}", el),
|
||||||
Expect::Iq(_) => panic!("Missing matching sent iq"),
|
Expect::Iq(_, desc) => panic!("Missing iq: {}", desc),
|
||||||
Expect::Message(_) => panic!("Missing matching sent message"),
|
Expect::Message(_, desc) => panic!("Missing message: {}", desc),
|
||||||
Expect::Presence(_) => panic!("Missing matching sent presence"),
|
Expect::Presence(_, desc) => panic!("Missing presence: {}", desc),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
59
src/tests.rs
59
src/tests.rs
|
@ -134,7 +134,7 @@ async fn test_join_presence_empty_room() {
|
||||||
String::from(&expected),
|
String::from(&expected),
|
||||||
String::from(&Into::<Element>::into(out))
|
String::from(&Into::<Element>::into(out))
|
||||||
);
|
);
|
||||||
});
|
}, "Room subject to participant1");
|
||||||
|
|
||||||
handle_stanza(&mut component, &mut rooms).await.unwrap();
|
handle_stanza(&mut component, &mut rooms).await.unwrap();
|
||||||
component.assert();
|
component.assert();
|
||||||
|
@ -174,10 +174,8 @@ async fn test_join_presence_nick_already_assigned() {
|
||||||
let mut component = TestComponent::new(vec![join1, join2]);
|
let mut component = TestComponent::new(vec![join1, join2]);
|
||||||
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
||||||
|
|
||||||
// Ignore self-presence for first participant
|
component.expect_presence(|_| (), "Self-presence for first participant");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject for first participant");
|
||||||
// Ignore message subject for first participant
|
|
||||||
component.expect_message(|_| ());
|
|
||||||
|
|
||||||
component.expect(
|
component.expect(
|
||||||
Presence::new(PresenceType::Error)
|
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 component = TestComponent::new(vec![join1, join2]);
|
||||||
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
||||||
|
|
||||||
// Ignore self-presence for first participant
|
component.expect_presence(|_| (), "Self-presence for participant1");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject for participant1");
|
||||||
// Ignore message subject for first participant
|
|
||||||
component.expect_message(|_| ());
|
|
||||||
|
|
||||||
// Participant1 presence for participant2
|
// Participant1 presence for participant2
|
||||||
component.expect(
|
component.expect(
|
||||||
|
@ -265,7 +261,6 @@ async fn test_join_presence_existing_room() {
|
||||||
.into()]),
|
.into()]),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Subject for participant2
|
|
||||||
component.expect_message(|el| {
|
component.expect_message(|el| {
|
||||||
let mut subjects = BTreeMap::new();
|
let mut subjects = BTreeMap::new();
|
||||||
subjects.insert(String::from("en"), MessageSubject::from_str("").unwrap());
|
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(&expected),
|
||||||
String::from(&Into::<Element>::into(out))
|
String::from(&Into::<Element>::into(out))
|
||||||
);
|
);
|
||||||
});
|
}, "Subject for participant2");
|
||||||
|
|
||||||
handle_stanza(&mut component, &mut rooms).await.unwrap();
|
handle_stanza(&mut component, &mut rooms).await.unwrap();
|
||||||
component.assert();
|
component.assert();
|
||||||
|
@ -326,18 +321,12 @@ async fn test_presence_resync() {
|
||||||
let mut component = TestComponent::new(vec![join1.clone(), join2, join1]);
|
let mut component = TestComponent::new(vec![join1.clone(), join2, join1]);
|
||||||
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
||||||
|
|
||||||
// Self-presence for participant1
|
component.expect_presence(|_| (), "Self-presence for participant1");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject for participant1");
|
||||||
// Subject for participant1
|
component.expect_presence(|_| (), "Participant1 presence for participant2");
|
||||||
component.expect_message(|_| ());
|
component.expect_presence(|_| (), "Participant2 presence for participant1");
|
||||||
// Participant1 presence for participant2
|
component.expect_presence(|_| (), "Self-presence for participant2");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject for participant2");
|
||||||
// Participant2 presence for participant1
|
|
||||||
component.expect_presence(|_| ());
|
|
||||||
// Self-presence for participant2
|
|
||||||
component.expect_presence(|_| ());
|
|
||||||
// Subject for participant2
|
|
||||||
component.expect_message(|_| ());
|
|
||||||
|
|
||||||
// Resync: Participant2 presence for participant1
|
// Resync: Participant2 presence for participant1
|
||||||
component.expect(
|
component.expect(
|
||||||
|
@ -413,10 +402,8 @@ async fn test_leave_last_participant() {
|
||||||
let mut component = TestComponent::new(vec![join1, leave1]);
|
let mut component = TestComponent::new(vec![join1, leave1]);
|
||||||
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
||||||
|
|
||||||
// Ignore self-presence for participant1
|
component.expect_presence(|_| (), "Self-presence for participant1");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject for participant1");
|
||||||
// Ignore subject message for participant1
|
|
||||||
component.expect_message(|_| ());
|
|
||||||
|
|
||||||
component.expect(
|
component.expect(
|
||||||
Presence::new(PresenceType::Unavailable)
|
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 component = TestComponent::new(vec![join1, join2, leave2]);
|
||||||
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
let mut rooms: HashMap<BareJid, Room> = HashMap::new();
|
||||||
|
|
||||||
// Ignore self-presence for participant1
|
component.expect_presence(|_| (), "Self-presence for participant1");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject message for participant1");
|
||||||
// Ignore subject message for participant1
|
component.expect_presence(|_| (), "Participant1 presence for participant2");
|
||||||
component.expect_message(|_| ());
|
component.expect_presence(|_| (), "Self-presence for participant2");
|
||||||
// Ignore participant1 presence for participant2
|
component.expect_presence(|_| (), "Participant2 presence for participant1");
|
||||||
component.expect_presence(|_| ());
|
component.expect_message(|_| (), "Subject message for participant2");
|
||||||
// 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(
|
component.expect(
|
||||||
Presence::new(PresenceType::Unavailable)
|
Presence::new(PresenceType::Unavailable)
|
||||||
|
|
Loading…
Reference in a new issue