scala - 如何扩展 scalatest 断言宏

标签 scala macros scalatest

我想转向:

eventually { assert(x == 0) }

进入:

verify { x == 0 }

并且仍然得到一个不错的控制台消息:

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

如何实现验证

最佳答案

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
}

关于scala - 如何扩展 scalatest 断言宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389616/

相关文章:

由变量控制的 C++ 调试宏

scala - Map 中的惰性求值

scala - 我如何定义带有子特征的特征,以便它们的方法只采用共享实现?

scala - 从宏中获取具有匿名类方法的结构类型

scala - 我如何在 Scala 中对控制台输入进行单元测试?

scala - 如何检查 Failure[T] 中包含哪个异常?

Scalatest 模拟数据库

scala - 类型类参数的 ClassTag

scala - Scala 对象定义中没有父类(super class)名称的 "extends {..}"子句有什么作用?

c++ - 如何在另一个#define 中实现#define?