|
@@ -1,59 +1,73 @@
|
|
-use crate::{helper::new_user_after_login, init_sdk, tester::Tester};
|
|
|
|
-use flowy_dispatch::prelude::{EventDispatch, FromBytes, ModuleRequest, ToBytes};
|
|
|
|
-use flowy_user::{entities::UserDetail, event::UserEvent::SignOut};
|
|
|
|
|
|
+use flowy_dispatch::prelude::{FromBytes, ToBytes};
|
|
|
|
+use flowy_user::entities::UserDetail;
|
|
use std::{
|
|
use std::{
|
|
fmt::{Debug, Display},
|
|
fmt::{Debug, Display},
|
|
hash::Hash,
|
|
hash::Hash,
|
|
};
|
|
};
|
|
|
|
|
|
-pub struct TestBuilder<Error> {
|
|
|
|
- login: Option<bool>,
|
|
|
|
- inner: Option<Tester<Error>>,
|
|
|
|
- pub user_detail: Option<UserDetail>,
|
|
|
|
|
|
+use crate::tester::{TesterContext, TesterTrait};
|
|
|
|
+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,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-impl<Error> TestBuilder<Error>
|
|
|
|
-where
|
|
|
|
- Error: FromBytes + Debug,
|
|
|
|
-{
|
|
|
|
|
|
+pub type UserTestBuilder = TestBuilder<RandomUserTester<UserError>>;
|
|
|
|
+impl UserTestBuilder {
|
|
pub fn new() -> Self {
|
|
pub fn new() -> Self {
|
|
- TestBuilder::<Error> {
|
|
|
|
- login: None,
|
|
|
|
- inner: None,
|
|
|
|
|
|
+ Self {
|
|
|
|
+ tester: Box::new(RandomUserTester::<UserError>::new()),
|
|
user_detail: None,
|
|
user_detail: None,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
|
|
|
|
+pub struct TestBuilder<T: TesterTrait> {
|
|
|
|
+ pub tester: Box<T>,
|
|
|
|
+ pub user_detail: Option<UserDetail>,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl<T> TestBuilder<T>
|
|
|
|
+where
|
|
|
|
+ T: TesterTrait,
|
|
|
|
+{
|
|
pub fn login(mut self) -> Self {
|
|
pub fn login(mut self) -> Self {
|
|
- let user_detail = new_user_after_login();
|
|
|
|
|
|
+ let user_detail = self.tester.login();
|
|
self.user_detail = Some(user_detail);
|
|
self.user_detail = Some(user_detail);
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
pub fn logout(self) -> Self {
|
|
pub fn logout(self) -> Self {
|
|
- init_sdk();
|
|
|
|
- let _ = EventDispatch::sync_send(ModuleRequest::new(SignOut));
|
|
|
|
|
|
+ self.tester.logout();
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn event<E>(mut self, event: E) -> Self
|
|
|
|
|
|
+ pub fn request<P>(mut self, request: P) -> Self
|
|
where
|
|
where
|
|
- E: Eq + Hash + Debug + Clone + Display,
|
|
|
|
|
|
+ P: ToBytes,
|
|
{
|
|
{
|
|
- self.inner = Some(Tester::<Error>::new(event));
|
|
|
|
|
|
+ self.tester.set_payload(request);
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn request<P>(mut self, request: P) -> Self
|
|
|
|
|
|
+ pub fn event<E>(mut self, event: E) -> Self
|
|
where
|
|
where
|
|
- P: ToBytes,
|
|
|
|
|
|
+ E: Eq + Hash + Debug + Clone + Display,
|
|
{
|
|
{
|
|
- self.inner.as_mut().unwrap().set_request(request);
|
|
|
|
|
|
+ self.tester.set_event(event);
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
pub fn sync_send(mut self) -> Self {
|
|
pub fn sync_send(mut self) -> Self {
|
|
- self.inner.as_mut().unwrap().sync_send();
|
|
|
|
|
|
+ self.tester.sync_send();
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
@@ -61,22 +75,70 @@ where
|
|
where
|
|
where
|
|
R: FromBytes,
|
|
R: FromBytes,
|
|
{
|
|
{
|
|
- let inner = self.inner.take().unwrap();
|
|
|
|
- inner.parse::<R>()
|
|
|
|
|
|
+ self.tester.parse::<R>()
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn error(mut self) -> Error {
|
|
|
|
- let inner = self.inner.take().unwrap();
|
|
|
|
- inner.error()
|
|
|
|
- }
|
|
|
|
|
|
+ pub fn error(mut self) -> <T as TesterTrait>::Error { self.tester.error() }
|
|
|
|
|
|
pub fn assert_error(mut self) -> Self {
|
|
pub fn assert_error(mut self) -> Self {
|
|
- self.inner.as_mut().unwrap().assert_error();
|
|
|
|
|
|
+ self.tester.assert_error();
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
pub fn assert_success(mut self) -> Self {
|
|
pub fn assert_success(mut self) -> Self {
|
|
- self.inner.as_mut().unwrap().assert_success();
|
|
|
|
|
|
+ self.tester.assert_success();
|
|
self
|
|
self
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+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;
|
|
|
|
+
|
|
|
|
+ fn context(&mut self) -> &mut TesterContext { &mut self.context }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+pub struct FixedUserTester<Error> {
|
|
|
|
+ context: TesterContext,
|
|
|
|
+ err_phantom: PhantomData<Error>,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl<Error> FixedUserTester<Error>
|
|
|
|
+where
|
|
|
|
+ Error: FromBytes + Debug,
|
|
|
|
+{
|
|
|
|
+ pub fn new() -> Self {
|
|
|
|
+ Self {
|
|
|
|
+ context: TesterContext::default(),
|
|
|
|
+ err_phantom: PhantomData,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl<Error> TesterTrait for FixedUserTester<Error>
|
|
|
|
+where
|
|
|
|
+ Error: FromBytes + Debug,
|
|
|
|
+{
|
|
|
|
+ type Error = Error;
|
|
|
|
+
|
|
|
|
+ fn context(&mut self) -> &mut TesterContext { &mut self.context }
|
|
|
|
+}
|