Skip to content

Commit

Permalink
In a failed Try matcher the actual exception can be a subclass of the…
Browse files Browse the repository at this point in the history
… expected one. fixes #291
  • Loading branch information
etorreborre committed Aug 31, 2014
1 parent 382c437 commit 56c0b1c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions matcher/src/main/scala/org/specs2/matcher/AnyMatchers.scala
Expand Up @@ -143,10 +143,11 @@ trait AnyBaseMatchers {
def apply[S <: Any](x: Expectable[S]) = {
val c = implicitly[ClassTag[T]].runtimeClass
val xClass = x.value.asInstanceOf[java.lang.Object].getClass
val xWithClass = x.mapDescription(d => s"'$d: ${xClass.getName}'")
result(c.isAssignableFrom(xClass),
x.description + " is an instance of " + q(c.getName),
x.description + " is not an instance of " + q(c.getName),
x)
xWithClass.description + " is an instance of " + q(c.getName),
xWithClass.description + " is not an instance of " + q(c.getName),
xWithClass)
}
}
}
Expand Down
Expand Up @@ -61,7 +61,7 @@ case class TryFailureMatcher[T]() extends OptionLikeMatcher[Try, T, Throwable]("
def withValue(t: ValueCheck[Throwable]) = TryFailureCheckedMatcher(t)

def withThrowable[E <: Throwable : ClassTag] = TryFailureCheckedMatcher[T](ValueChecks.functionIsValueCheck { t: Throwable =>
createExpectable(t).applyMatcher(AnyMatchers.haveClass[E]).toResult
createExpectable(t).applyMatcher(AnyMatchers.beAnInstanceOf[E]).toResult
})

def withThrowable[E <: Throwable : ClassTag](pattern: String) = TryFailureCheckedMatcher[T](ValueChecks.functionIsValueCheck { t: Throwable =>
Expand Down
Expand Up @@ -95,7 +95,7 @@ class AnyMatchersSpec extends script.Specification with Groups with ResultMatche
beAnInstanceOf checks if an object is an instance of a given type
${ type1 must beAnInstanceOf[Type1] }
${ type1 must not be anInstanceOf[Type2] }
${ (type1 must beAnInstanceOf[Type2]).message must_== "'type1' is not an instance of 'org.specs2.matcher.Type2'" }
${ (type1 must beAnInstanceOf[Type2]).message must_== s"'type1: ${type1.getClass.getName}' is not an instance of 'org.specs2.matcher.Type2'" }

Implicits
=========
Expand Down
Expand Up @@ -32,7 +32,9 @@ class TryMatchersSpec extends Specification with ResultMatchers { def is = s2"""
${ (Succeeded(1) must be failedTry) returns "'Success(1)' is not a Failure" }
${ Failed[I](e) must be aFailedTry }
${ Failed[I](e) must beFailedTry.withThrowable[MyException] }
${ (Failed[I](e) must beFailedTry.withThrowable[OtherException]) returns s"'Failure(boom)' is a Failure but '$e' doesn't have class '${classOf[OtherException].getName}'. It has class '${classOf[MyException].getName}'" }
${ Failed[I](e) must beFailedTry.withThrowable[Exception] }
${ (Failed[I](e) must beFailedTry.withThrowable[OtherException]) returns
s"'Failure(boom)' is a Failure but '$e: ${classOf[MyException].getName}' is not an instance of '${classOf[OtherException].getName}'" }
${ Failed[I](e) must beAFailedTry.withThrowable[MyException](".*oo.*") }
${ Failed[I](e) must beFailedTry.like { case e: MyException => e.getMessage must startWith("b") }}
${ Failed[I](e) must not be aFailedTry.withThrowable[MyException]("bang") }
Expand Down

0 comments on commit 56c0b1c

Please sign in to comment.