pub struct Unimock { /* private fields */ }
Expand description
A type whose purpose is to provide mocked behaviour for the traits that it implements.
All traits implemented by Unimock can be considered mock implementations, except marker traits, Clone and Drop.
The mock configuration is specified up front, as a constructor argument in the form of a simple or compound Clause. After instantiation, the unimock configuration is immutable.
Unimock implements Send and Sync, and is therefore thread safe.
Unimock is meant to be used as a testing utility. Using it for other purposes is not recommended.
§Clone semantics
Calling clone
on unimock creates a derived object which shares internal state with the original.
Unimock runs post-test verifications automatically upon drop
(it is a RAII utility).
Interaction verifications should be performed only after all expected interactions have completed.
Because of this, only the original instance will run any verifications upon being dropped,
and when dropping the original instance, it is an error if there are derived objects still alive.
In a typical testing context, this scenario usually means that a spawned thread (that owns a derived Unimock) has escaped the test.
Unimock will panic with an appropriate message if this is detected.
This detection is usually reliable. Unimock will also induce a panic if the original instance gets dropped in a thread that does not equal the creator thread. Therefore, Unimock should always be cloned before sending off to another thread.
Implementations§
source§impl Unimock
impl Unimock
sourcepub fn new(setup: impl Clause) -> Self
pub fn new(setup: impl Clause) -> Self
Construct a unimock instance which strictly adheres to the description in the passed Clause.
Every call hitting the instance must be declared in advance as a clause, or else panic will ensue.
§Example
#[unimock(api=TraitMock)]
trait Trait {
fn foo(&self) -> &'static str;
}
let mocked = Unimock::new(TraitMock::foo.some_call(matching!()).returns("mocked"));
assert_eq!("mocked", mocked.foo());
sourcepub fn new_partial(setup: impl Clause) -> Self
pub fn new_partial(setup: impl Clause) -> Self
Construct a unimock instance using partial mocking.
In a partially mocked environment, every clause acts as an override over the default behaviour, which is to hit “real world” code.
Trait methods which support the unmock
feature get this behaviour automatically in a partial mock, unless explicitly overridden in the passed Clause.
Methods that cannot be unmocked still need to be explicitly mocked with a clause.
§Example
#[unimock(api=TraitMock, unmock_with=[real_foo])]
trait Trait {
fn foo(&self) -> &'static str;
}
fn real_foo(_: &impl std::any::Any) -> &'static str {
"real thing"
}
// A partial mock with no overrides:
assert_eq!("real thing", Unimock::new_partial(()).foo());
// A partial mock that overrides the behaviour of `Trait::foo`:
let clause = TraitMock::foo.next_call(matching!()).returns("mocked");
assert_eq!("mocked", Unimock::new_partial(clause).foo());
sourcepub fn no_verify_in_drop(self) -> Self
pub fn no_verify_in_drop(self) -> Self
Turn off auto-verification within Drop::drop.
The current use case for this is [no_std]
. In [no_std]
there is no thread API,
and therefore no way to check if the current thread is already panicking.
Leaving verify-in-drop on in such circumstances (in the context of panicking unit tests) easily leads to double panics, which are quite hard to debug.
sourcepub fn verify(self)
pub fn verify(self)
Explicitly verify this unimock instance.
There is no need to do this explicitly unless Self::no_verify_in_drop has been called.
source§impl Unimock
impl Unimock
sourcepub fn make_fragile_ref<T: 'static>(&self, value: T) -> &T
Available on crate feature fragile
only.
pub fn make_fragile_ref<T: 'static>(&self, value: T) -> &T
fragile
only.Convert the given value into a reference.
The value does not need to implement Send. Unimock will panic/abort if the instance is sent to another thread after calling this.
sourcepub fn make_fragile_mut<T: 'static>(&mut self, value: T) -> &mut T
Available on crate feature fragile
only.
pub fn make_fragile_mut<T: 'static>(&mut self, value: T) -> &mut T
fragile
only.Convert the given value into a mutable reference.
The value does not need to implement Send. Unimock will panic/abort if the instance is sent to another thread after calling this.
Trait Implementations§
source§impl AsyncBufRead for Unimock
Available on crate feature mock-futures-io-0-3
only.
impl AsyncBufRead for Unimock
mock-futures-io-0-3
only.Mocked implementation. Mock API is available at AsyncBufReadMock.
source§impl AsyncBufRead for Unimock
Available on crate feature mock-tokio-1
only.
impl AsyncBufRead for Unimock
mock-tokio-1
only.Mocked implementation. Mock API is available at AsyncBufReadMock.
source§impl AsyncRead for Unimock
Available on crate feature mock-futures-io-0-3
only.
impl AsyncRead for Unimock
mock-futures-io-0-3
only.Mocked implementation. Mock API is available at AsyncReadMock.
source§impl AsyncRead for Unimock
Available on crate feature mock-tokio-1
only.
impl AsyncRead for Unimock
mock-tokio-1
only.Mocked implementation. Mock API is available at AsyncReadMock.
source§impl AsyncSeek for Unimock
Available on crate feature mock-tokio-1
only.
impl AsyncSeek for Unimock
mock-tokio-1
only.Mocked implementation. Mock API is available at AsyncSeekMock.
source§impl AsyncSeek for Unimock
Available on crate feature mock-futures-io-0-3
only.
impl AsyncSeek for Unimock
mock-futures-io-0-3
only.Mocked implementation. Mock API is available at AsyncSeekMock.
source§impl AsyncWrite for Unimock
Available on crate feature mock-tokio-1
only.
impl AsyncWrite for Unimock
mock-tokio-1
only.Mocked implementation. Mock API is available at AsyncWriteMock.
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
buf
into the object. Read moresource§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>
poll_write
, except that it writes from a slice of buffers. Read moresource§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
poll_write_vectored
implementation. Read moresource§impl AsyncWrite for Unimock
Available on crate feature mock-futures-io-0-3
only.
impl AsyncWrite for Unimock
mock-futures-io-0-3
only.Mocked implementation. Mock API is available at AsyncWriteMock.
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize, Error>>
buf
into the object. Read moresource§fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
fn poll_flush( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll<Result<(), Error>>
source§impl BufRead for Unimock
Available on crate feature mock-std
only.
impl BufRead for Unimock
mock-std
only.Mocked implementation. Mock API is available at BufReadMock.
source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moresource§fn read_line(&mut self, buf: &mut String) -> Result<usize>
fn read_line(&mut self, buf: &mut String) -> Result<usize>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moresource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read moresource§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
bufread_skip_until
)byte
or EOF is reached. Read moresource§impl Debug for Unimock
Available on crate feature mock-core
only.
impl Debug for Unimock
mock-core
only.Mocked implementation. Mock API is available at DebugMock.
source§impl DelayNs for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl DelayNs for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at DelayNsMock.
source§fn delay_ns(&mut self, ns: u32)
fn delay_ns(&mut self, ns: u32)
ns
nanoseconds. Pause can be longer
if the implementation requires it due to precision/timing issues.source§impl Display for Unimock
Available on crate feature mock-core
only.
impl Display for Unimock
mock-core
only.Mocked implementation. Mock API is available at DisplayMock.
source§impl Error for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl Error for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at ErrorMock.
source§impl Error for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl Error for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at ErrorMock.
source§impl Error for Unimock
Available on crate feature mock-std
only.
impl Error for Unimock
mock-std
only.Mocked implementation. Mock API is available at ErrorMock.
source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
source§impl Error for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl Error for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at ErrorMock.
source§impl Error for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl Error for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at ErrorMock.
source§impl Hasher for Unimock
Available on crate feature mock-core
only.
impl Hasher for Unimock
mock-core
only.Mocked implementation. Mock API is available at HasherMock.
source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128
into this hasher.source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize
into this hasher.source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128
into this hasher.source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize
into this hasher.source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras
)source§impl<A: AddressMode + 'static> I2c<A> for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl<A: AddressMode + 'static> I2c<A> for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at I2cMock.
source§fn transaction(
&mut self,
address: A,
operations: &mut [Operation<'_>]
) -> Result<(), <Self as ErrorType>::Error>
fn transaction( &mut self, address: A, operations: &mut [Operation<'_>] ) -> Result<(), <Self as ErrorType>::Error>
source§impl InputPin for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl InputPin for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at InputPinMock.
source§impl OutputPin for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl OutputPin for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at OutputPinMock.
source§impl Read for Unimock
Available on crate feature mock-std
only.
impl Read for Unimock
mock-std
only.Mocked implementation. Mock API is available at ReadMock.
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read
, except that it reads into a slice of buffers. Read moresource§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf
. Read moresource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf
. Read moresource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf
. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)source§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> 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 moresource§impl Seek for Unimock
Available on crate feature mock-std
only.
impl Seek for Unimock
mock-std
only.Mocked implementation. Mock API is available at SeekMock.
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
source§fn stream_position(&mut self) -> Result<u64>
fn stream_position(&mut self) -> Result<u64>
source§impl SetDutyCycle for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl SetDutyCycle for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at SetDutyCycleMock.
source§fn max_duty_cycle(&self) -> u16
fn max_duty_cycle(&self) -> u16
source§fn set_duty_cycle(
&mut self,
duty: u16
) -> Result<(), <Self as ErrorType>::Error>
fn set_duty_cycle( &mut self, duty: u16 ) -> Result<(), <Self as ErrorType>::Error>
duty / max_duty
. Read moresource§fn set_duty_cycle_fully_off(&mut self) -> Result<(), <Self as ErrorType>::Error>
fn set_duty_cycle_fully_off(&mut self) -> Result<(), <Self as ErrorType>::Error>
source§fn set_duty_cycle_fully_on(&mut self) -> Result<(), <Self as ErrorType>::Error>
fn set_duty_cycle_fully_on(&mut self) -> Result<(), <Self as ErrorType>::Error>
source§impl<Word: Copy + 'static> SpiBus<Word> for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl<Word: Copy + 'static> SpiBus<Word> for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at SpiBusMock.
source§fn read(&mut self, words: &mut [Word]) -> Result<(), <Self as ErrorType>::Error>
fn read(&mut self, words: &mut [Word]) -> Result<(), <Self as ErrorType>::Error>
words
from the slave. Read moresource§fn write(&mut self, words: &[Word]) -> Result<(), <Self as ErrorType>::Error>
fn write(&mut self, words: &[Word]) -> Result<(), <Self as ErrorType>::Error>
words
to the slave, ignoring all the incoming words. Read moresource§fn transfer(
&mut self,
read: &mut [Word],
write: &[Word]
) -> Result<(), <Self as ErrorType>::Error>
fn transfer( &mut self, read: &mut [Word], write: &[Word] ) -> Result<(), <Self as ErrorType>::Error>
write
is written to the slave on MOSI and
words received on MISO are stored in read
. Read moresource§impl<Word: Copy + 'static> SpiDevice<Word> for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl<Word: Copy + 'static> SpiDevice<Word> for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at SpiDeviceMock.
source§fn transaction(
&mut self,
operations: &mut [Operation<'_, Word>]
) -> Result<(), <Self as ErrorType>::Error>
fn transaction( &mut self, operations: &mut [Operation<'_, Word>] ) -> Result<(), <Self as ErrorType>::Error>
source§fn read(&mut self, buf: &mut [Word]) -> Result<(), <Self as ErrorType>::Error>
fn read(&mut self, buf: &mut [Word]) -> Result<(), <Self as ErrorType>::Error>
source§fn write(&mut self, buf: &[Word]) -> Result<(), <Self as ErrorType>::Error>
fn write(&mut self, buf: &[Word]) -> Result<(), <Self as ErrorType>::Error>
source§impl StatefulOutputPin for Unimock
Available on crate feature mock-embedded-hal-1
only.
impl StatefulOutputPin for Unimock
mock-embedded-hal-1
only.Mocked implementation. Mock API is available at StatefulOutputPinMock.
source§impl Termination for Unimock
Available on crate feature std
only.
impl Termination for Unimock
std
only.This implementation of Termination
may be used for returning a Unimock instance as the result of a test:
#[test]
fn test() -> Unimock {
Unimock::new(())
}
This enables a more functional test style, instead of relying on panic-in-drop.
Calling report
prevents unimock from panicking later (in drop) on failed verifications, so use with care.
§Mocking
The mock-std
feature also enables mocking of this trait through mock::std::process::TerminationMock.
This trait mock is partial by default: Unless explicitly mocked, it behaves as specified above.
source§impl Write for Unimock
Available on crate feature mock-std
only.
impl Write for Unimock
mock-std
only.Mocked implementation. Mock API is available at WriteMock.
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 write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)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
)