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:
scala> import org.scalatest._ import org.scalatest._ scala> import Assertions._ import Assertions._ scala> import Inspectors._ import Inspectors._ scala> val xs = List(1, 2, 3, 4, 5) xs: List[Int] = List(1, 2, 3, 4, 5) scala> forAll (xs) { x => assert(x > 0) }
Or, with matchers:
scala> import Matchers._ import Matchers._ scala> forAll (xs) { x => x should be > 0 }
To make assertions about nested collections, you can nest the inspector method invocations. For example, given the following list of lists of Int
:
scala> val yss = | List( | List(1, 2, 3), | List(1, 2, 3), | List(1, 2, 3) | ) yss: List[List[Int]] = List(List(1, 2, 3), List(1, 2, 3), List(1, 2, 3))
You can assert that all Int
elements in all nested lists are positive by nesting two forAll
method invocations, like this:
scala> forAll (yss) { ys => | forAll (ys) { y => y should be > 0 } | }
The full list of inspector methods are:
-
forAll
- succeeds if the assertion holds true for every element -
forAtLeast
- succeeds if the assertion holds true for at least the specified number of elements -
forAtMost
- succeeds if the assertion holds true for at most the specified number of elements -
forBetween
- succeeds if the assertion holds true for between the specified minimum and maximum number of elements, inclusive -
forEvery
- 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 elements
The 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:
scala> forAll (yss) { ys => | forAll (ys) { y => y should be < 2 } | } org.scalatest.exceptions.TestFailedException: forAll failed, because: at index 0, forAll failed, because: at index 1, 2 was not less than 2 (<console>:20) in List(1, 2, 3) (<console>:20) in List(List(1, 2, 3), List(1, 2, 3), List(1, 2, 3)) at org.scalatest.InspectorsHelper$.forAll(Inspectors.scala:146) ...
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 initial List
(i.e., at index 0) failed the assertion, which was that all elements of that initial List[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
and forEvery
methods are similar in that both succeed only if the assertion holds for all elements of the collection. They differ in that forAll
will only report the first element encountered that failed the assertion, but forEvery
will report all elements that fail the assertion. The tradeoff is that while forEvery
gives more information, it may take longer to run because it must inspect every element of the collection. The forAll
method can simply stop inspecting once it encounters the first failing element. Here's an example that shows the difference in the forAll
and forEvery
error messages:
scala> forAll (xs) { x => x should be < 3 } org.scalatest.exceptions.TestFailedException: forAll failed, because: at index 2, 3 was not less than 3 (<console>:18) in List(1, 2, 3, 4, 5) at org.scalatest.InspectorsHelper$.forAll(Inspectors.scala:146) ... scala> forEvery (xs) { x => x should be < 3 } org.scalatest.exceptions.TestFailedException: forEvery failed, because: at index 2, 3 was not less than 3 (<console>:18), at index 3, 4 was not less than 3 (<console>:18), at index 4, 5 was not less than 3 (<console>:18) in List(1, 2, 3, 4, 5) at org.scalatest.InspectorsHelper$.forEvery(Inspectors.scala:226) ...
Note that if you're using matchers, you can alternatively use inspector shorthands for writing non-nested inspections. Here's an example:
scala> all (xs) should be > 3 org.scalatest.exceptions.TestFailedException: 'all' inspection failed, because: at index 0, 1 was not greater than 3 in List(1, 2, 3, 4, 5) at org.scalatest.InspectorsHelper$.forAll(Inspectors.scala:146)
You can use Inspectors
on any org.scalactic.ColCompatHelper.Iterable
, java.util.Collection
, java.util.Map
(with Entry
), Array
, or String
. Here are some examples:
scala> import org.scalatest._ import org.scalatest._ scala> import Inspectors._ import Inspectors._ scala> import Matchers._ import Matchers._ scala> forAll (Array(1, 2, 3)) { e => e should be < 5 } scala> import collection.JavaConverters._ import collection.JavaConverters._ scala> val js = List(1, 2, 3).asJava js: java.util.List[Int] = [1, 2, 3] scala> forAll (js) { j => j should be < 5 } scala> val jmap = Map("a" -> 1, "b" -> 2).asJava jmap: java.util.Map[String,Int] = {a=1, b=2} scala> forAtLeast(1, jmap) { e => e shouldBe Entry("b", 2) } scala> forAtLeast(2, "hello, world!") { c => c shouldBe 'o' }
Attributes
- Companion
- object
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
object Inspectors.type
Members list
Value members
Concrete methods
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).
Type parameters
- C
-
the type of collection
- E
-
the type of element in the collection
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the collection of elements
Attributes
Ensure that all elements in a given scala.collection.GenMap
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 scala.collection.GenMap
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 scala.collection.GenMap
entries after the first failure (and report all failures).
Type parameters
- K
-
the type of key in the Map
- MAP
-
subtype of
java.util.Map
- V
-
the type of value in the Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the
java.util.Map
Attributes
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).
Type parameters
- JMAP
-
subtype of
java.util.Map
- K
-
the type of key in the Java Map
- V
-
the type of value in the Java Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the
java.util.Map
Attributes
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).
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the
String
Attributes
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.
Type parameters
- C
-
the type of collection
- E
-
the type of element in the collection
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- min
-
the minimum number of elements that must pass the inspection function
- xs
-
the collection of elements
Attributes
Ensure that at least min
number of elements in a given scala.collection.GenMap
pass the given inspection function.
Ensure that at least min
number of elements in a given scala.collection.GenMap
pass the given inspection function.
Type parameters
- K
-
the type of key in the
scala.collection.GenMap
- MAP
-
subtype of
scala.collection.GenMap
- V
-
the type of value in the
scala.collection.GenMap
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- min
-
the minimum number of elements that must pass the inspection function
- xs
-
the
scala.collection.GenMap
Attributes
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.
Type parameters
- JMAP
-
subtype of
java.util.Map
- K
-
the type of key in the
java.util.Map
- V
-
the type of value in the
java.util.Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- min
-
the minimum number of elements that must pass the inspection function
- xs
-
the
java.util.Map
Attributes
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.
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- min
-
the minimum number of characters in
String
that must pass the inspection function - xs
-
the
String
Attributes
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.
Type parameters
- C
-
the type of collection
- E
-
the type of element in the collection
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- max
-
the maximum number of elements that must pass the inspection function
- xs
-
the collection of elements
Attributes
Ensure that at most max
number of elements in a given scala.collection.GenMap
pass the given inspection function.
Ensure that at most max
number of elements in a given scala.collection.GenMap
pass the given inspection function.
Type parameters
- K
-
the type of key in the
scala.collection.GenMap
- MAP
-
subtype of
scala.collection.GenMap
- V
-
the type of value in the
scala.collection.GenMap
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- max
-
the maximum number of elements in the
scala.collection.GenMap
that must pass the inspection function - xs
-
the
scala.collection.GenMap
Attributes
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.
Type parameters
- JMAP
-
subtype of
java.util.Map
- K
-
the type of key in the
java.util.Map
- V
-
the type of value in the
java.util.Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- max
-
the maximum number of elements in the
java.util.Map
that must pass the inspection function - xs
-
the
java.util.Map
Attributes
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.
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- max
-
the maximum number of characters in
String
that must pass the inspection function - xs
-
the
String
Attributes
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
.
Type parameters
- C
-
the type of collection
- E
-
the type of element in the collection
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- from
-
the minimum number of elements that must pass the inspection number
- fun
-
the inspection function
- upTo
-
the maximum number of elements that must pass the inspection number
- xs
-
the collection of elements
Attributes
Ensure the number of elements in a given scala.collection.GenMap
that pass the given inspection function is between from
and upTo
.
Ensure the number of elements in a given scala.collection.GenMap
that pass the given inspection function is between from
and upTo
.
Type parameters
- K
-
the type of key in the
scala.collection.GenMap
- MAP
-
subtype of
scala.collection.GenMap
- V
-
the type of value in the
scala.collection.GenMap
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- from
-
the minimum number of elements in the
scala.collection.GenMap
that must pass the inspection number - fun
-
the inspection function
- upTo
-
the maximum number of elements in the
scala.collection.GenMap
that must pass the inspection number - xs
-
the
scala.collection.GenMap
Attributes
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
.
Type parameters
- JMAP
-
subtype of
java.util.Map
- K
-
the type of key in the
java.util.Map
- V
-
the type of value in the
java.util.Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- from
-
the minimum number of elements in the
java.util.Map
that must pass the inspection number - fun
-
the inspection function
- upTo
-
the maximum number of elements in the
java.util.Map
that must pass the inspection number - xs
-
the
java.util.Map
Attributes
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
.
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- from
-
the minimum number of characters in the
String
that must pass the inspection number - fun
-
the inspection function
- upTo
-
the maximum number of characters in the
String
that must pass the inspection number - xs
-
the
String
Attributes
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.
Type parameters
- C
-
the type of collection
- E
-
the type of element in the collection
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the collection of elements
Attributes
Ensure that every element in a given scala.collection.GenMap
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 scala.collection.GenMap
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 entries in the scala.collection.GenMap
after first failure, and report all failures, whereas forAll
will stop on (and only report) the first failure.
Type parameters
- K
-
the type of key in the
scala.collection.GenMap
- MAP
-
subtype of
scala.collection.GenMap
- V
-
the type of value in the
scala.collection.GenMap
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the
scala.collection.GenMap
Attributes
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.
Type parameters
- JMAP
-
subtype of
java.util.Map
- K
-
the type of key in the
java.util.Map
- V
-
the type of value in the
java.util.Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the
java.util.Map
Attributes
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.
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- xs
-
the
String
Attributes
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.
Type parameters
- C
-
the type of collection
- E
-
the type of element in the collection
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- succeededCount
-
the number of elements that must pass the inspection function
- xs
-
the collection of elements
Attributes
Ensure that exactly succeededCount
number of elements in a given scala.collection.GenMap
pass the given inspection function.
Ensure that exactly succeededCount
number of elements in a given scala.collection.GenMap
pass the given inspection function.
Type parameters
- K
-
the type of key in the
scala.collection.GenMap
- MAP
-
subtype of
scala.collection.GenMap
- V
-
the type of value in the
scala.collection.GenMap
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- succeededCount
-
the number of entries in the
scala.collection.GenMap
that must pass the inspection function - xs
-
the
scala.collection.GenMap
Attributes
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.
Type parameters
- JMAP
-
subtype of
java.util.Map
- K
-
the type of key in the
java.util.Map
- V
-
the type of value in the
java.util.Map
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- succeededCount
-
the number of elements in the
java.util.Map
that must pass the inspection function - xs
-
the
java.util.Map
Attributes
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.
Value parameters
- collecting
-
the implicit
Collecting
that can transformxs
into aorg.scalactic.ColCompatHelper.Iterable
- fun
-
the inspection function
- succeededCount
-
the number of characters in the
String
that must pass the inspection function - xs
-
the
String