I want to compare two biiiiigggg strings. But when...
# kotest
a
I want to compare two biiiiigggg strings. But when
stringA shouldBe stringB
fails the error floods the logs with the string content, obscuring the actual failure. Is there a better way? If it helps, I can save the strings to files.
k
I would also like to see if there's a Kotest assertion that gives a helpful message in such a case. In the meantime, I can think of a couple of workarounds. First, we can use an alternative assertion library. AssertK gives a useful message if we turn the strings into arrays:
Copy code
class MyTest : FreeSpec({
    "test" {
        val s = buildString {
            repeat(1000) { append(Random.nextInt(' '.code, 127).toChar()) }
        }
        assertThat((s + 'a' + s).toCharArray()).isEqualTo((s + s).toCharArray())
    }
})
Result:
Copy code
org.opentest4j.AssertionFailedError: expected:<...a', 'D', 'A', 'e', '[]U', 'n', 'L', '*', '...> but was:<...a', 'D', 'A', 'e', '[a', ']U', 'n', 'L', '*', '...>
The difference is highlighted by the `[]`s. Second, we can use a utility such as
StringUtils.difference
from Apache commons-lang3.
c
if you use assertEquals you get a diff viewer in idea.
a
I'm working on one right now
1
it outputs like the following:
Copy code
val line = "The quick brown fox jumps over the lazy dog"

         "find one match" {
            val actual = describePartialMatchesInString("brown fox jumps over", line)
            actual.partialMatchesList shouldBe "Substring[0] of expected with indexes: 0..19 matched a substring of actual with indexes: 10..29"
            actual.partialMatchesDescription shouldBe
               """The quick brown fox jumps over the lazy dog
                 |----------++++++++++++++++++++-------------""".trimMargin()
         }
and it handles strings that span multiple lines
how big are your strings?
a
4,000 lines
a
I'm not sure about performance. When it's more baked, I'd like to try it out against your files
Can you DM some strings?
@christophsturm does it show diffs for multi-line strings nicely as well?
c
totally. it shows a diff view that works perfect for any size of string or multi line string
a
Is it Ultimate version?
a
Can you DM some strings?
Just run
./gradlew :foo-subproject:kotlinDslAccessorsReport
:)
👍🏼 1
I really dont know if its ultimate only.
k
But this is only in IntelliJ IDEA. As the OP talked about logs, I expect the tests are run in a CI pipeline.
a
Kotest also supports 'click to see difference', but it doesn't work for large strings, even for JUnit or kotlin.test
💯 1
a
git diff
should be able to handle
c
thats just not true at all, ideas diff viewer works perfectly with any size of strings. https://gist.github.com/christophsturm/244c390a2da8cfe5fb880ed6ca834ac1
DEFAULT_MESSAGE_LENGTH_THRESHOLD = 10_000
a
I hear you loud and clear, but
assertEquals
is a JUnit method, right?
c
the diff viewer works with any assert that throws the same exception that assertEquals throws
a
nice
c
I think showing differnces is a fascinating topic, especially for collections. (lists, sets, etc)
a
yes exactly, and we are improving those right now, like this one https://github.com/kotest/kotest/pull/4291
do you have any ideas on what to improve in kotest 's collections matchers @christophsturm?
k
One thing that might be improved: if you compare two Lists of 1000+ items each, and there is a mismatch in the middle, it shows the first few elements of each list followed by ellipses, and "mismatch at 500". It ought to show the part of the list where the mismatch occurs, rather than the beginning of the list.
gratitude thank you 1
a
makes sense.
c
do you have any ideas on what to improve in kotest ’s collections matchers
I’m not using kotest assertions so I have no idea what to improve there. I think there should be a good diffing library that is not tied to an assertion lib and can figure out the best way to show changes in 2 objects graphs. I would probably start with a test suite where i just have test data and expectations what the perfect way to show the difference between them would be.
👍🏼 1
a
I encourage you to try out kotest
c
I encourage you to try out failgood :)
d
Maybe this might help: https://github.com/dmcg/okey-doke?