Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/libtest2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 20 additions & 9 deletions crates/libtest2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
}
}
Expand All @@ -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
}};
}
36 changes: 32 additions & 4 deletions crates/libtest2/tests/testsuite/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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


"#]];
Expand Down