From 00ac6d08f9c94f5ce4695943834af756d0da76f9 Mon Sep 17 00:00:00 2001 From: Devin R Date: Mon, 6 Apr 2020 07:12:02 -0400 Subject: [PATCH] bring over test builder --- src/lib.rs | 3 ++ src/models/room.rs | 5 +++ src/test_builder.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/test_builder.rs diff --git a/src/lib.rs b/src/lib.rs index c8ab3964..89c3d2a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,9 @@ mod event_emitter; mod models; mod session; +// TODO remove +mod test_builder; + #[cfg(feature = "encryption")] mod crypto; diff --git a/src/models/room.rs b/src/models/room.rs index 53034cc9..9de464ba 100644 --- a/src/models/room.rs +++ b/src/models/room.rs @@ -398,6 +398,11 @@ mod test { use std::str::FromStr; use std::time::Duration; + #[tokio::test] + async fn add_member() { + + } + #[tokio::test] async fn user_presence() { let homeserver = Url::from_str(&mockito::server_url()).unwrap(); diff --git a/src/test_builder.rs b/src/test_builder.rs new file mode 100644 index 00000000..994c2fa1 --- /dev/null +++ b/src/test_builder.rs @@ -0,0 +1,96 @@ + + + +#[cfg(test)] +mod test_it { + #![allow(unused)] + + use std::fs; + use std::path::Path; + + use crate::identifiers::UserId; + use crate::events::collections::all::RoomEvent; + use crate::{AsyncClient, Session, SyncSettings}; + + use serde_json as json; + use mockito::{mock, Matcher}; + use serde::{Serialize, Deserialize}; + use url::Url; + + use std::convert::TryFrom; + use std::str::FromStr; + use std::time::Duration; + use std::panic; + + + pub struct ResponseBuilder { + events: Vec + } + + pub struct TestRunner { + client: Option, + /// A `Vec` of callbacks that should assert something about the client. + /// + /// The callback should panic if the state is unexpected (use `assert_*!` macro) + assertions: Vec, + /// `mokito::Mock` + mock: mockito::Mock, + } + + impl ResponseBuilder { + + /// Creates an `IncomingResponse` to hold events for a sync. + pub fn create_sync_response(&mut self) -> &mut Self { + + self + } + + /// Just throw events at the client, not part of a specific response. + pub fn create_event_stream(&mut self) -> &mut Self { + + self + } + + /// Add an event either to the event stream or the appropriate `IncomingResponse` field. + pub fn add_event_from_file>(&mut self, path: P) -> &mut Self { + let json = fs::read_to_string(path.as_ref()).expect(&format!("file not found {:?}", path.as_ref())); + let event = serde_json::from_str::(&json).expect("deserialization failed"); + self.events.push(event); + self + } + + fn add_event(&mut self, content: T, event: fn(T) -> RoomEvent) { + self.events.push(event(content)) + } + + /// Consumes `ResponseBuilder and returns a `TestRunner`. + /// + /// The `TestRunner` streams the events to the client and enables methods to set assertions + /// about the state of the client. + pub fn build(self) -> TestRunner { + let mock = mock("POST", "/_matrix/client/r0/login") + .with_status(200) + .with_body_from_file("tests/data/login_response.json") + .create(); + + TestRunner { + client: None, + assertions: Vec::new(), + mock, + } + } + } + + impl TestRunner { + pub fn set_client(&mut self, client: AsyncClient) -> &mut Self { + self.client = Some(client); + self + } + pub fn run_test(self) -> Result<(), Vec> { + + + Ok(()) + } + } + +}