There is a case when we want to compare JSON arrays regardless of their order. For example, an API for testing might return items in no guaranteed order.
For those cases where you need to assert that all array elements are present but in any order you can do this:
* def data = { foo: [1, 2, 3] }
* match data.foo contains 1
* match data.foo contains [2]
* match data.foo contains [3, 2]
* match data.foo contains only [3, 2, 1]
* match data.foo contains only [2, 3, 1]
# this will fail
# * match data.foo contains only [2, 3]
In addition, assertion match contains only deep provides order insensitive comparison for the deep array.
This is exactly like match == but the order of arrays does not matter. All arrays no matter the "depth" will be checked in this way.
* def response = { foo: [ 'a', 'b' ] }
* match response contains only deep { foo: [ 'b', 'a' ] }
Motivation
There is a case when we want to compare JSON arrays regardless of their order. For example, an API for testing might return items in no guaranteed order.
Related work
AssertJ
AbstractIterableAssert#containsExactlyInAnyOrder provides comparison regardless of order.
Karate
Assertion match contains only provides comparison regardless of order.
In addition, assertion match contains only deep provides order insensitive comparison for the deep array.
Solution
To be determined. Ideas are welcome!