remove internal assertions in favor of returning clients and rooms
parent
4dbc0aaad6
commit
2137d198fc
|
@ -64,5 +64,4 @@ serde_json = { version = "1.0.49" }
|
||||||
tracing-subscriber = "0.2.3"
|
tracing-subscriber = "0.2.3"
|
||||||
tempfile = "3.1.0"
|
tempfile = "3.1.0"
|
||||||
mockito = "0.23.3"
|
mockito = "0.23.3"
|
||||||
serde = "1.0.105"
|
# serde = "1.0.105"
|
||||||
ansi_term = "0.12.1"
|
|
||||||
|
|
|
@ -806,21 +806,11 @@ mod test {
|
||||||
use crate::identifiers::{RoomId, UserId};
|
use crate::identifiers::{RoomId, UserId};
|
||||||
|
|
||||||
use crate::test_builder::EventBuilder;
|
use crate::test_builder::EventBuilder;
|
||||||
use crate::{assert_eq_, async_assert};
|
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn client_runner() {
|
async fn client_runner() {
|
||||||
// TODO make this actually test something
|
|
||||||
|
|
||||||
async_assert! {
|
|
||||||
async fn test_client_homeserver<'a>(cli: &'a AsyncClient) -> Result<(), String> {
|
|
||||||
assert_eq_!(cli.homeserver(), &Url::parse(&mockito::server_url()).unwrap());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let session = crate::Session {
|
let session = crate::Session {
|
||||||
access_token: "12345".to_owned(),
|
access_token: "12345".to_owned(),
|
||||||
user_id: UserId::try_from("@example:localhost").unwrap(),
|
user_id: UserId::try_from("@example:localhost").unwrap(),
|
||||||
|
@ -832,31 +822,26 @@ mod test {
|
||||||
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
||||||
let uid = UserId::try_from("@example:localhost").unwrap();
|
let uid = UserId::try_from("@example:localhost").unwrap();
|
||||||
|
|
||||||
let bld = EventBuilder::default();
|
let mut bld = EventBuilder::default()
|
||||||
let runner = bld
|
|
||||||
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
||||||
.add_room_event_from_file(
|
.add_room_event_from_file(
|
||||||
"./tests/data/events/power_levels.json",
|
"./tests/data/events/power_levels.json",
|
||||||
RoomEvent::RoomPowerLevels,
|
RoomEvent::RoomPowerLevels,
|
||||||
)
|
)
|
||||||
.build_client_runner(rid, uid)
|
.build_client_runner(rid, uid);
|
||||||
.set_client(client)
|
|
||||||
.add_client_assert(test_client_homeserver);
|
|
||||||
|
|
||||||
runner.run_test().await;
|
let cli = bld.set_client(client).to_client().await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cli.homeserver(),
|
||||||
|
&Url::parse(&mockito::server_url()).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn mock_runner() {
|
async fn mock_runner() {
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
async_assert! {
|
|
||||||
async fn test_mock_homeserver<'a>(cli: &'a AsyncClient) -> Result<(), String> {
|
|
||||||
assert_eq_!(cli.homeserver(), &url::Url::parse(&mockito::server_url()).unwrap());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let session = crate::Session {
|
let session = crate::Session {
|
||||||
access_token: "12345".to_owned(),
|
access_token: "12345".to_owned(),
|
||||||
user_id: UserId::try_from("@example:localhost").unwrap(),
|
user_id: UserId::try_from("@example:localhost").unwrap(),
|
||||||
|
@ -866,8 +851,7 @@ mod test {
|
||||||
let homeserver = url::Url::parse(&mockito::server_url()).unwrap();
|
let homeserver = url::Url::parse(&mockito::server_url()).unwrap();
|
||||||
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
|
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
|
||||||
|
|
||||||
let bld = EventBuilder::default();
|
let mut bld = EventBuilder::default()
|
||||||
let runner = bld
|
|
||||||
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
||||||
.add_room_event_from_file(
|
.add_room_event_from_file(
|
||||||
"./tests/data/events/power_levels.json",
|
"./tests/data/events/power_levels.json",
|
||||||
|
@ -876,10 +860,13 @@ mod test {
|
||||||
.build_mock_runner(
|
.build_mock_runner(
|
||||||
"GET",
|
"GET",
|
||||||
mockito::Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
|
mockito::Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
|
||||||
)
|
);
|
||||||
.set_client(client)
|
|
||||||
.add_client_assert(test_mock_homeserver);
|
|
||||||
|
|
||||||
runner.run_test().await;
|
let cli = bld.set_client(client).to_client().await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cli.homeserver(),
|
||||||
|
&Url::parse(&mockito::server_url()).unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -389,7 +389,6 @@ mod test {
|
||||||
use crate::events::room::member::MembershipState;
|
use crate::events::room::member::MembershipState;
|
||||||
use crate::identifiers::UserId;
|
use crate::identifiers::UserId;
|
||||||
use crate::test_builder::EventBuilder;
|
use crate::test_builder::EventBuilder;
|
||||||
use crate::{assert_, assert_eq_};
|
|
||||||
use crate::{AsyncClient, Session, SyncSettings};
|
use crate::{AsyncClient, Session, SyncSettings};
|
||||||
|
|
||||||
use mockito::{mock, Matcher};
|
use mockito::{mock, Matcher};
|
||||||
|
@ -439,40 +438,31 @@ mod test {
|
||||||
assert!(room.deref().power_levels.is_some())
|
assert!(room.deref().power_levels.is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[test]
|
||||||
async fn room_events() {
|
fn room_events() {
|
||||||
fn test_room_users(room: &Room) -> Result<(), String> {
|
|
||||||
assert_eq_!(room.members.len(), 1);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_room_power(room: &Room) -> Result<(), String> {
|
|
||||||
assert_!(room.power_levels.is_some());
|
|
||||||
assert_eq_!(
|
|
||||||
room.power_levels.as_ref().unwrap().kick,
|
|
||||||
js_int::Int::new(50).unwrap()
|
|
||||||
);
|
|
||||||
let admin = room
|
|
||||||
.members
|
|
||||||
.get(&UserId::try_from("@example:localhost").unwrap())
|
|
||||||
.unwrap();
|
|
||||||
assert_eq_!(admin.power_level.unwrap(), js_int::Int::new(100).unwrap());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
||||||
let uid = UserId::try_from("@example:localhost").unwrap();
|
let uid = UserId::try_from("@example:localhost").unwrap();
|
||||||
let bld = EventBuilder::default();
|
|
||||||
let runner = bld
|
let mut bld = EventBuilder::default()
|
||||||
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
||||||
.add_room_event_from_file(
|
.add_room_event_from_file(
|
||||||
"./tests/data/events/power_levels.json",
|
"./tests/data/events/power_levels.json",
|
||||||
RoomEvent::RoomPowerLevels,
|
RoomEvent::RoomPowerLevels,
|
||||||
)
|
)
|
||||||
.build_room_runner(&rid, &uid)
|
.build_room_runner(&rid, &uid);
|
||||||
.add_room_assert(test_room_power)
|
|
||||||
.add_room_assert(test_room_users);
|
|
||||||
|
|
||||||
runner.run_test().await;
|
let room = bld.to_room();
|
||||||
|
|
||||||
|
assert_eq!(room.members.len(), 1);
|
||||||
|
assert!(room.power_levels.is_some());
|
||||||
|
assert_eq!(
|
||||||
|
room.power_levels.as_ref().unwrap().kick,
|
||||||
|
js_int::Int::new(50).unwrap()
|
||||||
|
);
|
||||||
|
let admin = room
|
||||||
|
.members
|
||||||
|
.get(&UserId::try_from("@example:localhost").unwrap())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(admin.power_level.unwrap(), js_int::Int::new(100).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,78 +189,57 @@ mod test {
|
||||||
use crate::events::room::member::MembershipState;
|
use crate::events::room::member::MembershipState;
|
||||||
use crate::identifiers::{RoomId, UserId};
|
use crate::identifiers::{RoomId, UserId};
|
||||||
use crate::test_builder::EventBuilder;
|
use crate::test_builder::EventBuilder;
|
||||||
use crate::{assert_, assert_eq_, Room};
|
|
||||||
|
|
||||||
use js_int::{Int, UInt};
|
use js_int::{Int, UInt};
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
#[tokio::test]
|
#[test]
|
||||||
async fn room_member_events() {
|
fn room_member_events() {
|
||||||
fn test_room_member(room: &Room) -> Result<(), String> {
|
|
||||||
let member = room
|
|
||||||
.members
|
|
||||||
.get(&UserId::try_from("@example:localhost").unwrap())
|
|
||||||
.unwrap();
|
|
||||||
assert_eq_!(member.membership, MembershipState::Join);
|
|
||||||
assert_eq_!(member.power_level, Int::new(100));
|
|
||||||
println!("{:#?}", room);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
||||||
let uid = UserId::try_from("@example:localhost").unwrap();
|
let uid = UserId::try_from("@example:localhost").unwrap();
|
||||||
let bld = EventBuilder::default();
|
let mut bld = EventBuilder::default()
|
||||||
let runner = bld
|
|
||||||
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
||||||
.add_room_event_from_file(
|
.add_room_event_from_file(
|
||||||
"./tests/data/events/power_levels.json",
|
"./tests/data/events/power_levels.json",
|
||||||
RoomEvent::RoomPowerLevels,
|
RoomEvent::RoomPowerLevels,
|
||||||
)
|
)
|
||||||
.build_room_runner(&rid, &uid)
|
.build_room_runner(&rid, &uid);
|
||||||
.add_room_assert(test_room_member);
|
let room = bld.to_room();
|
||||||
|
|
||||||
runner.run_test().await;
|
let member = room
|
||||||
|
.members
|
||||||
|
.get(&UserId::try_from("@example:localhost").unwrap())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(member.membership, MembershipState::Join);
|
||||||
|
assert_eq!(member.power_level, Int::new(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[test]
|
||||||
async fn member_presence_events() {
|
fn member_presence_events() {
|
||||||
fn test_room_member(room: &Room) -> Result<(), String> {
|
|
||||||
let member = room
|
|
||||||
.members
|
|
||||||
.get(&UserId::try_from("@example:localhost").unwrap())
|
|
||||||
.unwrap();
|
|
||||||
assert_eq_!(member.membership, MembershipState::Join);
|
|
||||||
assert_eq_!(member.power_level, Int::new(100));
|
|
||||||
println!("{:#?}", room);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_presence(room: &Room) -> Result<(), String> {
|
|
||||||
let member = room
|
|
||||||
.members
|
|
||||||
.get(&UserId::try_from("@example:localhost").unwrap())
|
|
||||||
.unwrap();
|
|
||||||
assert_!(member.avatar_url.is_some());
|
|
||||||
assert_eq_!(member.last_active_ago, UInt::new(1));
|
|
||||||
assert_eq_!(member.presence, Some(PresenceState::Online));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
let rid = RoomId::try_from("!roomid:room.com").unwrap();
|
||||||
let uid = UserId::try_from("@example:localhost").unwrap();
|
let uid = UserId::try_from("@example:localhost").unwrap();
|
||||||
let bld = EventBuilder::default();
|
let mut bld = EventBuilder::default()
|
||||||
let runner = bld
|
|
||||||
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
|
||||||
.add_room_event_from_file(
|
.add_room_event_from_file(
|
||||||
"./tests/data/events/power_levels.json",
|
"./tests/data/events/power_levels.json",
|
||||||
RoomEvent::RoomPowerLevels,
|
RoomEvent::RoomPowerLevels,
|
||||||
)
|
)
|
||||||
.add_presence_event_from_file("./tests/data/events/presence.json")
|
.add_presence_event_from_file("./tests/data/events/presence.json")
|
||||||
.build_room_runner(&rid, &uid)
|
.build_room_runner(&rid, &uid);
|
||||||
.add_room_assert(test_presence)
|
|
||||||
.add_room_assert(test_room_member);
|
|
||||||
|
|
||||||
runner.run_test().await;
|
let room = bld.to_room();
|
||||||
|
|
||||||
|
let member = room
|
||||||
|
.members
|
||||||
|
.get(&UserId::try_from("@example:localhost").unwrap())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(member.membership, MembershipState::Join);
|
||||||
|
assert_eq!(member.power_level, Int::new(100));
|
||||||
|
|
||||||
|
assert!(member.avatar_url.is_some());
|
||||||
|
assert_eq!(member.last_active_ago, UInt::new(1));
|
||||||
|
assert_eq!(member.presence, Some(PresenceState::Online));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,176 +15,14 @@ use crate::events::{
|
||||||
use crate::identifiers::{RoomId, UserId};
|
use crate::identifiers::{RoomId, UserId};
|
||||||
use crate::AsyncClient;
|
use crate::AsyncClient;
|
||||||
|
|
||||||
use ansi_term::Colour;
|
|
||||||
use mockito::{self, mock, Mock};
|
use mockito::{self, mock, Mock};
|
||||||
|
|
||||||
use crate::models::Room;
|
use crate::models::Room;
|
||||||
|
|
||||||
/// `assert` to use in `TestRunner`.
|
/// Easily create events to stream into either a Client or a `Room` for testing.
|
||||||
///
|
|
||||||
/// This returns an `Err` on failure, instead of panicking.
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! assert_ {
|
|
||||||
($truth:expr) => {
|
|
||||||
if !$truth {
|
|
||||||
return Err(format!(
|
|
||||||
r#"assertion failed: `(left == right)`
|
|
||||||
expression: `{:?}`
|
|
||||||
failed at {}"#,
|
|
||||||
stringify!($truth),
|
|
||||||
file!(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `assert_eq` to use in `TestRunner.
|
|
||||||
///
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
/// 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!(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience macro for declaring an `async` assert function to store in the `TestRunner`.
|
|
||||||
///
|
|
||||||
/// Declares an async function that can be stored in a struct.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
/// ```rust
|
|
||||||
/// # use matrix_sdk::AsyncClient;
|
|
||||||
/// # use url::Url;
|
|
||||||
/// async_assert!{
|
|
||||||
/// async fn foo(cli: &AsyncClient) -> Result<(), String> {
|
|
||||||
/// assert_eq_!(cli.homeserver(), &url::Url::parse("matrix.org").unwrap());
|
|
||||||
/// 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)]
|
#[derive(Default)]
|
||||||
pub struct EventBuilder {
|
pub struct EventBuilder {
|
||||||
/// The events that determine the state of a `Room`.
|
/// The events that determine the state of a `Room`.
|
||||||
///
|
|
||||||
/// When testing the models `RoomEvent`s are needed.
|
|
||||||
room_events: Vec<RoomEvent>,
|
room_events: Vec<RoomEvent>,
|
||||||
/// The presence events that determine the presence state of a `RoomMember`.
|
/// The presence events that determine the presence state of a `RoomMember`.
|
||||||
presence_events: Vec<PresenceEvent>,
|
presence_events: Vec<PresenceEvent>,
|
||||||
|
@ -204,17 +42,11 @@ pub struct RoomTestRunner {
|
||||||
/// The account data events that determine the state of a `Room`.
|
/// The account data events that determine the state of a `Room`.
|
||||||
account_data: Vec<Event>,
|
account_data: Vec<Event>,
|
||||||
/// The events that determine the state of a `Room`.
|
/// The events that determine the state of a `Room`.
|
||||||
///
|
|
||||||
/// When testing the models `RoomEvent`s are needed.
|
|
||||||
room_events: Vec<RoomEvent>,
|
room_events: Vec<RoomEvent>,
|
||||||
/// The presence events that determine the presence state of a `RoomMember`.
|
/// The presence events that determine the presence state of a `RoomMember`.
|
||||||
presence_events: Vec<PresenceEvent>,
|
presence_events: Vec<PresenceEvent>,
|
||||||
/// The state events that determine the state of a `Room`.
|
/// The state events that determine the state of a `Room`.
|
||||||
state_events: Vec<StateEvent>,
|
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>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ClientTestRunner {
|
pub struct ClientTestRunner {
|
||||||
|
@ -229,19 +61,13 @@ pub struct ClientTestRunner {
|
||||||
/// The account data events that determine the state of a `Room`.
|
/// The account data events that determine the state of a `Room`.
|
||||||
account_data: Vec<Event>,
|
account_data: Vec<Event>,
|
||||||
/// The events that determine the state of a `Room`.
|
/// The events that determine the state of a `Room`.
|
||||||
///
|
|
||||||
/// When testing the models `RoomEvent`s are needed.
|
|
||||||
room_events: Vec<RoomEvent>,
|
room_events: Vec<RoomEvent>,
|
||||||
/// The presence events that determine the presence state of a `RoomMember`.
|
/// The presence events that determine the presence state of a `RoomMember`.
|
||||||
presence_events: Vec<PresenceEvent>,
|
presence_events: Vec<PresenceEvent>,
|
||||||
/// The state events that determine the state of a `Room`.
|
/// The state events that determine the state of a `Room`.
|
||||||
state_events: Vec<StateEvent>,
|
state_events: Vec<StateEvent>,
|
||||||
/// A `Vec` of callbacks that should assert something about the client.
|
|
||||||
///
|
|
||||||
/// The callback should use the provided `assert_`, `assert_*_` macros.
|
|
||||||
client_assertions: Vec<AsyncAssert>,
|
|
||||||
}
|
}
|
||||||
// the compiler complains that the event Vec's are never used they are.
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct MockTestRunner {
|
pub struct MockTestRunner {
|
||||||
/// Used when testing the whole client
|
/// Used when testing the whole client
|
||||||
|
@ -251,17 +77,11 @@ pub struct MockTestRunner {
|
||||||
/// The account data events that determine the state of a `Room`.
|
/// The account data events that determine the state of a `Room`.
|
||||||
account_data: Vec<Event>,
|
account_data: Vec<Event>,
|
||||||
/// The events that determine the state of a `Room`.
|
/// The events that determine the state of a `Room`.
|
||||||
///
|
|
||||||
/// When testing the models `RoomEvent`s are needed.
|
|
||||||
room_events: Vec<RoomEvent>,
|
room_events: Vec<RoomEvent>,
|
||||||
/// The presence events that determine the presence state of a `RoomMember`.
|
/// The presence events that determine the presence state of a `RoomMember`.
|
||||||
presence_events: Vec<PresenceEvent>,
|
presence_events: Vec<PresenceEvent>,
|
||||||
/// The state events that determine the state of a `Room`.
|
/// The state events that determine the state of a `Room`.
|
||||||
state_events: Vec<StateEvent>,
|
state_events: Vec<StateEvent>,
|
||||||
/// A `Vec` of callbacks that should assert something about the client.
|
|
||||||
///
|
|
||||||
/// The callback should use the provided `assert_`, `assert_*_` macros.
|
|
||||||
client_assertions: Vec<AsyncAssert>,
|
|
||||||
/// `mokito::Mock`
|
/// `mokito::Mock`
|
||||||
mock: Option<mockito::Mock>,
|
mock: Option<mockito::Mock>,
|
||||||
}
|
}
|
||||||
|
@ -269,16 +89,6 @@ pub struct MockTestRunner {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
impl EventBuilder {
|
impl EventBuilder {
|
||||||
/// Creates an `IncomingResponse` to hold events for a sync.
|
|
||||||
pub fn create_sync_response(mut self) -> Self {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Just throw events at the client, not part of a specific response.
|
|
||||||
pub fn create_event_stream(mut self) -> Self {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add an event to the room events `Vec`.
|
/// Add an event to the room events `Vec`.
|
||||||
pub fn add_ephemeral_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
pub fn add_ephemeral_from_file<Ev: TryFromRaw, P: AsRef<Path>>(
|
||||||
mut self,
|
mut self,
|
||||||
|
@ -420,7 +230,6 @@ impl EventBuilder {
|
||||||
room_events: Vec::new(),
|
room_events: Vec::new(),
|
||||||
presence_events: Vec::new(),
|
presence_events: Vec::new(),
|
||||||
state_events: Vec::new(),
|
state_events: Vec::new(),
|
||||||
client_assertions: Vec::new(),
|
|
||||||
mock,
|
mock,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -438,7 +247,6 @@ impl EventBuilder {
|
||||||
room_events: self.room_events,
|
room_events: self.room_events,
|
||||||
presence_events: self.presence_events,
|
presence_events: self.presence_events,
|
||||||
state_events: self.state_events,
|
state_events: self.state_events,
|
||||||
client_assertions: Vec::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -454,27 +262,22 @@ impl EventBuilder {
|
||||||
room_events: self.room_events,
|
room_events: self.room_events,
|
||||||
presence_events: self.presence_events,
|
presence_events: self.presence_events,
|
||||||
state_events: self.state_events,
|
state_events: self.state_events,
|
||||||
room_assertions: Vec::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RoomTestRunner {
|
impl RoomTestRunner {
|
||||||
/// Set `Room`
|
/// Set `Room`
|
||||||
pub fn set_room(mut self, room: Room) -> Self {
|
pub fn set_room(&mut self, room: Room) -> &mut Self {
|
||||||
self.room = Some(room);
|
self.room = Some(room);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_room_assert(mut self, assert: fn(&Room) -> Result<(), String>) -> Self {
|
fn stream_room_events(&mut self) {
|
||||||
self.room_assertions.push(assert);
|
let room = self
|
||||||
self
|
.room
|
||||||
}
|
.as_mut()
|
||||||
|
.expect("`Room` must be set use `RoomTestRunner::set_room`");
|
||||||
fn run_room_tests(&mut self) -> Result<(), Vec<String>> {
|
|
||||||
let mut errs = Vec::new();
|
|
||||||
let room = self.room.as_mut().unwrap();
|
|
||||||
|
|
||||||
for event in &self.account_data {
|
for event in &self.account_data {
|
||||||
match event {
|
match event {
|
||||||
// Event::IgnoredUserList(iu) => room.handle_ignored_users(iu),
|
// Event::IgnoredUserList(iu) => room.handle_ignored_users(iu),
|
||||||
|
@ -502,52 +305,29 @@ impl RoomTestRunner {
|
||||||
for event in &self.state_events {
|
for event in &self.state_events {
|
||||||
room.receive_state_event(event);
|
room.receive_state_event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
for assert in &mut self.room_assertions {
|
|
||||||
if let Err(e) = assert(&room) {
|
|
||||||
errs.push(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if errs.is_empty() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(errs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_test(mut self) {
|
pub fn to_room(&mut self) -> &mut Room {
|
||||||
let (count, errs) = if let Some(_) = &self.room {
|
self.stream_room_events();
|
||||||
(self.room_assertions.len(), self.run_room_tests())
|
self.room.as_mut().unwrap()
|
||||||
} 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 {
|
impl ClientTestRunner {
|
||||||
pub fn set_client(mut self, client: AsyncClient) -> Self {
|
pub fn set_client(&mut self, client: AsyncClient) -> &mut Self {
|
||||||
self.client = Some(client);
|
self.client = Some(client);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_client_assert(mut self, assert: AsyncAssert) -> Self {
|
async fn stream_client_events(&mut self) {
|
||||||
self.client_assertions.push(assert);
|
let mut cli = self
|
||||||
self
|
.client
|
||||||
}
|
.as_ref()
|
||||||
|
.expect("`AsyncClient` must be set use `ClientTestRunner::set_client`")
|
||||||
|
.base_client
|
||||||
|
.write()
|
||||||
|
.await;
|
||||||
|
|
||||||
async fn run_client_tests(&mut self) -> Result<(), Vec<String>> {
|
|
||||||
let mut errs = Vec::new();
|
|
||||||
let mut cli = self.client.as_ref().unwrap().base_client.write().await;
|
|
||||||
let room_id = &self.room_user_id.0;
|
let room_id = &self.room_user_id.0;
|
||||||
|
|
||||||
for event in &self.account_data {
|
for event in &self.account_data {
|
||||||
|
@ -573,40 +353,16 @@ impl ClientTestRunner {
|
||||||
for event in &self.state_events {
|
for event in &self.state_events {
|
||||||
cli.receive_joined_state_event(room_id, event).await;
|
cli.receive_joined_state_event(room_id, event).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
pub async fn to_client(&mut self) -> &mut AsyncClient {
|
||||||
let (count, errs) = if let Some(_) = &self.client {
|
self.stream_client_events().await;
|
||||||
(self.client_assertions.len(), self.run_client_tests().await)
|
self.client.as_mut().unwrap()
|
||||||
} 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 MockTestRunner {
|
impl MockTestRunner {
|
||||||
pub fn set_client(mut self, client: AsyncClient) -> Self {
|
pub fn set_client(&mut self, client: AsyncClient) -> &mut Self {
|
||||||
self.client = Some(client);
|
self.client = Some(client);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -616,48 +372,13 @@ impl MockTestRunner {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_client_assert(mut self, assert: AsyncAssert) -> Self {
|
pub async fn to_client(&mut self) -> Result<&mut AsyncClient, crate::Error> {
|
||||||
self.client_assertions.push(assert);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run_mock_tests(&mut self) -> Result<(), Vec<String>> {
|
|
||||||
let mut errs = Vec::new();
|
|
||||||
self.client
|
self.client
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.sync(crate::SyncSettings::default())
|
.sync(crate::SyncSettings::default())
|
||||||
.await
|
.await?;
|
||||||
.map(|_r| ())
|
|
||||||
.map_err(|e| vec![e.to_string()])?;
|
|
||||||
|
|
||||||
for assert in &mut self.client_assertions {
|
Ok(self.client.as_mut().unwrap())
|
||||||
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);
|
|
||||||
if !errs.is_empty() {
|
|
||||||
panic!("{} tests failed", errs.len());
|
|
||||||
} else {
|
|
||||||
println!("{}. {} passed", Colour::Green.paint("Ok"), count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue