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

Rob Elliot

05/05/2022, 4:04 PM
Is there a form of
Map<K, V>.shouldContainAll(expected: Map<K, V>)
which recurses into any maps in the top level structure?
I'd like this to pass:
Copy code
mapOf(
      "1" to "1",
      "2" to mapOf(
        "2.1" to "2.1",
        "2.2" to "2.2",
      )
    ) shouldLooselyMatch mapOf(
      "1" to "1",
      "2" to mapOf(
        "2.2" to "2.2",
      )
    )
And it needs to work if the map is nested inside a List too:
Copy code
mapOf(
      "1" to "1",
      "2" to listOf(
        mapOf(
        "2.1" to "2.1",
        "2.2" to "2.2",
        )
      )
    ) shouldLooselyMatch mapOf(
      "1" to "1",
      "2" to listOf(
        mapOf(
          "2.1" to "2.1",
        )
      )
    )
(As you can probably guess this is at root a json matching issue - I suspect
shouldEqualSpecifiedJson
would do it but I've got a List/Map based representation rather than a String one...)
s

sam

05/05/2022, 4:09 PM
there's some new json stuff in 5.3.0
e

Emil Kantis

05/05/2022, 8:29 PM
r

Rob Elliot

05/05/2022, 8:38 PM
No, it's not a schema check, it's a match that ignores properties not present in the expectation.
e

Emil Kantis

05/05/2022, 8:43 PM
Okay, I don't know if there's such an assertion, I've never come across it at least. I would probably look into testing at another layer where the structure is serialized, or serialize it as part of the test. But I don't know the context here 🙂
r

Rob Elliot

05/05/2022, 9:01 PM
It's basically
shouldEqualSpecifiedJson
, but with Maps on either side rather than strings. I could serialise the Maps to json I suppose... Or alternatively convert them to
io.kotest.assertions.json.JsonTree
and use
equalJson
with
compareJsonOptions { fieldComparison = FieldComparison.Lenient }
, which would save using a string as the intermediate step.
s

sam

05/05/2022, 9:02 PM
We have a should match json ignoring fields I thought ?
r

Rob Elliot

05/05/2022, 9:12 PM
Yes, as I say the existing
shouldEqualSpecifiedJson
matcher is exactly the concept I want; I just haven't got json strings, I've got Maps.
s

sam

05/05/2022, 10:19 PM
Ahh now I get you
@Emil Kantis just needs to work harder 😉
😅 1
😁 1
4 Views