1
0
Fork 0
mirror of https://codeberg.org/demostf/sync.git synced 2026-06-03 08:34:08 +02:00

mocks are only for tests

This commit is contained in:
Robin Appelman 2019-06-14 16:13:14 +02:00
commit 8880c6b62c

View file

@ -1,8 +1,5 @@
use crate::SyncCommand;
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use mio::Token; use mio::Token;
use std::cell::RefCell;
use std::rc::Rc;
use ws::{Result, Sender}; use ws::{Result, Sender};
#[enum_dispatch(Client)] #[enum_dispatch(Client)]
@ -31,19 +28,28 @@ impl From<Sender> for SenderClient {
} }
} }
#[derive(Clone, Debug)] #[cfg(test)]
pub(crate) struct MockClient { mod mock {
use crate::client::ClientTrait;
use crate::SyncCommand;
use mio::Token;
use std::cell::RefCell;
use std::rc::Rc;
use ws::Result;
#[derive(Clone, Debug)]
pub(crate) struct MockClient {
received: Rc<RefCell<Vec<String>>>, received: Rc<RefCell<Vec<String>>>,
token: Token, token: Token,
} }
impl PartialEq for MockClient { impl PartialEq for MockClient {
fn eq(&self, other: &MockClient) -> bool { fn eq(&self, other: &MockClient) -> bool {
self.token == other.token self.token == other.token
} }
} }
impl ClientTrait for MockClient { impl ClientTrait for MockClient {
fn send(&self, msg: &str) -> Result<()> { fn send(&self, msg: &str) -> Result<()> {
self.received.borrow_mut().push(msg.into()); self.received.borrow_mut().push(msg.into());
Ok(()) Ok(())
@ -52,9 +58,9 @@ impl ClientTrait for MockClient {
fn token(&self) -> Token { fn token(&self) -> Token {
self.token self.token
} }
} }
impl MockClient { impl MockClient {
pub fn new(token: usize) -> Self { pub fn new(token: usize) -> Self {
MockClient { MockClient {
received: Rc::new(RefCell::new(Vec::new())), received: Rc::new(RefCell::new(Vec::new())),
@ -72,8 +78,20 @@ impl MockClient {
pub fn clear(&self) { pub fn clear(&self) {
self.received.borrow_mut().clear() self.received.borrow_mut().clear()
} }
}
} }
#[cfg(test)]
pub(crate) use mock::MockClient;
#[cfg(not(test))]
#[enum_dispatch]
#[derive(PartialEq, Clone, Debug)]
pub(crate) enum Client {
Sender(SenderClient),
}
#[cfg(test)]
#[enum_dispatch] #[enum_dispatch]
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
pub(crate) enum Client { pub(crate) enum Client {
@ -81,6 +99,7 @@ pub(crate) enum Client {
Mock(MockClient), Mock(MockClient),
} }
#[cfg(test)]
impl Client { impl Client {
pub fn mock(token: usize) -> Self { pub fn mock(token: usize) -> Self {
Client::Mock(MockClient::new(token)) Client::Mock(MockClient::new(token))