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

feature request: test_case generators #111

Closed
jtmoon79 opened this issue Dec 31, 2022 · 1 comment · Fixed by #128
Closed

feature request: test_case generators #111

jtmoon79 opened this issue Dec 31, 2022 · 1 comment · Fixed by #128

Comments

@jtmoon79
Copy link

jtmoon79 commented Dec 31, 2022

tl;dr would be great if some primitive test_case generators were possible, e.g. #[test_case(1; 3; "test cases 1 2 and 3")] (made-up syntax).

Problem

For my project, I have a large static vector DATA with complex entries. DATA length corresponds to a global constant DATA_LEN. Each DATA entry carries it's own built-in test case (only in test builds). Previously, I had created one test case and looped on all entries.

#[test]
fn test_DATA_entries() {
    for entry in DATA.iter() {
         assert(...)
    }
}

But I wanted more granular test cases, one test case for each entry in the DATA. So I added #[test_case] declaration for each

#[test_case(0)]
#[test_case(1)]
// ...
#[test_case(70)]
fn test_DATA_entry(index: usize) {
    let entry = &DATA[&index];
    assert(...)
}

However, I have to manually update the listing of #[test_case] when DATA changes.

Solution

It would be great if I could have test_case help me generate the test cases, 0 up to DATA_LEN.

const DATA_LEN: usize = 70;

#[test_case(0; DATA_LEN; "a test case for each entry in DATA")]
fn test_DATA_entry(index: usize) {
    let entry = &DATA[&index];
    assert(...)
}

the code snippet would run 70 unique test cases.

Suggestions

I'm imagining some new formatting like

#[test_case(1; 3; "test cases 1 2 and 3")]

Or perhaps a new test_case library export like

#[test_case_generator(1; 3; "test cases 1 2 and 3")]

Or perhaps it has a start, stop, step that takes closures. The stop closure returns Option<T>. When None the test cases stop else pass T to the test case

#[test_case_generator(1, |x| if x > 3 { None } else { Some(x) }, |x| x + 1; "test cases 1 2 and 3")]
@jtmoon79
Copy link
Author

Similar to #94

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

Successfully merging a pull request may close this issue.

1 participant