Spocklight: Assert Elements In Collections In Any Order
Since Spock 2.1 we have 2 new operators we can use for assertions to check collections: =~
and ==~
. We can use these operators with implementations of the Iterable
interface when we want to check that a given collection has the same elements as an expected collection and we don’t care about the order of the elements. Without the new operators we would have to cast our collections to a Set
first and than use the ==
operator.
The difference between the operators =~
and ==~
is that =~
is lenient and ==~
is strict. With the lenient match operator we expect that each element in our expected collection appears at least once in the collection we want to assert. The strict match operator expects that each element in our expected collection appears exactly once in the collection we want to assert.
In the following example we see different uses of the new operators and some other idiomatic Groovy ways to check elements in a collection in any order:
package mrhaki
import spock.lang.Specification;
class CollectionConditions extends Specification {
void "check items in list are present in the same order"() {
when:
List<Integer> result = [1, 10, 2, 20]
then:
// List result is ordered so the items
// are not the same as the expected List.
result != [20, 10, 2, 1]
result == [1, 10, 2, 20]
// We can cast the result and expected list to Set
// and now the contents and order is the same.
result as Set == [20, 10, 2, 1] as Set
}
void "check all items in list are present in any order"() {
when:
List<Integer> result = [1, 10, 2, 20]
then:
result ==~ [20, 10, 2, 1]
/* The following assert would fail:
result ==~ [20, 10]
Condition not satisfied:
result ==~ [20, 10]
| |
| false
[1, 10, 2, 20]
Expected: iterable with items [<20>, <10>] in any order
but: not matched: <1>*/
// Negating also works
result !==~ [20, 10]
}
void "lenient check all items in list are present in any order"() {
when:
List<Integer> result = [1, 1, 10, 2, 2, 20]
then:
// result has extra values 1, 2 but with lenient
// check the assert is still true.
result =~ [20, 10, 2, 1]
/* The following assert would fail:
result =~ [20, 10]
Condition not satisfied:
result =~ [20, 10]
| |
| false
| 2 differences (50% similarity, 0 missing, 2 extra)
| missing: []
| extra: [1, 2]
[1, 10, 2, 20] */
// Negating works
result !=~ [20, 10]
}
void "check at least one item in list is part of expected list in any order"() {
when:
List<Integer> result = [1, 10, 2, 20]
then:
result.any { i -> i in [20, 10]}
}
void "check every item in list is part of expected list in any order"() {
when:
List<Integer> result = [1, 1, 10, 2, 2, 20]
then:
result.every { i -> i in [20, 10, 2, 1]}
}
}
Written with Spock 2.3-groovy-4.0.