]> git.lizzy.rs Git - rust.git/blobdiff - src/libtest/test_result.rs
print a more useful error message on should_panic mismatch
[rust.git] / src / libtest / test_result.rs
index 80ca9dea18f5aff3ab26b4d57964850ddd7315bc..5dbbd71554e984bcf9b6c11ebe311f6deaf669e5 100644 (file)
@@ -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)
+                        )
+                    }
                 }
             }
         }