package should
Trait and object for ScalaTest Matchers DSL using should.
This package is released as part of the scalatest-shouldmatchers module.
- Source
- package.scala
- Alphabetic
- By Inheritance
- should
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- trait Matchers extends Assertions with Tolerance with ShouldVerb with MatcherWords with Explicitly
Trait that provides a domain specific language (DSL) for expressing assertions in tests using the word
should.Trait that provides a domain specific language (DSL) for expressing assertions in tests using the word
should.For example, if you mix
Matchersinto a suite class, you can write an equality assertion in that suite like this:result should equal (3)Here
resultis a variable, and can be of any type. If the object is anIntwith the value 3, execution will continue (i.e., the expression will result in the unit value,()). Otherwise, aTestFailedExceptionwill be thrown with a detail message that explains the problem, such as"7 did not equal 3". ThisTestFailedExceptionwill cause the test to fail.Here is a table of contents for this documentation:
- Matchers migration in ScalaTest 2.0
- Checking equality with matchers
- Checking size and length
- Checking strings
- Greater and less than
- Checking
Booleanproperties withbe - Using custom
BeMatchers - Checking object identity
- Checking an object's class
- Checking numbers against a range
- Checking for emptiness
- Working with "containers"
- Working with "aggregations"
- Working with "sequences"
- Working with "sortables"
- Working with iterators
- Inspector shorthands
- Single-element collections
- Java collections and maps
Strings andArrays as collections- Be as an equality comparison
- Being negative
- Checking that a snippet of code does not compile
- Logical expressions with
andandor - Working with
Options - Checking arbitrary properties with
have - Using
lengthandsizewithHavePropertyMatchers - Checking that an expression matches a pattern
- Using custom matchers
- Checking for expected exceptions
- Those pesky parens
Trait
must.Matchersis an alternative toshould.Matchersthat provides the exact same meaning, syntax, and behavior asshould. The two traits differ only in the English semantics of the verb:shouldis informal, making the code feel like conversation between the writer and the reader;mustis more formal, making the code feel more like a written specification.Checking equality with matchers
ScalaTest matchers provides five different ways to check equality, each designed to address a different need. They are:
result should equal (3) // can customize equality result should === (3) // can customize equality and enforce type constraints result should be (3) // cannot customize equality, so fastest to compile result shouldEqual 3 // can customize equality, no parentheses required result shouldBe 3 // cannot customize equality, so fastest to compile, no parentheses required
The “
leftshouldequal(right)” syntax requires anorg.scalactic.Equality[L]to be provided (either implicitly or explicitly), whereLis the left-hand type on whichshouldis invoked. In the "leftshouldequal(right)" case, for example,Lis the type ofleft. Thus ifleftis typeInt, the "leftshouldequal(right)" statement would require anEquality[Int].By default, an implicit
Equality[T]instance is available for any typeT, in which equality is implemented by simply invoking==on theleftvalue, passing in therightvalue, with special treatment for arrays. If eitherleftorrightis an array,deepwill be invoked on it before comparing with ==. Thus, the following expression will yield false, becauseArray'sequalsmethod compares object identity:Array(1, 2) == Array(1, 2) // yields false
The next expression will by default not result in a
TestFailedException, because defaultEquality[Array[Int]]compares the two arrays structurally, taking into consideration the equality of the array's contents:Array(1, 2) should equal (Array(1, 2)) // succeeds (i.e., does not throw TestFailedException)
If you ever do want to verify that two arrays are actually the same object (have the same identity), you can use the
be theSameInstanceAssyntax, described below.You can customize the meaning of equality for a type when using "
shouldequal," "should===," orshouldEqualsyntax by defining implicitEqualityinstances that will be used instead of defaultEquality. You might do this to normalize types before comparing them with==, for instance, or to avoid calling the==method entirely, such as if you want to compareDoubles with a tolerance. For an example, see the main documentation of traitorg.scalactic.Equality.You can always supply implicit parameters explicitly, but in the case of implicit parameters of type
Equality[T], Scalactic provides a simple "explictly" DSL. For example, here's how you could explicitly supply anEquality[String]instance that normalizes both left and right sides (which must be strings), by transforming them to lowercase:scala> import org.scalatest.matchers.should.Matchers._ import org.scalatest.matchers.should.Matchers._ scala> import org.scalactic.Explicitly._ import org.scalactic.Explicitly._ scala> import org.scalactic.StringNormalizations._ import org.scalactic.StringNormalizations._ scala> "Hi" should equal ("hi") (after being lowerCased)The
afterbeinglowerCasedexpression results in anEquality[String], which is then passed explicitly as the second curried parameter toequal. For more information on the explictly DSL, see the main documentation for traitorg.scalactic.Explicitly.The "
shouldbe" andshouldBesyntax do not take anEquality[T]and can therefore not be customized. They always use the default approach to equality described above. As a result, "shouldbe" andshouldBewill likely be the fastest-compiling matcher syntax for equality comparisons, since the compiler need not search for an implicitEquality[T]each time.The
should===syntax (and its complement,should!==) can be used to enforce type constraints at compile-time between the left and right sides of the equality comparison. Here's an example:scala> import org.scalatest.matchers.should.Matchers._ import org.scalatest.matchers.should.Matchers._ scala> import org.scalactic.TypeCheckedTripleEquals._ import org.scalactic.TypeCheckedTripleEquals._ scala> Some(2) should === (2) <console>:17: error: types Some[Int] and Int do not adhere to the equality constraint selected for the === and !== operators; the missing implicit parameter is of type org.scalactic.CanEqual[Some[Int],Int] Some(2) should === (2) ^By default, the "
Some(2)should===(2)" statement would fail at runtime. By mixing in the equality constraints provided byTypeCheckedTripleEquals, however, the statement fails to compile. For more information and examples, see the main documentation for traitorg.scalactic.TypeCheckedTripleEquals.Checking size and length
You can check the size or length of any type of object for which it makes sense. Here's how checking for length looks:
result should have length 3Size is similar:
result should have size 10The
lengthsyntax can be used withString,Array, anyscala.collection.GenSeq, anyjava.util.List, and any typeTfor which an implicitLength[T]type class is available in scope. Similarly, thesizesyntax can be used withArray, anyscala.collection.GenTraversable, anyjava.util.Collection, anyjava.util.Map, and any typeTfor which an implicitSize[T]type class is available in scope. You can enable thelengthorsizesyntax for your own arbitrary types, therefore, by definingLengthorSizetype classes for those types.In addition, the
lengthsyntax can be used with any object that has a field or method namedlengthor a method namedgetLength. Similarly, thesizesyntax can be used with any object that has a field or method namedsizeor a method namedgetSize. The type of alengthorsizefield, or return type of a method, must be eitherIntorLong. Any such method must take no parameters. (The Scala compiler will ensure at compile time that the object on whichshouldis being invoked has the appropriate structure.)Checking strings
You can check for whether a string starts with, ends with, or includes a substring like this:
string should startWith ("Hello") string should endWith ("world") string should include ("seven")
You can check for whether a string starts with, ends with, or includes a regular expression, like this:
string should startWith regex "Hel*o" string should endWith regex "wo.ld" string should include regex "wo.ld"
And you can check whether a string fully matches a regular expression, like this:
string should fullyMatch regex """(-)?(\d+)(\.\d*)?"""The regular expression passed following the
regextoken can be either aStringor ascala.util.matching.Regex.With the
startWith,endWith,include, andfullyMatchtokens can also be used with an optional specification of required groups, like this:"abbccxxx" should startWith regex ("a(b*)(c*)" withGroups ("bb", "cc")) "xxxabbcc" should endWith regex ("a(b*)(c*)" withGroups ("bb", "cc")) "xxxabbccxxx" should include regex ("a(b*)(c*)" withGroups ("bb", "cc")) "abbcc" should fullyMatch regex ("a(b*)(c*)" withGroups ("bb", "cc"))
You can check whether a string is empty with
empty:s shouldBe empty
You can also use most of ScalaTest's matcher syntax for collections on
Stringby treating theStrings as collections of characters. For examples, see theStrings andArrays as collections section below.Greater and less than
You can check whether any type for which an implicit
Ordering[T]is available is greater than, less than, greater than or equal, or less than or equal to a value of typeT. The syntax is:one should be < 7 one should be > 0 one should be <= 7 one should be >= 0
Checking
Booleanproperties withbeIf an object has a method that takes no parameters and returns boolean, you can check it by placing a
Symbol(afterbe) that specifies the name of the method (excluding an optional prefix of "is"). A symbol literal in Scala begins with a tick mark and ends at the first non-identifier character. Thus,'traversableAgainresults in aSymbolobject at runtime, as does'completedand'file. Here's an example:iter shouldBe 'traversableAgainGiven this code, ScalaTest will use reflection to look on the object referenced from
emptySetfor a method that takes no parameters and results inBoolean, with either the nameemptyorisEmpty. If found, it will invoke that method. If the method returnstrue, execution will continue. But if it returnsfalse, aTestFailedExceptionwill be thrown that will contain a detail message, such as:non-empty iterator was not traversableAgain
This
besyntax can be used with any reference (AnyRef) type. If the object does not have an appropriately named predicate method, you'll get aTestFailedExceptionat runtime with a detailed message that explains the problem. (For the details on how a field or method is selected during this process, see the documentation forBeWord.)If you think it reads better, you can optionally put
aoranafterbe. For example,java.io.Filehas two predicate methods,isFileandisDirectory. Thus with aFileobject namedtemp, you could write:temp should be a 'fileOr, given
java.awt.event.KeyEventhas a methodisActionKeythat takes no arguments and returnsBoolean, you could assert that aKeyEventis an action key with:keyEvent should be an 'actionKeyIf you prefer to check
Booleanproperties in a type-safe manner, you can use aBePropertyMatcher. This would allow you to write expressions such as:xs shouldBe traversableAgain temp should be a file keyEvent should be an actionKey
These expressions would fail to compile if
shouldis used on an inappropriate type, as determined by the type parameter of theBePropertyMatcherbeing used. (For example,filein this example would likely be of typeBePropertyMatcher[java.io.File]. If used with an appropriate type, such an expression will compile and at run time theBooleanproperty method or field will be accessed directly; i.e., no reflection will be used. See the documentation forBePropertyMatcherfor more information.Using custom
BeMatchersIf you want to create a new way of using
be, which doesn't map to an actual property on the type you care about, you can create aBeMatcher. You could use this, for example, to createBeMatcher[Int]calledodd, which would match any oddInt, andeven, which would match any evenInt. Given this pair ofBeMatchers, you could check whether anIntwas odd or even with expressions like:num shouldBe odd num should not be even
For more information, see the documentation for
BeMatcher.Checking object identity
If you need to check that two references refer to the exact same object, you can write:
ref1 should be theSameInstanceAs ref2
Checking an object's class
If you need to check that an object is an instance of a particular class or trait, you can supply the type to “
bea” or “bean”:result1 shouldBe a [Tiger] result1 should not be an [Orangutan]
Because type parameters are erased on the JVM, we recommend you insert an underscore for any type parameters when using this syntax. Both of the following test only that the result is an instance of
List[_], because at runtime the type parameter has been erased:result shouldBe a [List[_]] // recommended result shouldBe a [List[Fruit]] // discouraged
Checking numbers against a range
Often you may want to check whether a number is within a range. You can do that using the
+-operator, like this:sevenDotOh should equal (6.9 +- 0.2) sevenDotOh should === (6.9 +- 0.2) sevenDotOh should be (6.9 +- 0.2) sevenDotOh shouldEqual 6.9 +- 0.2 sevenDotOh shouldBe 6.9 +- 0.2
Any of these expressions will cause a
TestFailedExceptionto be thrown if the floating point value,sevenDotOhis outside the range6.7to7.1. You can use+-with any typeTfor which an implicitNumeric[T]exists, such as integral types:seven should equal (6 +- 2) seven should === (6 +- 2) seven should be (6 +- 2) seven shouldEqual 6 +- 2 seven shouldBe 6 +- 2
Checking for emptiness
You can check whether an object is "empty", like this:
traversable shouldBe empty javaMap should not be empty
The
emptytoken can be used with any typeLfor which an implicitEmptiness[L]exists. TheEmptinesscompanion object provides implicits forGenTraversable[E],java.util.Collection[E],java.util.Map[K, V],String,Array[E], andOption[E]. In addition, theEmptinesscompanion object provides structural implicits for types that declare anisEmptymethod that returns aBoolean. Here are some examples:scala> import org.scalatest.matchers.should.Matchers._ import org.scalatest.matchers.should.Matchers._ scala> List.empty shouldBe empty scala> None shouldBe empty scala> Some(1) should not be empty scala> "" shouldBe empty scala> new java.util.HashMap[Int, Int] shouldBe empty scala> new { def isEmpty = true} shouldBe empty scala> Array(1, 2, 3) should not be emptyWorking with "containers"
You can check whether a collection contains a particular element like this:
traversable should contain ("five")The
containsyntax shown above can be used with any typeCthat has a "containing" nature, evidenced by an implicitorg.scalatest.enablers.Containing[L], whereLis left-hand type on whichshouldis invoked. In theContainingcompanion object, implicits are provided for typesGenTraversable[E],java.util.Collection[E],java.util.Map[K, V],String,Array[E], andOption[E]. Here are some examples:scala> import org.scalatest.matchers.should.Matchers._ import org.scalatest.matchers.should.Matchers._ scala> List(1, 2, 3) should contain (2) scala> Map('a' -> 1, 'b' -> 2, 'c' -> 3) should contain ('b' -> 2) scala> Set(1, 2, 3) should contain (2) scala> Array(1, 2, 3) should contain (2) scala> "123" should contain ('2') scala> Some(2) should contain (2)ScalaTest's implicit methods that provide the
Containing[L]type classes require anEquality[E], whereEis an element type. For example, to obtain aContaining[Array[Int]]you must supply anEquality[Int], either implicitly or explicitly. Thecontainsyntax uses thisEquality[E]to determine containership. Thus if you want to change how containership is determined for an element typeE, place an implicitEquality[E]in scope or use the explicitly DSL. Although the implicit parameter required for thecontainsyntax is of typeContaining[L], implicit conversions are provided in theContainingcompanion object fromEquality[E]to the various types of containers ofE. Here's an example:scala> import org.scalatest.matchers.should.Matchers._ import org.scalatest.matchers.should.Matchers._ scala> List("Hi", "Di", "Ho") should contain ("ho") org.scalatest.exceptions.TestFailedException: List(Hi, Di, Ho) did not contain element "ho" at ... scala> import org.scalactic.Explicitly._ import org.scalactic.Explicitly._ scala> import org.scalactic.StringNormalizations._ import org.scalactic.StringNormalizations._ scala> (List("Hi", "Di", "Ho") should contain ("ho")) (after being lowerCased)Note that when you use the explicitly DSL with
containyou need to wrap the entirecontainexpression in parentheses, as shown here.(List("Hi", "Di", "Ho") should contain ("ho")) (after being lowerCased) ^ ^In addition to determining whether an object contains another object, you can use
containto make other determinations. For example, thecontainoneOfsyntax ensures that one and only one of the specified elements are contained in the containing object:List(1, 2, 3, 4, 5) should contain oneOf (5, 7, 9) Some(7) should contain oneOf (5, 7, 9) "howdy" should contain oneOf ('a', 'b', 'c', 'd')
Note that if multiple specified elements appear in the containing object,
oneOfwill fail:scala> List(1, 2, 3) should contain oneOf (2, 3, 4) org.scalatest.exceptions.TestFailedException: List(1, 2, 3) did not contain one (and only one) of (2, 3, 4) at ...If you really want to ensure one or more of the specified elements are contained in the containing object, use
atLeastOneOf, described below, instead ofoneOf. Keep in mind,oneOfmeans "exactly one of."Note also that with any
containsyntax, you can place custom implicitEquality[E]instances in scope to customize how containership is determined, or use the explicitly DSL. Here's an example:(Array("Doe", "Ray", "Me") should contain oneOf ("X", "RAY", "BEAM")) (after being lowerCased)
If you have a collection of elements that you'd like to use in a "one of" comparison, you can use "oneElementOf," like this:
List(1, 2, 3, 4, 5) should contain oneElementOf List(5, 7, 9) Some(7) should contain oneElementOf Vector(5, 7, 9) "howdy" should contain oneElementOf Set('a', 'b', 'c', 'd') (Array("Doe", "Ray", "Me") should contain oneElementOf List("X", "RAY", "BEAM")) (after being lowerCased)
The
containnoneOfsyntax does the opposite ofoneOf: it ensures none of the specified elements are contained in the containing object:List(1, 2, 3, 4, 5) should contain noneOf (7, 8, 9) Some(0) should contain noneOf (7, 8, 9) "12345" should contain noneOf ('7', '8', '9')
If you have a collection of elements that you'd like to use in a "none of" comparison, you can use "noElementsOf," like this:
List(1, 2, 3, 4, 5) should contain noElementsOf List(7, 8, 9) Some(0) should contain noElementsOf Vector(7, 8, 9) "12345" should contain noElementsOf Set('7', '8', '9')
Working with "aggregations"
As mentioned, the "
contain," "containoneOf," and "containnoneOf" syntax requires aContaining[L]be provided, whereLis the left-hand type. Othercontainsyntax, which will be described in this section, requires anAggregating[L]be provided, where againLis the left-hand type. (AnAggregating[L]instance defines the "aggregating nature" of a typeL.) The reason, essentially, is thatcontainsyntax that makes sense forOptionis enabled byContaining[L], whereas syntax that does not make sense forOptionis enabled byAggregating[L]. For example, it doesn't make sense to assert that anOption[Int]contains all of a set of integers, as it could only ever contain one of them. But this does make sense for a type such asList[Int]that can aggregate zero to many integers.The
Aggregatingcompanion object provides implicit instances ofAggregating[L]for typesGenTraversable[E],java.util.Collection[E],java.util.Map[K, V],String,Array[E]. Note that these are the same types as are supported withContaining, but withOption[E]missing. Here are some examples:The
containatLeastOneOfsyntax, for example, works for any typeLfor which anAggregating[L]exists. It ensures that at least one of (i.e., one or more of) the specified objects are contained in the containing object:List(1, 2, 3) should contain atLeastOneOf (2, 3, 4) Array(1, 2, 3) should contain atLeastOneOf (3, 4, 5) "abc" should contain atLeastOneOf ('c', 'a', 't')
Similar to
Containing[L], the implicit methods that provide theAggregating[L]instances require anEquality[E], whereEis an element type. For example, to obtain aAggregating[Vector[String]]you must supply anEquality[String], either implicitly or explicitly. Thecontainsyntax uses thisEquality[E]to determine containership. Thus if you want to change how containership is determined for an element typeE, place an implicitEquality[E]in scope or use the explicitly DSL. Although the implicit parameter required for thecontainsyntax is of typeAggregating[L], implicit conversions are provided in theAggregatingcompanion object fromEquality[E]to the various types of aggregations ofE. Here's an example:(Vector(" A", "B ") should contain atLeastOneOf ("a ", "b", "c")) (after being lowerCased and trimmed)
If you have a collection of elements that you'd like to use in an "at least one of" comparison, you can use "atLeastOneElementOf," like this:
List(1, 2, 3) should contain atLeastOneElementOf List(2, 3, 4) Array(1, 2, 3) should contain atLeastOneElementOf Vector(3, 4, 5) "abc" should contain atLeastOneElementOf Set('c', 'a', 't') (Vector(" A", "B ") should contain atLeastOneElementOf List("a ", "b", "c")) (after being lowerCased and trimmed)
The "
containatMostOneOf" syntax lets you specify a set of objects at most one of which should be contained in the containing object:List(1, 2, 3, 4, 5) should contain atMostOneOf (5, 6, 7)
If you have a collection of elements that you'd like to use in a "at most one of" comparison, you can use "atMostOneElementOf," like this:
List(1, 2, 3, 4, 5) should contain atMostOneElementOf Vector(5, 6, 7)
The "
containallOf" syntax lets you specify a set of objects that should all be contained in the containing object:List(1, 2, 3, 4, 5) should contain allOf (2, 3, 5)
If you have a collection of elements that you'd like to use in a "all of" comparison, you can use "allElementsOf," like this:
List(1, 2, 3, 4, 5) should contain allElementsOf Array(2, 3, 5)
The "
containonly" syntax lets you assert that the containing object contains only the specified objects, though it may contain more than one of each:List(1, 2, 3, 2, 1) should contain only (1, 2, 3)
The "
containtheSameElementsAs" and "containtheSameElementsInOrderAssyntax differ from the others in that the right hand side is aGenTraversable[_]rather than a varargs ofAny. (Note: in a future 2.0 milestone release, possibly 2.0.M6, these will likely be widened to accept any typeRfor which anAggregating[R]exists.)The "
containtheSameElementsAs" syntax lets you assert that two aggregations contain the same objects:List(1, 2, 2, 3, 3, 3) should contain theSameElementsAs Vector(3, 2, 3, 1, 2, 3)
The number of times any family of equal objects appears must also be the same in both the left and right aggregations. The specified objects may appear multiple times, but must appear in the order they appear in the right-hand list. For example, if the last 3 element is left out of the right-hand list in the previous example, the expression would fail because the left side has three 3's and the right hand side has only two:
List(1, 2, 2, 3, 3, 3) should contain theSameElementsAs Vector(3, 2, 3, 1, 2) org.scalatest.exceptions.TestFailedException: List(1, 2, 2, 3, 3, 3) did not contain the same elements as Vector(3, 2, 3, 1, 2) at ...Note that no
onlyElementsOfmatcher is provided, because it would have the same behavior astheSameElementsAs. (I.e., if you were looking foronlyElementsOf, please usetheSameElementsAsinstead.)Working with "sequences"
The rest of the
containsyntax, which will be described in this section, requires aSequencing[L]be provided, where againLis the left-hand type. (ASequencing[L]instance defines the "sequencing nature" of a typeL.) The reason, essentially, is thatcontainsyntax that implies an "order" of elements makes sense only for types that place elements in a sequence. For example, it doesn't make sense to assert that aMap[String, Int]orSet[Int]contains all of a set of integers in a particular order, as these types don't necessarily define an order for their elements. But this does make sense for a type such asSeq[Int]that does define an order for its elements.The
Sequencingcompanion object provides implicit instances ofSequencing[L]for typesGenSeq[E],java.util.List[E],String, andArray[E]. Here are some examples:Similar to
Containing[L], the implicit methods that provide theAggregating[L]instances require anEquality[E], whereEis an element type. For example, to obtain aAggregating[Vector[String]]you must supply anEquality[String], either implicitly or explicitly. Thecontainsyntax uses thisEquality[E]to determine containership. Thus if you want to change how containership is determined for an element typeE, place an implicitEquality[E]in scope or use the explicitly DSL. Although the implicit parameter required for thecontainsyntax is of typeAggregating[L], implicit conversions are provided in theAggregatingcompanion object fromEquality[E]to the various types of aggregations ofE. Here's an example:The "
containinOrderOnly" syntax lets you assert that the containing object contains only the specified objects, in order. The specified objects may appear multiple times, but must appear in the order they appear in the right-hand list. Here's an example:List(1, 2, 2, 3, 3, 3) should contain inOrderOnly (1, 2, 3)
The "
containinOrder" syntax lets you assert that the containing object contains only the specified objects in order, likeinOrderOnly, but allows other objects to appear in the left-hand aggregation as well: contain more than one of each:List(0, 1, 2, 2, 99, 3, 3, 3, 5) should contain inOrder (1, 2, 3)
If you have a collection of elements that you'd like to use in a "in order" comparison, you can use "inOrderElementsOf," like this:
List(0, 1, 2, 2, 99, 3, 3, 3, 5) should contain inOrderElementsOf Array(1, 2, 3)
Note that "order" in
inOrder,inOrderOnly, andtheSameElementsInOrderAs(described below) in theAggregation[L]instances built-in to ScalaTest is defined as "iteration order".Lastly, the "
containtheSameElementsInOrderAs" syntax lets you assert that two aggregations contain the same exact elements in the same (iteration) order:List(1, 2, 3) should contain theSameElementsInOrderAs collection.mutable.TreeSet(3, 2, 1)
The previous assertion succeeds because the iteration order of a
TreeSetis the natural ordering of its elements, which in this case is 1, 2, 3. An iterator obtained from the left-handListwill produce the same elements in the same order.Note that no
inOrderOnlyElementsOfmatcher is provided, because it would have the same behavior astheSameElementsInOrderAs. (I.e., if you were looking forinOrderOnlyElementsOf, please usetheSameElementsInOrderAsinstead.)Working with "sortables"
You can also ask whether the elements of "sortable" objects (such as
Arrays, JavaLists, andGenSeqs) are in sorted order, like this:List(1, 2, 3) shouldBe sorted
Working with iterators
Although it seems desireable to provide similar matcher syntax for Scala and Java iterators to that provided for sequences like
Seqs,Array, andjava.util.List, the ephemeral nature of iterators makes this problematic. Some syntax (such asshouldcontain) is relatively straightforward to support on iterators, but other syntax (such as, for example,Inspectorexpressions on nested iterators) is not. Rather than allowing inconsistencies between sequences and iterators in the API, we chose to not support any such syntax directly on iterators:scala> val it = List(1, 2, 3).iterator it: Iterator[Int] = non-empty iterator
scala> it should contain (2) <console>:15: error: could not find implicit value for parameter typeClass1: org.scalatest.enablers.Containing[Iterator[Int]] it should contain (2) ^Instead, you will need to convert your iterators to a sequence explicitly before using them in matcher expressions:
scala> it.toStream should contain (2)We recommend you convert (Scala or Java) iterators to
Streams, as shown in the previous example, so that you can continue to reap any potential benefits provided by the laziness of the underlying iterator.Inspector shorthands
You can use the
Inspectorssyntax with matchers as well as assertions. If you have a multi-dimensional collection, such as a list of lists, usingInspectorsis your best option:val yss = List( List(1, 2, 3), List(1, 2, 3), List(1, 2, 3) )
forAll (yss) { ys => forAll (ys) { y => y should be > 0 } }For assertions on one-dimensional collections, however, matchers provides "inspector shorthands." Instead of writing:
val xs = List(1, 2, 3) forAll (xs) { x => x should be < 10 }
You can write:
all (xs) should be < 10The previous statement asserts that all elements of the
xslist should be less than 10. All of the inspectors have shorthands in matchers. Here is the full list:all- succeeds if the assertion holds true for every elementatLeast- succeeds if the assertion holds true for at least the specified number of elementsatMost- succeeds if the assertion holds true for at most the specified number of elementsbetween- succeeds if the assertion holds true for between the specified minimum and maximum number of elements, inclusiveevery- same asall, but lists all failing elements if it fails (whereasalljust reports the first failing element)exactly- succeeds if the assertion holds true for exactly the specified number of elements
Here are some examples:
scala> import org.scalatest.matchers.should.Matchers._ import org.scalatest.matchers.should.Matchers._ scala> val xs = List(1, 2, 3, 4, 5) xs: List[Int] = List(1, 2, 3, 4, 5) scala> all (xs) should be > 0 scala> atMost (2, xs) should be >= 4 scala> atLeast (3, xs) should be < 5 scala> between (2, 3, xs) should (be > 1 and be < 5) scala> exactly (2, xs) should be <= 2 scala> every (xs) should be < 10 scala> // And one that fails... scala> exactly (2, xs) shouldEqual 2 org.scalatest.exceptions.TestFailedException: 'exactly(2)' inspection failed, because only 1 element satisfied the assertion block at index 1: at index 0, 1 did not equal 2, at index 2, 3 did not equal 2, at index 3, 4 did not equal 2, at index 4, 5 did not equal 2 in List(1, 2, 3, 4, 5) at ...Like
Inspectors, objects used with inspector shorthands can be any typeTfor which aCollecting[T, E]is availabe, which by default includesGenTraversable, JavaCollection, JavaMap,Arrays, andStrings. Here are some examples:scala> import org.scalatest._ import org.scalatest._ scala> import matchers.should.Matchers._ import matchers.should.Matchers._ scala> all (Array(1, 2, 3)) 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> all (js) should be < 5 scala> val jmap = Map("a" -> 1, "b" -> 2).asJava jmap: java.util.Map[String,Int] = {a=1, b=2} scala> atLeast(1, jmap) shouldBe Entry("b", 2) scala> atLeast(2, "hello, world!") shouldBe 'o'Single-element collections
To assert both that a collection contains just one "lone" element as well as something else about that element, you can use the
loneElementsyntax provided by traitLoneElement. For example, if aSet[Int]should contain just one element, anIntless than or equal to 10, you could write:import LoneElement._ set.loneElement should be <= 10
You can invoke
loneElementon any typeTfor which an implicitCollecting[E, T]is available, whereEis the element type returned by theloneElementinvocation. By default, you can useloneElementonGenTraversable, JavaCollection, JavaMap,Array, andString.Java collections and maps
You can use similar syntax on Java collections (
java.util.Collection) and maps (java.util.Map). For example, you can check whether a JavaCollectionorMapisempty, like this:javaCollection should be ('empty) javaMap should be ('empty)
Even though Java's
Listtype doesn't actually have alengthorgetLengthmethod, you can nevertheless check the length of a JavaList(java.util.List) like this:javaList should have length 9You can check the size of any Java
CollectionorMap, like this:javaMap should have size 20 javaSet should have size 90
In addition, you can check whether a Java
Collectioncontains a particular element, like this:javaCollection should contain ("five")One difference to note between the syntax supported on Java and Scala collections is that in Java,
Mapis not a subtype ofCollection, and does not actually define an element type. You can ask a JavaMapfor an "entry set" via theentrySetmethod, which will return theMap's key/value pairs wrapped in a set ofjava.util.Map.Entry, but aMapis not actually a collection ofEntry. To make JavaMaps easier to work with, however, ScalaTest matchers allows you to treat a JavaMapas a collection ofEntry, and defines a convenience implementation ofjava.util.Map.Entryinorg.scalatest.Entry. Here's how you use it:javaMap should contain (Entry(2, 3)) javaMap should contain oneOf (Entry(2, 3), Entry(3, 4))
You can you alse just check whether a Java
Mapcontains a particular key, or value, like this:javaMap should contain key 1 javaMap should contain value "Howdy"
Strings andArrays as collectionsYou can also use all the syntax described above for Scala and Java collections on
Arrays andStrings. Here are some examples:scala> import org.scalatest._ import org.scalatest._ scala> import matchers.should.Matchers._ import matchers.should.Matchers._ scala> atLeast (2, Array(1, 2, 3)) should be > 1 scala> atMost (2, "halloo") shouldBe 'o' scala> Array(1, 2, 3) shouldBe sorted scala> "abcdefg" shouldBe sorted scala> Array(1, 2, 3) should contain atMostOneOf (3, 4, 5) scala> "abc" should contain atMostOneOf ('c', 'd', 'e')beas an equality comparisonAll uses of
beother than those shown previously perform an equality comparison. They work the same asequalwhen it is used with default equality. This redundancy betweenbeandequalsexists in part because it enables syntax that sometimes sounds more natural. For example, instead of writing:result should equal (null)You can write:
result should be (null)(Hopefully you won't write that too much given
nullis error prone, andOptionis usually a better, well, option.) As mentioned previously, the other difference betweenequalandbeis thatequaldelegates the equality check to anEqualitytypeclass, whereasbealways uses default equality. Here are some other examples ofbeused for equality comparison:sum should be (7.0) boring should be (false) fun should be (true) list should be (Nil) option should be (None) option should be (Some(1))
As with
equalused with default equality, usingbeon arrays results indeepbeing called on both arrays prior to callingequal. As a result, the following expression would not throw aTestFailedException:Array(1, 2) should be (Array(1, 2)) // succeeds (i.e., does not throw TestFailedException)
Because
beis used in several ways in ScalaTest matcher syntax, just as it is used in many ways in English, one potential point of confusion in the event of a failure is determining whetherbewas being used as an equality comparison or in some other way, such as a property assertion. To make it more obvious whenbeis being used for equality, the failure messages generated for those equality checks will include the wordequalin them. For example, if this expression fails with aTestFailedException:option should be (Some(1))
The detail message in that
TestFailedExceptionwill include the words"equal to"to signifybewas in this case being used for equality comparison:Some(2) was not equal to Some(1)
Being negative
If you wish to check the opposite of some condition, you can simply insert
notin the expression. Here are a few examples:result should not be (null) sum should not be <= (10) mylist should not equal (yourList) string should not startWith ("Hello")
Checking that a snippet of code does not compile
Often when creating libraries you may wish to ensure that certain arrangements of code that represent potential “user errors” do not compile, so that your library is more error resistant. ScalaTest
Matcherstrait includes the following syntax for that purpose:"val a: String = 1" shouldNot compileIf you want to ensure that a snippet of code does not compile because of a type error (as opposed to a syntax error), use:
"val a: String = 1" shouldNot typeCheckNote that the
shouldNottypeChecksyntax will only succeed if the given snippet of code does not compile because of a type error. A syntax error will still result on a thrownTestFailedException.If you want to state that a snippet of code does compile, you can make that more obvious with:
"val a: Int = 1" should compileAlthough the previous three constructs are implemented with macros that determine at compile time whether the snippet of code represented by the string does or does not compile, errors are reported as test failures at runtime.
Logical expressions with
andandorYou can also combine matcher expressions with
andand/oror, however, you must place parentheses or curly braces around theandororexpression. For example, thisand-expression would not compile, because the parentheses are missing:map should contain key ("two") and not contain value (7) // ERROR, parentheses missing!
Instead, you need to write:
map should (contain key ("two") and not contain value (7))
Here are some more examples:
number should (be > (0) and be <= (10)) option should (equal (Some(List(1, 2, 3))) or be (None)) string should ( equal ("fee") or equal ("fie") or equal ("foe") or equal ("fum") )
Two differences exist between expressions composed of these
andandoroperators and the expressions you can write on regularBooleans using its&&and||operators. First, expressions withandandordo not short-circuit. The following contrived expression, for example, would print"hello, world!":"yellow" should (equal ("blue") and equal { println("hello, world!"); "green" })
In other words, the entire
andororexpression is always evaluated, so you'll see any side effects of the right-hand side even if evaluating only the left-hand side is enough to determine the ultimate result of the larger expression. Failure messages produced by these expressions will "short-circuit," however, mentioning only the left-hand side if that's enough to determine the result of the entire expression. This "short-circuiting" behavior of failure messages is intended to make it easier and quicker for you to ascertain which part of the expression caused the failure. The failure message for the previous expression, for example, would be:"yellow" did not equal "blue"
Most likely this lack of short-circuiting would rarely be noticeable, because evaluating the right hand side will usually not involve a side effect. One situation where it might show up, however, is if you attempt to
andanullcheck on a variable with an expression that uses the variable, like this:map should (not be (null) and contain key ("ouch"))
If
mapisnull, the test will indeed fail, but with aNullArgumentException, not aTestFailedException. Here, theNullArgumentExceptionis the visible right-hand side effect. To get aTestFailedException, you would need to check each assertion separately:map should not be (null) map should contain key ("ouch")
If
mapisnullin this case, thenullcheck in the first expression will fail with aTestFailedException, and the second expression will never be executed.The other difference with
Booleanoperators is that although&&has a higher precedence than||,andandorhave the same precedence. Thus although theBooleanexpression(a || b && c)will evaluate the&&expression before the||expression, like(a || (b && c)), the following expression:traversable should (contain (7) or contain (8) and have size (9))
Will evaluate left to right, as:
traversable should ((contain (7) or contain (8)) and have size (9))
If you really want the
andpart to be evaluated first, you'll need to put in parentheses, like this:traversable should (contain (7) or (contain (8) and have size (9)))
Working with
OptionsYou can work with options using ScalaTest's equality,
empty,defined, andcontainsyntax. For example, if you wish to check whether an option isNone, you can write any of:option shouldEqual None option shouldBe None option should === (None) option shouldBe empty
If you wish to check an option is defined, and holds a specific value, you can write any of:
option shouldEqual Some("hi") option shouldBe Some("hi") option should === (Some("hi"))
If you only wish to check that an option is defined, but don't care what it's value is, you can write:
option shouldBe defined
If you mix in (or import the members of)
OptionValues, you can write one statement that indicates you believe an option should be defined and then say something else about its value. Here's an example:import org.scalatest.OptionValues._ option.value should be < 7
As mentioned previously, you can use also use ScalaTest's
contain,contain oneOf, andcontain noneOfsyntax with options:Some(2) should contain (2) Some(7) should contain oneOf (5, 7, 9) Some(0) should contain noneOf (7, 8, 9)
Checking arbitrary properties with
haveUsing
have, you can check properties of any type, where a property is an attribute of any object that can be retrieved either by a public field, method, or JavaBean-stylegetorismethod, like this:book should have ( 'title ("Programming in Scala"), 'author (List("Odersky", "Spoon", "Venners")), 'pubYear (2008) )
This expression will use reflection to ensure the
title,author, andpubYearproperties of objectbookare equal to the specified values. For example, it will ensure thatbookhas either a public Java field or method namedtitle, or a public method namedgetTitle, that when invoked (or accessed in the field case) results in a the string"Programming in Scala". If all specified properties exist and have their expected values, respectively, execution will continue. If one or more of the properties either does not exist, or exists but results in an unexpected value, aTestFailedExceptionwill be thrown that explains the problem. (For the details on how a field or method is selected during this process, see the documentation forHavePropertyMatcherGenerator.)When you use this syntax, you must place one or more property values in parentheses after
have, seperated by commas, where a property value is a symbol indicating the name of the property followed by the expected value in parentheses. The only exceptions to this rule is the syntax for checking size and length shown previously, which does not require parentheses. If you forget and put parentheses in, however, everything will still work as you'd expect. Thus instead of writing:array should have length (3) set should have size (90)
You can alternatively, write:
array should have (length (3)) set should have (size (90))
If a property has a value different from the specified expected value, a
TestFailedErrorwill be thrown with a detailed message that explains the problem. For example, if you assert the following on abookwhose title isMoby Dick:book should have ('title ("A Tale of Two Cities"))
You'll get a
TestFailedExceptionwith this detail message:The title property had value "Moby Dick", instead of its expected value "A Tale of Two Cities", on object Book("Moby Dick", "Melville", 1851)If you prefer to check properties in a type-safe manner, you can use a
HavePropertyMatcher. This would allow you to write expressions such as:book should have ( title ("Programming in Scala"), author (List("Odersky", "Spoon", "Venners")), pubYear (2008) )
These expressions would fail to compile if
shouldis used on an inappropriate type, as determined by the type parameter of theHavePropertyMatcherbeing used. (For example,titlein this example might be of typeHavePropertyMatcher[org.publiclibrary.Book]. If used with an appropriate type, such an expression will compile and at run time the property method or field will be accessed directly; i.e., no reflection will be used. See the documentation forHavePropertyMatcherfor more information.Using
lengthandsizewithHavePropertyMatchersIf you want to use
lengthorsizesyntax with your own customHavePropertyMatchers, you can do so, but you must write(of [“the type”])afterwords. For example, you could write:book should have ( title ("A Tale of Two Cities"), length (220) (of [Book]), author ("Dickens") )
Prior to ScalaTest 2.0, “
length(22)” yielded aHavePropertyMatcher[Any, Int]that used reflection to dynamically look for alengthfield orgetLengthmethod. In ScalaTest 2.0, “length(22)” yields aMatcherFactory1[Any, Length], so it is no longer aHavePropertyMatcher. The(of [<type>])syntax converts the theMatcherFactory1[Any, Length]to aHavePropertyMatcher[<type>, Int].Checking that an expression matches a pattern
ScalaTest's
Insidetrait allows you to make assertions after a pattern match. Here's an example:case class Name(first: String, middle: String, last: String)
val name = Name("Jane", "Q", "Programmer")
inside(name) { case Name(first, _, _) => first should startWith ("S") }You can use
insideto just ensure a pattern is matched, without making any further assertions, but a better alternative for that kind of assertion ismatchPattern. ThematchPatternsyntax allows you to express that you expect a value to match a particular pattern, no more and no less:name should matchPattern { case Name("Sarah", _, _) => }Using custom matchers
If none of the built-in matcher syntax (or options shown so far for extending the syntax) satisfy a particular need you have, you can create custom
Matchers that allow you to place your own syntax directly aftershould. For example, classjava.io.Filehas a methodisHidden, which indicates whether a file of a certain path and name is hidden. Because theisHiddenmethod takes no parameters and returnsBoolean, you can call it usingbewith a symbol orBePropertyMatcher, yielding assertions like:file should be ('hidden) // using a symbol file should be (hidden) // using a BePropertyMatcher
If it doesn't make sense to have your custom syntax follow
be, you might want to create a customMatcherinstead, so your syntax can followshoulddirectly. For example, you might want to be able to check whether ajava.io.File's name ends with a particular extension, like this:// using a plain-old Matcher file should endWithExtension ("txt")
ScalaTest provides several mechanism to make it easy to create custom matchers, including ways to compose new matchers out of existing ones complete with new error messages. For more information about how to create custom
Matchers, please see the documentation for theMatchertrait.Checking for expected exceptions
Sometimes you need to test whether a method throws an expected exception under certain circumstances, such as when invalid arguments are passed to the method. With
Matchersmixed in, you can check for an expected exception like this:an [IndexOutOfBoundsException] should be thrownBy s.charAt(-1)
If
charAtthrows an instance ofStringIndexOutOfBoundsException, this expression will result in that exception. But ifcharAtcompletes normally, or throws a different exception, this expression will complete abruptly with aTestFailedException.If you need to further isnpect an expected exception, you can capture it using this syntax:
val thrown = the [IndexOutOfBoundsException] thrownBy s.charAt(-1)
This expression returns the caught exception so that you can inspect it further if you wish, for example, to ensure that data contained inside the exception has the expected values. Here's an example:
thrown.getMessage should equal ("String index out of range: -1")If you prefer you can also capture and inspect an expected exception in one statement, like this:
the [ArithmeticException] thrownBy 1 / 0 should have message "/ by zero" the [IndexOutOfBoundsException] thrownBy { s.charAt(-1) } should have message "String index out of range: -1"
You can also state that no exception should be thrown by some code, like this:
noException should be thrownBy 0 / 1
Those pesky parens
Perhaps the most tricky part of writing assertions using ScalaTest matchers is remembering when you need or don't need parentheses, but bearing in mind a few simple rules should help. It is also reassuring to know that if you ever leave off a set of parentheses when they are required, your code will not compile. Thus the compiler will help you remember when you need the parens. That said, the rules are:
1. Although you don't always need them, you may choose to always put parentheses around right-hand values, such as the
7innum should equal (7):result should equal (4) array should have length (3) book should have ( 'title ("Programming in Scala"), 'author (List("Odersky", "Spoon", "Venners")), 'pubYear (2008) ) option should be ('defined) catMap should (contain key (9) and contain value ("lives")) keyEvent should be an ('actionKey) javaSet should have size (90)
2. Except for
length,sizeandmessage, you must always put parentheses around the list of one or more property values following ahave:file should (exist and have ('name ("temp.txt"))) book should have ( title ("Programming in Scala"), author (List("Odersky", "Spoon", "Venners")), pubYear (2008) ) javaList should have length (9) // parens optional for length and size
3. You must always put parentheses around
andandorexpressions, as in:catMap should (contain key (9) and contain value ("lives")) number should (equal (2) or equal (4) or equal (8))
4. Although you don't always need them, you may choose to always put parentheses around custom
Matchers when they appear directly afternot:file should exist file should not (exist) file should (exist and have ('name ("temp.txt"))) file should (not (exist) and have ('name ("temp.txt")) file should (have ('name ("temp.txt") or exist) file should (have ('name ("temp.txt") or not (exist))
That's it. With a bit of practice it should become natural to you, and the compiler will always be there to tell you if you forget a set of needed parentheses.
Note: ScalaTest's matchers are in part inspired by the matchers of RSpec, Hamcrest, and specs2, and its “
shouldNot compile” syntax by theillTypedmacro of shapeless.
Value Members
- object Matchers extends Matchers
Companion object that facilitates the importing of
Matchersmembers as an alternative to mixing it the trait.Companion object that facilitates the importing of
Matchersmembers as an alternative to mixing it the trait. One use case is to importMatchersmembers so you can use them in the Scala interpreter.