frondeus / test-case

Rust procedural macro attribute for adding test cases easily
MIT License
614 stars 39 forks source link

Mixing test_case with cfg? #64

Closed lemunozm closed 3 years ago

lemunozm commented 3 years ago

Hi,

I am in a situation where my test case only must be compiled if some rust feature is enabled:

#[cfg(feature = "feature_type_a")]
#[test_case(TypeA)]
#[test_case(TypeB)]
fn do_stuff() {
...
}

The problem with the above code is that the cfg is set for the entire function (all cases). If feature_type_a is not enabled, the function do_stuff does not exist, but I want that it exists for TypeB. Is there any way to pass a cfg or a feature into a test_case? Something like: #[test_case(features = "feature_type_a", TypeA)]

Thank you so much!

luke-biel commented 3 years ago

I think what you are looking for is rust built-in cfg_attr. https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute . Test-case itself doesn't support conditional compilation.

Example:

#[cfg_attr(feature_type_a, test_case(TypeA))]

Does that help?

lemunozm commented 3 years ago

That's just what I want!

My knowledge of this part of rust is very limited.

Thanks a lot for your answer!