Skip to content

Commit

Permalink
Rollup merge of rust-lang#80868 - johanngan:should-panic-msg-with-exp…
Browse files Browse the repository at this point in the history
…ected, r=m-ou-se

Print failure message on all tests that should panic, but don't

Fixes rust-lang#80861. Tests with the `#[should_panic]` attribute should always print a failure message if no panic occurs, regardless of whether or not an `expected` panic message is specified.
  • Loading branch information
Dylan-DPC authored Jan 27, 2021
2 parents ccbd311 + b43aa96 commit f37d9d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion library/test/src/test_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn calc_result<'a>(
))
}
}
(&ShouldPanic::Yes, Ok(())) => {
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
TestResult::TrFailedMsg("test did not panic as expected".to_string())
}
_ if desc.allow_fail => TestResult::TrAllowedFail,
Expand Down
39 changes: 24 additions & 15 deletions library/test/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,30 @@ fn test_should_panic_non_string_message_type() {
#[test]
#[cfg(not(target_os = "emscripten"))]
fn test_should_panic_but_succeeds() {
fn f() {}
let desc = TestDescAndFn {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic: ShouldPanic::Yes,
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("test did not panic as expected".to_string()));
let should_panic_variants = [ShouldPanic::Yes, ShouldPanic::YesWithMessage("error message")];

for &should_panic in should_panic_variants.iter() {
fn f() {}
let desc = TestDescAndFn {
desc: TestDesc {
name: StaticTestName("whatever"),
ignore: false,
should_panic,
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("test did not panic as expected".to_string()),
"should_panic == {:?}",
should_panic
);
}
}

fn report_time_test_template(report_time: bool) -> Option<TestExecTime> {
Expand Down

0 comments on commit f37d9d0

Please sign in to comment.