embedded-hal-mock icon indicating copy to clipboard operation
embedded-hal-mock copied to clipboard

Feature Request: Add Optional labels to Mocks

Open silvergasp opened this issue 2 years ago • 2 comments

First of all, thanks for your work here. This project has been a real time-saver.

When you have a large set of mocks, it can become quite difficult to debug a test failure.

Say for example that you have a test that mocks 4 GPIO pins, with a specific sequence of reads and writes. As it currently stands you might get a message like this;

---- context::tests::send_command stdout ----
thread 'context::tests::send_command' panicked at 'no expectation for pin::is_high call', /usr/local/cargo/git/checkouts/embedded-hal-mock-54d083840a293f52/d394c81/src/pin.rs:159:50
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Apart from the offending function call, this error message doesn't yield much useful information to track down where the failure has occurred. To improve these error messages I'd like to add a label so that; 'no expectation for pin::is_high call' becomes 'no expectation for pin::is_high call on pin my_label'

From the user facing API this might look something like;

// Not labelled
let mut pin = PinMock::new(&expectations);

// Labelled
let mut pin = PinMock::new_with_label(&expectations, "my_label");

I'm happy to have a crack at implementing this, but I thought I get some input before going ahead with it.

silvergasp avatar May 12 '22 19:05 silvergasp

interesting, seems like a reasonable addition to me! aside, have you tried RUST_BACKTRACE=1? i have found this (+ color-backtrace) to be very helpful for determining the call site.

ryankurte avatar May 13 '22 00:05 ryankurte

@silvergasp looks useful, feel free to send a PR!

dbrgn avatar May 13 '22 07:05 dbrgn