Skip to content

Commit 6110d3e

Browse files
authored
Rollup merge of rust-lang#66503 - thomasetter:panic-error-msg, r=joshtriplett
More useful test error messages on should_panic(expected=...) mismatch Fixes rust-lang#66304 r? @gilescope Shows both the actual as well as the expected panic value when a test with `should_panic(expected=...)` fails. This makes `should_panic` more consistent with `assert_eq`. I am not sure whether printing the `Any::type_id()` is useful, is there something better that we could print for non-string panic values?
2 parents 135ccba + 16bf4f5 commit 6110d3e

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/libtest/test_result.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,30 @@ pub fn calc_result<'a>(
3737
let result = match (&desc.should_panic, task_result) {
3838
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
3939
(&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
40-
if err
40+
let maybe_panic_str = err
4141
.downcast_ref::<String>()
4242
.map(|e| &**e)
43-
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
44-
.map(|e| e.contains(msg))
45-
.unwrap_or(false)
46-
{
43+
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e));
44+
45+
if maybe_panic_str.map(|e| e.contains(msg)).unwrap_or(false) {
4746
TestResult::TrOk
47+
} else if desc.allow_fail {
48+
TestResult::TrAllowedFail
49+
} else if let Some(panic_str) = maybe_panic_str {
50+
TestResult::TrFailedMsg(format!(
51+
r#"panic did not contain expected string
52+
panic message: `{:?}`,
53+
expected substring: `{:?}`"#,
54+
panic_str, msg
55+
))
4856
} else {
49-
if desc.allow_fail {
50-
TestResult::TrAllowedFail
51-
} else {
52-
TestResult::TrFailedMsg(
53-
format!("panic did not include expected string '{}'", msg)
54-
)
55-
}
57+
TestResult::TrFailedMsg(format!(
58+
r#"expected panic with string value,
59+
found non-string value: `{:?}`
60+
expected substring: `{:?}`"#,
61+
(**err).type_id(),
62+
msg
63+
))
5664
}
5765
}
5866
(&ShouldPanic::Yes, Ok(())) => {

src/libtest/tests.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
// TestType, TrFailedMsg, TrIgnored, TrOk,
1616
},
1717
};
18+
use std::any::TypeId;
1819
use std::sync::mpsc::channel;
1920
use std::time::Duration;
2021

@@ -84,7 +85,7 @@ pub fn do_not_run_ignored_tests() {
8485
let (tx, rx) = channel();
8586
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
8687
let result = rx.recv().unwrap().result;
87-
assert!(result != TrOk);
88+
assert_ne!(result, TrOk);
8889
}
8990

9091
#[test]
@@ -103,7 +104,7 @@ pub fn ignored_tests_result_in_ignored() {
103104
let (tx, rx) = channel();
104105
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
105106
let result = rx.recv().unwrap().result;
106-
assert!(result == TrIgnored);
107+
assert_eq!(result, TrIgnored);
107108
}
108109

109110
// FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251)
@@ -126,7 +127,7 @@ fn test_should_panic() {
126127
let (tx, rx) = channel();
127128
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
128129
let result = rx.recv().unwrap().result;
129-
assert!(result == TrOk);
130+
assert_eq!(result, TrOk);
130131
}
131132

132133
// FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251)
@@ -149,7 +150,7 @@ fn test_should_panic_good_message() {
149150
let (tx, rx) = channel();
150151
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
151152
let result = rx.recv().unwrap().result;
152-
assert!(result == TrOk);
153+
assert_eq!(result, TrOk);
153154
}
154155

155156
// FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251)
@@ -161,7 +162,9 @@ fn test_should_panic_bad_message() {
161162
panic!("an error message");
162163
}
163164
let expected = "foobar";
164-
let failed_msg = "panic did not include expected string";
165+
let failed_msg = r#"panic did not contain expected string
166+
panic message: `"an error message"`,
167+
expected substring: `"foobar"`"#;
165168
let desc = TestDescAndFn {
166169
desc: TestDesc {
167170
name: StaticTestName("whatever"),
@@ -175,7 +178,35 @@ fn test_should_panic_bad_message() {
175178
let (tx, rx) = channel();
176179
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
177180
let result = rx.recv().unwrap().result;
178-
assert!(result == TrFailedMsg(format!("{} '{}'", failed_msg, expected)));
181+
assert_eq!(result, TrFailedMsg(failed_msg.to_string()));
182+
}
183+
184+
// FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251)
185+
#[test]
186+
#[cfg(not(target_os = "emscripten"))]
187+
fn test_should_panic_non_string_message_type() {
188+
use crate::tests::TrFailedMsg;
189+
fn f() {
190+
panic!(1i32);
191+
}
192+
let expected = "foobar";
193+
let failed_msg = format!(r#"expected panic with string value,
194+
found non-string value: `{:?}`
195+
expected substring: `"foobar"`"#, TypeId::of::<i32>());
196+
let desc = TestDescAndFn {
197+
desc: TestDesc {
198+
name: StaticTestName("whatever"),
199+
ignore: false,
200+
should_panic: ShouldPanic::YesWithMessage(expected),
201+
allow_fail: false,
202+
test_type: TestType::Unknown,
203+
},
204+
testfn: DynTestFn(Box::new(f)),
205+
};
206+
let (tx, rx) = channel();
207+
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
208+
let result = rx.recv().unwrap().result;
209+
assert_eq!(result, TrFailedMsg(failed_msg));
179210
}
180211

181212
// FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251)
@@ -196,7 +227,7 @@ fn test_should_panic_but_succeeds() {
196227
let (tx, rx) = channel();
197228
run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No);
198229
let result = rx.recv().unwrap().result;
199-
assert!(result == TrFailedMsg("test did not panic as expected".to_string()));
230+
assert_eq!(result, TrFailedMsg("test did not panic as expected".to_string()));
200231
}
201232

202233
fn report_time_test_template(report_time: bool) -> Option<TestExecTime> {
@@ -570,7 +601,7 @@ pub fn sort_tests() {
570601
];
571602

572603
for (a, b) in expected.iter().zip(filtered) {
573-
assert!(*a == b.desc.name.to_string());
604+
assert_eq!(*a, b.desc.name.to_string());
574605
}
575606
}
576607

0 commit comments

Comments
 (0)