Skip to content

Instantly share code, notes, and snippets.

@bvenners
Last active August 29, 2015 13:57
Show Gist options
  • Save bvenners/9405709 to your computer and use it in GitHub Desktop.
Save bvenners/9405709 to your computer and use it in GitHub Desktop.
ScalaTest's Inspectors and Inspector Shorthands are now more open minded
// In ScalaTest 2.1.0, the inspector and inspector shorthand constructs accept more types.
// Previously they just worked on Scala collections (though through implicit conversions
// you could pass strings and arrays to them). Now they work on any type C for which an implicit
// org.scalatest.enablers.Collecting[C] is available, which out-the-box includes Scala and Java
// collections, strings, and arrays. Here are a few examples:
scala> import org.scalatest._
import org.scalatest._
scala> import MustMatchers._
import MustMatchers._
scala> every (List(1, 2, 3)) must be > 2
// org.scalatest.exceptions.TestFailedException: 'every' inspection failed, because:
// at index 0, 1 was not greater than 2,
// at index 1, 2 was not greater than 2
// in List(1, 2, 3)
scala> atLeast (1, "hello") mustBe 'f'
// org.scalatest.exceptions.TestFailedException: 'atLeast(1)' inspection failed, because no element
// satisfied the assertion block:
// at index 0, 'h' was not 'f' (<console>:32),
// at index 1, 'e' was not 'f' (<console>:32),
// at index 2, 'l' was not 'f' (<console>:32),
// at index 3, 'l' was not 'f' (<console>:32),
// at index 4, 'o' was not 'f' (<console>:32)
// in "hello"
scala> all (Array(1, 2, 3)) must be < 3
// org.scalatest.exceptions.TestFailedException: 'all' inspection failed, because:
// at index 2, 3 was not less than 3
// in Array(1, 2, 3)
scala> import scala.collection.JavaConverters._
// import scala.collection.JavaConverters._
scala> val words = List("fee", "fie", "foe", "fum").asJava
// words: java.util.List[String] = [fee, fie, foe, fum]
scala> all (words) must (have length 3 and endWith ("e"))
// org.scalatest.exceptions.TestFailedException: 'all' inspection failed, because:
// at index 3, "fum" had length 3, but "fum" did not end with substring "e"
// in ["fee", "fie", "foe", "fum"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment