trait
Inspectors extends AnyRef
Value Members
-
final
def
!=(arg0: AnyRef): Boolean
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: AnyRef): Boolean
-
final
def
==(arg0: Any): Boolean
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
def
forAll[T](xs: GenTraversable[T])(fun: (T) ⇒ Unit): Unit
-
def
forAtLeast[T](min: Int, xs: GenTraversable[T])(fun: (T) ⇒ Unit): Unit
-
def
forAtMost[T](max: Int, xs: GenTraversable[T])(fun: (T) ⇒ Unit): Unit
-
def
forBetween[T](from: Int, upTo: Int, xs: GenTraversable[T])(fun: (T) ⇒ Unit): Unit
-
def
forEvery[T](xs: GenTraversable[T])(fun: (T) ⇒ Unit): Unit
-
def
forExactly[T](succeededCount: Int, xs: GenTraversable[T])(fun: (T) ⇒ Unit): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from AnyRef
Inherited from Any
Provides nestable inspector methods (or just inspectors) that enable assertions to be made about collections.
For example, the
forAll
method enables you to state that something should be true about all elements of a collection, such as that all elements should be positive:Or, with matchers:
To make assertions about nested collections, you can nest the inspector method invocations. For example, given the following list of lists of
Int
:You can assert that all
Int
elements in all nested lists are positive by nesting twoforAll
method invocations, like this:The full list of inspector methods are:
forAll
- succeeds if the assertion holds true for every elementforAtLeast
- succeeds if the assertion holds true for at least the specified number of elementsforAtMost
- succeeds if the assertion holds true for at most the specified number of elementsforBetween
- succeeds if the assertion holds true for between the specified minimum and maximum number of elements, inclusiveforEvery
- same asforAll
, but lists all failing elements if it fails (whereasforAll
just reports the first failing element)forExactly
- succeeds if the assertion holds true for exactly the specified number of elementsThe error messages produced by inspector methods are designed to make sense no matter how deeply you nest the method invocations. Here's an example of a nested inspection that fails and the resulting error message:
One way the error message is designed to help you understand the error is by using indentation that mimics the indentation of the source code (optimistically assuming the source will be nicely indented). The error message above indicates the outer
forAll
failed because its initialList
(i.e., at index 0) failed the assertion, which was that all elements of that initialList[Int]
at index 0 should be less than 2. This assertion failed because index 1 of that inner list contained the value 2, which was indeed “not less than 2.” The error message for the inner list is an indented line inside the error message for the outer list. The actual contents of each list are displayed at the end in inspector error messages, also indented appropriately. The actual contents are placed at the end so that for very large collections, the contents will not drown out and make it difficult to find the messages that describe actual causes of the failure.The
forAll
andforEvery
methods are similar in that both succeed only if the assertion holds for all elements of the collection. They differ in thatforAll
will only report the first element encountered that failed the assertion, butforEvery
will report all elements that fail the assertion. The tradeoff is that whileforEvery
gives more information, it may take longer to run because it must inspect every element of the collection. TheforAll
method can simply stop inspecting once it encounters the first failing element. Here's an example that shows the difference in theforAll
andforEvery
error messages:Note that if you're using matchers, you can alternatively use inspector shorthands for writing non-nested inspections. Here's an example: