如何使用 Scalatest 在 Scala 中开发编译器插件 [英] How to use Scalatest to develop a compiler-plugin in Scala
问题描述
实际上我正在根据 http://www 上的文章为 Scala 开发编译器插件.scala-lang.org/node/140.
插件代码如下:
package localhost
import scala.tools.nsc
import nsc.Global
import nsc.Phase
import nsc.plugins.Plugin
import nsc.plugins.PluginComponent
class DivByZero(val global: Global) extends Plugin {
import global._
val name = "divbyzero"
val description = "checks for division by zero"
val components = List[PluginComponent](Component)
private object Component extends PluginComponent {
val global: DivByZero.this.global.type = DivByZero.this.global
val runsAfter = "refchecks"
// Using the Scala Compiler 2.8.x the runsAfter should be written as below
// val runsAfter = List[String]("refchecks");
val phaseName = DivByZero.this.name
def newPhase(_prev: Phase) = new DivByZeroPhase(_prev)
class DivByZeroPhase(prev: Phase) extends StdPhase(prev) {
override def name = DivByZero.this.name
def apply(unit: CompilationUnit) {
for ( tree @ Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0)))) <- unit.body;
if rcvr.tpe <:< definitions.IntClass.tpe)
{
unit.error(tree.pos, "definitely division by zero")
}
}
}
}
}
我正在做那里提到的事情并编写了一些编译所有内容的 makefile,然后创建一个 jar 文件.然后我使用以下命令加载带有 testfile 的插件 jar 文件:
I'm doing the things mentioned there and wrote some makefile which compiles everything and then creates a jar-file. Then I'm loading the the plugin jar file with the testfile with the following command:
scalac -Xplugin:myplugin.jar test.scala
看看输出是什么.我不喜欢这种方式,因为我从 ruby 知道如何做 tdd 和 bdd.我安装了 Scalatest http://www.scalatest.org/.是否可以以某种方式测试 jar 文件或类 divbyzero?我知道插件在使用文件执行时将首先加载.我脑子里很乱,不知道能不能直接测试插件类而不创建jar文件(或者甚至可以测试jar文件的一些功能和类)?
and see what the output is. I don't like this way because I knew from ruby how to do tdd and bdd. I installed Scalatest http://www.scalatest.org/. Is it in someway possible to test the jar-file or the class divbyzero? I know the plugin will first be load when executes with a file. I'm very wired in my head and don't know if it is possible to directly test the plugin class without creating the jar file (or is it even possible to test some functions and classes of the jar file)?
如果没有人可以帮助我,我可以像过去一样继续发展
If no one can help me, I can keep on developing like in the good old days
感谢您的时间和帮助马蒂亚斯
Thanks for your time and help Matthias
推荐答案
我认为您正在查看模拟对象(我喜欢 EasyMock,但还有许多其他)和一些重构.
I think you're looking at mock objects (I like EasyMock, but there are many others) and some refactoring.
当你提到你的 makefile 时,我得到的印象是你在使用旧的 make.如果是这样,我是否建议您查看类似 SBT、Gradle,或者,当您来自 Ruby 世界时,BuildR.它们都内置了对各种 Scala 测试框架的支持.
When you refer to your makefile, I get the impression you're using good old make. If so, might I suggest you look at something like SBT, Gradle, or, as you're coming from the Ruby world, BuildR. All of them have built in support for various scala test frameworks.
这篇关于如何使用 Scalatest 在 Scala 中开发编译器插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!