Skip to content

Commit 0ff7c92

Browse files
committed
fix: Add tests for '#[should_panic]' macro
1 parent 165e904 commit 0ff7c92

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
#[libtest2::test]
37+
#[should_panic]
38+
#[should_panic = "very long and specific message"]
39+
fn passes_because_subsequent_panics_are_ignored(_context: &libtest2::TestContext) {
40+
panic!("any message should be valid")
41+
}
42+
"#,
43+
false,
44+
);
45+
let bin = crate::util::compile_test(&package_root);
46+
(bin, package_root)
47+
});
48+
snapbox::cmd::Command::new(bin).current_dir(current_dir)
49+
}
50+
51+
fn check(args: &[&str], code: i32, single: impl IntoData, parallel: impl IntoData) {
52+
test_cmd()
53+
.args(args)
54+
.args(["--test-threads", "1"])
55+
.assert()
56+
.code(code)
57+
.stdout_eq(single);
58+
test_cmd()
59+
.args(args)
60+
.assert()
61+
.code(code)
62+
.stdout_eq(parallel);
63+
}
64+
65+
#[test]
66+
fn normal() {
67+
check(
68+
&[],
69+
101,
70+
str![[r#"
71+
72+
running 5 tests
73+
test accidentally_panics ... FAILED
74+
test intentionally_panics ... ok
75+
test intentionally_panics_with_message ... ok
76+
test panics_with_the_wrong_message ... FAILED
77+
test passes_because_subsequent_panics_are_ignored ... ok
78+
79+
failures:
80+
81+
---- accidentally_panics ----
82+
test panicked: uh oh
83+
84+
---- panics_with_the_wrong_message ----
85+
panic did not contain expected string
86+
panic message: "with the wrong message"
87+
expected substring: "in a controlled manner"
88+
89+
90+
failures:
91+
accidentally_panics
92+
panics_with_the_wrong_message
93+
94+
test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 filtered out; finished in [..]s
95+
96+
97+
"#]],
98+
str![[r#"
99+
100+
running 5 tests
101+
...
102+
103+
failures:
104+
105+
---- accidentally_panics ----
106+
test panicked: uh oh
107+
108+
---- panics_with_the_wrong_message ----
109+
panic did not contain expected string
110+
panic message: "with the wrong message"
111+
expected substring: "in a controlled manner"
112+
113+
114+
failures:
115+
accidentally_panics
116+
panics_with_the_wrong_message
117+
118+
test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 filtered out; finished in [..]s
119+
120+
121+
"#]],
122+
);
123+
}

0 commit comments

Comments
 (0)