Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendation on choosing between different sets of #[values(...)]? #261

Open
ghost-in-the-zsh opened this issue Jun 14, 2024 · 0 comments

Comments

@ghost-in-the-zsh
Copy link

ghost-in-the-zsh commented Jun 14, 2024

I have the following use-case, but I'm not clear on how to proceed1. We can already do this:

#[rstest]
fn your_test(#[values(1, 2, 3)] val: u8) { ... }

But suppose you want to build different sets of #[values(...)], based on some condition: how should I go about it? (NOTE: This is about conditional comp-time selection, not dynamic #[values()].) For example, the following would result in a compile error, but probably communicates the intended idea:

#[rstest]
fn your_test(#[cfg_attr(feature = "myfeat", values(1, 2, 3))] val: u8) { ... }

I realize trying to make this part of the signature is likely the main problem, since the param must always be present and conditional compilation here would break that guarantee. But are there other options?

For example, is there a way to define the entries that #[values(...)] should hold outside, as a list2? I tried something like this, but it didn't give me what I was hoping it would:

// You could feature-flag the `const` def
const VALUES: [u8; 3] = [1,2,3];

#[rstest]
fn your_test(
    #[values(VALUES)]   // <<-- Error: expects `u8`, received `[u8; 3]`
    val: u8
) { ... }

Is there a workaround or a "best practice" to achieve the intent described above? Do I have to write two separate/duplicate functions and feature-gate them instead, as shown below?:

#[cfg(feature = "f1")]
#[rstest]
fn your_test(#[values(1, 2, 3)] val: u8) { ... }

#[cfg(feature = "f2")]
#[rstest]
fn your_test(#[values(4, 5, 6)] val: u8) { ... }

Or maybe I need to write a macro3 to split my arrays into comma-separated values at comp-time to make them acceptable by #[values(...)]?

I hope there's a better way. I've read the docs several times, but if the answer is there, I must've missed it. I'm fairly new to rstest itself (thanks, btw 👍) and I look forward to using it more; there're still some things I need to understand better.

Thanks again.

PS: Sorry if this is a duplicate. The other issues that had references to #[values()] seemed unrelated.

Footnotes

  1. If it is, it wasn't clear to me from the docs.

  2. I know I can define independent consts and then use them as #[values(CONST_1, CONST_2, CONST_3)] val: u8 ..., but I can't just plug a vector/array in there directly.

  3. Not sure it's as "easy" as it might sound at first.

@ghost-in-the-zsh ghost-in-the-zsh changed the title Recommendation on choosing between different #[values(...)] sets? Recommendation on choosing between different sets of #[values(...)]? Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant