AppFlowy/rust-lib/flowy-test/src/builder.rs

152 lines
3.2 KiB
Rust
Raw Normal View History

2021-07-17 00:24:17 +00:00
use flowy_dispatch::prelude::{FromBytes, ToBytes};
use flowy_user::entities::UserDetail;
2021-07-16 15:18:12 +00:00
use std::{
fmt::{Debug, Display},
hash::Hash,
};
2021-07-17 02:26:05 +00:00
use crate::{
helper::valid_email,
tester::{TesterContext, TesterTrait},
};
2021-07-17 00:24:17 +00:00
use flowy_user::errors::UserError;
use flowy_workspace::errors::WorkspaceError;
use std::marker::PhantomData;
pub type WorkspaceTestBuilder = TestBuilder<FixedUserTester<WorkspaceError>>;
impl WorkspaceTestBuilder {
pub fn new() -> Self {
Self {
tester: Box::new(FixedUserTester::<WorkspaceError>::new()),
user_detail: None,
}
}
2021-07-16 15:18:12 +00:00
}
2021-07-17 00:24:17 +00:00
pub type UserTestBuilder = TestBuilder<RandomUserTester<UserError>>;
impl UserTestBuilder {
2021-07-16 15:18:12 +00:00
pub fn new() -> Self {
2021-07-17 00:24:17 +00:00
Self {
tester: Box::new(RandomUserTester::<UserError>::new()),
2021-07-16 15:18:12 +00:00
user_detail: None,
}
}
2021-07-17 00:24:17 +00:00
}
2021-07-16 15:18:12 +00:00
2021-07-17 00:24:17 +00:00
pub struct TestBuilder<T: TesterTrait> {
pub tester: Box<T>,
pub user_detail: Option<UserDetail>,
}
impl<T> TestBuilder<T>
where
T: TesterTrait,
{
2021-07-16 15:18:12 +00:00
pub fn login(mut self) -> Self {
2021-07-17 00:24:17 +00:00
let user_detail = self.tester.login();
2021-07-16 15:18:12 +00:00
self.user_detail = Some(user_detail);
self
}
pub fn logout(self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.logout();
2021-07-16 15:18:12 +00:00
self
}
2021-07-17 00:24:17 +00:00
pub fn request<P>(mut self, request: P) -> Self
2021-07-16 15:18:12 +00:00
where
2021-07-17 00:24:17 +00:00
P: ToBytes,
2021-07-16 15:18:12 +00:00
{
2021-07-17 00:24:17 +00:00
self.tester.set_payload(request);
2021-07-16 15:18:12 +00:00
self
}
2021-07-17 00:24:17 +00:00
pub fn event<E>(mut self, event: E) -> Self
2021-07-16 15:18:12 +00:00
where
2021-07-17 00:24:17 +00:00
E: Eq + Hash + Debug + Clone + Display,
2021-07-16 15:18:12 +00:00
{
2021-07-17 00:24:17 +00:00
self.tester.set_event(event);
2021-07-16 15:18:12 +00:00
self
}
pub fn sync_send(mut self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.sync_send();
2021-07-16 15:18:12 +00:00
self
}
pub fn parse<R>(mut self) -> R
where
R: FromBytes,
{
2021-07-17 00:24:17 +00:00
self.tester.parse::<R>()
2021-07-16 15:18:12 +00:00
}
2021-07-17 00:24:17 +00:00
pub fn error(mut self) -> <T as TesterTrait>::Error { self.tester.error() }
2021-07-16 15:18:12 +00:00
pub fn assert_error(mut self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.assert_error();
2021-07-16 15:18:12 +00:00
self
}
pub fn assert_success(mut self) -> Self {
2021-07-17 00:24:17 +00:00
self.tester.assert_success();
2021-07-16 15:18:12 +00:00
self
}
}
2021-07-17 00:24:17 +00:00
pub struct RandomUserTester<Error> {
context: TesterContext,
err_phantom: PhantomData<Error>,
}
impl<Error> RandomUserTester<Error>
where
Error: FromBytes + Debug,
{
pub fn new() -> Self {
Self {
context: TesterContext::default(),
err_phantom: PhantomData,
}
}
}
impl<Error> TesterTrait for RandomUserTester<Error>
where
Error: FromBytes + Debug,
{
type Error = Error;
2021-07-17 02:26:05 +00:00
fn mut_context(&mut self) -> &mut TesterContext { &mut self.context }
fn context(&self) -> &TesterContext { &self.context }
2021-07-17 00:24:17 +00:00
}
pub struct FixedUserTester<Error> {
context: TesterContext,
err_phantom: PhantomData<Error>,
}
impl<Error> FixedUserTester<Error>
where
Error: FromBytes + Debug,
{
pub fn new() -> Self {
Self {
2021-07-17 02:26:05 +00:00
context: TesterContext::new(valid_email()),
2021-07-17 00:24:17 +00:00
err_phantom: PhantomData,
}
}
}
impl<Error> TesterTrait for FixedUserTester<Error>
where
Error: FromBytes + Debug,
{
type Error = Error;
2021-07-17 02:26:05 +00:00
fn mut_context(&mut self) -> &mut TesterContext { &mut self.context }
fn context(&self) -> &TesterContext { &self.context }
2021-07-17 00:24:17 +00:00
}