https://kotlinlang.org logo
#kotest
Title
# kotest
a

abendt

06/20/2022, 3:30 PM
hi @sam, kotest provides the `shouldMatchEach`matcher for collections. We found it helpful to have a similiar matcher for Maps and implemented it on our own.
fun <K, V> Map<K,V>.shouldMatchAll(vararg matcher: Pair<K, (V) -> Unit>)
Do you think this is generally helpful and would you accept a PR with that matcher?
s

sam

06/20/2022, 3:47 PM
Yep pr welcome
a

abendt

06/21/2022, 7:00 AM
looking at the corresponding list matcher implementation i don't fully understand why the
errorCollector.runWithMode(ErrorCollectionMode.Hard)
block is required. I tried to come up with a testcase that fails if its not there (played with
assertSoftly
) however didn't manage. Could you plese explain the purpose of that block? how to test its use in the implementation?
e

Emil Kantis

06/21/2022, 3:19 PM
Iirc any matcher applied within assetSoftly will not cause an AssertionError, since the errors are collected at a lower level. I'll play with your PR and see if I can make an example :)
Ok, here's an example 🙂
Copy code
shouldThrow<AssertionError> {
               assertSoftly {
                  mapOf("key" to "hi") should matchAll("key" to { it shouldHaveLength 4 })
               }
            }.message shouldBe """
               Expected map to match all assertions. Missing keys were=[key], Mismatched values were=[(key, "hi" should have length 4, but instead was 2)].
            """.trimIndent()
This test fails with :
Copy code
Expected :"Expected map to match all assertions. Missing keys were=[key], Mismatched values were=[(key, "hi" should have length 4, but instead was 2)]."
Actual   :""hi" should have length 4, but instead was 2"
Due to the message being wrong. The reason the message is wrong is because the inner
shouldHaveLength
matcher is not throwing (since we're inside
assertSoftly
) and then when the assertSoftly block terminates it throws all captured assertion errors, just giving us the underlying error.
I pushed a commit with the example on your branch. Feel free to revert/change it if you want.
a

abendt

06/21/2022, 3:53 PM
thanks @Emil Kantis, i understand it now!
🙌 1
70 Views