Skip to content

Commit 08e02cd

Browse files
authored
feat(test2): Support empty parameter list in #[test] (#164)
This adds support for using the `#[test]` attribute on functions with no parameters (previously these tests had to take the `TestContext` parameter as the only argument). So code like this should now work as expected: ``` #[libtest2::test] fn simple_test() { assert_eq!(2 + 2, 4); } ``` Other parameter combinations are not supported. Part of #13.
2 parents 3f6eb77 + 6bbfaf5 commit 08e02cd

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
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
}

crates/libtest2/tests/testsuite/macros.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ mod some_module {
1616
#[libtest2::test]
1717
fn foo(_context: &libtest2::TestContext) {}
1818
}
19+
20+
#[libtest2::test]
21+
fn takes_context(_context: &libtest2::TestContext) {}
22+
23+
#[libtest2::test]
24+
fn no_parameters() {}
25+
26+
#[libtest2::test]
27+
fn takes_context_return_result(_context: &libtest2::TestContext) -> libtest2::RunResult {
28+
Ok(())
29+
}
30+
31+
#[libtest2::test]
32+
fn no_parameters_return_result() -> libtest2::RunResult {
33+
Ok(())
34+
}
35+
36+
#[libtest2::test]
37+
fn ignored_context(_: &libtest2::TestContext) {}
38+
39+
#[libtest2::test]
40+
fn context_as_pattern(libtest2::TestContext { .. }: &libtest2::TestContext) {}
1941
"#,
2042
false,
2143
);
@@ -29,11 +51,17 @@ mod some_module {
2951
fn check() {
3052
let data = str![[r#"
3153
32-
running 2 tests
33-
test foo ... ok
34-
test some_module::foo ... ok
54+
running 8 tests
55+
test context_as_pattern ... ok
56+
test foo ... ok
57+
test ignored_context ... ok
58+
test no_parameters ... ok
59+
test no_parameters_return_result ... ok
60+
test some_module::foo ... ok
61+
test takes_context ... ok
62+
test takes_context_return_result ... ok
3563
36-
test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s
64+
test result: ok. 8 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s
3765
3866
3967
"#]];

0 commit comments

Comments
 (0)