asomers / mockall

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

Conditional Compiling with Mockall & double #489

Closed harshakhmk closed 10 months ago

harshakhmk commented 1 year ago

Hi, I am experimenting with mockall, I am trying to build code for linux and other OS, for writing test cases, I just needed an help with conditional compiling, when the OS is linux, I want the library to include functions, classes from a certain file and also include #[double] to them inorder to add mock related functionality and when it is another OS, then it should include files from another file with similar function and class names I used #[cfg(feature="linux")] and #[double]

asomers commented 1 year ago

#[double] is only used to conditionally compile based on #[cfg(test)]. And it isn't ever strictly necessary; it's just a convenience. In your case, if the OS-dependent code has the exact same names, then, you can probably do something like:

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux as os;
#[cfg(target_os = "freebsd")]
mod freebsd;
#[cfg(target_os = "freebsd")]
use freebsd as os;
#[double]
use os::MyStruct;

OTOH, if the OS-dependent code does not have the exact same names, then you should forget about #[double] and just use #[cfg(test)].

asomers commented 10 months ago

Closing for lack of feedback. Reopen if your question isn't fully answered.