Skip to content

Commit be993be

Browse files
author
Thomas Etter
committed
print a more useful error message on should_panic mismatch
1 parent 0f0c640 commit be993be

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/libtest/test_result.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ 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))
43+
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e));
44+
45+
if maybe_panic_str
4446
.map(|e| e.contains(msg))
4547
.unwrap_or(false)
4648
{
@@ -49,9 +51,19 @@ pub fn calc_result<'a>(
4951
if desc.allow_fail {
5052
TestResult::TrAllowedFail
5153
} else {
52-
TestResult::TrFailedMsg(
53-
format!("panic did not include expected string '{}'", msg)
54-
)
54+
if let Some(panic_str) = maybe_panic_str{
55+
TestResult::TrFailedMsg(
56+
format!(r#"panic did not contain expected string
57+
panic message: `{:?}`,
58+
expected substring: `{:?}`"#, panic_str, &*msg)
59+
)
60+
} else {
61+
TestResult::TrFailedMsg(
62+
format!(r#"expected panic with string value,
63+
found non-string value: `{:?}`
64+
expected substring: `{:?}`"#, (**err).type_id(), &*msg)
65+
)
66+
}
5567
}
5668
}
5769
}

src/libtest/tests.rs

+33-2
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

@@ -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)

0 commit comments

Comments
 (0)