Skip to content

Commit

Permalink
Add support for the ? operator in parameterized tests, similar to Rus…
Browse files Browse the repository at this point in the history
…t 2018's support for Result-returning tests.

This also adds a dependency on anyhow, to simplify result propagation and avoid requiring users to specify a particular Result-type.

Due to rust-lang/rust#69517 we manually unwrap() the user-provided Result for the time being. When that issue is resolved the implementation can be simplified slightly.
  • Loading branch information
dimo414 committed Dec 4, 2022
1 parent ecb3b9f commit e800962
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 deletions.
24 changes: 17 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
name: Rust

on: [push]
on: [push, pull_request]

jobs:
build:

CI:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Build
run: cargo build --verbose
- name: Run tests
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-targets --all-features
- name: Check
run: cargo check --all-targets --verbose
- name: Check (no-default-features)
run: cargo check --all-targets --verbose --no-default-features
- name: Check Documentation
env:
RUSTDOCFLAGS: -D warnings
run: cargo doc --no-deps --document-private-items
- name: Tests
run: cargo test --verbose
- name: Tests (no-default-features)
run: cargo test --verbose --no-default-features
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ keywords = ["testing", "parameterized", "macro"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/dimo414/parameterized_test"
authors = ["Michael Diamond <[email protected]>"]
version = "0.1.0"
version = "0.2.0"
edition = "2018"
include = [
"**/*.rs",
"Cargo.toml",
"README.md",
]

[features]
default = ['propagation']
propagation = ['anyhow']

[dependencies.anyhow]
optional = true
version = "1.0"
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod tests {
}
```

This example accepts multiple parameters:
Tests can also specify multiple parameters:

```rust
use parameterized_test::create;
Expand All @@ -59,3 +59,24 @@ mod tests {
}
}
```

The `?` operator is also supported, similar to
[standalone tests](https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html#tests-and-):

```rust
use parameterized_test::create;

#[cfg(test)]
mod tests {
use super::*;

parameterized_test::create!{ socket, path, {
let socket: SocketAddr = fs::read_to_string(path)?.parse()?;
assert_eq!(socket.is_ipv6(), true);
}}
socket! {
relative: "foo.txt",
absolute: "/tmp/bar.txt",
}
}
```
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ macro_rules! __with_dollar_sign {
}
}

// Duplicate the create!() macro to optionally support Result.
// https://stackoverflow.com/a/63011109/113632 suggests using another macro to reduce the amount of
// duplication, but this macro is messy enough already I think the redundancy is easier to deal with
#[cfg(feature="propagation")]
#[macro_export]
macro_rules! create {
($name:ident, $args:pat, $body:tt) => {
$crate::__with_dollar_sign! {
($d:tt) => {
macro_rules! $name {
($d($d pname:ident: $d values:expr,)*) => {
mod $name {
#![ allow( unused_imports ) ]
use super::*;
$d(
#[test]
fn $d pname() -> anyhow::Result<()> {
// TODO(https://github.com/rust-lang/rust/issues/69517)
// although Rust 2018 supports Result-returning tests, the
// failure behavior is very poor. This helper function f() can
// be removed once Result tests are handled better. Demo:
// https://play.rust-lang.org/?gist=b1a4d7bf42c885f42598d872877f2504
fn f() -> anyhow::Result<()> {
let $args = $d values;
$body
Ok(())
}
f().unwrap();
Ok(())
}
)*
}}}}}}}

#[cfg(not(feature="propagation"))]
#[macro_export]
macro_rules! create {
($name:ident, $args:pat, $body:tt) => {
Expand Down Expand Up @@ -71,4 +105,11 @@ mod tests {
calls_helper! {
arg: true,
}

#[cfg(feature="propagation")]
create!{ propagation, n, { assert_eq!(n.to_string().parse::<i8>()? as i32, n); } }
#[cfg(feature="propagation")]
propagation! {
a: 100, // use a larger value, like 200, to see a parse error test failure
}
}

0 comments on commit e800962

Please sign in to comment.