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 option to use return type in tests #76

Merged
merged 6 commits into from
Oct 12, 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
11 changes: 4 additions & 7 deletions .github/workflows/msrv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@ jobs:
uses: dtolnay/rust-toolchain@stable

- name: install_cargo_msrv
run: cargo install --version 0.16.0-beta.11 --no-default-features cargo-msrv
run: cargo install --version 0.16.0-beta.15 --no-default-features cargo-msrv

- name: version_of_cargo_msrv
run: cargo msrv --version

- name: show_msrv
run: export CURRENT_MSRV="$(cargo msrv show --output-format minimal)"
run: cargo msrv show --output-format minimal

- name: use_msrv_lock_file
run: cp Cargo.lock.msrv Cargo.lock

- name: run_cargo_msrv
run: cargo msrv --output-format json verify -- cargo check
run: cargo msrv --output-format json verify -- cargo check --frozen

- name: run_cargo_msrv_on_verify_failure
if: ${{ failure() }}
run: cargo msrv --output-format json -- cargo check

- name: test_msrv # The MSRV is an older Rust compiler version, which produces slightly different error messages
run: cargo +$CURRENT_MSRV test --verbose --locked --features square-brackets-old-error-message
run: cargo msrv --output-format json -- cargo check --frozen
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ which provides an attribute macro, to be used for parameterized testing.
If you found an issue, have a suggestion or want to provide feedback or insights, please feel free to open an issue on
the [issue tracker](https://github.com/foresterre/parameterized/issues), or open a topic in the [discussions section](https://github.com/foresterre/parameterized/discussions).

## [1.1.0] - 2023-10-13

### Added

* `parameterized-macro` now supports test signatures with any return type supported by the test macro, not just the unit type

**Example**

```rust
use parameterized::parameterized;

#[parameterized(v = { Ok(1), Err("Oh noes".to_string()) })]
fn my_test(v: Result<u32, String>) -> Result<(), String> {
let value = v?; // Can use the question mark operator here, since return type is Result, which implements the Termination trait

assert_eq!(value, 1);

Ok(())
}
```

### Changed

* Updated MSRV to Rust `1.56`
* Updated parameterized-macro to `1.1.0`

[1.1.0]: https://github.com/foresterre/parameterized/releases/tag/v1.1.0

## [1.0.1] - 2022-11-09

### Changed
Expand Down
91 changes: 52 additions & 39 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "parameterized"
version = "1.0.1"
authors = ["Martijn Gribnau <[email protected]>"]
edition = "2018"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Procedural macro which brings a compact parameterized testing implementation to Rust (inspired by JUnit @ParameterizedTest)"
documentation = "https://docs.rs/crate/parameterized"
Expand All @@ -12,7 +12,7 @@ keywords = ["parameterized", "parametrized", "test", "unit-test", "junit"]
categories = ["development-tools", "development-tools::testing"]

[package.metadata]
msrv = "1.38.0"
msrv = "1.56.0"

[dependencies]
parameterized-macro = { path = "parameterized-macro", version = "1" }
Expand All @@ -22,4 +22,4 @@ members = ["parameterized-macro"]

[features]
# not semver protected
square-brackets-old-error-message = ["parameterized-macro/square-brackets-old-error-message"]
square-brackets-old-error-message = ["parameterized-macro/square-brackets-old-error-message"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::{Span, TokenStream};

use crate::impls::map::TestCases;
use crate::impls::{map, AttributeArgList};
use crate::parser::AttributeArgList;
use crate::test_cases::TestCases;

pub(crate) fn generate_test_cases(
argument_lists: AttributeArgList,
Expand Down Expand Up @@ -78,6 +78,7 @@ fn generate_test_case(
let vis = &f.vis;
let body_block = &f.block;
let identifier = syn::Ident::new(&format!("case_{}", i), Span::call_site());
let return_type = &f.sig.output;

// Construction let bindings for all parameters
let bindings = parameters.iter().map(|(identifier, ty)| {
Expand All @@ -89,7 +90,7 @@ fn generate_test_case(
quote::quote! {
#[test]
#(#attributes)*
#vis fn #identifier() {
#vis fn #identifier() #return_type {
#(#bindings)*

#body_block
Expand Down
15 changes: 5 additions & 10 deletions parameterized-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
extern crate syn;
extern crate proc_macro;

mod impls;
mod generation;
mod parser;
mod test_cases;

#[proc_macro_attribute]
pub fn parameterized(
args: ::proc_macro::TokenStream,
input: ::proc_macro::TokenStream,
) -> ::proc_macro::TokenStream {
impl_macro(args, input)
}

fn impl_macro(
args: ::proc_macro::TokenStream,
input: ::proc_macro::TokenStream,
) -> ::proc_macro::TokenStream {
let argument_lists = parse_macro_input!(args as impls::AttributeArgList);
let argument_lists = parse_macro_input!(args as parser::AttributeArgList);
let func = parse_macro_input!(input as ::syn::ItemFn);

impls::restructure::generate_test_cases(argument_lists, func)
generation::generate_test_cases(argument_lists, func)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub(crate) mod map;
pub(crate) mod restructure;

use std::fmt::Formatter;
use syn::braced;
use syn::parse::{Parse, ParseStream, Result};
Expand Down
3 changes: 3 additions & 0 deletions parameterized-macro/tests/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ fn individual_cases() {
t.pass("tests/ok/18_trailing_comma3.rs");
t.pass("tests/ok/19_trailing_comma4.rs");
t.pass("tests/ok/20_empty.rs");
t.pass("tests/ok/21_result.rs");
t.pass("tests/ok/22_try_operator.rs");
t.pass("tests/ok/23_return_not_impl_try.rs");

t.compile_fail("tests/fail/id_already_defined.rs");
t.compile_fail("tests/fail/inequal_amount_of_arg.rs");
Expand Down
8 changes: 8 additions & 0 deletions parameterized-macro/tests/ok/21_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use parameterized_macro::parameterized;

#[parameterized(v = { 1, 2 })]
fn my_test(v: i32) -> Result<(), ()> {
Ok(())
}

fn main() {}
9 changes: 9 additions & 0 deletions parameterized-macro/tests/ok/22_try_operator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use parameterized_macro::parameterized;

#[parameterized(v = { 1, 2 })]
fn my_test(v: i32) -> Result<(), ()> {
let unit = Err(())?;
Ok(unit)
}

fn main() {}
10 changes: 10 additions & 0 deletions parameterized-macro/tests/ok/23_return_not_impl_try.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use parameterized_macro::parameterized;

struct NotTry;

#[parameterized(_v = { 1, 2 })]
fn my_test(_v: i32) -> NotTry {
NotTry
}

fn main() {}
Loading
Loading