diff options
| author | Thomas Etter <thomas.etter@auterion.com> | 2019-11-18 00:51:18 +0100 |
|---|---|---|
| committer | Thomas Etter <thomas.etter@auterion.com> | 2019-11-18 00:51:18 +0100 |
| commit | be993beb0b0294c6ce4519bf007f73063187809e (patch) | |
| tree | c1649cba1eeac08a26c0d6b2de516b1efa7a2b1a /src | |
| parent | 0f0c640e0ee5a9ad365e78e3c62239b3d65b7045 (diff) | |
print a more useful error message on should_panic mismatch
Diffstat (limited to 'src')
| -rw-r--r-- | src/libtest/test_result.rs | 22 | ||||
| -rw-r--r-- | src/libtest/tests.rs | 35 |
2 files changed, 50 insertions, 7 deletions
diff --git a/src/libtest/test_result.rs b/src/libtest/test_result.rs index 80ca9dea18f..5dbbd71554e 100644 --- a/src/libtest/test_result.rs +++ b/src/libtest/test_result.rs @@ -37,10 +37,12 @@ pub fn calc_result<'a>( let result = match (&desc.should_panic, task_result) { (&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk, (&ShouldPanic::YesWithMessage(msg), Err(ref err)) => { - if err + let maybe_panic_str = err .downcast_ref::<String>() .map(|e| &**e) - .or_else(|| err.downcast_ref::<&'static str>().map(|e| *e)) + .or_else(|| err.downcast_ref::<&'static str>().map(|e| *e)); + + if maybe_panic_str .map(|e| e.contains(msg)) .unwrap_or(false) { @@ -49,9 +51,19 @@ pub fn calc_result<'a>( if desc.allow_fail { TestResult::TrAllowedFail } else { - TestResult::TrFailedMsg( - format!("panic did not include expected string '{}'", msg) - ) + if let Some(panic_str) = maybe_panic_str{ + TestResult::TrFailedMsg( + format!(r#"panic did not contain expected string + panic message: `{:?}`, + expected substring: `{:?}`"#, panic_str, &*msg) + ) + } else { + TestResult::TrFailedMsg( + format!(r#"expected panic with string value, + found non-string value: `{:?}` + expected substring: `{:?}`"#, (**err).type_id(), &*msg) + ) + } } } } diff --git a/src/libtest/tests.rs b/src/libtest/tests.rs index 5f55b647f5e..fc82bb4f47a 100644 --- a/src/libtest/tests.rs +++ b/src/libtest/tests.rs @@ -15,6 +15,7 @@ use crate::{ // TestType, TrFailedMsg, TrIgnored, TrOk, }, }; +use std::any::TypeId; use std::sync::mpsc::channel; use std::time::Duration; @@ -161,7 +162,9 @@ fn test_should_panic_bad_message() { panic!("an error message"); } let expected = "foobar"; - let failed_msg = "panic did not include expected string"; + let failed_msg = r#"panic did not contain expected string + panic message: `"an error message"`, + expected substring: `"foobar"`"#; let desc = TestDescAndFn { desc: TestDesc { name: StaticTestName("whatever"), @@ -175,7 +178,35 @@ fn test_should_panic_bad_message() { let (tx, rx) = channel(); run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No); let result = rx.recv().unwrap().result; - assert!(result == TrFailedMsg(format!("{} '{}'", failed_msg, expected))); + assert_eq!(result, TrFailedMsg(failed_msg.to_string())); +} + +// FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251) +#[test] +#[cfg(not(target_os = "emscripten"))] +fn test_should_panic_non_string_message_type() { + use crate::tests::TrFailedMsg; + fn f() { + panic!(1i32); + } + let expected = "foobar"; + let failed_msg = format!(r#"expected panic with string value, + found non-string value: `{:?}` + expected substring: `"foobar"`"#, TypeId::of::<i32>()); + let desc = TestDescAndFn { + desc: TestDesc { + name: StaticTestName("whatever"), + ignore: false, + should_panic: ShouldPanic::YesWithMessage(expected), + allow_fail: false, + test_type: TestType::Unknown, + }, + testfn: DynTestFn(Box::new(f)), + }; + let (tx, rx) = channel(); + run_test(&TestOpts::new(), false, desc, RunStrategy::InProcess, tx, Concurrent::No); + let result = rx.recv().unwrap().result; + assert_eq!(result, TrFailedMsg(failed_msg)); } // FIXME: Re-enable emscripten once it can catch panics again (introduced by #65251) |
