From 56e39d260ce5ac948922a05dda30f1e712303076 Mon Sep 17 00:00:00 2001 From: Herman Skogseth Date: Mon, 15 Dec 2025 12:12:35 +0100 Subject: [PATCH 1/2] feat(test2): Support empty parameter list in `#[test]` This supports using the `test` attribute on function with no parameters: ``` #[libtest2::test] fn simple_test() { ... } ``` --- crates/libtest2/src/lib.rs | 1 + crates/libtest2/src/macros.rs | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/libtest2/src/lib.rs b/crates/libtest2/src/lib.rs index 17634d2..bab4e9f 100644 --- a/crates/libtest2/src/lib.rs +++ b/crates/libtest2/src/lib.rs @@ -66,6 +66,7 @@ pub mod _private { pub use crate::_main_parse as main_parse; pub use crate::_parse_ignore as parse_ignore; pub use crate::_run_test as run_test; + pub use crate::_test_expr as test_expr; pub use crate::_test_parse as test_parse; pub use crate::case::DynCase; } diff --git a/crates/libtest2/src/macros.rs b/crates/libtest2/src/macros.rs index 9441dbb..0afed57 100644 --- a/crates/libtest2/src/macros.rs +++ b/crates/libtest2/src/macros.rs @@ -102,7 +102,7 @@ macro_rules! _test_parse { }; // End result - (break: name=$name:ident body=[$($item:tt)*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => { + (break: name=$name:ident body=[($($params:tt)*) $($item:tt)*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => { #[allow(non_camel_case_types)] struct $name; @@ -125,12 +125,12 @@ macro_rules! _test_parse { } fn run(&self, context: &$crate::TestContext) -> $crate::RunResult { - fn run $($item)* + fn run($($params)*) $($item)* $crate::_private::parse_ignore!(context, $($ignore)?); use $crate::IntoRunResult; - let result = $crate::_private::run_test!(context, $($should_panic)?); + let result = $crate::_private::run_test!($crate::_private::test_expr!(context, [$($params)*]), $($should_panic)?); IntoRunResult::into_run_result(result) } } @@ -151,14 +151,25 @@ macro_rules! _parse_ignore { #[macro_export] #[doc(hidden)] -macro_rules! _run_test { - ($context:expr, [$expected:literal]) => { - $crate::panic::assert_panic_contains(|| run($context), $expected) - }; +macro_rules! _test_expr { ($context:expr, []) => { - $crate::panic::assert_panic(|| run($context)) + run() }; - ($context:expr $(,)?) => {{ + ($context:expr, [$($params:tt)+]) => { run($context) + }; +} + +#[macro_export] +#[doc(hidden)] +macro_rules! _run_test { + ($test:expr, [$expected:literal]) => { + $crate::panic::assert_panic_contains(|| $test, $expected) + }; + ($test:expr, []) => { + $crate::panic::assert_panic(|| $test) + }; + ($test:expr $(,)?) => {{ + $test }}; } From 6bbfaf55845586b6e05629625409e51a55a4ed3d Mon Sep 17 00:00:00 2001 From: Herman Skogseth Date: Mon, 15 Dec 2025 11:13:54 +0100 Subject: [PATCH 2/2] test(test2): Add more tests for attribute macros These tests aim to specifically test that the macros accept various edge cases that a user might expect us to support. This includes: - No parameters - Return result (with and without context as a parameter) - Include context as a parameter, but ignore it - Include context as a parameter and pattern match on it --- crates/libtest2/tests/testsuite/macros.rs | 36 ++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/libtest2/tests/testsuite/macros.rs b/crates/libtest2/tests/testsuite/macros.rs index 37b439c..19f50a8 100644 --- a/crates/libtest2/tests/testsuite/macros.rs +++ b/crates/libtest2/tests/testsuite/macros.rs @@ -16,6 +16,28 @@ mod some_module { #[libtest2::test] fn foo(_context: &libtest2::TestContext) {} } + +#[libtest2::test] +fn takes_context(_context: &libtest2::TestContext) {} + +#[libtest2::test] +fn no_parameters() {} + +#[libtest2::test] +fn takes_context_return_result(_context: &libtest2::TestContext) -> libtest2::RunResult { + Ok(()) +} + +#[libtest2::test] +fn no_parameters_return_result() -> libtest2::RunResult { + Ok(()) +} + +#[libtest2::test] +fn ignored_context(_: &libtest2::TestContext) {} + +#[libtest2::test] +fn context_as_pattern(libtest2::TestContext { .. }: &libtest2::TestContext) {} "#, false, ); @@ -29,11 +51,17 @@ mod some_module { fn check() { let data = str![[r#" -running 2 tests -test foo ... ok -test some_module::foo ... ok +running 8 tests +test context_as_pattern ... ok +test foo ... ok +test ignored_context ... ok +test no_parameters ... ok +test no_parameters_return_result ... ok +test some_module::foo ... ok +test takes_context ... ok +test takes_context_return_result ... ok -test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s +test result: ok. 8 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s "#]];