Skip to content

Commit

Permalink
fixed the .await method for future matchers and failed futures. fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Jun 25, 2014
1 parent dce7451 commit c56cace
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Expand Up @@ -55,6 +55,7 @@ trait ExceptionBaseMatchers extends Expectations {
checkMatchResult(value, (e: Throwable) => classType(e), f)
}
}

private val classType =
(e: Throwable) => {
errorMustBeThrownIfExceptionIsExpected(e, klass)
Expand Down
13 changes: 11 additions & 2 deletions matcher/src/main/scala/org/specs2/matcher/FutureMatchers.scala
Expand Up @@ -38,12 +38,21 @@ trait FutureMatchers extends Expectations with ConcurrentExecutionContext {
}
}

def await[T](m: Matcher[T]): Matcher[Future[T]] = awaitFor(m)()
def await[T](m: Matcher[T])(retries: Int = 0, timeout: FiniteDuration = 1.seconds): Matcher[Future[T]] = awaitFor(m)(retries, timeout)

private def awaitFor[T](m: Matcher[T])(retries: Int = 0, timeout: FiniteDuration = 1.seconds): Matcher[Future[T]] = new Matcher[Future[T]] {
def apply[S <: Future[T]](a: Expectable[S]) = {
val r = a.value.map(v => createExpectable(v).applyMatcher(m).toResult).await(retries, timeout)
result(r.isSuccess, r.message, r.message, a)
try {
val r = a.value.map(v => createExpectable(v).applyMatcher(m).toResult).await(retries, timeout)
result(r.isSuccess, r.message, r.message, a)
} catch {
// if awaiting on the future throws an exception because it was a failed future
// there try to match again because the matcher can be a `throwA` matcher
case t: Throwable =>
val r = createExpectable(throw t).applyMatcher(m).toResult
result(r.isSuccess, r.message, r.message, a)
}
}
}
}
Expand Down
Expand Up @@ -21,6 +21,10 @@ class FutureMatchersSpec extends Specification with Groups with NoTimeConversion
A `Future` returning a `Matcher[T]` can be transformed into a `Result`
${ Future(1 === 1).await }

A `throwA[T]` matcher can be used to match a failed future with the `await` method
${ Future.failed[Int](new RuntimeException) must throwA[RuntimeException].await }
${ { Future.failed[Int](new RuntimeException) must be_===(1).await } must throwA[RuntimeException] }

"""

// the current execution context can be overridden here
Expand Down

0 comments on commit c56cace

Please sign in to comment.