From cb5651c528f24b2ea33367f5a1adf5d38c5c1428 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 25 May 2024 11:35:19 +0200 Subject: [PATCH] Test serialization --- crates/proto-xmpp/src/lib.rs | 2 ++ crates/proto-xmpp/src/muc/mod.rs | 11 ++++------- crates/proto-xmpp/src/testkit.rs | 12 ++++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 crates/proto-xmpp/src/testkit.rs diff --git a/crates/proto-xmpp/src/lib.rs b/crates/proto-xmpp/src/lib.rs index 71e8a94..5519c45 100644 --- a/crates/proto-xmpp/src/lib.rs +++ b/crates/proto-xmpp/src/lib.rs @@ -15,6 +15,8 @@ pub mod streamerror; pub mod tls; pub mod xml; +pub mod testkit; + // Implemented as a macro instead of a fn due to borrowck limitations macro_rules! skip_text { ($reader: ident, $buf: ident) => { diff --git a/crates/proto-xmpp/src/muc/mod.rs b/crates/proto-xmpp/src/muc/mod.rs index 4df427c..a2c77e4 100644 --- a/crates/proto-xmpp/src/muc/mod.rs +++ b/crates/proto-xmpp/src/muc/mod.rs @@ -330,6 +330,7 @@ impl ToXml for XmppHistoryMessage { mod test { use super::*; use crate::bind::{Name, Resource, Server}; + use crate::testkit::assemble_string_from_event_flow; #[test] fn test_history_success_empty() { @@ -439,17 +440,13 @@ mod test { body: "Hello World.".to_string(), }; let mut events = vec![]; - let expected = r#""#; + let expected = r#"Hello World."#; // Act history_message.serialize(&mut events); + let flow = assemble_string_from_event_flow(&events); // Assert - assert_eq!( - // events.pop().unwrap().to_vec(), - // expected.as_bytes() - String::from_utf8(events.pop().unwrap().to_vec()).unwrap(), - expected - ); + assert_eq!(flow, expected); } } diff --git a/crates/proto-xmpp/src/testkit.rs b/crates/proto-xmpp/src/testkit.rs new file mode 100644 index 0000000..a40381f --- /dev/null +++ b/crates/proto-xmpp/src/testkit.rs @@ -0,0 +1,12 @@ +use quick_xml::events::Event; +use quick_xml::Writer; +use std::io::Cursor; + +pub fn assemble_string_from_event_flow(events: &Vec>) -> String { + let mut writer = Writer::new(Cursor::new(Vec::new())); + for event in events { + writer.write_event(event).unwrap(); + } + let result = writer.into_inner().into_inner(); + String::from_utf8(result).unwrap() +}