kotest icon indicating copy to clipboard operation
kotest copied to clipboard

Contain exactly more detail (#26)

Open AlexCue987 opened this issue 1 year ago • 6 comments

improve output of shouldContainExactly, try to find matching sublists, just like git diff does, as follows:

            val expected = listOf(sweetGreenApple, sweetRedApple, sweetGreenPear)
            val actual = listOf(sweetGreenPear, sweetGreenApple, sweetRedApple)
            shouldThrow<AssertionError> {
               actual shouldContainExactly expected
            }.shouldHaveMessage("""
               |Collection should contain exactly: [Fruit(name=apple, color=green, taste=sweet), Fruit(name=apple, color=red, taste=sweet), Fruit(name=pear, color=green, taste=sweet)] but was: [Fruit(name=pear, color=green, taste=sweet), Fruit(name=apple, color=green, taste=sweet), Fruit(name=apple, color=red, taste=sweet)]
               |
               |Mismatch:
               |expected[0] = Fruit(name=pear, color=green, taste=sweet)
               |
               |Match:
               |expected[1] == actual[0]: Fruit(name=apple, color=green, taste=sweet)
               |expected[2] == actual[1]: Fruit(name=apple, color=red, taste=sweet)
               |
               |Mismatch:
               |actual[2] = Fruit(name=pear, color=green, taste=sweet)
               |
               |""".trimMargin())

AlexCue987 avatar Jan 26 '24 17:01 AlexCue987

@AlexCue987 Should this be refactored to use the similarity package now?

Kantis avatar Feb 27 '24 21:02 Kantis

@AlexCue987 Should this be refactored to use the similarity package now?

of course, that's the plan. This will add more info to the output. No code will be removed, a couple lines will be added. As this PR is large as is, do we want to make it slightly larger, or should we add similarity in a later PR? WDYT @Kantis ?

AlexCue987 avatar Feb 28 '24 00:02 AlexCue987

so the enhanced message should look like this:

shouldHaveMessage("""
               |Collection should contain exactly: [Fruit(name=apple, color=green, taste=sweet), Fruit(name=apple, color=red, taste=sweet), Fruit(name=pear, color=green, taste=sweet)] but was: [Fruit(name=pear, color=green, taste=sweet), Fruit(name=apple, color=green, taste=sweet), Fruit(name=apple, color=red, taste=sweet)]
               |
               |Mismatch:
               |expected[0] = Fruit(name=pear, color=green, taste=sweet)
               |
               |Match:
               |expected[1] == actual[0]: Fruit(name=apple, color=green, taste=sweet)
               |expected[2] == actual[1]: Fruit(name=apple, color=red, taste=sweet)
               |
               |Mismatch:
               |actual[2] = Fruit(name=pear, color=green, taste=sweet)
               |
               | ----- <<< add similarity output here
               |""".trimMargin())

AlexCue987 avatar Feb 28 '24 17:02 AlexCue987

There are conflicts on the merge.

sksamuel avatar Mar 10 '24 21:03 sksamuel

There are conflicts on the merge.

just resolved a redeclaration. did I miss any other conflicts?

AlexCue987 avatar Mar 11 '24 15:03 AlexCue987

@AlexCue987 Should this be refactored to use the similarity package now?

this is the PR which adds search for similar elements in the same matcher: https://github.com/kotest/kotest/pull/3910 it adds some other useful output, not replacing the need for this one.

AlexCue987 avatar Mar 14 '24 15:03 AlexCue987

Needs the conflicts fixing.

sksamuel avatar Jul 21 '24 21:07 sksamuel

Closing this PR, to resolve conflicts and re-open it later.

AlexCue987 avatar Jul 30 '24 18:07 AlexCue987