Skip to content

Instantly share code, notes, and snippets.

@grimrose
Last active September 20, 2019 19:14
Show Gist options
  • Save grimrose/ded2a0d3b348ec3718c5bfc0de9f2f6f to your computer and use it in GitHub Desktop.
Save grimrose/ded2a0d3b348ec3718c5bfc0de9f2f6f to your computer and use it in GitHub Desktop.
AirSpec(19.9.7) for Scala Future
package ninja.grimrose.sandbox
import wvlet.airframe.SourceCode
import wvlet.airspec.AirSpec
import wvlet.airspec.spi.AssertionFailure
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
trait ScalaFutureSupport { this: AirSpec =>
import scala.concurrent.duration._
import scala.language.implicitConversions
protected def timeout: Duration = 1.minutes
implicit def executionContext: ExecutionContext
implicit class RichFuture[T](scalaFuture: Future[T]) {
def assertAsync(cond: T => Boolean)(implicit code: SourceCode): Unit = {
scalaFuture.value match {
case Some(s: Success[T]) => assert(cond(s.value))
case Some(f: Failure[T]) => throw f.exception
case None => throw AssertionFailure("future value is None", code)
}
}
}
}
@grimrose
Copy link
Author

grimrose commented Sep 20, 2019

scala.js 0.6.29

def test(): Unit = {
  val future = Future {
    1 + 2
  }

  future.assertAsync { actual =>
    actual == 3
  }
}
failed: future value is None

def test(): Unit = {
  val future = Future.successful {
    1 + 2
  }

  future.assertAsync { actual =>
    actual == 3
  }
}
Passed: Total 1, Failed 0, Errors 0, Passed 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment