question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ShouldEqualJson comparing order misbehaving in lists

See original GitHub issue

Which 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sksamuelcommented, Sep 17, 2021

Maybe LenientProperties and LenientAll but yeah great.

0reactions
Kantiscommented, Sep 16, 2021

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 new CompareOrder 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?

enum class CompareOrder {
   /**
    * All object properties and array items must be in same order as expected.
    * 
    * For example, { "x": 14.2, "y": 13.0 }` and `{ "y": 13.0, "x: 14.2 }` will NOT be considered equal.
    */
   Strict,

   /**
    * See [IgnoreProperties]
    */
   @Deprecated(
      replaceWith = ReplaceWith("CompareOrder.IgnoreProperties"),
      message = "Will be renamed to `IgnoreProperties` in 5.0"
   )
   Lenient,

   /**
    * Ignore the order of object properties, but array order matters. (Default)
    *
    * For example, { "x": 14.2, "y": 13.0 }` and `{ "y": 13.0, "x: 14.2 }` would also be considered equal,
    * since they have the same properties and values.
    * However, `[1, 2]` and `[2, 1]` would NOT be considered equal
    */
   IgnoreProperties,

   /**
    * Ignore the order of object properties and arrays.
    *
    * For example, `[1, 2]` and `[2, 1]` will be considered equal, since they contain the same items.
    * `{ "x": 14.2, "y": 13.0 }` and `{ "y": 13.0, "x: 14.2 }` would also be considered equal, since they have the
    * same properties and values.
    */
   IgnoreAll,
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found