The most frequent way to specify some expected behaviour with specs2 is to use matchers. You generally execute an action, a command or a function and then check if the actual value you get is equal to an expected one (the “arrange-act-assert” paradigm).
For example, if you create a specification for an object manipulating paths:
// describe the functionality
s2"the directoryPath method should return well-formed paths $e1"
// give an example with some code
def e1 = Paths.directoryPath("/tmp/path/to/dir") must beEqualTo("/tmp/path/to/dir/")
The must operator takes the actual value returned by directoryPath and applies it to a Matcher built with the expected value. beEqualTo is one of the many matchers defined by specs2, it just checks if 2 values are equal.
In the following sections you will learn:
the different ways of checking the equality of values
how to use the matchers for the most common data types in Scala, and most notably collections
The most common type of matcher is beEqualTo to test the equality of 2 values with the underlying == operator where:
the compared types must be the same (as if the language:strictEquality option has been turned on)
the comparison of Arrays uses the .deep method on Arrays, transforming them to IndexedSeqs (possibly nested) Otherwise == on arrays uses the reference equality, so that Array(1, 2, 3) === Array(1, 2, 3), despite the fact that Array(1, 2, 3) != Array(1, 2, 3)
Several syntaxes can be used, according to your own taste
Matcher
Comment
1 must beEqualTo(1)
the normal way
1 must be_==(1)
with a symbol
1 should be_==(1)
for should lovers
1 === 1
the ultimate shortcut
There are also other notions of equality:
Matcher
Comment
be_==~
check if (a: A) === conversion(b: B) when there is an implicit conversion Conversion[B, A]
beTheSameAs
reference equality: check if a eq b (a must be(b) also works)
be
a must be(b): synonym for beTheSameAs
beTrue, beFalse
shortcuts for Boolean equality
beLike
partial equality, using a PartialFunction[T, Result]: (1, 2) must beLike { case (1, _) => ok }
Now let’s check out the other matchers.
Out of the box
These are the all the available matchers when you extend Specification:
Optional
Those matchers are optional. To use them, you need to add a new trait to your specification.
Those are additional “data” matchers:
Optional data matchers
That's only if you want to check the result of other matchers!
// you need to extend the ResultMatchers trait
class MatchersSpec extends Specification with matcher.ResultMatchers:
def is =
"beMatching is using a regexp" ! {
("Hello" must beMatching("h.*")) must beSuccessful
}
Sometimes you just want to specify that a block of code is going to terminate.
The org.specs2.matcher.TerminationMatchers trait is here to help. If you mix in that trait, you can write:
Thread.sleep(100) must terminate
// the default is retries = 0, sleep = 100.millis
Thread.sleep(100) must terminate(retries = 1, sleep = 60.millis)
Note that the behaviour of this matcher is a bit different from the eventually operator. In this case, we let the current Thread sleep during the given sleep time and then we check if the computation is finished, then, we retry for the given number of retries.
In a further scenario, we might want to check that triggering another action is able to unblock the first one:
action must terminate.when(action2)
action must terminate.when("starting the second action", action2)
action must terminate(retries = 3, sleep = 100.millis).when(action2)
When a second action is specified like that, Action will be started and action2 will be started on the first retry. Otherwise, if you want to specify that Action can only terminate when action2 is started, you write:
The terminate matcher needs an implicit ExecutionEnv to be used. See the page to learn how to get one.
Those matchers can be used to check “content”:
Optional content matchers
It is very useful to have literal Xml in Scala, it is even more useful to have matchers for it! If you want to use those matchers you need to extend the org.specs2.matcher.XmlMatchers trait:
beEqualToIgnoringSpace compares 2 Nodes, without considering spaces <a><b/></a> must ==/(<a> <b/></a>) <a><b/></a> must beEqualToIgnoringSpace(<a> <b/></a>)
beEqualToIgnoringSpace can also do an ordered comparison <a><c/> <b/></a> must ==/(<a> <c/><b/></a>).ordered
on the other hand beEqualToIgnoringSpace will not check attributes order <n a="1" b="2"/> must ==/(<n b="2" a="1"/>)
the \\ matcher is an XPath-like matcher matching if a node is a direct child of another <a><b/></a> must \\\\("b")
You can also check attribute names <a><b name="value"></b></a> must \\("b", "name")
And attribute names and values as well (values are checked using a regular expression, use the quote method if you want an exact match) <a><b n="v" n2="v2" n3="v3"></b></a> must \\("b", "n"->"v", "n2"->"v\\d")
Or the content of a Text node <a>hello</a> must \\("a") \\> "hello" (alias textIs) <a>hello</a> must \\("a") \\>~ "h.*" (alias textMatches)
The equivalent of \\ for a "deep" match is simply \\\\ <a><s><c></c></s></a> must \\\\("c")
Json is a simple data format essentially modeling recursive key-values.
You can use the following matchers provided by the org.specs2.matcher.JsonMatchers trait to check JSON strings:
/(value) checks if a value is present at the root of the document. This can only be the case if that document is an Array
/(regex) checks if a value matching the regex is present at the root of the document. This can only be the case if that document is an Array
/(key -> value) checks if a pair is present at the root of the document. This can only be the case if that document is a Map
*/(value) checks if a value is present anywhere in the document, either as an entry in an Array, or as the value for a key in a Map
*/(key -> value) checks if a pair is present anywhere in a Map of the document
/#(i) selects the ith element in a 0-based indexed Array or a Map and allow further checks on that element
Now the interesting part comes from the fact that those matchers can be chained to search specific paths in the Json document. For example, for the following document:
// taken from an example in the Lift project
val person = """{
"person": {
"name": "Joe",
"age": 35,
"spouse": {
"person": {
"name": "Marilyn",
"age": 33
}
}
}
}
"""
You can use these combinations:
person must /("person") */ ("person") / ("age" -> 33.0) // by default numbers are parsed as Doubles
You can as well use regular expressions or String matchers instead of values to verify the presence of keys or elements. For example:
person must /("p.*".r) */ ".*on".r / ("age" -> "\\d+\\.\\d".r)
person must /("p.*".r) */ ".*on".r / ("age" -> startWith("3"))
person must /("p.*".r) */ ".*on".r / ("age" -> (be_>(30) ^^ ((_: String).toInt)))
You can also access some records by their index:
person must /("person") /# 2 / "person"
Finally you can use Json matchers to match elements in an array:
The andHave method accepts any Matcher[JsonType] where JsonType is either JsonArray, JsonMap, JsonNumber, JsonString, JsonNull. In the example above we pass directly shirt and 10 as Matcher[JsonType] because there are implicit conversions from Int, Boolean, Double, String and Traversable[String] matchers (like allOf) to a Matcher[JsonType].
The Java api for files is more or less mimicked as matchers which can operate on strings denoting paths or on Files (with the org.specs2.matcher.FileMatchers trait)
beEqualToIgnoringSep checks if 2 paths are the same regardless of their separators "c:\temp\hello" must beEqualToIgnoringSep("c:/temp/hello")
beAnExistingPath checks if a path exists
beAReadablePath checks if a path is readable
beAWritablePath checks if a path is writable
beAnAbsolutePath checks if a path is absolute
beAHiddenPath checks if a path is hidden
beAFilePath checks if a path is a file
beADirectoryPath checks if a path is a directory
havePathName checks if a path has a given name
haveAsAbsolutePath checks if a path has a given absolute path
haveAsCanonicalPath checks if a path has a given canonical path
haveParentPath checks if a path has a given parent path
listPaths checks if a path has a given list of children
exist checks if a file exists
beReadable checks if a file is readable
beWritable checks if a file is writable
beAbsolute checks if a file is absolute
beHidden checks if a file is hidden
beAFile checks if a file is a file
beADirectory checks if a file is a directory
haveName checks if a file has a given name
haveAbsolutePath checks if a file has a given absolute path
haveCanonicalPath checks if afile has a given canonical path
haveParent checks if a file has a given parent path
haveList checks if a file has a given list of children
The matchers from the org.specs2.matcher.ContentMatchers trait can help us check the contents of files. For example we can check that 2 text files have the same lines:
(file1, file2) must haveSameLines
file1 must haveSameLinesAs(file2)
We can check that the content of one file is contained in another one:
file1 must containLines(file2)
If the files are binary files we can also check that they have the same MD5 hash:
(file1, file2) must haveSameMD5
file1 must haveSameMD5As(file2)
Order
It is possible to relax the constraint by requiring the equality or containment to be true regardless of the order of lines:
(file1, file2) must haveSameLines.unordered
file1 must haveSameLinesAs(file2).unordered
file1 must containLines(file2).unordered
Show differences
By default only the different lines are being shown with a bit of context (lines before and after the differences).
You can however change this strategy. For example if there are too many differences, you can specify that you only want the first 10:
(file1, file2) must haveSameLines.showOnly(10.differences)
In the code above 10.differences builds a DifferenceFilter which is merely a filtering function: (lines: Seq[LineComparison]) => Seq[LineComparison])
keeping the first 10 differences. A LineComparison is the result of comparing a list of lines, either a line has been added, deleted, modified or is the same.
We can compare the contents of 2 directories. We can for example check if no files are missing and none has been added:
actualDir must haveSamePathsAs(expectedDir)
// with a file filter applied to both the actual and expected directories
actualDir must haveSamePathsAs(expectedDir).withFilter((file: File) => !file.isHidden)
Once we know that all files are present we can check their content:
// the default comparison expects that files are text files and that comparison must be done line by line
actualDir must haveSameFilesAs(expectedDir)
// with a file filter applied to both the actual and expected directories
actualDir must haveSameFilesAs(expectedDir).withFilter((file: File) => !file.isHidden)
// with a MD5 matcher for binary files
actualDir must haveSameFilesAs(expectedDir).withMatcher(haveSameMD5)
// it is also possible to only check the content of actual files when they exist in the expected directory
actualDir must haveSameFilesContentAs(expectedDir)
Files are not the only possible source of lines and it is useful to be able to check the content of a File with a Seq[String]:
file1 must haveSameLinesAs(Seq(line1, line2, line3))
This is because those 2 types implement the org.specs2.text.LinesContent trait, defining:
a name for the overall content
a method for returning the lines
a default method for computing the differences of 2 sequences of lines (in case you need to override this logic)
So if you have a specific type T which you can represent as a Seq[String], you can create an implicit LinesContent
and then you'll be able to use the ContentMatchers:
given [T]: LinesContent[T] with
def name(t: T) = "My list of lines"
def lines(t: T): Seq[String] = Seq()// your implementation goes here
And finally those matchers are Scala / Language related
Some behaviours can be encoded with the type system and you just want to check that a given expression will typecheck:
import org.specs2.execute.*, Typecheck.*
import org.specs2.matcher.TypecheckMatchers.*
typecheck {
"""
// there must be a Monoid instance for Text
Monoid[Text].zero
"""
} must succeed
You might also want to check that another expression will fail to typecheck:
typecheck {
"""
// there must be not a Monoid instance for Plane
Monoid[Plane].zero
"""
} must not(succeed)
typecheck {
"""
// there must be not a Monoid instance for Plane
Monoid[Plane].zero
"""
} must failWith("no implicit argument of type org.specs2.fp.Monoid\\[org.specs2.guide.matchers.Plane\\] was found")
Note that you actually don't have to use the succeed matcher because typecheck returns a TypecheckResult object which has an AsResult instance:
"this should typecheck ok" ! typecheck {
"""
// there must be not a Monoid instance for Plane
Monoid[Plane].zero
"""
}
This is also why you can indicate that a block of code must be marked as Pending until it typechecks:
typecheck {
"""
// there must be not a Monoid instance for Plane
Monoid[Plane].zero
"""
}.pendingUntilFixed("find a way to make this typecheck!")
In the rare case where you want to use the Scala interpreter and execute a script:
class ScalaInterpreterMatchersSpec extends org.specs2.mutable.Spec with ScalaInterpreterMatchers {
def interpret(s: String): String = "" // you have to provide your own Scala interpreter here
"A script can be interpreted" >> {
"1 + 1" >| "2"
}
}
Derive matchers
The easiest way to create a new matcher is to derive it from an existing one. You can:
use logical operators
def beBetween(i: Int, j: Int) = be_>=(i) and be_<=(j)
“adapt” the actual value
// This matcher adapts the existing `be_<=` matcher to a matcher applicable to `Any`
def beShort1 = be_<=(5) ^^ { (t: Any) => t.toString.length }
// you can use aka to provide some information about the original value, before adaptation
def beShort2 = be_<=(5) ^^ { (t: Any) => t.toString.length aka "the string size" }
// The adaptation can also be done the other way around when it's more readable
def haveExtension(extension: =>String) = ((_: File).getPath) ^^ endWith(extension)
adapt the actual and expected values. This matcher compares 2 Human objects but set their wealth field to 0 so that the equals method will not fail on that field:
use eventually to try a matcher a number of times until it succeeds:
val iterator = List(1, 2, 3).iterator
// Use eventually(retries, n.millis) to specify the number of tries and waiting time
iterator.next must be_==(3).eventually
use await to create a matcher that will match on Matcher[Future[T]] (this requires an execution environment):
Future(1) must be_>(0).await
Future { Thread.sleep(100); 1 } must be_>(0).await(retries = 2, timeout = 100.millis)
use when or unless to apply a matcher only if a condition is satisfied:
1 must be_==(2).when(false) // will return a success
1 must be_==(2).unless(true) // same thing
1 must be_==(2).when(false, "don't check this") // will return a success
1 must be_==(2).unless(true, "don't check this") // same thing
use iff to say that a matcher must succeed if and only if a condition is satisfied:
1 must be_==(1).iff(true) // will return a success
1 must be_==(2).iff(true) // will return a failure
1 must be_==(2).iff(false) // will return a success
1 must be_==(1).iff(false) // will return a failure
use orSkip to return a Skipped result instead of a Failure if the condition is not satisfied
1 must be_==(2).orSkip
1 must be_==(2).orSkip("Precondition failed") // prints "Precondition failed: '1' is not equal to '2'"
1 must be_==(2).orSkip((ko: String) => "BAD " + ko) // prints "BAD '1' is not equal to '2'"
use orPending to return a Pending result instead of a Failure if the condition is not satisfied
1 must be_==(2).orPending
1 must be_==(2).orPending("Precondition failed") // prints "Precondition failed: '1' is not equal to '2'"
1 must be_==(2).orPending((ko: String) => "BAD " + ko) // prints "BAD '1' is not equal to '2'"
Create your own
The easiest way to create a new matcher is to create it from a function returning a tuple with a boolean and one or more messages:
// import the necessary implicit conversions if you are outside of a Specification
// import org.specs2.matcher.Matcher.{given}
// annotate the return type so that implicit conversions can transform your function into a Matcher object
// here just return a boolean and a failure message
def startWithHello: Matcher[String] = { (s: String) =>
(s.startsWith("hello"), s + " doesn't start with hello")
}
If you want absolute power over matching, you can define your own matcher extending Matcher:
import org.specs2.execute.Result.*
case class BeMyOwnEmpty() extends Matcher[String] {
def apply[S <: String](s: Expectable[S]) = {
result(s.value.isEmpty, s.description + " is not empty")
}
}
"" must BeMyOwnEmpty()
In the code above you have to:
define the apply method (and its somewhat complex signature)
use the protected result method to return a Boolean condition and a failure message
you can use the description method on the Expectable class to return the full description of the expectable including the optional description you setup using the aka method
Now learn how to...
use standard results (failure, success, skipped, todo…) instead of matchers
add descriptions to your expectations to create even better failure messages
use datatables to conveniently group several examples into one
use ScalaCheck to generate and verify data for your examples
use Forms to display actual and expected values in html tables