embassy-rs / chiptool

Apache License 2.0
39 stars 25 forks source link

Mocking? #30

Open cylewitruk opened 7 months ago

cylewitruk commented 7 months ago

I'd be curious to hear thoughts from you guys on adding a mocking framework like mockall for PAC-generation?

Basically, I'd like to be able to rp-pac { version = "0.1.0", features = ["mocking"] } as a dev-dep, and have something like this in the generation (for example):

#[cfg(all(test, feature = "mocking"))]
use mockall:automock;
...
// in fieldset.rs
...
#[cfg_attr(all(test, feature = "mockall", automock))]
pub struct #name (pub #ty);

#[cfg_attr(all(test, feature = "mockall", automock))]
impl #name {
    #items
}
...

Then, in my code I could use:

#[double]
use pac::PWM as PWM;

Which would automatically expand PWM to MockPWM when I'm in test -- this would let me mock registers etc. so that I can more easily test that the registers end up with the correct values (logic-wise, at least) in drivers without needing to involve a board.

Concrete example is working on this PR where via mocks I could manually step through the PWM counter and verify that the correct div/top/compares/etc. are set for each step in a test.

Thoughts?

crabdancing commented 7 months ago

That sounds like a great idea! :D