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

Add test_matrix macro #128

Merged
merged 6 commits into from
Sep 16, 2023
Merged

Conversation

overhacked
Copy link
Contributor

@overhacked overhacked commented Jul 27, 2023

The test_matrix macro generates test cases from the Cartesian product of possible values given for test function arguments. For each argument, it supports lists of possible values, numeric ranges, or single expressions (for constant arguments). It also supports the => syntax for asserting test case output, which is handled identically to test_case, because the parsed TestCaseExpression is cloned and included in each generated TestCase before it is rendered.

Acceptance tests and basic documentation are included in this PR. I'd like to also update the Wiki documentation, and will open a parallel PR to the wiki repo.

Usage example

#[test_matrix(
    [-2, 2],
    [-4, 4]
)]
fn multiplication_tests(x: i8, y: i8) {
    let actual = (x * y).abs();

    assert_eq!(8, actual)
}

Related Issues

@overhacked
Copy link
Contributor Author

I've just discovered that there is no way to fork a GitHub Wiki. Seriously, GitHub!? 👹 So, if you decide to merge this PR, I can send you updated Wiki Markdown files at that time. I've cloned the wiki into my forked repo, so you can see my proposed changes... just not in the nice PR interface. 🤷

@luke-biel
Copy link
Collaborator

Thanks for contributing :) I think I like the approach. I'll look into this over the weekend. If I didn't answer by Monday, feel free to ping me.

Allows generating Cartesian product test matrices.
Tests and docs forthcoming.
@overhacked
Copy link
Contributor Author

overhacked commented Jul 27, 2023

@overhacked
Copy link
Contributor Author

@luke-biel, if you have a second to approve running the CI workflow before Monday, I've got time over the weekend to troubleshoot any possible CI issues.

@frondeus
Copy link
Owner

@luke-biel, if you have a second to approve running the CI workflow before Monday, I've got time over the weekend to troubleshoot any possible CI issues.

Approved :)

@frondeus
Copy link
Owner

I definitely like what I see, however it might be a good idea to have this feature behind a flag. What I noticed at first glance, is that we add Itertools dependency - which might affect build times for those that are not going to use test_matrix.

What do you think?

Test basic features and for correct compilation error
output
Required that `TestCaseExpression` and all it's descendant structs be
`Clone` so that TestMatrix can clone them for each TestCase it creates.
Also made TestComment Clone for parity.

Implemented quote::ToTokens for TestComment so that the "illegal
comment" error generated by TestMatrix can point to the correct span of
where the illegal comment begins. On stable Rust, this just highlights
the first token, the semicolon, but on nightly Rust (which has
`quote::Span::join`) it will highlight the entire illegal comment.
`isize` will allow for negative numbers and be compatible
with 32-bit and 64-bit platforms.

Add some acceptance tests for the same.
Add unreleased CHANGELOG fragment, README documentation,
and doc comment to src/lib.rs and crate/test-case-macros/src/lib.rs.

Detailed Wiki documentation forthcoming.
@overhacked
Copy link
Contributor Author

Fixed the CI issues. I didn't understand the nightly/stable cargo insta setup in the repo at first, but then I discovered scripts/test_all.sh. 😄

Also, the last commit in the series is a crazy idea that I'm not even sure I'm 💯 on board with. The itertools code base is MIT licensed, so what if, instead of feature-gating the external dependency, you just vendor the one module for making a Cartesian product out of iterators into the test-case code? It's basically self-contained in itertools; I just brought in two helper functions and changed one call from an itertools::Itertools trait function to a call to std::iter::Iterator::filter_map() that works the same.

Happy to discuss. Totally fine if you're not at all on board with it.

Copied multi_product module from itertools 0.11.0
under MIT license. Eliminates dependency on building
all of itertools.
@luke-biel luke-biel merged commit ada5d82 into frondeus:master Sep 16, 2023
8 checks passed
@luke-biel
Copy link
Collaborator

Finally had time to tackle this. Code looked good, didn't need to contact you. The itertools mod copy-paste was something I was considering suggesting earlier, so I'm happy it was done :)

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 this pull request may close these issues.

feature request: test_case generators Generate test case for each enum variant
3 participants