2020-04-07 00:59:44 +00:00
|
|
|
#![cfg(test)]
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::panic;
|
2020-04-07 12:55:42 +00:00
|
|
|
use std::path::Path;
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
use crate::events::{
|
2020-04-07 12:55:42 +00:00
|
|
|
collections::{
|
|
|
|
all::{RoomEvent, StateEvent},
|
|
|
|
only::Event,
|
|
|
|
},
|
2020-04-07 00:59:44 +00:00
|
|
|
presence::PresenceEvent,
|
|
|
|
EventResult, TryFromRaw,
|
|
|
|
};
|
2020-04-07 12:55:42 +00:00
|
|
|
use crate::identifiers::{RoomId, UserId};
|
|
|
|
use crate::AsyncClient;
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
use ansi_term::Colour;
|
2020-04-07 12:55:42 +00:00
|
|
|
use mockito::{self, mock, Mock};
|
2020-04-07 00:59:44 +00:00
|
|
|
|
|
|
|
use crate::models::Room;
|
|
|
|
|
|
|
|
/// `assert` to use in `TestRunner`.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// This returns an `Err` on failure, instead of panicking.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_ {
|
|
|
|
($truth:expr) => {
|
|
|
|
if !$truth {
|
2020-04-07 12:55:42 +00:00
|
|
|
return Err(format!(
|
|
|
|
r#"assertion failed: `(left == right)`
|
2020-04-07 00:59:44 +00:00
|
|
|
expression: `{:?}`
|
2020-04-07 12:55:42 +00:00
|
|
|
failed at {}"#,
|
2020-04-07 00:59:44 +00:00
|
|
|
stringify!($truth),
|
|
|
|
file!(),
|
2020-04-07 12:55:42 +00:00
|
|
|
));
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
2020-04-06 13:11:38 +00:00
|
|
|
};
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `assert_eq` to use in `TestRunner.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// This returns an `Err` on failure, instead of panicking.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_eq_ {
|
|
|
|
($left:expr, $right:expr) => ({
|
|
|
|
match (&$left, &$right) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if !(*left_val == *right_val) {
|
|
|
|
return Err(format!(r#"assertion failed: `(left == right)`
|
|
|
|
left: `{:?}`,
|
|
|
|
right: `{:?}`
|
|
|
|
failed at {}:{}"#,
|
|
|
|
&*left_val,
|
|
|
|
&*right_val,
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
($left:expr, $right:expr,) => ({
|
|
|
|
$crate::assert_eq!($left, $right)
|
|
|
|
});
|
|
|
|
($left:expr, $right:expr, $($arg:tt)+) => ({
|
|
|
|
match (&($left), &($right)) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if !(*left_val == *right_val) {
|
|
|
|
return Err(format!(r#"assertion failed: `(left == right)`
|
|
|
|
left: `{:?}`,
|
|
|
|
right: `{:?}` : {}
|
|
|
|
failed at {}:{}"#,
|
|
|
|
&*left_val,
|
|
|
|
&*right_val,
|
|
|
|
$crate::format_args!($($arg)+),
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `assert_ne` to use in `TestRunner.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// This returns an `Err` on failure, instead of panicking.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_ne_ {
|
|
|
|
($left:expr, $right:expr) => ({
|
|
|
|
match (&$left, &$right) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if (*left_val == *right_val) {
|
|
|
|
return Err(format!(r#"assertion failed: `(left == right)`
|
|
|
|
left: `{:?}`,
|
|
|
|
right: `{:?}`
|
|
|
|
failed at {}:{}"#,
|
|
|
|
&*left_val,
|
|
|
|
&*right_val,
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
($left:expr, $right:expr,) => ({
|
|
|
|
$crate::assert_eq!($left, $right)
|
|
|
|
});
|
|
|
|
($left:expr, $right:expr, $($arg:tt)+) => ({
|
|
|
|
match (&($left), &($right)) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if (*left_val == *right_val) {
|
|
|
|
return Err(format!(r#"assertion failed: `(left == right)`
|
|
|
|
left: `{:?}`,
|
|
|
|
right: `{:?}` : {}
|
|
|
|
failed at {}:{}"#,
|
|
|
|
&*left_val,
|
|
|
|
&*right_val,
|
|
|
|
$crate::format_args!($($arg)+),
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Convenience macro for declaring an `async` assert function to store in the `TestRunner`.
|
|
|
|
///
|
|
|
|
/// Declares an async function that can be stored in a struct.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// # Examples
|
|
|
|
/// ```rust
|
|
|
|
/// # use matrix_sdk::AsyncClient;
|
2020-04-07 12:55:42 +00:00
|
|
|
/// # use url::Url;
|
|
|
|
/// async_assert!{
|
2020-04-07 00:59:44 +00:00
|
|
|
/// async fn foo(cli: &AsyncClient) -> Result<(), String> {
|
2020-04-07 12:55:42 +00:00
|
|
|
/// assert_eq_!(cli.homeserver(), &url::Url::parse("matrix.org").unwrap());
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! async_assert {
|
|
|
|
(
|
|
|
|
$( #[$attr:meta] )*
|
|
|
|
$pub:vis async fn $fname:ident<$lt:lifetime> ( $($args:tt)* ) $(-> $Ret:ty)? {
|
|
|
|
$($body:tt)*
|
|
|
|
}
|
|
|
|
) => (
|
|
|
|
$( #[$attr] )*
|
|
|
|
#[allow(unused_parens)]
|
|
|
|
$pub fn $fname<$lt> ( $($args)* )
|
|
|
|
-> ::std::pin::Pin<::std::boxed::Box<dyn ::std::future::Future<Output = ($($Ret)?)>
|
|
|
|
+ ::std::marker::Send + $lt>>
|
|
|
|
{
|
|
|
|
::std::boxed::Box::pin(async move { $($body)* })
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(
|
|
|
|
$( #[$attr:meta] )*
|
|
|
|
$pub:vis async fn $fname:ident ( $($args:tt)* ) $(-> $Ret:ty)? {
|
|
|
|
$($body:tt)*
|
|
|
|
}
|
|
|
|
) => (
|
|
|
|
$( #[$attr] )*
|
|
|
|
#[allow(unused_parens)]
|
|
|
|
$pub fn $fname ( $($args)* )
|
|
|
|
-> ::std::pin::Pin<::std::boxed::Box<(dyn ::std::future::Future<Output = ($($Ret)?)>
|
|
|
|
+ ::std::marker::Send)>>
|
|
|
|
{
|
|
|
|
::std::boxed::Box::pin(async move { $($body)* })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DynFuture<'lt, T> = ::std::pin::Pin<Box<dyn 'lt + Send + ::std::future::Future<Output = T>>>;
|
|
|
|
pub type AsyncAssert = fn(&AsyncClient) -> DynFuture<Result<(), String>>;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct EventBuilder {
|
|
|
|
/// The events that determine the state of a `Room`.
|
|
|
|
///
|
|
|
|
/// When testing the models `RoomEvent`s are needed.
|
|
|
|
room_events: Vec<RoomEvent>,
|
|
|
|
/// The presence events that determine the presence state of a `RoomMember`.
|
|
|
|
presence_events: Vec<PresenceEvent>,
|
|
|
|
/// The state events that determine the state of a `Room`.
|
|
|
|
state_events: Vec<StateEvent>,
|
2020-04-07 12:55:42 +00:00
|
|
|
/// The ephemeral room events that determine the state of a `Room`.
|
|
|
|
ephemeral: Vec<Event>,
|
|
|
|
/// The account data events that determine the state of a `Room`.
|
|
|
|
account_data: Vec<Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RoomTestRunner {
|
|
|
|
/// Used To test the models
|
|
|
|
room: Option<Room>,
|
|
|
|
/// The ephemeral room events that determine the state of a `Room`.
|
|
|
|
ephemeral: Vec<Event>,
|
|
|
|
/// The account data events that determine the state of a `Room`.
|
|
|
|
account_data: Vec<Event>,
|
|
|
|
/// The events that determine the state of a `Room`.
|
|
|
|
///
|
|
|
|
/// When testing the models `RoomEvent`s are needed.
|
|
|
|
room_events: Vec<RoomEvent>,
|
|
|
|
/// The presence events that determine the presence state of a `RoomMember`.
|
|
|
|
presence_events: Vec<PresenceEvent>,
|
2020-04-07 00:59:44 +00:00
|
|
|
/// The state events that determine the state of a `Room`.
|
2020-04-07 12:55:42 +00:00
|
|
|
state_events: Vec<StateEvent>,
|
|
|
|
/// A `Vec` of callbacks that should assert something about the room.
|
|
|
|
///
|
|
|
|
/// The callback should use the provided `assert_`, `assert_*_` macros.
|
|
|
|
room_assertions: Vec<fn(&Room) -> Result<(), String>>,
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
pub struct ClientTestRunner {
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Used when testing the whole client
|
|
|
|
client: Option<AsyncClient>,
|
2020-04-07 12:55:42 +00:00
|
|
|
/// RoomId and UserId to use for the events.
|
2020-04-07 00:59:44 +00:00
|
|
|
///
|
2020-04-07 12:55:42 +00:00
|
|
|
/// The RoomId must match the RoomId of the events to track.
|
|
|
|
room_user_id: (RoomId, UserId),
|
|
|
|
/// The ephemeral room events that determine the state of a `Room`.
|
|
|
|
ephemeral: Vec<Event>,
|
|
|
|
/// The account data events that determine the state of a `Room`.
|
|
|
|
account_data: Vec<Event>,
|
2020-04-07 00:59:44 +00:00
|
|
|
/// The events that determine the state of a `Room`.
|
|
|
|
///
|
|
|
|
/// When testing the models `RoomEvent`s are needed.
|
|
|
|
room_events: Vec<RoomEvent>,
|
|
|
|
/// The presence events that determine the presence state of a `RoomMember`.
|
|
|
|
presence_events: Vec<PresenceEvent>,
|
|
|
|
/// The state events that determine the state of a `Room`.
|
|
|
|
state_events: Vec<StateEvent>,
|
|
|
|
/// A `Vec` of callbacks that should assert something about the client.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// The callback should use the provided `assert_`, `assert_*_` macros.
|
|
|
|
client_assertions: Vec<AsyncAssert>,
|
2020-04-07 12:55:42 +00:00
|
|
|
}
|
|
|
|
// the compiler complains that the event Vec's are never used they are.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct MockTestRunner {
|
|
|
|
/// Used when testing the whole client
|
|
|
|
client: Option<AsyncClient>,
|
|
|
|
/// The ephemeral room events that determine the state of a `Room`.
|
|
|
|
ephemeral: Vec<Event>,
|
|
|
|
/// The account data events that determine the state of a `Room`.
|
|
|
|
account_data: Vec<Event>,
|
|
|
|
/// The events that determine the state of a `Room`.
|
|
|
|
///
|
|
|
|
/// When testing the models `RoomEvent`s are needed.
|
|
|
|
room_events: Vec<RoomEvent>,
|
|
|
|
/// The presence events that determine the presence state of a `RoomMember`.
|
|
|
|
presence_events: Vec<PresenceEvent>,
|
|
|
|
/// The state events that determine the state of a `Room`.
|
|
|
|
state_events: Vec<StateEvent>,
|
|
|
|
/// A `Vec` of callbacks that should assert something about the client.
|
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// The callback should use the provided `assert_`, `assert_*_` macros.
|
2020-04-07 12:55:42 +00:00
|
|
|
client_assertions: Vec<AsyncAssert>,
|
2020-04-07 00:59:44 +00:00
|
|
|
/// `mokito::Mock`
|
|
|
|
mock: Option<mockito::Mock>,
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
impl EventBuilder {
|
|
|
|
/// Creates an `IncomingResponse` to hold events for a sync.
|
|
|
|
pub fn create_sync_response(mut self) -> Self {
|
|
|
|
self
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Just throw events at the client, not part of a specific response.
|
|
|
|
pub fn create_event_stream(mut self) -> Self {
|
2020-04-07 12:55:42 +00:00
|
|
|
self
|
|
|
|
}
|
2020-04-07 00:59:44 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
/// Add an event to the room events `Vec`.
|
|
|
|
pub fn add_ephemeral_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> Event,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
|
|
|
.expect(&format!("file not found {:?}", path.as_ref()));
|
|
|
|
let event = serde_json::from_str::<EventResult<Ev>>(&val)
|
|
|
|
.unwrap()
|
|
|
|
.into_result()
|
|
|
|
.unwrap();
|
|
|
|
self.ephemeral.push(variant(event));
|
2020-04-07 00:59:44 +00:00
|
|
|
self
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add an event to the room events `Vec`.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn add_account_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> Event,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
|
|
|
.expect(&format!("file not found {:?}", path.as_ref()));
|
|
|
|
let event = serde_json::from_str::<EventResult<Ev>>(&val)
|
|
|
|
.unwrap()
|
|
|
|
.into_result()
|
|
|
|
.unwrap();
|
|
|
|
self.account_data.push(variant(event));
|
2020-04-07 00:59:44 +00:00
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add an event to the room events `Vec`.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn add_room_event_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> RoomEvent,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
|
|
|
.expect(&format!("file not found {:?}", path.as_ref()));
|
|
|
|
let event = serde_json::from_str::<EventResult<Ev>>(&val)
|
|
|
|
.unwrap()
|
|
|
|
.into_result()
|
|
|
|
.unwrap();
|
2020-04-07 00:59:44 +00:00
|
|
|
self.room_events.push(variant(event));
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add a state event to the state events `Vec`.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn add_state_event_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
|
|
|
mut self,
|
|
|
|
path: P,
|
|
|
|
variant: fn(Ev) -> StateEvent,
|
|
|
|
) -> Self {
|
|
|
|
let val = fs::read_to_string(path.as_ref())
|
|
|
|
.expect(&format!("file not found {:?}", path.as_ref()));
|
|
|
|
let event = serde_json::from_str::<EventResult<Ev>>(&val)
|
|
|
|
.unwrap()
|
|
|
|
.into_result()
|
|
|
|
.unwrap();
|
2020-04-07 00:59:44 +00:00
|
|
|
self.state_events.push(variant(event));
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Add a presence event to the presence events `Vec`.
|
|
|
|
pub fn add_presence_event_from_file<P: AsRef<Path>>(mut self, path: P) -> Self {
|
2020-04-07 12:55:42 +00:00
|
|
|
let val = fs::read_to_string(path.as_ref())
|
|
|
|
.expect(&format!("file not found {:?}", path.as_ref()));
|
|
|
|
let event = serde_json::from_str::<EventResult<PresenceEvent>>(&val)
|
|
|
|
.unwrap()
|
|
|
|
.into_result()
|
|
|
|
.unwrap();
|
2020-04-07 00:59:44 +00:00
|
|
|
self.presence_events.push(event);
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Consumes `ResponseBuilder and returns a `TestRunner`.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
2020-04-07 00:59:44 +00:00
|
|
|
/// The `TestRunner` streams the events to the client and holds methods to make assertions
|
|
|
|
/// about the state of the client.
|
2020-04-07 12:55:42 +00:00
|
|
|
pub fn build_mock_runner<P: Into<mockito::Matcher>>(
|
|
|
|
mut self,
|
|
|
|
method: &str,
|
|
|
|
path: P,
|
|
|
|
) -> MockTestRunner {
|
|
|
|
let body = serde_json::json! {
|
|
|
|
{
|
|
|
|
"device_one_time_keys_count": {},
|
|
|
|
"next_batch": "s526_47314_0_7_1_1_1_11444_1",
|
|
|
|
"device_lists": {
|
|
|
|
"changed": [],
|
|
|
|
"left": []
|
|
|
|
},
|
|
|
|
"rooms": {
|
|
|
|
"invite": {},
|
|
|
|
"join": {
|
|
|
|
"!SVkFJHzfwvuaIEawgC:localhost": {
|
|
|
|
"summary": {},
|
|
|
|
"account_data": {
|
|
|
|
"events": self.account_data
|
|
|
|
},
|
|
|
|
"ephemeral": {
|
|
|
|
"events": self.ephemeral
|
|
|
|
},
|
|
|
|
"state": {
|
|
|
|
"events": self.state_events
|
|
|
|
},
|
|
|
|
"timeline": {
|
|
|
|
"events": self.room_events,
|
|
|
|
"limited": true,
|
|
|
|
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
|
|
|
|
},
|
|
|
|
"unread_notifications": {
|
|
|
|
"highlight_count": 0,
|
|
|
|
"notification_count": 11
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"leave": {}
|
|
|
|
},
|
|
|
|
"to_device": {
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"presence": {
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mock = Some(
|
|
|
|
mock(method, path)
|
|
|
|
.with_status(200)
|
|
|
|
.with_body(body.to_string())
|
|
|
|
.create(),
|
|
|
|
);
|
|
|
|
MockTestRunner {
|
2020-04-07 00:59:44 +00:00
|
|
|
client: None,
|
2020-04-07 12:55:42 +00:00
|
|
|
ephemeral: Vec::new(),
|
|
|
|
account_data: Vec::new(),
|
2020-04-07 00:59:44 +00:00
|
|
|
room_events: Vec::new(),
|
|
|
|
presence_events: Vec::new(),
|
|
|
|
state_events: Vec::new(),
|
|
|
|
client_assertions: Vec::new(),
|
|
|
|
mock,
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Consumes `ResponseBuilder and returns a `TestRunner`.
|
2020-04-07 12:55:42 +00:00
|
|
|
///
|
|
|
|
/// The `TestRunner` streams the events to the `AsyncClient` and holds methods to make assertions
|
|
|
|
/// about the state of the `AsyncClient`.
|
|
|
|
pub fn build_client_runner(self, room_id: RoomId, user_id: UserId) -> ClientTestRunner {
|
|
|
|
ClientTestRunner {
|
2020-04-07 00:59:44 +00:00
|
|
|
client: None,
|
2020-04-07 12:55:42 +00:00
|
|
|
room_user_id: (room_id, user_id),
|
|
|
|
ephemeral: self.ephemeral,
|
|
|
|
account_data: self.account_data,
|
2020-04-07 00:59:44 +00:00
|
|
|
room_events: self.room_events,
|
|
|
|
presence_events: self.presence_events,
|
|
|
|
state_events: self.state_events,
|
|
|
|
client_assertions: Vec::new(),
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
/// Consumes `ResponseBuilder and returns a `TestRunner`.
|
|
|
|
///
|
|
|
|
/// The `TestRunner` streams the events to the `Room` and holds methods to make assertions
|
|
|
|
/// about the state of the `Room`.
|
|
|
|
pub fn build_room_runner(self, room_id: &RoomId, user_id: &UserId) -> RoomTestRunner {
|
|
|
|
RoomTestRunner {
|
|
|
|
room: Some(Room::new(room_id, user_id)),
|
|
|
|
ephemeral: self.ephemeral,
|
|
|
|
account_data: self.account_data,
|
|
|
|
room_events: self.room_events,
|
|
|
|
presence_events: self.presence_events,
|
|
|
|
state_events: self.state_events,
|
|
|
|
room_assertions: Vec::new(),
|
|
|
|
}
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
2020-04-07 12:55:42 +00:00
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
impl RoomTestRunner {
|
2020-04-07 00:59:44 +00:00
|
|
|
/// Set `Room`
|
|
|
|
pub fn set_room(mut self, room: Room) -> Self {
|
|
|
|
self.room = Some(room);
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 19:29:38 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
pub fn add_room_assert(mut self, assert: fn(&Room) -> Result<(), String>) -> Self {
|
|
|
|
self.room_assertions.push(assert);
|
|
|
|
self
|
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
fn run_room_tests(&mut self) -> Result<(), Vec<String>> {
|
2020-04-07 00:59:44 +00:00
|
|
|
let mut errs = Vec::new();
|
2020-04-07 12:55:42 +00:00
|
|
|
let room = self.room.as_mut().unwrap();
|
2020-04-06 19:29:38 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
for event in &self.account_data {
|
2020-04-07 00:59:44 +00:00
|
|
|
match event {
|
|
|
|
// Event::IgnoredUserList(iu) => room.handle_ignored_users(iu),
|
2020-04-07 12:55:42 +00:00
|
|
|
Event::Presence(p) => room.receive_presence_event(p),
|
2020-04-07 00:59:44 +00:00
|
|
|
// Event::PushRules(pr) => room.handle_push_rules(pr),
|
2020-04-07 12:55:42 +00:00
|
|
|
_ => todo!("implement more account data events"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for event in &self.ephemeral {
|
|
|
|
match event {
|
|
|
|
// Event::IgnoredUserList(iu) => room.handle_ignored_users(iu),
|
|
|
|
Event::Presence(p) => room.receive_presence_event(p),
|
|
|
|
// Event::PushRules(pr) => room.handle_push_rules(pr),
|
|
|
|
_ => todo!("implement more account data events"),
|
2020-04-07 00:59:44 +00:00
|
|
|
};
|
2020-04-06 19:29:38 +00:00
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
for event in &self.room_events {
|
2020-04-07 12:55:42 +00:00
|
|
|
room.receive_timeline_event(event);
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
for event in &self.presence_events {
|
2020-04-07 12:55:42 +00:00
|
|
|
room.receive_presence_event(event);
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
for event in &self.state_events {
|
2020-04-07 12:55:42 +00:00
|
|
|
room.receive_state_event(event);
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|
2020-04-06 19:29:38 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
for assert in &mut self.room_assertions {
|
|
|
|
if let Err(e) = assert(&room) {
|
2020-04-07 00:59:44 +00:00
|
|
|
errs.push(e);
|
2020-04-06 19:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 00:59:44 +00:00
|
|
|
if errs.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(errs)
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 19:29:38 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
pub async fn run_test(mut self) {
|
|
|
|
let (count, errs) = if let Some(_) = &self.room {
|
|
|
|
(self.room_assertions.len(), self.run_room_tests())
|
|
|
|
} else {
|
|
|
|
panic!("must have either AsyncClient or Room")
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(errs) = errs {
|
|
|
|
let err_str = errs.join(&format!("\n\n"));
|
|
|
|
eprintln!("{}\n{}", Colour::Red.paint("Error: "), err_str);
|
|
|
|
if !errs.is_empty() {
|
|
|
|
panic!("{} tests failed", errs.len());
|
|
|
|
} else {
|
|
|
|
println!("{}. {} passed", Colour::Green.paint("Ok"), count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientTestRunner {
|
|
|
|
pub fn set_client(mut self, client: AsyncClient) -> Self {
|
|
|
|
self.client = Some(client);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_client_assert(mut self, assert: AsyncAssert) -> Self {
|
|
|
|
self.client_assertions.push(assert);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_client_tests(&mut self) -> Result<(), Vec<String>> {
|
2020-04-07 00:59:44 +00:00
|
|
|
let mut errs = Vec::new();
|
2020-04-07 12:55:42 +00:00
|
|
|
let mut cli = self.client.as_ref().unwrap().base_client.write().await;
|
|
|
|
let room_id = &self.room_user_id.0;
|
2020-04-07 00:59:44 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
for event in &self.account_data {
|
2020-04-07 00:59:44 +00:00
|
|
|
match event {
|
|
|
|
// Event::IgnoredUserList(iu) => room.handle_ignored_users(iu),
|
2020-04-07 12:55:42 +00:00
|
|
|
Event::Presence(p) => cli.receive_presence_event(room_id, p).await,
|
2020-04-07 00:59:44 +00:00
|
|
|
// Event::PushRules(pr) => room.handle_push_rules(pr),
|
2020-04-07 12:55:42 +00:00
|
|
|
_ => todo!("implement more account data events"),
|
2020-04-06 19:29:38 +00:00
|
|
|
};
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
2020-04-06 19:29:38 +00:00
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
for event in &self.ephemeral {
|
|
|
|
cli.receive_ephemeral_event(room_id, event).await;
|
|
|
|
}
|
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
for event in &self.room_events {
|
2020-04-07 12:55:42 +00:00
|
|
|
cli.receive_joined_timeline_event(room_id, &mut EventResult::Ok(event.clone()))
|
|
|
|
.await;
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
for event in &self.presence_events {
|
2020-04-07 12:55:42 +00:00
|
|
|
cli.receive_presence_event(room_id, event).await;
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
for event in &self.state_events {
|
2020-04-07 12:55:42 +00:00
|
|
|
cli.receive_joined_state_event(room_id, event).await;
|
2020-04-06 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
for assert in &mut self.client_assertions {
|
|
|
|
if let Err(e) = assert(self.client.as_ref().unwrap()).await {
|
2020-04-07 00:59:44 +00:00
|
|
|
errs.push(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errs.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(errs)
|
|
|
|
}
|
2020-04-06 19:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
pub async fn run_test(mut self) {
|
2020-04-07 12:55:42 +00:00
|
|
|
let (count, errs) = if let Some(_) = &self.client {
|
2020-04-07 00:59:44 +00:00
|
|
|
(self.client_assertions.len(), self.run_client_tests().await)
|
|
|
|
} else {
|
|
|
|
panic!("must have either AsyncClient or Room")
|
|
|
|
};
|
2020-04-06 11:12:02 +00:00
|
|
|
|
2020-04-07 00:59:44 +00:00
|
|
|
if let Err(errs) = errs {
|
|
|
|
let err_str = errs.join(&format!("\n\n"));
|
2020-04-07 12:55:42 +00:00
|
|
|
eprintln!("{}\n{}", Colour::Red.paint("Error: "), err_str);
|
|
|
|
if !errs.is_empty() {
|
|
|
|
panic!("{} tests failed", errs.len());
|
|
|
|
} else {
|
|
|
|
println!("{}. {} passed", Colour::Green.paint("Ok"), count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MockTestRunner {
|
|
|
|
pub fn set_client(mut self, client: AsyncClient) -> Self {
|
|
|
|
self.client = Some(client);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_mock(mut self, mock: Mock) -> Self {
|
|
|
|
self.mock = Some(mock);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_client_assert(mut self, assert: AsyncAssert) -> Self {
|
|
|
|
self.client_assertions.push(assert);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_mock_tests(&mut self) -> Result<(), Vec<String>> {
|
|
|
|
let mut errs = Vec::new();
|
|
|
|
self.client
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.sync(crate::SyncSettings::default())
|
|
|
|
.await
|
|
|
|
.map(|_r| ())
|
|
|
|
.map_err(|e| vec![e.to_string()])?;
|
|
|
|
|
|
|
|
for assert in &mut self.client_assertions {
|
|
|
|
if let Err(e) = assert(self.client.as_ref().unwrap()).await {
|
|
|
|
errs.push(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errs.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_test(mut self) {
|
|
|
|
let (count, errs) = if let Some(_) = &self.mock {
|
|
|
|
(self.client_assertions.len(), self.run_mock_tests().await)
|
|
|
|
} else {
|
|
|
|
panic!("must have either AsyncClient or Room")
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(errs) = errs {
|
|
|
|
let err_str = errs.join(&format!("\n\n"));
|
|
|
|
eprintln!("{}\n{}", Colour::Red.paint("Error: "), err_str);
|
2020-04-07 00:59:44 +00:00
|
|
|
if !errs.is_empty() {
|
|
|
|
panic!("{} tests failed", errs.len());
|
|
|
|
} else {
|
2020-04-07 12:55:42 +00:00
|
|
|
println!("{}. {} passed", Colour::Green.paint("Ok"), count);
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-06 13:11:38 +00:00
|
|
|
}
|
2020-04-06 11:12:02 +00:00
|
|
|
}
|