Module embedded_hal_mock::eh1::serial

source ·
Available on crate feature eh1 only.
Expand description

Serial mock implementations.

You can set expectations for serial read and write transactions on a mock Serial device. Creating error transactions is supported as well.

Note that the embedded_hal crate provides both non-blocking and blocking serial traits. You can use the same mock for both interfaces.

§Usage: Non-blocking serial traits

// Note that we're using the non-blocking serial traits
use embedded_hal_mock::eh1::serial::{Mock as SerialMock, Transaction as SerialTransaction};
use embedded_hal_nb::serial::{Read, Write};

// Configure expectations
let expectations = [
    SerialTransaction::read(0x0A),
    SerialTransaction::read_many(b"xy"),
    SerialTransaction::write_many([1, 2]), // (1)
    SerialTransaction::flush(),
];

let mut serial = SerialMock::new(&expectations);

// Expect three reads
assert_eq!(serial.read().unwrap(), 0x0A);
assert_eq!(serial.read().unwrap(), b'x');
assert_eq!(serial.read().unwrap(), b'y');

// When designing against the non-blocking serial
// trait, we expect two separate writes. These could be
// expressed as two separate transactions, too. See (1) above.
serial.write(1).unwrap();
serial.write(2).unwrap();

// Finally, we expect a flush
serial.flush().unwrap();

// When you believe there are no more calls on the mock,
// call done() to assert there are no pending transactions.
serial.done();

§Testing Error Handling

If you want to test error handling of your code, you can also add error transactions. When the transaction is executed, an error is returned.

use embedded_hal_nb::{
    nb,
    serial::{ErrorKind, Read, Write},
};

// Configure expectations
let expectations = [
    SerialTransaction::read(42),
    SerialTransaction::read_error(nb::Error::WouldBlock),
    SerialTransaction::write_error(23, nb::Error::Other(ErrorKind::Other)),
    SerialTransaction::flush_error(nb::Error::Other(ErrorKind::Parity)),
];
let mut serial = SerialMock::new(&expectations);

// The first read will succeed
assert_eq!(serial.read().unwrap(), 42);

// The second read will return an error
assert_eq!(serial.read().unwrap_err(), nb::Error::WouldBlock);

// The following write/flush calls will return errors as well
assert_eq!(
    serial.write(23).unwrap_err(),
    nb::Error::Other(ErrorKind::Other)
);
assert_eq!(
    serial.flush().unwrap_err(),
    nb::Error::Other(ErrorKind::Parity)
);

// When you believe there are no more calls on the mock,
// call done() to assert there are no pending transactions.
serial.done();

Structs§