Skip to content

Commit de4ad2a

Browse files
committed
fix(test2): Maintain fn path with #[test] macro
1 parent 07e84b5 commit de4ad2a

3 files changed

Lines changed: 56 additions & 45 deletions

File tree

crates/libtest2/src/macros.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,37 +103,38 @@ macro_rules! _test_parse {
103103

104104
// End result
105105
(break: name=$name:ident body=[($($params:tt)*) $($item:tt)*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => {
106-
#[allow(non_camel_case_types)]
107-
struct $name;
106+
fn $name($($params)*) $($item)*
108107

109-
impl $crate::Case for $name {
110-
fn name(&self) -> &str {
111-
$crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&$name));
108+
const _: () = const {
109+
struct Test;
110+
111+
impl $crate::Case for Test {
112+
fn name(&self) -> &str {
113+
$crate::_private::push!(crate::TESTS, _: $crate::_private::DynCase = $crate::_private::DynCase(&Test));
112114

113-
const FULL_PATH: &str = concat!(std::module_path!(), "::", stringify!($name));
114-
let i = FULL_PATH.find("::").expect("we have inserted this in the line above so it must be there");
115-
&FULL_PATH[(i+2)..]
116-
}
117-
fn kind(&self) -> $crate::_private::TestKind {
118-
Default::default()
119-
}
120-
fn source(&self) -> Option<&$crate::_private::Source> {
121-
None
122-
}
123-
fn exclusive(&self, _: &$crate::TestContext) -> bool {
124-
false
125-
}
126-
127-
fn run(&self, context: &$crate::TestContext) -> $crate::RunResult {
128-
fn run($($params)*) $($item)*
115+
const FULL_PATH: &str = concat!(std::module_path!(), "::", stringify!($name));
116+
let i = FULL_PATH.find("::").expect("we have inserted this in the line above so it must be there");
117+
&FULL_PATH[(i+2)..]
118+
}
119+
fn kind(&self) -> $crate::_private::TestKind {
120+
Default::default()
121+
}
122+
fn source(&self) -> Option<&$crate::_private::Source> {
123+
None
124+
}
125+
fn exclusive(&self, _: &$crate::TestContext) -> bool {
126+
false
127+
}
129128

130-
$crate::_private::parse_ignore!(context, $($ignore)?);
129+
fn run(&self, context: &$crate::TestContext) -> $crate::RunResult {
130+
$crate::_private::parse_ignore!(context, $($ignore)?);
131131

132-
use $crate::IntoRunResult;
133-
let result = $crate::_private::run_test!($crate::_private::test_expr!(context, [$($params)*]), $($should_panic)?);
134-
IntoRunResult::into_run_result(result)
132+
use $crate::IntoRunResult;
133+
let result = $crate::_private::run_test!($crate::_private::test_expr!($name, context, [$($params)*]), $($should_panic)?);
134+
IntoRunResult::into_run_result(result)
135+
}
135136
}
136-
}
137+
};
137138
};
138139
}
139140

@@ -152,11 +153,11 @@ macro_rules! _parse_ignore {
152153
#[macro_export]
153154
#[doc(hidden)]
154155
macro_rules! _test_expr {
155-
($context:expr, []) => {
156-
run()
156+
($name:ident, $context:expr, []) => {
157+
$name()
157158
};
158-
($context:expr, [$($params:tt)+]) => {
159-
run($context)
159+
($name:ident, $context:expr, [$($params:tt)+]) => {
160+
$name($context)
160161
};
161162
}
162163

crates/libtest2/tests/testsuite/macros.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ fn foo(_context: &libtest2::TestContext) {}
1515
mod some_module {
1616
#[libtest2::test]
1717
fn foo(_context: &libtest2::TestContext) {}
18+
19+
#[libtest2::test]
20+
fn can_call_foo(context: &libtest2::TestContext) {
21+
foo(context);
22+
}
23+
24+
#[libtest2::test]
25+
fn can_call_super_foo(context: &libtest2::TestContext) {
26+
super::foo(context);
27+
}
1828
}
1929
2030
#[libtest2::test]
@@ -51,17 +61,19 @@ fn context_as_pattern(libtest2::TestContext { .. }: &libtest2::TestContext) {}
5161
fn check() {
5262
let data = str![[r#"
5363
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
63-
64-
test result: ok. 8 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s
64+
running 10 tests
65+
test context_as_pattern ... ok
66+
test foo ... ok
67+
test ignored_context ... ok
68+
test no_parameters ... ok
69+
test no_parameters_return_result ... ok
70+
test some_module::can_call_foo ... ok
71+
test some_module::can_call_super_foo ... ok
72+
test some_module::foo ... ok
73+
test takes_context ... ok
74+
test takes_context_return_result ... ok
75+
76+
test result: ok. 10 passed; 0 failed; 0 ignored; 0 filtered out; finished in [..]s
6577
6678
6779
"#]];

crates/libtest2/tests/ui/unsupported_parameter.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ error[E0308]: mismatched types
88
| arguments to this function are incorrect
99
|
1010
note: function defined here
11-
--> tests/ui/unsupported_parameter.rs:4:1
11+
--> tests/ui/unsupported_parameter.rs:5:4
1212
|
13-
4 | #[libtest2::test]
14-
| ^^^^^^^^^^^^^^^^^
1513
5 | fn takes_integer(_: i32) {}
16-
| ------
14+
| ^^^^^^^^^^^^^ ------
1715
= note: this error originates in the macro `$crate::_private::test_parse` which comes from the expansion of the attribute macro `libtest2::test` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)