Skip to content

Commit 56e39d2

Browse files
committed
feat(test2): Support empty parameter list in #[test]
This supports using the `test` attribute on function with no parameters: ``` #[libtest2::test] fn simple_test() { ... } ```
1 parent 3f6eb77 commit 56e39d2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

crates/libtest2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub mod _private {
6666
pub use crate::_main_parse as main_parse;
6767
pub use crate::_parse_ignore as parse_ignore;
6868
pub use crate::_run_test as run_test;
69+
pub use crate::_test_expr as test_expr;
6970
pub use crate::_test_parse as test_parse;
7071
pub use crate::case::DynCase;
7172
}

crates/libtest2/src/macros.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ macro_rules! _test_parse {
102102
};
103103

104104
// End result
105-
(break: name=$name:ident body=[$($item:tt)*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => {
105+
(break: name=$name:ident body=[($($params:tt)*) $($item:tt)*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => {
106106
#[allow(non_camel_case_types)]
107107
struct $name;
108108

@@ -125,12 +125,12 @@ macro_rules! _test_parse {
125125
}
126126

127127
fn run(&self, context: &$crate::TestContext) -> $crate::RunResult {
128-
fn run $($item)*
128+
fn run($($params)*) $($item)*
129129

130130
$crate::_private::parse_ignore!(context, $($ignore)?);
131131

132132
use $crate::IntoRunResult;
133-
let result = $crate::_private::run_test!(context, $($should_panic)?);
133+
let result = $crate::_private::run_test!($crate::_private::test_expr!(context, [$($params)*]), $($should_panic)?);
134134
IntoRunResult::into_run_result(result)
135135
}
136136
}
@@ -151,14 +151,25 @@ macro_rules! _parse_ignore {
151151

152152
#[macro_export]
153153
#[doc(hidden)]
154-
macro_rules! _run_test {
155-
($context:expr, [$expected:literal]) => {
156-
$crate::panic::assert_panic_contains(|| run($context), $expected)
157-
};
154+
macro_rules! _test_expr {
158155
($context:expr, []) => {
159-
$crate::panic::assert_panic(|| run($context))
156+
run()
160157
};
161-
($context:expr $(,)?) => {{
158+
($context:expr, [$($params:tt)+]) => {
162159
run($context)
160+
};
161+
}
162+
163+
#[macro_export]
164+
#[doc(hidden)]
165+
macro_rules! _run_test {
166+
($test:expr, [$expected:literal]) => {
167+
$crate::panic::assert_panic_contains(|| $test, $expected)
168+
};
169+
($test:expr, []) => {
170+
$crate::panic::assert_panic(|| $test)
171+
};
172+
($test:expr $(,)?) => {{
173+
$test
163174
}};
164175
}

0 commit comments

Comments
 (0)