# Allow renaming left/right label in assert\_eq!

**URL:** <https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687>\
**Category:** libs\
**Created:** [October 11, 2023, 2:36am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687 "2023-10-11T02:36:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nyurik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nyurik/32/9660_2.png) [@nyurik](https://internals.rust-lang.org/u/nyurik)\
**Post date:** [October 11, 2023, 2:36am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/1 "2023-10-11T02:36:18Z")

</div>

Now that `assert_eq!` output has been cleaned up, I would like to propose that we allow renaming of the labels `left` and `right` into something more meaningful. Some design ideas:

```rust
assert_eq2!(# expected = 1, actual = 2);
assert_eq2!(@ left = "expected value", right = " actual value", 1, 2);

```

Output:

```rust
assertion `left == right` failed
expected: 1
actual: 2

```

```rust
assertion `left == right` failed
expected value: 1
  actual value: 2

```

Possible implementation, renamed for testing purposes

```rust
macro_rules! assert_eq2 {
    (# $left_label:ident = $left:expr, $right_label:ident = $right:expr $(,)?) => {{
        assert_eq!($left, $right, "{} != {}", stringify!($left_label), stringify!($right_label))
    }};
    (@ left = $left_label:literal, right = $right_label:literal, $left:expr, $right:expr $(,)?) => {{
        assert_eq!($left, $right, "{} != {}", $left_label, $right_label)
    }};
    ($($arg:tt)*) => {{
        assert_eq!($($arg)+)
    }};
}

```

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [October 11, 2023, 3:52am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/2 "2023-10-11T03:52:38Z")

</div>

Is there a situation where the expected vs. actual isn't clear when looking at the source code of the test? And if there is, _should_ there be?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 11, 2023, 4:43am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/3 "2023-10-11T04:43:28Z")

</div>

A cleverer and automatic option would be to print the identifiers given to the macro, if noncomplex expressions are provided. I.e. if you say `assert_eq!(expected, actual)`, have the error be

```plaintext
thread 'main' panicked at src/main.rs:4:5:
assertion `expected == actual` failed
 expected: 1
   actual: 2

```

---

<div class="post-metadata">

**Author:** ![nyurik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nyurik/32/9660_2.png) [@nyurik](https://internals.rust-lang.org/u/nyurik)\
**Post date:** [October 11, 2023, 5:35am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/4 "2023-10-11T05:35:23Z")

</div>

@CAD97 I tried that as part of the [https://github.com/rust-lang/rust/pull/111030](https://github.com/rust-lang/rust/pull/111030) -- it caused compiling performance regression by 1.1% - not something we would like to have just for the prettier names

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [October 11, 2023, 9:00am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/5 "2023-10-11T09:00:24Z")

</div>

I'd expect (heh) that manually repeating "expected" and "actual" in every `assert_eq` call would get old pretty fast when writing tests, prompting people to write their own shortcut macros anyway (`expect_eq`?) Half the people would probably prefer (expected, actual) and the other half (actual, expected), though…

---

<div class="post-metadata">

**Author:** ![farnz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/farnz/32/6587_2.png) [@farnz](https://internals.rust-lang.org/u/farnz)\
**Post date:** [October 11, 2023, 11:14am UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/6 "2023-10-11T11:14:59Z")

</div>

> [@jdahlstrom](#):
>
> I'd expect (heh) that manually repeating "expected" and "actual" in every `assert_eq` call would get old pretty fast when writing tests

In practice, a lot of asserts in my nearest code base already look like:

```rust
assert_eq!(computed_value, "Hand-written value that should be the same");

```

or

```rust
const expected: &str = "Hand-written value that should be the same");
/* More code here */
assert_eq!(expected, task.output);

```

If it could be done cheaply enough (and @nyurik's attempt suggests it can't), then it'd be nice to have the first line of those classes of assert become:

```rust
assertion `computed_value == <literal>` failed

```

and

```rust
assertion `expected == task.output` failed

```

But that depends on it being cheap enough - I'd have to look at the code anyway to diagnose the problem, and once I'm used to `left == right`, it's easy enough to spot the error.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [October 11, 2023, 4:57pm UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/8 "2023-10-11T16:57:33Z")

</div>

> [@jhpratt](#):
>
> Is there a situation where the expected vs. actual isn't clear when looking at the source code of the test? And if there is, _should_ there be?

In my experience, it is valuable in large code bases for test failures to be at least somewhat comprehensible without reference to the source code. This way you can read a list of test failures (which may be in modules quite distant from what you were editing) and assemble from them an understanding of what property you broke, without having to read the code of each of those tests (which may push what you were working on out of short term memory).

---

<div class="post-metadata">

**Author:** ![nyurik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nyurik/32/9660_2.png) [@nyurik](https://internals.rust-lang.org/u/nyurik)\
**Post date:** [October 11, 2023, 7:58pm UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/9 "2023-10-11T19:58:13Z")

</div>

I wonder if it would be possible for `assert_eq!` to match the parameter variable names as keywords, and act accordingly?

- `assert_eq!(expected, <expr>)`
- `assert_eq!(expect, <expr>)`
- `assert_eq!(actual, <expr>)`
- `assert_eq!(<expr>, expected)`
- `assert_eq!(<expr>, expect)`
- `assert_eq!(<expr>, actual)`

So if any of the above patterns are used, `assert_eq!` would use those instead of `left` vs `right`. This would only solve the common case of expected vs actual values.

The issue with the above is that if a macro treats `expected` as a keyword parameter, I have no way for the macro content to use that as a variable I think

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [January 9, 2024, 7:58pm UTC](https://internals.rust-lang.org/t/allow-renaming-left-right-label-in-assert-eq/19687/10 "2024-01-09T19:58:37Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
