Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to embedded-hal-1.0.0-alpha.1 #33

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: rust:1.31
- image: rust:1.35
steps:
- checkout

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "embedded-hal-mock"
version = "0.7.2"
version = "0.8.0"
authors = ["Danilo Bargen <mail@dbrgn.ch>"]
categories = ["embedded", "hardware-support", "development-tools::testing"]
description = "A collection of mocked devices that implement the embedded-hal traits"
Expand All @@ -20,5 +20,5 @@ include = [
edition = "2018"

[dependencies]
embedded-hal = { version = "0.2.3", features = ["unproven"] }
embedded-hal = "=1.0.0-alpha.1"
nb = "0.1.1"
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ or no-op implementations are used.
The goal of the crate is to be able to test drivers in CI without having access
to hardware.

This crate requires Rust 1.31+!

[Docs](https://docs.rs/embedded-hal-mock/)


Expand Down Expand Up @@ -45,8 +43,8 @@ See [docs](https://docs.rs/embedded-hal-mock/).

## Development Version of `embedded-hal`

If you would like to use the current development version of `embedded-hal` (or any other version),
so long as they are API compatible you can use a patch field in your `Cargo.toml` file to override
If you would like to use the current development version of `embedded-hal` (or any other version),
so long as they are API compatible you can use a patch field in your `Cargo.toml` file to override
the dependency version.

```yaml
Expand All @@ -55,6 +53,12 @@ embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal" }
```


# Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.35 and up. It *might*
compile with older versions but that may change in any new patch release.


## License

Licensed under either of
Expand Down
23 changes: 19 additions & 4 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! [`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html)
//! to implement the delay.

use core::convert::Infallible;
use std::thread;
use std::time::Duration;

Expand All @@ -30,8 +31,12 @@ impl MockNoop {
macro_rules! impl_noop_delay_us {
($type:ty) => {
impl delay::DelayUs<$type> for MockNoop {
type Error = Infallible;

/// A no-op delay implementation.
fn delay_us(&mut self, _n: $type) {}
fn try_delay_us(&mut self, _n: $type) -> Result<(), Self::Error> {
Ok(())
}
}
};
}
Expand All @@ -44,8 +49,12 @@ impl_noop_delay_us!(u64);
macro_rules! impl_noop_delay_ms {
($type:ty) => {
impl delay::DelayMs<$type> for MockNoop {
type Error = Infallible;

/// A no-op delay implementation.
fn delay_ms(&mut self, _n: $type) {}
fn try_delay_ms(&mut self, _n: $type) -> Result<(), Self::Error> {
Ok(())
}
}
};
}
Expand All @@ -68,9 +77,12 @@ impl StdSleep {
macro_rules! impl_stdsleep_delay_us {
($type:ty) => {
impl delay::DelayUs<$type> for StdSleep {
type Error = Infallible;

/// A `Delay` implementation that uses `std::thread::sleep`.
fn delay_us(&mut self, n: $type) {
fn try_delay_us(&mut self, n: $type) -> Result<(), Self::Error> {
thread::sleep(Duration::from_micros(n as u64));
Ok(())
}
}
};
Expand All @@ -84,9 +96,12 @@ impl_stdsleep_delay_us!(u64);
macro_rules! impl_stdsleep_delay_ms {
($type:ty) => {
impl delay::DelayMs<$type> for StdSleep {
type Error = Infallible;

/// A `Delay` implementation that uses `std::thread::sleep`.
fn delay_ms(&mut self, n: $type) {
fn try_delay_ms(&mut self, n: $type) -> Result<(), Self::Error> {
thread::sleep(Duration::from_millis(n as u64));
Ok(())
}
}
};
Expand Down
52 changes: 26 additions & 26 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
//! let mut i2c = I2cMock::new(&expectations);
//!
//! // Writing
//! i2c.write(0xaa, &vec![1, 2]).unwrap();
//! i2c.try_write(0xaa, &vec![1, 2]).unwrap();
//!
//! // Reading
//! let mut buf = vec![0u8; 2];
//! i2c.read(0xbb, &mut buf).unwrap();
//! i2c.try_read(0xbb, &mut buf).unwrap();
//! assert_eq!(buf, vec![3, 4]);
//!
//! // Finalise expectations
Expand Down Expand Up @@ -60,11 +60,11 @@
//! let mut i2c = I2cMock::new(&expectations);
//!
//! // Writing returns without an error
//! i2c.write(0xaa, &vec![1, 2]).unwrap();
//! i2c.try_write(0xaa, &vec![1, 2]).unwrap();
//!
//! // Reading returns an error
//! let mut buf = vec![0u8; 2];
//! let err = i2c.read(0xbb, &mut buf).unwrap_err();
//! let err = i2c.try_read(0xbb, &mut buf).unwrap_err();
//! assert_eq!(err, MockError::Io(ErrorKind::Other));
//!
//! // Finalise expectations
Expand Down Expand Up @@ -158,7 +158,7 @@ pub type Mock = Generic<Transaction>;
impl i2c::Read for Mock {
type Error = MockError;

fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
let w = self
.next()
.expect("no pending expectation for i2c::read call");
Expand All @@ -185,7 +185,7 @@ impl i2c::Read for Mock {
impl i2c::Write for Mock {
type Error = MockError;

fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
fn try_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
let w = self
.next()
.expect("no pending expectation for i2c::write call");
Expand All @@ -207,7 +207,7 @@ impl i2c::Write for Mock {
impl i2c::WriteRead for Mock {
type Error = MockError;

fn write_read(
fn try_write_read(
&mut self,
address: u8,
bytes: &[u8],
Expand Down Expand Up @@ -257,7 +257,7 @@ mod test {
let expectations = [Transaction::write(0xaa, vec![10, 12])];
let mut i2c = Mock::new(&expectations);

i2c.write(0xaa, &vec![10, 12]).unwrap();
i2c.try_write(0xaa, &vec![10, 12]).unwrap();

i2c.done();
}
Expand All @@ -268,7 +268,7 @@ mod test {
let mut i2c = Mock::new(&expectations);

let mut buff = vec![0u8; 2];
i2c.read(0xaa, &mut buff).unwrap();
i2c.try_read(0xaa, &mut buff).unwrap();
assert_eq!(vec![1, 2], buff);

i2c.done();
Expand All @@ -281,7 +281,7 @@ mod test {

let v = vec![1, 2];
let mut buff = vec![0u8; 2];
i2c.write_read(0xaa, &v, &mut buff).unwrap();
i2c.try_write_read(0xaa, &v, &mut buff).unwrap();
assert_eq!(vec![3, 4], buff);

i2c.done();
Expand All @@ -295,10 +295,10 @@ mod test {
];
let mut i2c = Mock::new(&expectations);

i2c.write(0xaa, &vec![1, 2]).unwrap();
i2c.try_write(0xaa, &vec![1, 2]).unwrap();

let mut v = vec![0u8; 2];
i2c.read(0xbb, &mut v).unwrap();
i2c.try_read(0xbb, &mut v).unwrap();

assert_eq!(v, vec![3, 4]);

Expand All @@ -311,7 +311,7 @@ mod test {
let expectations = [Transaction::write(0xaa, vec![1, 2])];
let mut i2c = Mock::new(&expectations);

i2c.write(0xaa, &vec![1, 3]).unwrap();
i2c.try_write(0xaa, &vec![1, 3]).unwrap();

i2c.done();
}
Expand All @@ -323,7 +323,7 @@ mod test {
let mut i2c = Mock::new(&expectations);

let mut buff = vec![0u8; 2];
i2c.write(0xaa, &mut buff).unwrap();
i2c.try_write(0xaa, &mut buff).unwrap();
assert_eq!(vec![10, 12], buff);

i2c.done();
Expand All @@ -337,7 +337,7 @@ mod test {

let v = vec![1, 2];
let mut buff = vec![0u8; 2];
i2c.write_read(0xaa, &v, &mut buff).unwrap();
i2c.try_write_read(0xaa, &v, &mut buff).unwrap();
assert_eq!(vec![3, 4], buff);

i2c.done();
Expand All @@ -349,7 +349,7 @@ mod test {
let expectations = [Transaction::read(0xaa, vec![10, 12])];
let mut i2c = Mock::new(&expectations);

i2c.write(0xaa, &vec![10, 12]).unwrap();
i2c.try_write(0xaa, &vec![10, 12]).unwrap();

i2c.done();
}
Expand All @@ -363,7 +363,7 @@ mod test {
];
let mut i2c = Mock::new(&expectations);

i2c.write(0xaa, &vec![10, 12]).unwrap();
i2c.try_write(0xaa, &vec![10, 12]).unwrap();

i2c.done();
}
Expand All @@ -375,7 +375,7 @@ mod test {
let mut i2c_clone = i2c.clone();

let mut buff = vec![0u8; 2];
i2c.read(0xaa, &mut buff).unwrap();
i2c.try_read(0xaa, &mut buff).unwrap();
assert_eq!(vec![1, 2], buff);

i2c.done();
Expand All @@ -400,7 +400,7 @@ mod test {
let mut i2c = Mock::new(&[
Transaction::write(0xaa, vec![10, 12]).with_error(expected_err.clone())
]);
let err = i2c.write(0xaa, &vec![10, 12]).unwrap_err();
let err = i2c.try_write(0xaa, &vec![10, 12]).unwrap_err();
assert_eq!(err, expected_err);
i2c.done();
}
Expand All @@ -412,7 +412,7 @@ mod test {
let mut i2c = Mock::new(&[Transaction::write(0xaa, vec![10, 12])
.with_error(MockError::Io(IoErrorKind::Other))]);
let mut buf = vec![0u8; 2];
let _ = i2c.read(0xaa, &mut buf);
let _ = i2c.try_read(0xaa, &mut buf);
}

/// The transaction bytes should still be validated.
Expand All @@ -421,7 +421,7 @@ mod test {
fn write_wrong_data() {
let mut i2c = Mock::new(&[Transaction::write(0xaa, vec![10, 12])
.with_error(MockError::Io(IoErrorKind::Other))]);
let _ = i2c.write(0xaa, &vec![10, 13]);
let _ = i2c.try_write(0xaa, &vec![10, 13]);
}

#[test]
Expand All @@ -432,7 +432,7 @@ mod test {
&[Transaction::read(0xaa, vec![10, 12]).with_error(expected_err.clone())],
);
let mut buf = vec![0u8; 2];
let err = i2c.read(0xaa, &mut buf).unwrap_err();
let err = i2c.try_read(0xaa, &mut buf).unwrap_err();
assert_eq!(err, expected_err);
i2c.done();
}
Expand All @@ -444,7 +444,7 @@ mod test {
let mut i2c =
Mock::new(&[Transaction::read(0xaa, vec![10, 12])
.with_error(MockError::Io(IoErrorKind::Other))]);
let _ = i2c.write(0xaa, &vec![10, 12]);
let _ = i2c.try_write(0xaa, &vec![10, 12]);
}

#[test]
Expand All @@ -453,7 +453,7 @@ mod test {
let mut i2c = Mock::new(&[Transaction::write_read(0xaa, vec![10, 12], vec![13, 14])
.with_error(expected_err.clone())]);
let mut buf = vec![0u8; 2];
let err = i2c.write_read(0xaa, &[10, 12], &mut buf).unwrap_err();
let err = i2c.try_write_read(0xaa, &[10, 12], &mut buf).unwrap_err();
assert_eq!(err, expected_err);
i2c.done();
}
Expand All @@ -464,7 +464,7 @@ mod test {
fn write_read_wrong_mode() {
let mut i2c = Mock::new(&[Transaction::write_read(0xaa, vec![10, 12], vec![13, 14])
.with_error(MockError::Io(IoErrorKind::Other))]);
let _ = i2c.write(0xaa, &vec![10, 12]);
let _ = i2c.try_write(0xaa, &vec![10, 12]);
}

/// The transaction bytes should still be validated.
Expand All @@ -474,7 +474,7 @@ mod test {
let mut i2c = Mock::new(&[Transaction::write_read(0xaa, vec![10, 12], vec![13, 14])
.with_error(MockError::Io(IoErrorKind::Other))]);
let mut buf = vec![0u8; 2];
let _ = i2c.write_read(0xaa, &vec![10, 13], &mut buf);
let _ = i2c.try_write_read(0xaa, &vec![10, 13], &mut buf);
}
}
}
Loading