google/truth

Trigger diff-style output more aggressively

vitiral opened this issue · 3 comments

Add an implementation of StringSubject.isEqualTo(String expected) which will do diffing in the error message if the strings are not equal.

https://truth.dev/api/1.0/com/google/common/truth/StringSubject.html

Adding a dependency on diff_match_patch should make this very easy.

Thanks. We do render a diff in some cases:

$ wget https://repo1.maven.org/maven2/{com/google/truth/truth/1.0/truth-1.0,com/google/guava/guava/27.1-jre/guava-27.1-jre,junit/junit/4.12/junit-4.12,com/googlecode/java-diff-utils/diffutils/1.3.0/diffutils-1.3.0,org/ow2/asm/asm/7.1/asm-7.1}.jar && echo 'import static com.google.common.truth.Truth.assertThat; class Foo { public static void main(String[] args) { assertThat("foo\nfoo\nfoo\nfoo\nfoo\nfoo\nbar\nbar\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\n").isEqualTo("foo\nfoo\nfoo\nfoo\nfoo\nfoo\nfoo\nbar\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\nbaz\n"); } }' > Foo.java && CP=.:truth-1.0.jar:guava-27.1-jre.jar:junit-4.12.jar:diffutils-1.3.0.jar:asm-7.1.jar && javac -cp $CP Foo.java && java -cp $CP Foo
Exception in thread "main" diff:
    @@ -4,8 +4,8 @@
     foo
     foo
     foo
    -foo
     bar
    +bar
     baz
     baz
     baz
  at Foo.main(Foo.java:1)

However, maybe we should do so more aggressively? (I had to add a lot of foos and bazzes to get the diff to trigger.)

Additionally, we could consider diff_match_patch for its ability to generate inline diffs, rather than only the line-by-line diffs that we currently support.

Are you interested more in generating line-by-line diffs more of the time or in generating inline diffs?

Interesting! I'm surprised the diff exists at all with the current code. I should have done some more internal testing.

I'd say when the diff is sufficiently large, inline diffs are probably better.