Inspectors
Inspectors allow us to test elements in a collection. They are extension functions for collections and arrays that test that all, none or some of the elements pass the given assertions. For example, to test that a list of names contains at least two elements which have a length of 7 or more, we can do this:
val xs = listOf("sam", "gareth", "timothy", "muhammad")
xs.forAtLeast(2) {
it.shouldHaveMinLength(7)
}
Similarly, if we wanted to asset that no elements in a collection passed the assertions, we could do something like:
xs.forNone {
it.shouldContain("x")
it.shouldStartWith("bb")
}
If you want to filter a collection to only elements that pass the assertions, you can use the filterMatching method:
xs.filterMatching {
it.shouldContain("x")
it.shouldStartWith("bb")
}.shouldBeEmpty()
The full list of inspectors are:
forAllwhich asserts every element passes the assertionsforNonewhich asserts no element passesforOnewhich asserts only a single element passedforAtMostOnewhich asserts that either 0 or 1 elements passforAtLeastOnewhich asserts that 1 or more elements passedforAtLeast(k)which is a generalization that k or more elements passedforAtMost(k)which is a generalization that k or fewer elements passedforAnywhich is an alias forforAtLeastOneforSomewhich asserts that between 1 and n-1 elements passed. Ie, if NONE pass or ALL pass then we consider that a failure.forExactly(k)which is a generalization that exactly k elements passed. This is the basis for the implementation of the other methodsfilterMatchingwhich filters the collection to only include elements that pass the assertions