Implement tests for script instance capturing in the host tests
This commit is contained in:
committed by
TeamCityServer
parent
10c5071eda
commit
6d6de9dd76
+57
-9
@@ -3,7 +3,13 @@ import kotlinx.coroutines.runBlocking
|
|||||||
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
|
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
|
||||||
import org.jetbrains.kotlin.scripting.compiler.test.assertEqualsTrimmed
|
import org.jetbrains.kotlin.scripting.compiler.test.assertEqualsTrimmed
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.File
|
||||||
|
import java.io.InputStream
|
||||||
import java.io.PrintStream
|
import java.io.PrintStream
|
||||||
|
import java.nio.file.attribute.FileTime
|
||||||
|
import java.util.zip.ZipEntry
|
||||||
|
import java.util.zip.ZipFile
|
||||||
|
import java.util.zip.ZipOutputStream
|
||||||
import kotlin.script.experimental.api.*
|
import kotlin.script.experimental.api.*
|
||||||
import kotlin.script.experimental.host.toScriptSource
|
import kotlin.script.experimental.host.toScriptSource
|
||||||
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
|
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
|
||||||
@@ -19,7 +25,7 @@ import kotlin.script.experimental.jvm.util.renderError
|
|||||||
class ScriptEvaluationTest : TestCase() {
|
class ScriptEvaluationTest : TestCase() {
|
||||||
|
|
||||||
fun testExceptionWithCause() {
|
fun testExceptionWithCause() {
|
||||||
checkEvaluate(
|
checkEvaluateAsError(
|
||||||
"""
|
"""
|
||||||
try {
|
try {
|
||||||
throw Exception("Error!")
|
throw Exception("Error!")
|
||||||
@@ -36,15 +42,40 @@ class ScriptEvaluationTest : TestCase() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkEvaluate(script: SourceCode, expectedOutput: String) {
|
// KT-19423
|
||||||
val compilationConfiguration = ScriptCompilationConfiguration()
|
fun testClassCapturingScriptInstance() {
|
||||||
val compiler = ScriptJvmCompilerIsolated(defaultJvmScriptingHostConfiguration)
|
val res = checkEvaluate(
|
||||||
val compiled = compiler.compile(script, compilationConfiguration).valueOrThrow()
|
"""
|
||||||
val evaluationConfiguration = ScriptEvaluationConfiguration()
|
val used = "abc"
|
||||||
val evaluator = BasicJvmScriptEvaluator()
|
class User {
|
||||||
val res = runBlocking {
|
val property = used
|
||||||
evaluator.invoke(compiled, evaluationConfiguration).valueOrThrow()
|
}
|
||||||
|
|
||||||
|
User().property
|
||||||
|
""".trimIndent().toScriptSource()
|
||||||
|
)
|
||||||
|
assertEquals("abc", (res.returnValue as ResultValue.Value).value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testObjectCapturingScriptInstance() {
|
||||||
|
val res = checkCompile(
|
||||||
|
"""
|
||||||
|
val used = "abc"
|
||||||
|
object User {
|
||||||
|
val property = used
|
||||||
|
}
|
||||||
|
|
||||||
|
User.property
|
||||||
|
""".trimIndent().toScriptSource()
|
||||||
|
)
|
||||||
|
assertTrue(res is ResultWithDiagnostics.Failure)
|
||||||
|
if (!res.reports.any { it.message == "Object User captures the script class instance. Try to use class or anonymous object instead" }) {
|
||||||
|
fail("expecting error about object capturing script instance, got:\n ${res.reports.joinToString("\n ") { it.message }}")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkEvaluateAsError(script: SourceCode, expectedOutput: String): EvaluationResult {
|
||||||
|
val res = checkEvaluate(script)
|
||||||
assert(res.returnValue is ResultValue.Error)
|
assert(res.returnValue is ResultValue.Error)
|
||||||
ByteArrayOutputStream().use { os ->
|
ByteArrayOutputStream().use { os ->
|
||||||
val ps = PrintStream(os)
|
val ps = PrintStream(os)
|
||||||
@@ -52,5 +83,22 @@ class ScriptEvaluationTest : TestCase() {
|
|||||||
ps.flush()
|
ps.flush()
|
||||||
assertEqualsTrimmed(expectedOutput, os.toString())
|
assertEqualsTrimmed(expectedOutput, os.toString())
|
||||||
}
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkCompile(script: SourceCode): ResultWithDiagnostics<CompiledScript> {
|
||||||
|
val compilationConfiguration = ScriptCompilationConfiguration()
|
||||||
|
val compiler = ScriptJvmCompilerIsolated(defaultJvmScriptingHostConfiguration)
|
||||||
|
return compiler.compile(script, compilationConfiguration)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkEvaluate(script: SourceCode): EvaluationResult {
|
||||||
|
val compiled = checkCompile(script).valueOrThrow()
|
||||||
|
val evaluationConfiguration = ScriptEvaluationConfiguration()
|
||||||
|
val evaluator = BasicJvmScriptEvaluator()
|
||||||
|
val res = runBlocking {
|
||||||
|
evaluator.invoke(compiled, evaluationConfiguration).valueOrThrow()
|
||||||
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user