asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.5k stars 62 forks source link

`automock` super traits #452

Closed wuerges closed 1 year ago

wuerges commented 1 year ago

I use automock in one of the projects I work on. We have a few super traits there. Super traits are not yet automocked.

I would love to help building this feature. I've read mockall codebase, and I think I know what I need to do.

We would need to store MockTrait struct for each "inherited" mocked trait, and then mix them in when parsing the super trait.

Is there something that I'm not aware of that prevents this?

I never worked with complex procedural macros. I'm sorry if I said something stupid!

asomers commented 1 year ago

This isn't possible, because proc macros just don't have enough information. The only thing that #[automock] knows about a super trait is its name. The only workaround would be something like storing the super traits in a file somewhere in between invocations of #[automock], and IMHO that's way too fragile. The best you can do is probably something like this:

pub trait Base {...}
#[automock]
pub trait Derived: Base {...}
impl Base for MockDerived {...}

I haven't tried it, but I think that would work.