question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compare two strings and show results with diff - which allows to quickly spot the difference

See original GitHub issue

When comparing two strings with #kotlintest with the test like this:

    "compare two strings" {
      "1: one\n2: two\n3: three".shouldBe("1: one1\n22: two\n3: three")
    }

, the output looks like this:

expected: "1: one1
22: two
3: three" but was: "1: one
2: two
3: three"
org.opentest4j.AssertionFailedError: expected: "1: one1
22: two
3: three" but was: "1: one
2: two
3: three"

Even with such a short string - it’s quite hard to spot, what exactly differs between those two strings. And it’s much harder if comparing longer strings, or object attributes, as the result becomes just one long horizontally scrollable line.

However - we are all very used to read the “diff” - with its +/- lines, and colors, which instantly allow us to spot the difference.

For example, here’s the result of the same assert, when ran by rspec (test lib for ruby):

expected: "1: one\n22: two\n3: three"
     got: "1: one\n2: two\n3: three"

(compared using ==)

Diff:
@@ -1,4 +1,4 @@
 1: one
-22: two
+2: two
 3: three

As you can see - it helps me in two ways. First - it puts both strings right under each other so that I can spot character level differences. Secondly, it shows nice diff with our familiar coloring so that I instantly see what’s not different, and what’s different.

Is there a way to achieve some test result comparison which would be similar, and give similar benefits to quickly spot the difference?

I tried KotlinTest and JUnit5 - both comparison results are very “horizontally scrollable in one line”.

Am I overlooking something obvious here?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sksamuelcommented, Mar 17, 2020

kotest will do the diff if a) you’re running on the JVM and b) your string is over 50 lines long. However you can control that 50 line limit. If you set kotest.assertions.multi-line-diff-size=0 as a system property then it should use diff all the time.

1reaction
sksamuelcommented, Jan 12, 2022

assert with diff is a good idea

“qweqwe” shouldBeWtihDiff “rtertert” or something.

On Wed, 12 Jan 2022 at 12:52, Holger Brandl @.***> wrote:

My problem was more the other way round: There was no diff and no indication how to enable it. That’s why ended up writing my own extension function (see referenced commit).

So adding some hint why a diff was not reported and how to enable it, would be more useful. A system property is a wrong approach IMHO, because it’s a behaviour the user may want to set on a per test basis (or maybe even within a single test function). So a parameter or a different assert* seems better to me.

— Reply to this email directly, view it on GitHub https://github.com/kotest/kotest/issues/1084#issuecomment-1011351879, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFVSGSHMCH33HQGMECBSFLUVXEVTANCNFSM4JPPIC4A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

kotlin - Compare two strings and show results with diff
As you can see - it helps me in two ways. First - it puts both strings right under each other so that...
Read more >
How to compare two strings for similarity or highlight ...
If you want to compare two strings and highlight the similarities or differences between them. The following VBA code can help you. 1....
Read more >
How to Compare Two Strings in Python (in 8 Easy Ways)
Use difflib to to compare two strings and return the difference; String comparison is not working? Check for trailing or leading spaces, or ......
Read more >
9 Online Tools to Compare Files and Find Differences
As the name suggests, Diffchecker is an online tool to check the differences between two files. It lets you compare texts, PDFs, ...
Read more >
Compare text online - Show Text Difference - Word Counter
This incredible tool allows everyone to simply make an online text comparison and find out the differences amidst two texts. The super easy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found