Skip to content

Commit 5df9018

Browse files
committed
feat: Add support for '#[should_panic]' macro
1 parent 35d5302 commit 5df9018

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

crates/libtest2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub mod _private {
6262

6363
pub use crate::_main_parse as main_parse;
6464
pub use crate::_parse_ignore as parse_ignore;
65+
pub use crate::_run_test as run_test;
6566
pub use crate::_test_parse as test_parse;
6667
pub use crate::case::DynCase;
6768
}

crates/libtest2/src/macros.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,57 @@ macro_rules! _test_parse {
2828
// Recursively handle attributes:
2929

3030
// Edge condition (no more attributes to parse)
31-
(continue: name=$name:ident body=[$($item:tt)*] attrs=[] $(ignore=$ignore:tt)?) => {
31+
(continue: name=$name:ident body=[$($item:tt)*] attrs=[] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => {
3232
$crate::_private::test_parse!(break:
3333
name=$name
3434
body=[$($item)*]
3535
$(ignore=$ignore)?
36+
$(should_panic=$should_panic)?
3637
);
3738
};
3839
// Process `#[ignore]` macro (NOTE: This will only match if an `#[ignore]` macro has not already been parsed)
39-
(continue: name=$name:ident body=[$($item:tt)*] attrs=[#[ignore $(= $reason:literal)?] $(#[$($attr:tt)+])*]) => {
40+
(continue: name=$name:ident body=[$($item:tt)*] attrs=[#[ignore $(= $reason:literal)?] $(#[$($attr:tt)+])*] $(should_panic=$should_panic:tt)?) => {
4041
$crate::_private::test_parse!(continue:
4142
name=$name
4243
body=[$($item)*]
4344
attrs=[$(#[$($attr)*])*]
4445
ignore=[$($reason)?]
46+
$(should_panic=$should_panic)?
47+
);
48+
};
49+
// Process `#[should_panic]` macro (NOTE: This will only match if `#[should_panic]` macro has not already been parsed)
50+
(continue: name=$name:ident body=[$($item:tt)*] attrs=[#[should_panic $(= $expected:literal)?] $(#[$($attr:tt)+])*] $(ignore=$ignore:tt)?) => {
51+
$crate::_private::test_parse!(continue:
52+
name=$name
53+
body=[$($item)*]
54+
attrs=[$(#[$($attr)*])*]
55+
$(ignore=$ignore)?
56+
should_panic=[$($expected)?]
57+
);
58+
};
59+
// Process `#[should_panic(expected = "..")]` macro (NOTE: Same as branch above)
60+
(continue: name=$name:ident body=[$($item:tt)*] attrs=[#[should_panic(expected = $expected:literal)] $(#[$($attr:tt)+])*] $(ignore=$ignore:tt)?) => {
61+
$crate::_private::test_parse!(continue:
62+
name=$name
63+
body=[$($item)*]
64+
attrs=[$(#[$($attr)*])*]
65+
$(ignore=$ignore)?
66+
should_panic=[$expected]
4567
);
4668
};
4769
// Discard unknown attributes
48-
(continue: name=$name:ident body=[$($item:tt)*] attrs=[#[$($unknown_attr:tt)+] $(#[$($attr:tt)+])*] $(ignore=$ignore:tt)?) => {
70+
(continue: name=$name:ident body=[$($item:tt)*] attrs=[#[$($unknown_attr:tt)+] $(#[$($attr:tt)+])*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => {
4971
$crate::_private::test_parse!(continue:
5072
name=$name
5173
body=[$($item)*]
5274
attrs=[$(#[$($attr)*])*]
5375
$(ignore=$ignore)?
76+
$(should_panic=$should_panic)?
5477
);
5578
};
5679

5780
// End result
58-
(break: name=$name:ident body=[$($item:tt)*] $(ignore=$ignore:tt)?) => {
81+
(break: name=$name:ident body=[$($item:tt)*] $(ignore=$ignore:tt)? $(should_panic=$should_panic:tt)?) => {
5982
#[allow(non_camel_case_types)]
6083
struct $name;
6184

@@ -83,7 +106,7 @@ macro_rules! _test_parse {
83106
)?
84107

85108
use $crate::IntoRunResult;
86-
let result = run(context);
109+
let result = $crate::_private::run_test!(context, $($should_panic)?);
87110
IntoRunResult::into_run_result(result)
88111
}
89112
}
@@ -99,3 +122,16 @@ macro_rules! _parse_ignore {
99122
$context.ignore()
100123
};
101124
}
125+
126+
#[macro_export]
127+
macro_rules! _run_test {
128+
($context:expr, [$expected:literal]) => {
129+
$crate::assert_panic_contains(|| run($context), $expected)
130+
};
131+
($context:expr, []) => {
132+
$crate::assert_panic(|| run($context))
133+
};
134+
($context:expr $(,)?) => {{
135+
run($context)
136+
}};
137+
}

0 commit comments

Comments
 (0)