Skip to content

Commit 9c0909c

Browse files
authored
Rollup merge of rust-lang#138603 - xizheyin:issue-137405, r=chenyukang
Report line number of test when should_panic test failed Closing rust-lang#137405 r? `@joshka`
2 parents 8fb32ab + cc2ed5a commit 9c0909c

5 files changed

+108
-2
lines changed

Diff for: library/test/src/test_result.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ pub(crate) fn calc_result<'a>(
4545
time_opts: Option<&time::TestTimeOptions>,
4646
exec_time: Option<&time::TestExecTime>,
4747
) -> TestResult {
48+
let fn_location = if !desc.source_file.is_empty() {
49+
&format!(" at {}:{}:{}", desc.source_file, desc.start_line, desc.start_col)
50+
} else {
51+
""
52+
};
53+
4854
let result = match (&desc.should_panic, task_result) {
4955
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
5056
(&ShouldPanic::YesWithMessage(msg), Err(err)) => {
@@ -72,7 +78,7 @@ pub(crate) fn calc_result<'a>(
7278
}
7379
}
7480
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
75-
TestResult::TrFailedMsg("test did not panic as expected".to_string())
81+
TestResult::TrFailedMsg(format!("test did not panic as expected{}", fn_location))
7682
}
7783
_ => TestResult::TrFailed,
7884
};

Diff for: tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test $DIR/failed-doctest-should-panic.rs - Foo (line 10) - should panic ... FAIL
55
failures:
66

77
---- $DIR/failed-doctest-should-panic.rs - Foo (line 10) stdout ----
8-
note: test did not panic as expected
8+
note: test did not panic as expected at $DIR/failed-doctest-should-panic.rs:10:0
99

1010
failures:
1111
$DIR/failed-doctest-should-panic.rs - Foo (line 10)

Diff for: tests/ui/test-should-panic-failed-show-span.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@ run-fail
2+
//@ check-run-results
3+
//@ compile-flags: --test
4+
//@ run-flags: --test-threads=1
5+
6+
#[test]
7+
#[should_panic]
8+
fn should_panic_with_any_message() {
9+
panic!("Panic!");
10+
}
11+
12+
#[test]
13+
#[should_panic = "message"]
14+
fn should_panic_with_message() {
15+
panic!("message");
16+
}
17+
18+
#[test]
19+
#[should_panic]
20+
fn should_panic_with_any_message_does_not_panic() {
21+
// DON'T PANIC
22+
}
23+
24+
25+
#[test]
26+
#[should_panic = "message"]
27+
fn should_panic_with_message_does_not_panic() {
28+
// DON'T PANIC
29+
}
30+
31+
#[test]
32+
#[should_panic = "message"]
33+
fn should_panic_with_substring_panics_with_incorrect_string() {
34+
panic!("ZOMGWTFBBQ");
35+
}
36+
37+
#[test]
38+
#[should_panic = "message"]
39+
fn should_panic_with_substring_panics_with_non_string_value() {
40+
panic!(123); //~ WARNING panic message is not a string literal
41+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
running 6 tests
3+
test should_panic_with_any_message - should panic ... ok
4+
test should_panic_with_any_message_does_not_panic - should panic ... FAILED
5+
test should_panic_with_message - should panic ... ok
6+
test should_panic_with_message_does_not_panic - should panic ... FAILED
7+
test should_panic_with_substring_panics_with_incorrect_string - should panic ... FAILED
8+
test should_panic_with_substring_panics_with_non_string_value - should panic ... FAILED
9+
10+
failures:
11+
12+
---- should_panic_with_any_message_does_not_panic stdout ----
13+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:20:4
14+
---- should_panic_with_message_does_not_panic stdout ----
15+
note: test did not panic as expected at $DIR/test-should-panic-failed-show-span.rs:27:4
16+
---- should_panic_with_substring_panics_with_incorrect_string stdout ----
17+
18+
thread 'should_panic_with_substring_panics_with_incorrect_string' panicked at $DIR/test-should-panic-failed-show-span.rs:34:5:
19+
ZOMGWTFBBQ
20+
note: panic did not contain expected string
21+
panic message: `"ZOMGWTFBBQ"`,
22+
expected substring: `"message"`
23+
---- should_panic_with_substring_panics_with_non_string_value stdout ----
24+
25+
thread 'should_panic_with_substring_panics_with_non_string_value' panicked at $DIR/test-should-panic-failed-show-span.rs:40:5:
26+
Box<dyn Any>
27+
note: expected panic with string value,
28+
found non-string value: `TypeId(0x56ced5e4a15bd89050bb9674fa2df013)`
29+
expected substring: `"message"`
30+
31+
failures:
32+
should_panic_with_any_message_does_not_panic
33+
should_panic_with_message_does_not_panic
34+
should_panic_with_substring_panics_with_incorrect_string
35+
should_panic_with_substring_panics_with_non_string_value
36+
37+
test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
38+

Diff for: tests/ui/test-should-panic-failed-show-span.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: panic message is not a string literal
2+
--> $DIR/test-should-panic-failed-show-span.rs:40:12
3+
|
4+
LL | panic!(123);
5+
| ^^^
6+
|
7+
= note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
9+
= note: `#[warn(non_fmt_panics)]` on by default
10+
help: add a "{}" format string to `Display` the message
11+
|
12+
LL | panic!("{}", 123);
13+
| +++++
14+
help: or use std::panic::panic_any instead
15+
|
16+
LL - panic!(123);
17+
LL + std::panic::panic_any(123);
18+
|
19+
20+
warning: 1 warning emitted
21+

0 commit comments

Comments
 (0)