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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [Unreleased]
### New features
* Add `test_matrix` macro: generates test cases from Cartesian product of possible test function argument values.

## 3.1.0
### New features
* Copy attribute span to generated test functions so that IDEs recognize them properly as individual tests
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ test tests::multiplication_tests::when_operands_are_swapped ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```

### Test Matrix

The `#[test_matrix(...)]` macro allows generating multiple test cases from the
Cartesian product of one or more possible values for each test function argument. The
number of arguments to the `test_matrix` macro must be the same as the number of arguments to
the test function. Each macro argument can be:

1. A list in array (`[x, y, ...]`) or tuple (`(x, y, ...)`) syntax. The values can be any
valid [expression](https://doc.rust-lang.org/reference/expressions.html).
2. A closed numeric range expression (e.g. `0..100` or `1..=99`), which will generate
argument values for all integers in the range.
3. A single expression, which can be used to keep one argument constant while varying the
other test function arguments using a list or range.

#### Example usage:

```rust
#[cfg(test)]
mod tests {
use test_case::test_matrix;

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

assert_eq!(8, actual)
}
}
```

## MSRV Policy

Starting with version 3.0 and up `test-case` introduces policy of only supporting latest stable Rust.
Expand Down
11 changes: 10 additions & 1 deletion crates/test-case-core/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::TokenStream2;
use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::{LitStr, Token};

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TestCaseComment {
_semicolon: Token![;],
pub comment: LitStr,
Expand All @@ -16,6 +18,13 @@ impl Parse for TestCaseComment {
}
}

impl ToTokens for TestCaseComment {
fn to_tokens(&self, tokens: &mut TokenStream2) {
self._semicolon.to_tokens(tokens);
self.comment.to_tokens(tokens);
}
}

#[cfg(test)]
mod tests {
use crate::comment::TestCaseComment;
Expand Down
22 changes: 11 additions & 11 deletions crates/test-case-core/src/complex_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod kw {
syn::custom_keyword!(matches_regex);
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OrderingToken {
Eq,
Lt,
Expand All @@ -47,57 +47,57 @@ pub enum OrderingToken {
Geq,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PathToken {
Any,
Dir,
File,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ord {
pub token: OrderingToken,
pub expected_value: Box<Expr>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AlmostEqual {
pub expected_value: Box<Expr>,
pub precision: Box<Expr>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Path {
pub token: PathToken,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Contains {
pub expected_element: Box<Expr>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ContainsInOrder {
pub expected_slice: Box<Expr>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Len {
pub expected_len: Box<Expr>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Count {
pub expected_len: Box<Expr>,
}

#[cfg(feature = "with-regex")]
#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Regex {
pub expected_regex: Box<Expr>,
}

#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub enum ComplexTestCase {
Not(Box<ComplexTestCase>),
And(Vec<ComplexTestCase>),
Expand Down
4 changes: 2 additions & 2 deletions crates/test-case-core/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pub mod kw {
syn::custom_keyword!(panics);
}

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TestCaseExpression {
_token: Token![=>],
pub extra_keywords: HashSet<Modifier>,
pub result: TestCaseResult,
}

#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum TestCaseResult {
// test_case(a, b, c => keywords)
Empty,
Expand Down
2 changes: 2 additions & 0 deletions crates/test-case-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod complex_expr;
mod expr;
mod modifier;
mod test_case;
mod test_matrix;
mod utils;

pub use test_case::TestCase;
pub use test_matrix::TestMatrix;
2 changes: 1 addition & 1 deletion crates/test-case-core/src/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod kw {
syn::custom_keyword!(ignore);
}

#[derive(PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Modifier {
Inconclusive,
InconclusiveWithReason(LitStr),
Expand Down
17 changes: 15 additions & 2 deletions crates/test-case-core/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use syn::{parse_quote, Error, Expr, Ident, ItemFn, ReturnType, Token};
#[derive(Debug)]
pub struct TestCase {
args: Punctuated<Expr, Token![,]>,
expression: Option<TestCaseExpression>,
comment: Option<TestCaseComment>,
pub(crate) expression: Option<TestCaseExpression>,
pub(crate) comment: Option<TestCaseComment>,
}

impl Parse for TestCase {
Expand All @@ -24,6 +24,19 @@ impl Parse for TestCase {
}
}

impl<I> From<I> for TestCase
where
I: IntoIterator<Item = Expr>,
{
fn from(into_iter: I) -> Self {
Self {
args: into_iter.into_iter().collect(),
expression: None,
comment: None,
}
}
}

impl TestCase {
pub fn test_case_name(&self) -> Ident {
let case_desc = self
Expand Down
Loading
Loading