mirror of https://github.com/sbt/sbt.git
Refactor ScalaCompilerForUnitTesting.
Refactor ScalaCompilerForUnitTesting by introducing a new method `extractApiFromSrc` which better describes the intent than `compileSrc`. The `compileSrc` becomes a private, utility method. Also, `compileSrc` method changed it's signature so it can take multiple source code snippets as input. This functionality will be used in future commits.
This commit is contained in:
parent
aac19fd02b
commit
89914975e1
|
|
@ -17,7 +17,7 @@ class ExtractAPISpecification extends Specification {
|
|||
def stableExistentialNames: Boolean = {
|
||||
def compileAndGetFooMethodApi(src: String): Def = {
|
||||
val compilerForTesting = new ScalaCompilerForUnitTesting
|
||||
val sourceApi = compilerForTesting.compileSrc(src)
|
||||
val sourceApi = compilerForTesting.extractApiFromSrc(src)
|
||||
val FooApi = sourceApi.definitions().find(_.name() == "Foo").get.asInstanceOf[ClassLike]
|
||||
val fooMethodApi = FooApi.structure().declared().find(_.name == "foo").get
|
||||
fooMethodApi.asInstanceOf[Def]
|
||||
|
|
|
|||
|
|
@ -23,24 +23,45 @@ class ScalaCompilerForUnitTesting {
|
|||
* Compiles given source code using Scala compiler and returns API representation
|
||||
* extracted by ExtractAPI class.
|
||||
*/
|
||||
def compileSrc(src: String): SourceAPI = {
|
||||
import java.io.FileWriter
|
||||
def extractApiFromSrc(src: String): SourceAPI = {
|
||||
val (Seq(tempSrcFile), analysisCallback) = compileSrcs(src)
|
||||
analysisCallback.apis(tempSrcFile)
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles given source code snippets written to a temporary files. Each snippet is
|
||||
* written to a separate temporary file.
|
||||
*
|
||||
* The sequence of temporary files corresponding to passed snippets and analysis
|
||||
* callback is returned as a result.
|
||||
*/
|
||||
private def compileSrcs(srcs: String*): (Seq[File], TestCallback) = {
|
||||
withTemporaryDirectory { temp =>
|
||||
val analysisCallback = new TestCallback
|
||||
val classesDir = new File(temp, "classes")
|
||||
classesDir.mkdir()
|
||||
val compiler = prepareCompiler(classesDir, analysisCallback)
|
||||
val run = new compiler.Run
|
||||
val srcFile = new File(temp, "Test.scala")
|
||||
srcFile.createNewFile()
|
||||
val fw = new FileWriter(srcFile)
|
||||
fw.write(src)
|
||||
fw.close()
|
||||
run.compile(List(srcFile.getAbsolutePath()))
|
||||
analysisCallback.apis(srcFile)
|
||||
val srcFiles = srcs.toSeq.zipWithIndex map { case (src, i) =>
|
||||
val fileName = s"Test_$i.scala"
|
||||
prepareSrcFile(temp, fileName, src)
|
||||
}
|
||||
val srcFilePaths = srcFiles.map(srcFile => srcFile.getAbsolutePath).toList
|
||||
run.compile(srcFilePaths)
|
||||
(srcFiles, analysisCallback)
|
||||
}
|
||||
}
|
||||
|
||||
private def prepareSrcFile(baseDir: File, fileName: String, src: String): File = {
|
||||
import java.io.FileWriter
|
||||
val srcFile = new File(baseDir, fileName)
|
||||
srcFile.createNewFile()
|
||||
val fw = new FileWriter(srcFile)
|
||||
fw.write(src)
|
||||
fw.close()
|
||||
srcFile
|
||||
}
|
||||
|
||||
private def prepareCompiler(outputDir: File, analysisCallback: AnalysisCallback): CachedCompiler0#Compiler = {
|
||||
val args = Array.empty[String]
|
||||
object output extends SingleOutput {
|
||||
|
|
|
|||
Loading…
Reference in New Issue