【问题标题】:How to extend scalatest assert macro如何扩展 scalatest 断言宏
【发布时间】:2018-05-17 10:51:09
【问题描述】:

我想转:

eventually { assert(x == 0) }

进入:

verify { x == 0 }

仍然会收到一条不错的控制台消息:

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

verify如何实现?

【问题讨论】:

    标签: scala macros scalatest


    【解决方案1】:

    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
    }
    

    【讨论】:

    • 太棒了!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2023-03-17
    • 1970-01-01
    • 2013-01-09
    • 2016-05-14
    • 2014-11-16
    • 2017-12-12
    相关资源
    最近更新 更多