Skip to content

Commit

Permalink
feat: add option to use return type in tests
Browse files Browse the repository at this point in the history
Especially useful for types which use the try operator, `?` i.e. Result or Option, such that you can use ? instead of unwrapping everything. Accepted by the #[test] macro
  • Loading branch information
foresterre committed Oct 12, 2023
1 parent 6fd232d commit e26d00e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion parameterized-macro/src/generation.rs
Original file line number Diff line number Diff line change
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
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() {}
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,19 @@ mod tests {
input.unwrap()
}
}

mod result {
use super::*;

ide!();

#[pm(input = { 2, 3, 4 }, output = { 2, 3, 4 })]
fn ok(input: i32, output: i32) -> Result<(), ()> {
let result = Ok(input)?;

assert_eq!(result, output);

Ok(())
}
}
}

0 comments on commit e26d00e

Please sign in to comment.