Skip to content

Commit 3cf9f5d

Browse files
committed
Auto merge of #42541 - gilescope:patch-1, r=alexcrichton
assert_eq failure message easier to read By having the left and right strings aligned with one another it helps spot the difference between the two far quicker than if they are on the same line. E.g. Before: ``` thread 'tests::test_safe_filename' panicked at 'assertion failed: `(left == right)` left: `"-aandb--S123.html"` right: `"-aandb-S123.html"`', ``` After: ``` thread 'tests::test_safe_filename' panicked at 'assertion failed: `(left == right)` left: `"-aandb--S123.html"` right: `"-aandb-S123.html"`', ``` When the strings are both on the same line it take a lot longer to spot the difference. It is a small change but the small time savings add up with repetition. This would help Rust be an excellent language to write tests in out of the box. Closes #41615
2 parents 7e76505 + 940d5ca commit 3cf9f5d

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

src/libcore/macros.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ macro_rules! assert_eq {
116116
match (&$left, &$right) {
117117
(left_val, right_val) => {
118118
if !(*left_val == *right_val) {
119-
panic!("assertion failed: `(left == right)` \
120-
(left: `{:?}`, right: `{:?}`)", left_val, right_val)
119+
panic!(r#"assertion failed: `(left == right)`
120+
left: `{:?}`,
121+
right: `{:?}`"#, left_val, right_val)
121122
}
122123
}
123124
}
@@ -126,8 +127,9 @@ macro_rules! assert_eq {
126127
match (&($left), &($right)) {
127128
(left_val, right_val) => {
128129
if !(*left_val == *right_val) {
129-
panic!("assertion failed: `(left == right)` \
130-
(left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
130+
panic!(r#"assertion failed: `(left == right)`
131+
left: `{:?}`,
132+
right: `{:?}`: {}"#, left_val, right_val,
131133
format_args!($($arg)+))
132134
}
133135
}
@@ -162,8 +164,9 @@ macro_rules! assert_ne {
162164
match (&$left, &$right) {
163165
(left_val, right_val) => {
164166
if *left_val == *right_val {
165-
panic!("assertion failed: `(left != right)` \
166-
(left: `{:?}`, right: `{:?}`)", left_val, right_val)
167+
panic!(r#"assertion failed: `(left != right)`
168+
left: `{:?}`,
169+
right: `{:?}`"#, left_val, right_val)
167170
}
168171
}
169172
}
@@ -172,8 +175,9 @@ macro_rules! assert_ne {
172175
match (&($left), &($right)) {
173176
(left_val, right_val) => {
174177
if *left_val == *right_val {
175-
panic!("assertion failed: `(left != right)` \
176-
(left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
178+
panic!(r#"assertion failed: `(left != right)`
179+
left: `{:?}`,
180+
right: `{:?}`: {}"#, left_val, right_val,
177181
format_args!($($arg)+))
178182
}
179183
}

src/test/run-fail/assert-eq-macro-panic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:assertion failed: `(left == right)` (left: `14`, right: `15`)
11+
// error-pattern:assertion failed: `(left == right)`
12+
// error-pattern: left: `14`
13+
// error-pattern:right: `15`
1214

1315
fn main() {
1416
assert_eq!(14, 15);
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:assertion failed: `(left != right)`
12+
// error-pattern: left: `14`
13+
// error-pattern:right: `14`
14+
15+
fn main() {
16+
assert_ne!(14, 14);
17+
}

0 commit comments

Comments
 (0)