mirror of
https://codeberg.org/demostf/sync.git
synced 2026-06-03 16:44:07 +02:00
mocks are only for tests
This commit is contained in:
parent
cc4420241f
commit
8880c6b62c
1 changed files with 53 additions and 34 deletions
|
|
@ -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,49 +28,70 @@ impl From<Sender> for SenderClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[cfg(test)]
|
||||||
pub(crate) struct MockClient {
|
mod mock {
|
||||||
received: Rc<RefCell<Vec<String>>>,
|
use crate::client::ClientTrait;
|
||||||
token: Token,
|
use crate::SyncCommand;
|
||||||
}
|
use mio::Token;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use ws::Result;
|
||||||
|
|
||||||
impl PartialEq for MockClient {
|
#[derive(Clone, Debug)]
|
||||||
fn eq(&self, other: &MockClient) -> bool {
|
pub(crate) struct MockClient {
|
||||||
self.token == other.token
|
received: Rc<RefCell<Vec<String>>>,
|
||||||
}
|
token: Token,
|
||||||
}
|
|
||||||
|
|
||||||
impl ClientTrait for MockClient {
|
|
||||||
fn send(&self, msg: &str) -> Result<()> {
|
|
||||||
self.received.borrow_mut().push(msg.into());
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn token(&self) -> Token {
|
impl PartialEq for MockClient {
|
||||||
self.token
|
fn eq(&self, other: &MockClient) -> bool {
|
||||||
}
|
self.token == other.token
|
||||||
}
|
|
||||||
|
|
||||||
impl MockClient {
|
|
||||||
pub fn new(token: usize) -> Self {
|
|
||||||
MockClient {
|
|
||||||
received: Rc::new(RefCell::new(Vec::new())),
|
|
||||||
token: Token(token),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn received(&self) -> Vec<SyncCommand> {
|
impl ClientTrait for MockClient {
|
||||||
RefCell::borrow(&self.received)
|
fn send(&self, msg: &str) -> Result<()> {
|
||||||
.iter()
|
self.received.borrow_mut().push(msg.into());
|
||||||
.map(|msg| serde_json::from_str::<SyncCommand>(msg).expect("invalid message"))
|
Ok(())
|
||||||
.collect()
|
}
|
||||||
|
|
||||||
|
fn token(&self) -> Token {
|
||||||
|
self.token
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&self) {
|
impl MockClient {
|
||||||
self.received.borrow_mut().clear()
|
pub fn new(token: usize) -> Self {
|
||||||
|
MockClient {
|
||||||
|
received: Rc::new(RefCell::new(Vec::new())),
|
||||||
|
token: Token(token),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn received(&self) -> Vec<SyncCommand> {
|
||||||
|
RefCell::borrow(&self.received)
|
||||||
|
.iter()
|
||||||
|
.map(|msg| serde_json::from_str::<SyncCommand>(msg).expect("invalid message"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&self) {
|
||||||
|
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))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue