如何扩展scalatest断言宏

时间:2018-05-17 10:51:10

标签: scala macros scalatest

我想转:

id

成:

bar

仍然可以获得一个很好的控制台消息:

eventually { assert(x == 0) }

如何实施verify { x == 0 }

1 个答案:

答案 0 :(得分:3)

verify也必须是一个宏:

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
}