Implement tests to sam-with-receiver support in new scripting API

plus minor tests refactoring
This commit is contained in:
Ilya Chernikov
2019-10-14 15:07:13 +02:00
parent cf3bf5a9b9
commit 08b77bc916
8 changed files with 97 additions and 19 deletions
@@ -12,16 +12,16 @@ import org.junit.Test
import java.io.File
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.nio.file.Files
import java.security.MessageDigest
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.host.with
import kotlin.script.experimental.jvm.*
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
import kotlin.script.experimental.jvm.impl.KJvmCompiledScript
import kotlin.script.experimental.jvm.util.KotlinJars
import kotlin.script.experimental.jvmhost.*
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.CompiledScriptJarsCache
import kotlin.script.experimental.jvmhost.JvmScriptCompiler
class CachingTest : TestCase() {
@@ -260,12 +260,3 @@ private fun File.readCompiledScript(scriptCompilationConfiguration: ScriptCompil
private fun ByteArray.toHexString(): String = joinToString("", transform = { "%02x".format(it) })
private fun <R> withTempDir(keyName: String = "tmp", body: (File) -> R) {
val tempDir = Files.createTempDirectory(keyName).toFile()
try {
body(tempDir)
} finally {
tempDir.deleteRecursively()
}
}
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.script.experimental.jvmhost.test
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.scripting.definitions.annotationsForSamWithReceivers
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.JvmDependency
import kotlin.script.experimental.jvmhost.JvmScriptCompiler
class FeaturesTest : TestCase() {
fun testSamWithReceiver() {
withTempDir { tempDir ->
runBlocking {
val srcDir = File(TEST_DATA_DIR, "samWithReceiver")
val destDir = File(tempDir, "dest").also { it.mkdir() }
val javaRes = KotlinTestUtils.compileJavaFiles(
srcDir.listFiles { file: File -> file.extension == "java" }!!.toMutableList(),
mutableListOf("-d", destDir.absolutePath)
)
assertTrue(javaRes)
val baseConfig = ScriptCompilationConfiguration {
fileExtension("samwr.kts")
dependencies(JvmDependency(destDir))
}
JvmScriptCompiler()(File(srcDir, "test.samwr.kts").toScriptSource(), baseConfig).let { res ->
when (res) {
is ResultWithDiagnostics.Success -> fail("Expecting \"Unresolved reference\" error, got successful compilation")
is ResultWithDiagnostics.Failure ->
if (res.reports.none { it.message.contains("Unresolved reference") }) {
fail("Expecting \"Unresolved reference\" error, got:\n ${res.reports.joinToString("\n ")}")
}
}
}
val configWithSwr = baseConfig.with {
annotationsForSamWithReceivers("SamWithReceiver1")
}
JvmScriptCompiler()(File(srcDir, "test.samwr.kts").toScriptSource(), configWithSwr).onFailure { res ->
fail("Compilation failed:\n ${res.reports.joinToString("\n ")}")
}
}
}
}
}
@@ -27,9 +27,7 @@ import kotlin.script.experimental.host.BasicScriptingHost
import kotlin.script.experimental.host.FileBasedScriptSource
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
import kotlin.script.experimental.jvm.impl.KJvmCompiledScript
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvm.updateClasspath
import kotlin.script.experimental.jvm.util.KotlinJars
import kotlin.script.experimental.jvm.util.classpathFromClass
@@ -38,10 +36,6 @@ import kotlin.script.templates.standard.SimpleScriptTemplate
class ScriptingHostTest : TestCase() {
companion object {
const val TEST_DATA_DIR = "libraries/scripting/jvm-host-test/testData"
}
@Test
fun testSimpleUsage() {
val greeting = "Hello from script!"
@@ -461,7 +455,7 @@ internal fun ScriptCompilationConfiguration.Builder.makeSimpleConfigurationWithT
updateClasspath(classpathFromClass<ScriptingHostTest>()) // the lambda below should be in the classpath
refineConfiguration {
beforeCompiling { ctx ->
val importedScript = File(ScriptingHostTest.TEST_DATA_DIR, "importTest/helloWithVal.kts")
val importedScript = File(TEST_DATA_DIR, "importTest/helloWithVal.kts")
if ((ctx.script as? FileBasedScriptSource)?.file?.canonicalFile == importedScript.canonicalFile) {
ctx.compilationConfiguration
} else {
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.script.experimental.jvmhost.test
import java.io.File
import java.nio.file.Files
internal const val TEST_DATA_DIR = "libraries/scripting/jvm-host-test/testData"
internal fun <R> withTempDir(keyName: String = "tmp", body: (File) -> R) {
val tempDir = Files.createTempDirectory(keyName).toFile()
try {
body(tempDir)
} finally {
tempDir.deleteRecursively()
}
}
@@ -0,0 +1,3 @@
public class Exec {
void exec(Sam sam) {}
}
@@ -0,0 +1,4 @@
@SamWithReceiver1
public interface Sam {
void run(String a);
}
@@ -0,0 +1,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface SamWithReceiver1 {
}
@@ -0,0 +1,5 @@
val e = Exec()
e.exec {
this.substring(1)
}