Skip to content

Commit

Permalink
Stricter panic assertions (dbrgn#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy G authored Aug 4, 2023
1 parent 54365a1 commit 04f0ef5
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 66 deletions.
9 changes: 5 additions & 4 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,25 @@ mod tests {
}

#[test]
#[should_panic]
#[should_panic(
expected = "WARNING: A mock (from embedded-hal-mock) was dropped without calling the `.done()` method. See https://github.com/dbrgn/embedded-hal-mock/issues/34 for more details."
)]
fn panic_if_drop_not_called() {
let expectations = [0u8, 1u8];
let mut mock: Generic<u8> = Generic::new(&expectations);
assert_eq!(mock.next(), Some(0u8));
assert_eq!(mock.next(), Some(1u8));
// Note: done() not called
}

#[test]
#[should_panic]
#[should_panic(expected = "The `.done()` method was called twice!")]
fn panic_if_drop_called_twice() {
let expectations = [0u8, 1u8];
let mut mock: Generic<u8> = Generic::new(&expectations);
assert_eq!(mock.next(), Some(0u8));
assert_eq!(mock.next(), Some(1u8));
mock.done();
mock.done(); // This will panic
mock.done();
}
}
}
38 changes: 24 additions & 14 deletions src/eh0/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,37 +338,41 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[1, 2]`,\n right: `[1, 3]`: i2c::write data does not match expectation"
)]
fn write_data_mismatch() {
let expectations = [Transaction::write(0xaa, vec![1, 2])];
let mut i2c = Mock::new(&expectations);

let _ = i2c.write(0xaa, &vec![1, 3]); // Panics because unexpected data was written
let _ = i2c.write(0xaa, &vec![1, 3]);
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `Read`,\n right: `Write`: i2c::write unexpected mode"
)]
fn transaction_type_mismatch() {
let expectations = [Transaction::read(0xaa, vec![10, 12])];
let mut i2c = Mock::new(&expectations);

let mut buff = vec![0; 2];
let _ = i2c.write(0xaa, &mut buff); // Panics because it's a write, not a read
let _ = i2c.write(0xaa, &mut buff);
}

#[test]
#[should_panic]
#[should_panic(expected = "i2c::write_read address mismatch")]
fn address_mismatch() {
let expectations = [Transaction::write_read(0xbb, vec![1, 2], vec![3, 4])];
let mut i2c = Mock::new(&expectations);

let v = vec![1, 2];
let mut buff = vec![0; 2];
let _ = i2c.write_read(0xaa, &v, &mut buff); // Panics because an unexpected address was used
let _ = i2c.write_read(0xaa, &v, &mut buff);
}

#[test]
#[should_panic]
#[should_panic(expected = "Not all expectations consumed")]
fn unconsumed_expectations() {
let expectations = [
Transaction::write(0xaa, vec![10, 12]),
Expand All @@ -378,7 +382,7 @@ mod test {

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

i2c.done(); // Panics because not all transactions were consumed
i2c.done();
}

#[test]
Expand Down Expand Up @@ -428,21 +432,25 @@ mod test {

/// The transaction mode should still be validated.
#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `Write`,\n right: `Read`: i2c::read unexpected mode"
)]
fn write_wrong_mode() {
let mut i2c = Mock::new(&[Transaction::write(0xaa, vec![10, 12])
.with_error(MockError::Io(IoErrorKind::Other))]);
let mut buf = vec![0; 2];
let _ = i2c.read(0xaa, &mut buf); // Panics because it's a read, not a write
let _ = i2c.read(0xaa, &mut buf);
}

/// The transaction bytes should still be validated.
#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[10, 12]`,\n right: `[10, 13]`: i2c::write data does not match expectation"
)]
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]); // Panics because unexpected data was written
let _ = i2c.write(0xaa, &vec![10, 13]);
}

#[test]
Expand Down Expand Up @@ -471,12 +479,14 @@ mod test {

/// The transaction bytes should still be validated.
#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[10, 12]`,\n right: `[10, 13]`: i2c::write_read write data does not match expectation"
)]
fn write_read_wrong_data() {
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![0; 2];
let _ = i2c.write_read(0xaa, &vec![10, 13], &mut buf); // Panics because unexpected data was written
let _ = i2c.write_read(0xaa, &vec![10, 13], &mut buf);
}
}
}
26 changes: 18 additions & 8 deletions src/eh0/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ mod test {
}

#[test]
#[should_panic(expected = "unsatisfied expectations")]
#[should_panic(expected = "serial mock has unsatisfied expectations after call to done")]
fn test_serial_mock_blocking_write_not_enough() {
let ts = [Transaction::write_many([0xAB, 0xCD, 0xEF, 0x00])];
let mut ser = Mock::new(&ts);
Expand All @@ -524,7 +524,9 @@ mod test {
}

#[test]
#[should_panic(expected = "serial::write expected to write")]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `18`,\n right: `20`: serial::write expected to write 18 but actually wrote 20"
)]
fn test_serial_mock_wrong_write() {
let ts = [Transaction::write(0x12)];
let mut ser = Mock::new(&ts);
Expand All @@ -548,15 +550,15 @@ mod test {
}

#[test]
#[should_panic(expected = "unsatisfied expectations")]
#[should_panic(expected = "serial mock has unsatisfied expectations after call to done")]
fn test_serial_mock_pending_transactions() {
let ts = [Transaction::read(0x54)];
let mut ser = Mock::new(&ts);
ser.done();
}

#[test]
#[should_panic(expected = "unsatisfied expectations")]
#[should_panic(expected = "serial mock has unsatisfied expectations after call to done")]
fn test_serial_mock_reuse_pending_transactions() {
let ts = [Transaction::read(0x54)];
let mut ser = Mock::new(&ts);
Expand All @@ -568,23 +570,29 @@ mod test {
}

#[test]
#[should_panic(expected = "expected to perform a serial transaction 'Read(")]
#[should_panic(
expected = "expected to perform a serial transaction 'Read(84)' but instead did a write of 119"
)]
fn test_serial_mock_expected_read() {
let ts = [Transaction::read(0x54)];
let mut ser = Mock::new(&ts);
ser.bwrite_all(&[0x77]).unwrap();
}

#[test]
#[should_panic(expected = "expected to perform a serial transaction 'Write(")]
#[should_panic(
expected = "expected to perform a serial transaction 'Write(84)' but instead did a flush"
)]
fn test_serial_mock_expected_write() {
let ts = [Transaction::write(0x54)];
let mut ser = Mock::new(&ts);
ser.flush().unwrap();
}

#[test]
#[should_panic(expected = "expected to perform a serial transaction 'Flush'")]
#[should_panic(
expected = "expected to perform a serial transaction 'Flush', but instead did a read"
)]
fn test_serial_mock_expected_flush() {
let ts = [Transaction::flush()];
let mut ser: Mock<u128> = Mock::new(&ts);
Expand All @@ -610,7 +618,9 @@ mod test {
}

#[test]
#[should_panic(expected = "serial::write expected to write 42 but actually wrote 23")]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `42`,\n right: `23`: serial::write expected to write 42 but actually wrote 23"
)]
fn test_serial_mock_write_error_wrong_data() {
let error = nb::Error::Other(MockError::Io(io::ErrorKind::NotConnected));
let ts = [Transaction::write_error(42, error.clone())];
Expand Down
24 changes: 18 additions & 6 deletions src/eh0/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[10, 12]`,\n right: `[10, 12, 12]`: spi::write data does not match expectation"
)]
fn test_spi_mock_write_err() {
let expectations = [Transaction::write(vec![10, 12])];
let mut spi = Mock::new(&expectations);
Expand All @@ -334,7 +336,9 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[10, 12]`,\n right: `[10, 12, 12]`: spi::write_iter data does not match expectation"
)]
fn test_spi_mock_write_iter_err() {
let expectations = [Transaction::write(vec![10, 12])];
let mut spi = Mock::new(&expectations);
Expand All @@ -345,7 +349,9 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[12, 15]`,\n right: `[12, 13]`"
)]
fn test_spi_mock_transfer_err() {
let expectations = [Transaction::transfer(vec![10, 12], vec![12, 15])];
let mut spi = Mock::new(&expectations);
Expand All @@ -359,7 +365,9 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[1, 2]`,\n right: `[10, 12]`: spi::write data does not match expectation"
)]
fn test_spi_mock_transfer_response_err() {
let expectations = [Transaction::transfer(vec![1, 2], vec![3, 4, 5])];
let mut spi = Mock::new(&expectations);
Expand All @@ -373,7 +381,9 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `Transfer`,\n right: `Write`: spi::write unexpected mode"
)]
fn test_spi_mock_mode_err() {
let expectations = [Transaction::transfer(vec![10, 12], vec![])];
let mut spi = Mock::new(&expectations);
Expand All @@ -384,7 +394,9 @@ mod test {
}

#[test]
#[should_panic]
#[should_panic(
expected = "assertion failed: `(left == right)`\n left: `[10, 12]`,\n right: `[10, 12, 12]`: spi::write data does not match expectation"
)]
fn test_spi_mock_multiple_transaction_err() {
let expectations = [
Transaction::write(vec![10, 12]),
Expand Down
Loading

0 comments on commit 04f0ef5

Please sign in to comment.