From e7d31e41dc82327ecfe846fde7c9ba6c003c505b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 18 Sep 2022 16:21:51 +0200 Subject: [PATCH] Expect: add description alongside callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/component.rs | 61 +++++++++++++++++++++++++++++++----------------- src/tests.rs | 59 ++++++++++++++++------------------------------ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/component.rs b/src/component.rs index 2a9b30a..02f40d8 100644 --- a/src/component.rs +++ b/src/component.rs @@ -78,10 +78,14 @@ impl Component { } enum Expect { + /// Simple Element Element(TestElement), - Iq(Box), - Presence(Box), - Message(Box), + /// Callback taking an Iq, with a description alongside + Iq(Box, String), + /// Callback taking a Presence, with a description alongside + Presence(Box, String), + /// Callback taking a Message, with a description alongside + Message(Box, 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()"), - Expect::Message(_) => write!(f, "Message()"), - Expect::Presence(_) => write!(f, "Presence()"), + Expect::Iq(_, desc) => write!(f, "Iq(, {})", desc), + Expect::Message(_, desc) => write!(f, "Message(, {})", desc), + Expect::Presence(_, desc) => write!(f, "Presence(, {})", desc), } } } @@ -165,9 +169,11 @@ pub struct TestComponent { impl TestComponent { pub fn new(in_buffer: Vec) -> Self { TestComponent { - in_buffer: VecDeque::from(in_buffer.into_iter() - .map(|el| TestElement(el)) - .collect::>() + in_buffer: VecDeque::from( + in_buffer + .into_iter() + .map(|el| TestElement(el)) + .collect::>(), ), out_buffer: VecDeque::new(), expect_buffer: VecDeque::new(), @@ -179,18 +185,31 @@ impl TestComponent { self.expect_buffer.push_back(Expect::Element(el.into())) } - pub fn expect_iq(&mut self, callback: F) { - self.expect_buffer.push_back(Expect::Iq(Box::new(callback))) + pub fn expect_iq>( + &mut self, + callback: F, + desc: S, + ) { + self.expect_buffer + .push_back(Expect::Iq(Box::new(callback), desc.into())) } - pub fn expect_message(&mut self, callback: F) { + pub fn expect_message>( + &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(&mut self, callback: F) { + pub fn expect_presence>( + &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 @@ -203,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), }, } } diff --git a/src/tests.rs b/src/tests.rs index 7dc12bd..f6a8d8e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -134,7 +134,7 @@ async fn test_join_presence_empty_room() { String::from(&expected), String::from(&Into::::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 = 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 = 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::::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 = 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 = 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 = 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)