Skip to content

Commit 2d7b1cc

Browse files
committed
fix: Add tests for '#[should_panic]' macro
1 parent 1dae3b5 commit 2d7b1cc

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

crates/libtest2/tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod all_passing;
22
mod argfile;
33
mod mixed_bag;
44
mod panic;
5+
mod should_panic;
56
mod util;
67

78
pub use util::*;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use snapbox::prelude::*;
2+
use snapbox::str;
3+
4+
fn test_cmd() -> snapbox::cmd::Command {
5+
static BIN: once_cell_polyfill::sync::OnceLock<(std::path::PathBuf, std::path::PathBuf)> =
6+
once_cell_polyfill::sync::OnceLock::new();
7+
let (bin, current_dir) = BIN.get_or_init(|| {
8+
let package_root = crate::util::new_test(
9+
r#"
10+
#[libtest2::main]
11+
fn main() {}
12+
13+
#[libtest2::test]
14+
fn accidentally_panics(_context: &libtest2::TestContext) {
15+
panic!("uh oh")
16+
}
17+
18+
#[libtest2::test]
19+
#[should_panic]
20+
fn intentionally_panics(_context: &libtest2::TestContext) {
21+
panic!("any message would do")
22+
}
23+
24+
#[libtest2::test]
25+
#[should_panic = "intentional"]
26+
fn intentionally_panics_with_message(_context: &libtest2::TestContext) {
27+
panic!("this is intentional")
28+
}
29+
30+
#[libtest2::test]
31+
#[should_panic = "in a controlled manner"]
32+
fn panics_with_the_wrong_message(_context: &libtest2::TestContext) {
33+
panic!("with the wrong message")
34+
}
35+
"#,
36+
false,
37+
);
38+
let bin = crate::util::compile_test(&package_root);
39+
(bin, package_root)
40+
});
41+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
42+
}
43+
44+
fn check(args: &[&str], code: i32, single: impl IntoData, parallel: impl IntoData) {
45+
test_cmd()
46+
.args(args)
47+
.args(["--test-threads", "1"])
48+
.assert()
49+
.code(code)
50+
.stdout_eq(single);
51+
test_cmd()
52+
.args(args)
53+
.assert()
54+
.code(code)
55+
.stdout_eq(parallel);
56+
}
57+
58+
#[test]
59+
fn normal() {
60+
check(
61+
&[],
62+
101,
63+
str![[r#"
64+
65+
running 4 tests
66+
test accidentally_panics ... FAILED
67+
test intentionally_panics ... ok
68+
test intentionally_panics_with_message ... ok
69+
test panics_with_the_wrong_message ... FAILED
70+
71+
failures:
72+
73+
---- accidentally_panics ----
74+
test panicked: uh oh
75+
76+
---- panics_with_the_wrong_message ----
77+
panic did not contain expected string
78+
panic message: "with the wrong message"
79+
expected substring: "in a controlled manner"
80+
81+
82+
failures:
83+
accidentally_panics
84+
panics_with_the_wrong_message
85+
86+
test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 filtered out; finished in [..]s
87+
88+
89+
"#]],
90+
str![[r#"
91+
92+
running 4 tests
93+
...
94+
95+
failures:
96+
97+
---- accidentally_panics ----
98+
test panicked: uh oh
99+
100+
---- panics_with_the_wrong_message ----
101+
panic did not contain expected string
102+
panic message: "with the wrong message"
103+
expected substring: "in a controlled manner"
104+
105+
106+
failures:
107+
accidentally_panics
108+
panics_with_the_wrong_message
109+
110+
test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 filtered out; finished in [..]s
111+
112+
113+
"#]],
114+
);
115+
}

0 commit comments

Comments
 (0)