Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Last active August 29, 2015 14:05
Embed
What would you like to do?
Scalaz Failure tester for ScalaTest
import org.scalatest.Suite
import scalaz._
import Scalaz._
import org.scalatest.matchers._
/**
* Helper matchers for hacking with Scalaz.
*/
trait ScalazTestHelpers { self: Suite =>
case class HasScalazFailureMatcher[E](element: E) extends Matcher[ValidationNel[E, _]] {
def apply(validation: ValidationNel[E, _]): MatchResult = {
MatchResult(
validation match {
case Success(_) =>
true
case Failure(n: NonEmptyList[E]) =>
n.list contains element
case _ =>
false
},
s"$validation did not contain a Failure element matching '$element'.",
s"$validation contained a Failure element matching '$element', but should not have."
)
}
}
/**
* Checks if a scalaz.ValidationNel contains a specific failure element
* Usage:
* validationObj should haveFailure (someErrorMessageOrObject)
*
* Can also be used to test multiple elements:
* validationObj should (haveFailure (someErrorMessageOrObject) and
* haveFailure (someOtherErrorMessageOrObject))
*
*/
def haveFailure[E](element: E) = HasScalazFailureMatcher[E](element)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment