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 assert that no elements in a collection passed the assertions, we could do something like:
xs.forNone {
it.shouldContain("x")
it.shouldStartWith("bb")
}
The full list of inspectors are:
forAll
which asserts every element passes the assertionsforNone
which asserts no element passesforOne
which asserts only a single element passedforAtMostOne
which asserts that either 0 or 1 elements passforAtLeastOne
which 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 passedforAny
which is an alias forforAtLeastOne
forSome
which 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 methods