Add support for @CompilerOptions annotation for supplying compiler
options from scripts. #KT-34274 fixed
This commit is contained in:
committed by
Ilya Chernikov
parent
8c6916af52
commit
35d7bb4a26
+27
-3
@@ -7,9 +7,7 @@ package org.jetbrains.kotlin.mainKts.test
|
||||
import org.jetbrains.kotlin.mainKts.MainKtsScript
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.PrintStream
|
||||
import java.io.*
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.host.toScriptSource
|
||||
import kotlin.script.experimental.jvm.baseClassLoader
|
||||
@@ -90,6 +88,32 @@ class MainKtsTest {
|
||||
Assert.assertEquals(listOf("Hi from common", "Hi from middle", "sharedVar == 5"), out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompilerOptions() {
|
||||
|
||||
val out = captureOut {
|
||||
val res = evalFile(File("$TEST_DATA_ROOT/compile-java6.main.kts"))
|
||||
assertSucceeded(res)
|
||||
assertIsJava6Bytecode(res)
|
||||
}.lines()
|
||||
|
||||
Assert.assertEquals(listOf("Hi from sub", "Hi from super", "Hi from random"), out)
|
||||
}
|
||||
|
||||
private fun assertIsJava6Bytecode(res: ResultWithDiagnostics<EvaluationResult>) {
|
||||
val scriptClassResource = res.valueOrThrow().returnValue.scriptClass!!.java.run {
|
||||
getResource("$simpleName.class")
|
||||
}
|
||||
|
||||
DataInputStream(ByteArrayInputStream(scriptClassResource.readBytes())).use { stream ->
|
||||
val header = stream.readInt()
|
||||
if (0xCAFEBABE.toInt() != header) throw IOException("Invalid header class header: $header")
|
||||
val minor = stream.readUnsignedShort()
|
||||
val major = stream.readUnsignedShort()
|
||||
Assert.assertTrue(major == 50)
|
||||
}
|
||||
}
|
||||
|
||||
private fun assertSucceeded(res: ResultWithDiagnostics<EvaluationResult>) {
|
||||
Assert.assertTrue(
|
||||
"test failed:\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
@file:CompilerOptions("-jvm-target", "1.6")
|
||||
|
||||
interface Test {
|
||||
fun print()
|
||||
fun printSuper() = println("Hi from super")
|
||||
}
|
||||
|
||||
class TestImpl : Test {
|
||||
override fun print() = println("Hi from sub")
|
||||
}
|
||||
|
||||
inline fun printRandom() = println("Hi from random")
|
||||
|
||||
TestImpl().run {
|
||||
print()
|
||||
printSuper()
|
||||
printRandom()
|
||||
}
|
||||
Reference in New Issue
Block a user