forked from dotty-staging/dotty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotty-staging#1 from marek1840/add-simple-tasty/sc…
…alac-integration-tests Add scalac and dotty compilers
- Loading branch information
Showing
13 changed files
with
242 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tasty4scalac/integration/src/tasty4scalac/CompileSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package tasty4scalac | ||
|
||
import java.nio.file.Path | ||
|
||
final class CompileSource(val name: String, val source: Path) { | ||
override def toString: String = name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tasty4scalac | ||
|
||
|
||
trait Compiler { | ||
def compile(code: String): Unit | ||
|
||
final override def toString: String = getClass.getSimpleName | ||
} | ||
|
||
object Compiler { | ||
def dotty(): Compiler = Dotty() | ||
def scalac(): Compiler = Scalac() | ||
|
||
trait Factory { | ||
def apply(): Compiler | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tasty4scalac | ||
|
||
import dotty.tools.dotc.core.Comments.{ContextDoc, ContextDocstrings} | ||
import dotty.tools.dotc.core.Contexts | ||
import dotty.tools.dotc.core.Contexts.ContextBase | ||
import dotty.tools.dotc.reporting.Reporter.NoReporter | ||
import dotty.tools.io.VirtualDirectory | ||
import tasty4scalac.Compiler.Factory | ||
|
||
final class Dotty(val ctx: Contexts.Context) extends dotty.tools.dotc.Compiler with Compiler { | ||
override def compile(code: String): Unit = newRun(ctx.fresh).compile(code) | ||
} | ||
|
||
object Dotty extends Factory { | ||
private val classpath = System.getProperty("dotty.classpath") | ||
|
||
override def apply(): Compiler = { | ||
implicit val ctx: Contexts.FreshContext = new ContextBase().initialCtx.fresh | ||
ctx.setSetting(ctx.settings.classpath, classpath) | ||
ctx.setSetting(ctx.settings.YtestPickler, true) | ||
ctx.setSetting(ctx.settings.encoding, "UTF8") | ||
ctx.setSetting(ctx.settings.outputDir, new VirtualDirectory("<quote compilation output>")) | ||
|
||
ctx.setReporter(NoReporter) | ||
ctx.setProperty(ContextDoc, new ContextDocstrings) | ||
new Dotty(ctx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tasty4scalac | ||
|
||
import java.nio.file.{Files, Path, Paths} | ||
import java.util | ||
import java.util.regex.Pattern | ||
import scala.collection.JavaConverters._ | ||
|
||
trait Provider { | ||
def testCases(): util.Collection[Array[CompileSource]] | ||
} | ||
|
||
object Providers { | ||
private val root = Paths.get(System.getProperty("test.root.directory")) | ||
|
||
def compiledSourceProvider: Provider = new CompiledSources(root) | ||
|
||
private final class CompiledSources(root: Path) extends Provider { | ||
private val negativeTests = Seq( | ||
Pattern.compile(".*/neg(-.*)?/.*"), | ||
Pattern.compile(".*/run-with-compiler/.*"), | ||
Pattern.compile(".*/vulpix-tests/.*"), | ||
Pattern.compile(".*/disabled/.*"), | ||
) | ||
|
||
def testCases(): util.Collection[Array[CompileSource]] = { | ||
findTestCases(root) | ||
.map(createTestCase) | ||
.map(Array(_)) | ||
.asJavaCollection | ||
} | ||
|
||
private def findTestCases(path: Path): Seq[Path] = { | ||
Files.walk(path) | ||
.iterator() | ||
.asScala | ||
.filterNot(path => negativeTests.exists(p => p.matcher(path.toString).matches())) | ||
.filter(_.getFileName.toString.endsWith(".scala")) | ||
.toSeq | ||
} | ||
|
||
private def createTestCase(path: Path): CompileSource = { | ||
val name = root.relativize(path).toString.dropRight(6) // drop ".scala" | ||
new CompileSource(name, path) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package tasty4scalac | ||
|
||
import java.io._ | ||
import java.net.URL | ||
import java.nio.file._ | ||
|
||
import scala.reflect.internal.util.{BatchSourceFile, NoFile, SourceFile} | ||
import scala.tools.nsc.{Global, Settings} | ||
import scala.tools.nsc.reporters.NoReporter | ||
|
||
final class Scalac(settings: Settings) extends Global(settings, NoReporter) with Compiler { | ||
override def compile(code: String): Unit = { | ||
val file = new BatchSourceFile(NoFile, code.toCharArray) | ||
compile(file) | ||
} | ||
|
||
private def compile(files: SourceFile*): Unit = { | ||
new Run().compileSources(files.toList) | ||
} | ||
} | ||
|
||
object Scalac extends Compiler.Factory { | ||
private val classpath = System.getProperty("scalac.classpath") | ||
private val pluginPath = System.getProperty("scalac.plugin.classpath") | ||
|
||
def apply(): Scalac = { | ||
val settings = new Settings() | ||
settings.classpath.value = classpath | ||
settings.plugin.value = List(pluginPath) | ||
settings.stopAfter.value = List("tasty") | ||
settings.d.value = { | ||
val path = Files.createTempDirectory("tasty-generation") | ||
Files.createDirectories(path).toString | ||
} | ||
|
||
new Scalac(settings) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tasty4scalac/integration/test/tasty4scalac/CompilerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tasty4scalac; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
@RunWith(Parameterized.class) | ||
public final class CompilerTest { | ||
private final Compiler compiler; | ||
|
||
public CompilerTest(Compiler factory) { | ||
this.compiler = factory; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{index}: {0}") | ||
public static Collection<Compiler[]> parameters() { | ||
return Arrays.asList(new Compiler[][]{ | ||
{Scalac$.MODULE$.apply()}, | ||
{Dotty$.MODULE$.apply()} | ||
}); | ||
} | ||
|
||
@Test | ||
public void compilesSimpleClass() { | ||
String code = "class A"; | ||
compiler.compile(code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tasty4scalac; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Collection; | ||
|
||
@RunWith(Parameterized.class) | ||
public final class Runner { | ||
private static final Compiler compiler = Compiler$.MODULE$.scalac(); | ||
private final CompileSource test; | ||
|
||
public Runner(CompileSource test) { | ||
this.test = test; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{index}: {0}") | ||
public static Collection<CompileSource[]> parameters() { | ||
return Providers.compiledSourceProvider().testCases(); | ||
} | ||
|
||
@Test | ||
public void compile() throws IOException { | ||
String code = new String(Files.readAllBytes(test.source())); | ||
compiler.compile(code); | ||
} | ||
} | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.