Return errors from I2C transaction operations - #143
Conversation
Generated-by: OpenAI Codex Signed-off-by: aineoae86-sys <ai.neo.ae86@gmail.com>
|
@Old-Ding If you're copy-pasting directly from AI output, please ensure at the very least that the description is readable and well-formatted... |
dbrgn
left a comment
There was a problem hiding this comment.
Regarding the change itself: I was a bit on the fence, whether a TransactionEnd expectation should be part of the expectations (since the transaction is aborted, not completed). But on the wire a STOP happens as well, so TransactionEnd is probably correct.
Having more expectations between error and TransactionEnd would be wrong, and that will get caught by this crate. This is not yet tested, so an additional test would be nice (see inline comment).
| } | ||
|
|
||
| #[test] | ||
| fn transaction_returns_operation_error() { |
There was a problem hiding this comment.
This tests the happy path. Could you add a test for the unhappy path as well, where the user included additional assertions before transaction_end? This should cause the test to fail.
Probably something like this:
#[test]
#[should_panic(expected = "i2c::transaction_end unexpected mode")]
fn transaction_error_rejects_trailing_expectations() {
let expectations = [
Transaction::transaction_start(0x76),
Transaction::write(0x76, vec![0x88]).with_error(ErrorKind::Other),
Transaction::write(0x76, vec![0xAA]),
Transaction::transaction_end(0x76),
];
let mut i2c = Mock::new(&expectations);
let bytes = [0x88];
let more = [0xAA];
let mut ops = [i2c::Operation::Write(&bytes), i2c::Operation::Write(&more)];
let _ = i2c.transaction(0x76, &mut ops);
}
...but I did not test this in practice, so please validate that it works as intended.
There was a problem hiding this comment.
Addressed in dbc0ede: transaction_error_rejects_trailing_expectations adds the error-path case with an extra write expectation before transaction_end, and asserts that it panics with i2c::transaction_end unexpected mode.
I validated the current head with cargo test --all-features: 140 unit tests and 21 doc tests passed, including this regression test.
Signed-off-by: Old-Ding <ai.neo.ae86@gmail.com>
Fixes #127.
Summary:
eh1::i2c::Mock::transaction.transaction_endexpectation so the mock transaction boundary remains balanced when an operation returns an error.transaction_end.Testing:
cargo test transaction_returns_operation_error --features eh1cargo test transaction_error_rejects_trailing_expectations --features eh1cargo fmt --checkcargo test --all-featuresgit diff --check HEAD~1..HEADgit ls-files --eol src/eh1/i2c.rs