ShouldEqualJson comparing order misbehaving in lists
See original GitHub issueWhich version of Kotest are you using 4.6.0
Hello,
I’m using shouldEqualJson https://kotest.io/docs/assertions/json-matchers.html#shouldequaljson and standing to the docs, if I set CompareOrder.Lenient, in theory the order shouldn’t matter. As you can see in the following example, I’ve two lists with the same elements, but in different order:
import io.kotest.assertions.json.CompareMode
import io.kotest.assertions.json.CompareOrder
import io.kotest.assertions.json.shouldEqualJson
import org.junit.jupiter.api.Test
class SomeSillyTest {
@Test
fun `some silly test`() {
val currentOne = """
{
"some": "thing",
"listOfStuff": ["a2", "a1"],
"someList": [
{"type": "SOME_TYPE_2"},
{"type": "SOME_TYPE_1"}
]
}
""".trimIndent()
val expected = """
{
"some": "thing",
"listOfStuff": ["a1", "a2"],
"someList": [
{"type": "SOME_TYPE_1"},
{"type": "SOME_TYPE_2"}
]
}
""".trimIndent()
currentOne.shouldEqualJson(expected, mode = CompareMode.Lenient, order = CompareOrder.Lenient)
}
}
Result:
java.lang.AssertionError: At 'listOfStuff.[0]' expected 'a1' but was 'a2'
expected:
{
"some": "thing",
"listOfStuff": [
"a1",
"a2"
],
"someList": [
{
"type": "SOME_TYPE_1"
},
{
"type": "SOME_TYPE_2"
}
]
}
actual:
{
"some": "thing",
"listOfStuff": [
"a2",
"a1"
],
"someList": [
{
"type": "SOME_TYPE_2"
},
{
"type": "SOME_TYPE_1"
}
]
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
ShouldEqualJson comparing order misbehaving in lists
I think lists / arrays are naturally ordered, whereas object keys aren't. Perhaps the default should remain as-is, e.g. object key order doesn't...
Read more >Assert Two Lists for Equality Ignoring Order in Java - Baeldung
In this first test, the size of both lists is compared before we check if the elements in both lists are the same....
Read more >How to efficiently compare two unordered lists (not sets)?
The best way to do this is by sorting the lists and comparing them. ... it doesn't matter what order the objects are...
Read more >Json Matchers - Kotest
They provide precise error messages when comparing json so that the error ... The matcher allows for different formatting, and for different order...
Read more >Option to ignore order of named list elements? #72 - GitHub
I was wondering if it would be feasible to have an option that ignores the order of named elements in a list when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe LenientProperties and LenientAll but yeah great.
I think
lists
/arrays
are naturally ordered, whereas object keys aren’t. Perhaps the default should remain as-is, e.g. object key order doesn’t matter, while list order does. I think we should opt for a newCompareOrder
which could be used to allow arrays to be out-of-order as well, but I don’t think it makes sense to make that the default behaviour.Something like this sound ok?