pub struct Mock { /* private fields */ }mock only.Expand description
A mock backend for use in testing.
By default, the backend discards all data written to it without any validation.
However, the Mock::set_write_callback can be used to change this behaviour and to
dynamically generate custom responses from devices. Alternatively, responses can be
emulated by manually adding their raw bytes via Mock::push.
To test behaviour in the face of errors, there are dedicated methods for defining errors the mock will return.
Implementations§
Source§impl Mock
impl Mock
Sourcepub fn push<T: AsRef<[u8]>>(&mut self, bytes: T)
Available on crate features mock only.
pub fn push<T: AsRef<[u8]>>(&mut self, bytes: T)
mock only.Push data to the read buffer.
The data is not validated in any way.
Sourcepub fn is_empty(&self) -> bool
Available on crate features mock only.
pub fn is_empty(&self) -> bool
mock only.Whether the mock has any data available or not.
Sourcepub fn set_read_error(&mut self, err: Option<Error>)
Available on crate features mock only.
pub fn set_read_error(&mut self, err: Option<Error>)
mock only.Set the error to be returned when Mock::read is next called, if any.
The error is returned only once.
Sourcepub fn set_write_error(&mut self, err: Option<Error>)
Available on crate features mock only.
pub fn set_write_error(&mut self, err: Option<Error>)
mock only.Set the error to be returned when Mock::write is next called, if any.
The error is returned only once.
Sourcepub fn set_flush_error(&mut self, err: Option<Error>)
Available on crate features mock only.
pub fn set_flush_error(&mut self, err: Option<Error>)
mock only.Set the error to be returned when Mock::flush is next called, if any.
The error is returned only once.
Sourcepub fn set_read_timeout_error(&mut self, err: Option<Error>)
Available on crate features mock only.
pub fn set_read_timeout_error(&mut self, err: Option<Error>)
mock only.Set the error to be returned when Mock::set_read_timeout is next called, if any.
The error is returned only once.
Sourcepub fn set_write_callback(
&mut self,
callback: impl FnMut(&[u8], &mut dyn Write) + 'static,
)
Available on crate features mock only.
pub fn set_write_callback( &mut self, callback: impl FnMut(&[u8], &mut dyn Write) + 'static, )
mock only.Set the function to call when bytes are written to the backend.
The callback is passed two arguments: a reference to the written bytes and a mutable reference to the mock’s read buffer. Any emulated responses to the command’s bytes should be written to the read buffer.
By default, the Mock ignores all data written to it.
§Example
let mut port = Port::open_mock();
port.backend_mut().set_write_callback(|message, reply_buffer| {
match message {
b"/1 io get ai 1\n" => write!(reply_buffer, "@01 0 OK BUSY -- 5.5\r\n"),
b"/get pos\n" => write!(reply_buffer, "@01 0 OK BUSY -- 20\r\n@02 0 OK BUSY -- 10.1\r\n"),
_ => panic!("unexpected message"),
}.unwrap()
});
let reply = port.command_reply((1,"io get ai 1"))?.flag_ok()?;
assert_eq!(reply.data().parse::<f64>().unwrap(), 5.5);Trait Implementations§
Source§impl Read for Mock
Available on crate features mock only.
impl Read for Mock
mock only.Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
1.0.0 · Source§fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
limit bytes from it. Read moreSource§fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
read_array)Source§impl Write for Mock
Available on crate features mock only.
impl Write for Mock
mock only.Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)