Ensure that all characters in a given String
pass the given inspection function, where "pass" means returning normally from the function (i.e.,
without throwing an exception).
Ensure that all characters in a given String
pass the given inspection function, where "pass" means returning normally from the function (i.e.,
without throwing an exception).
The difference between forAll
and forEvery
is that
forAll
will stop on the first failure, while forEvery
will continue to inspect all characters in the String
after the
first failure (and report all failures).
the String
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that all elements in a given java.util.Map
pass the given inspection function, where "pass" means returning normally from the function (i.e.,
without throwing an exception).
Ensure that all elements in a given java.util.Map
pass the given inspection function, where "pass" means returning normally from the function (i.e.,
without throwing an exception).
The difference between forAll
and forEvery
is that
forAll
will stop on the first failure, while forEvery
will continue to inspect all java.util.Map
elements after the
first failure (and report all failures).
the type of key in the Java Map
the type of value in the Java Map
subtype of java.util.Map
the java.util.Map
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that all elements in a given collection pass the given inspection function, where "pass" means returning normally from the function (i.e., without throwing an exception).
Ensure that all elements in a given collection pass the given inspection function, where "pass" means returning normally from the function (i.e., without throwing an exception).
The difference between forAll
and forEvery
is that
forAll
will stop on the first failure, while forEvery
will continue to inspect all elements after the
first failure (and report all failures).
the type of element in the collection
the type of collection
the collection of elements
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that at least min
number of characters in a given String
pass the given inspection function.
Ensure that at least min
number of characters in a given String
pass the given inspection function.
the minimum number of characters in String
that must pass the inspection function
the String
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that at least min
number of elements in a given java.util.Map
pass the given inspection function.
Ensure that at least min
number of elements in a given java.util.Map
pass the given inspection function.
the type of key in the java.util.Map
the type of value in the java.util.Map
subtype of java.util.Map
the minimum number of elements that must pass the inspection function
the java.util.Map
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that at least min
number of elements of a given collection pass the given inspection function.
Ensure that at least min
number of elements of a given collection pass the given inspection function.
the type of element in the collection
the type of collection
the minimum number of elements that must pass the inspection function
the collection of elements
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that at most max
number of characters in a given String
pass the given inspection function.
Ensure that at most max
number of characters in a given String
pass the given inspection function.
the maximum number of characters in String
that must pass the inspection function
the String
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that at most max
number of elements in a given java.util.Map
pass the given inspection function.
Ensure that at most max
number of elements in a given java.util.Map
pass the given inspection function.
the type of key in the java.util.Map
the type of value in the java.util.Map
subtype of java.util.Map
the maximum number of elements in the java.util.Map
that must pass the inspection function
the java.util.Map
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that at most max
number of elements of a given collection pass the given inspection function.
Ensure that at most max
number of elements of a given collection pass the given inspection function.
the type of element in the collection
the type of collection
the maximum number of elements that must pass the inspection function
the collection of elements
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure the number of characters of a given String
that pass the given inspection function is between from
and upTo
.
Ensure the number of characters of a given String
that pass the given inspection function is between from
and upTo
.
the minimum number of characters in the String
that must pass the inspection number
the maximum number of characters in the String
that must pass the inspection number
the String
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure the number of elements in a given java.util.Map
that pass the given inspection function is between from
and upTo
.
Ensure the number of elements in a given java.util.Map
that pass the given inspection function is between from
and upTo
.
the type of key in the java.util.Map
the type of value in the java.util.Map
subtype of java.util.Map
the minimum number of elements in the java.util.Map
that must pass the inspection number
the maximum number of elements in the java.util.Map
that must pass the inspection number
the java.util.Map
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure the number of elements of a given collection that pass the given inspection function is between from
and upTo
.
Ensure the number of elements of a given collection that pass the given inspection function is between from
and upTo
.
the type of element in the collection
the type of collection
the minimum number of elements that must pass the inspection number
the maximum number of elements that must pass the inspection number
the collection of elements
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that every character in a given String
passes the given inspection function, where "pass" means returning normally from the function (i.e.,
without throwing an exception).
Ensure that every character in a given String
passes the given inspection function, where "pass" means returning normally from the function (i.e.,
without throwing an exception).
The difference between forEvery
and forAll
is that
forEvery
will continue to inspect all characters in the String
after first failure, and report all failures,
whereas forAll
will stop on (and only report) the first failure.
the String
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that every element in a given java.util.Map
passes the given inspection function, where "pass" means returning normally
from the function (i.e., without throwing an exception).
Ensure that every element in a given java.util.Map
passes the given inspection function, where "pass" means returning normally
from the function (i.e., without throwing an exception).
The difference between forEvery
and forAll
is that
forEvery
will continue to inspect all elements in the java.util.Map
after first failure, and report all failures,
whereas forAll
will stop on (and only report) the first failure.
the type of key in the java.util.Map
the type of value in the java.util.Map
subtype of java.util.Map
the java.util.Map
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that every element in a given collection passes the given inspection function, where "pass" means returning normally from the function (i.e., without throwing an exception).
Ensure that every element in a given collection passes the given inspection function, where "pass" means returning normally from the function (i.e., without throwing an exception).
The difference between forEvery
and forAll
is that
forEvery
will continue to inspect all elements after first failure, and report all failures,
whereas forAll
will stop on (and only report) the first failure.
the type of element in the collection
the type of collection
the collection of elements
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that exactly succeededCount
number of characters in a given String
pass the given inspection function.
Ensure that exactly succeededCount
number of characters in a given String
pass the given inspection function.
the number of characters in the String
that must pass the inspection function
the String
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that exactly succeededCount
number of elements in a given java.util.Map
pass the given inspection function.
Ensure that exactly succeededCount
number of elements in a given java.util.Map
pass the given inspection function.
the type of key in the java.util.Map
the type of value in the java.util.Map
subtype of java.util.Map
the number of elements in the java.util.Map
that must pass the inspection function
the java.util.Map
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
Ensure that exactly succeededCount
number of elements of a given collection pass the given inspection function.
Ensure that exactly succeededCount
number of elements of a given collection pass the given inspection function.
the type of element in the collection
the type of collection
the number of elements that must pass the inspection function
the collection of elements
the inspection function
the implicit Collecting
that can transform xs
into a scala.collection.GenTraversable
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:
You can use
Inspectors
on anyscala.collection.GenTraversable
,java.util.Collection
,java.util.Map
(withEntry
),Array
, orString
. Here are some examples: