如何使用Scalatest在Scala中开发编译器插件

Mat*_*her 4 scala

实际上我正在根据http://www.scala-lang.org/node/140上的文章为Scala开发一个编译器插件.

这是插件的代码:

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")
          }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在做那里提到的事情并编写了一些makefile来编译所有内容,然后创建一个jar文件.然后我使用以下命令使用testfile加载插件jar文件:

scalac -Xplugin:myplugin.jar test.scala
Run Code Online (Sandbox Code Playgroud)

并看看输出是什么.我不喜欢这种方式,因为我从红宝石中知道如何做tdd和bdd.我安装了Scalatest http://www.scalatest.org/.是否有可能测试jar文件或类divbyzero?我知道插件将在使用文件执行时首先加载.我非常关注并且不知道是否可以在不创建jar文件的情况下直接测试插件类(或者甚至可以测试jar文件的某些函数和类)?

如果没有人可以帮助我,我可以继续发展,就像过去一样

感谢您的时间和帮助Matthias

sam*_*ert 18

您可以使用以下代码以编程方式调用Scala编译器和插件:

import scala.tools.nsc.{Settings, Global}
import scala.tools.nsc.io.VirtualDirectory
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.util.BatchSourceFile

// prepare the code you want to compile
val code = "object Foo extends Application { println(42 / 0) }"
val sources = List(new BatchSourceFile("<test>", code))

val settings = new Settings
// save class files to a virtual directory in memory
settings.outputDirs.setSingleOutput(new VirtualDirectory("(memory)", None))

val compiler = new Global(settings, new ConsoleReporter(settings)) {
  override protected def computeInternalPhases () {
    super.computeInternalPhases
    for (phase <- new DivByZero(this).components)
      phasesSet += phase
  }
}
new compiler.Run() compileSources(sources)
Run Code Online (Sandbox Code Playgroud)

请注意,此代码在执行代码时需要scala-compiler.jar并且scala-library.jar在类路径上.如果您正在从SBT之类的东西中运行测试,那么遗憾的是情况并非如此.

为了让事情从SBT中运行,你必须做一些跳跃:

val settings = new Settings
val loader = getClass.getClassLoader.asInstanceOf[URLClassLoader]
val entries = loader.getURLs map(_.getPath)
// annoyingly, the Scala library is not in our classpath, so we have to add it manually
val sclpath = entries find(_.endsWith("scala-compiler.jar")) map(
  _.replaceAll("scala-compiler.jar", "scala-library.jar"))
settings.classpath.value = ClassPath.join((entries ++ sclpath) : _*)
Run Code Online (Sandbox Code Playgroud)

如果您在其他构建环境中运行,您可能会发现它scala-library.jar已经在类路径中,或者如果您真的很幸运,那么您需要的所有内容都在标准Java类路径上,在这种情况下,您可以将以上内容替换为:

val settings = new Settings
settings.usejavacp.value = true
Run Code Online (Sandbox Code Playgroud)

您可以打印出Java类路径的值,System.getProperty("java.class.path")当然可以entries从上面的代码打印出来,看看加载测试代码的类加载器使用的类路径.


小智 5

我想补充一下samskivert的答案.重写computeInternalPhases力量注入插件安装到整个phaseSet的阶段,但是,编译器不把他们当作插件的一部分.例如,如果您想"-P:divbyzero:someoption"使用以下选项将选项传递给插件:

settings.pluginOptions.appendToValue("divbyzero:someoption")

您将收到以下编译错误:

error: bad option: -P:divbyzero:someoption

那是因为编译器对命名的插件一无所知divbyzero.

添加插件的更合适的方法是覆盖loadRoughPluginsList方法并在那里添加插件,而不是手动将插件的每个阶段注入编译阶段集:

override protected def loadRoughPluginsList: List[Plugin] =
  new DivByZero(this) :: super.loadRoughPluginsList
Run Code Online (Sandbox Code Playgroud)