Last active
August 29, 2015 14:05
-
-
Save bwmcadams/46d2d5cde6b2ff8f50f4 to your computer and use it in GitHub Desktop.
Scalaz Failure tester for ScalaTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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