Jacek Kołodziejczyk
Jacek Kołodziejczyk

Reputation: 634

How to extend scalatest assert macro

I would like to turn:

eventually { assert(x == 0) }

into:

verify { x == 0 }

and still get a nice console message:

Caused by: org.scalatest.exceptions.TestFailedException: 1 did not equal 0

How to implement verify?

Upvotes: 2

Views: 133

Answers (1)

ghik
ghik

Reputation: 10764

verify must be a macro, too:

import scala.language.experimental.macros
import scala.reflect.macros.blackbox

class VerifyMacro(val c: blackbox.Context) {
  import c.universe._

  def verifyImpl(condition: Tree): Tree =
    q"${c.prefix}.eventually(${c.prefix}.assert($condition))"
}

import org.scalatest._
import org.scalatest.concurrent._

trait Verifications extends Assertions with Eventually {
  def verify(condition: Boolean): Assertion = macro VerifyMacro.verifyImpl
}

Upvotes: 3

Related Questions