Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwmcadams/46d2d5cde6b2ff8f50f4 to your computer and use it in GitHub Desktop.
Save bwmcadams/46d2d5cde6b2ff8f50f4 to your computer and use it in GitHub Desktop.
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