rust-pretty-assertions icon indicating copy to clipboard operation
rust-pretty-assertions copied to clipboard

Print warning when we cannot print good diff

Open lemolatoon opened this issue 3 years ago • 7 comments

Ref #100

If left and right are not equal, but we cannot print any diff, then make sure to print warning instead.

example:

use pretty_assertions::assert_eq;

fn main() {
    assert_eq!("\n", "\r\n");
}
thread 'main' panicked at 'assertion failed: `(left == right)`

Diff < left / right > :
Warning: values were different, but no line diff was generated. Do your values have the same line endings?

', pretty_assertions/examples/pretty_assertion.rs:4:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

lemolatoon avatar Sep 18 '22 07:09 lemolatoon

@lemolatoon Oh, that's quite tricky - even if a diff is shown, these differences could be hidden in the output. :-/

No Diff Case

The warning is good, but let's try to offer more - why not at least fall back to the default presentation AFTER the warning? IMO we don't need to ask the user whether there are different line endings, if we can also tell him.

General Case

I wonder if we should try to detect "invisible" differences and implement more general fallbacks?

colin-kiegel avatar Sep 19 '22 14:09 colin-kiegel

@colin-kiegel Thank you for replying. Let me comfirm what you suggested.

No Diff Case

So you mean Asking (Do your values have the same line endings?) is unnecessary. And Warning should be shown before. In which case, example would be...

thread 'main' panicked at 'assertion failed: `(left == right)`

Warning: values were different, but no line diff was generated.
Diff < left / right > :

', pretty_assertions/examples/pretty_assertion.rs:4:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

General Case

In another case, such as...

use pretty_assertions::assert_eq;

fn main() {
    assert_eq!("a\n", "ab\r\n");
}

As you pointed out, on current implementation, we cannot detect and print warning on this type.

What I am going to do

I am going to fix these points.

lemolatoon avatar Sep 20 '22 15:09 lemolatoon

@lemolatoon That's only part of what I meant - first let's keep the second part, but not as a question:

  • Warning: values were different, but no line diff was generated. It seems like these values only differ in line endings - falling back to debug output:

Then we could fallback to showing the debug-string of left and right, like the original "not pretty" assert_eq!() output´.

Warning: values were different, but no line diff was generated. It seems like these values only differ in line endings - falling back to debug output:
Diff < left / right > :
  left: `"Foo\n"`,
 right: `"Foo\r\n"`'

colin-kiegel avatar Sep 20 '22 17:09 colin-kiegel

Now by the commit I just did. It should be ok. example:

use pretty_assertions::assert_eq;

fn main() {
    assert_eq!("Foo\n", "Foo\r\n")
}
thread 'main' panicked at 'assertion failed: `(left == right)`

Warning: values were different, but no line diff was generated. It seems like these values only differ in line endings - falling back to debug output:
Diff < left / right > :
 left: `"Foo\n"`,
right: `"Foo\r\n"`

', pretty_assertions/examples/pretty_assertion.rs:4:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

lemolatoon avatar Sep 20 '22 18:09 lemolatoon

You might want to steal some code from similar-asserts which renders sigils for newlines if they matter: https://github.com/mitsuhiko/similar-asserts/blob/63eef1b71d2d21648c3b8c992c616a7c4b74d747/src/lib.rs#L214

mitsuhiko avatar Sep 20 '22 22:09 mitsuhiko

@mitsuhiko Nice, thank you!

You might want to steal some code from similar-asserts which renders sigils for newlines if they matter: https://github.com/mitsuhiko/similar-asserts/blob/63eef1b71d2d21648c3b8c992c616a7c4b74d747/src/lib.rs#L214

I also like the way you both print the original left / right output AND the difference. It seems worthwile to also copy this behaviour - so far rust-pretty-assertions only prints the difference...

grafik

Off-Topic: Do you see any other areas where either rust-pretty-assertions or similar-asserts perform better and could learn from each other?


PS: @tommilligan What's your thought on all of this?

colin-kiegel avatar Sep 21 '22 07:09 colin-kiegel

@colin-kiegel similar-asserts pretty much only exists because of the performance issues i with with this crate in the past on large diffs and I already had similar in my dependency tree for insta. But since pretty-assertions is used in a lot of places I am exposed to it :)

I personally believe that at this point similar-asserts is the better crate, but it has much lower adoption and in the most ideal of cases they would just become the same thing.

mitsuhiko avatar Sep 29 '22 09:09 mitsuhiko