Add output checking to script tests
This commit is contained in:
Vendored
+1
-1
@@ -9,7 +9,7 @@ fun fib(n: Int): Int {
|
|||||||
|
|
||||||
val hdr = "Num".decapitalize()
|
val hdr = "Num".decapitalize()
|
||||||
|
|
||||||
org.junit.Assert.assertTrue(false)
|
org.junit.Assert.assertTrue(true)
|
||||||
|
|
||||||
System.out.println("$hdr: $num")
|
System.out.println("$hdr: $num")
|
||||||
val result = fib(num)
|
val result = fib(num)
|
||||||
|
|||||||
@@ -24,7 +24,9 @@ import org.jetbrains.kotlin.daemon.client.RemoteOutputStreamServer
|
|||||||
import org.jetbrains.kotlin.daemon.common.*
|
import org.jetbrains.kotlin.daemon.common.*
|
||||||
import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase
|
import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase
|
||||||
import org.jetbrains.kotlin.progress.CompilationCanceledStatus
|
import org.jetbrains.kotlin.progress.CompilationCanceledStatus
|
||||||
|
import org.jetbrains.kotlin.script.*
|
||||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
|
import org.jetbrains.kotlin.utils.PathUtil
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.rmi.server.UnicastRemoteObject
|
import java.rmi.server.UnicastRemoteObject
|
||||||
|
|||||||
@@ -19,12 +19,14 @@ package org.jetbrains.kotlin.scripts
|
|||||||
import com.intellij.openapi.util.Disposer
|
import com.intellij.openapi.util.Disposer
|
||||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||||
import org.jetbrains.kotlin.cli.common.messages.*
|
import org.jetbrains.kotlin.cli.common.messages.*
|
||||||
|
import org.jetbrains.kotlin.cli.common.tryConstructScriptClass
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
||||||
import org.jetbrains.kotlin.codegen.CompilationException
|
import org.jetbrains.kotlin.codegen.CompilationException
|
||||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||||
import org.jetbrains.kotlin.config.addKotlinSourceRoot
|
import org.jetbrains.kotlin.config.addKotlinSourceRoot
|
||||||
|
import org.jetbrains.kotlin.daemon.toFile
|
||||||
import org.jetbrains.kotlin.script.*
|
import org.jetbrains.kotlin.script.*
|
||||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
@@ -50,84 +52,125 @@ class ScriptTemplateTest {
|
|||||||
fun testScriptWithParam() {
|
fun testScriptWithParam() {
|
||||||
val aClass = compileScript("fib.kts", ScriptWithIntParam::class, null)
|
val aClass = compileScript("fib.kts", ScriptWithIntParam::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithClassParameter() {
|
fun testScriptWithClassParameter() {
|
||||||
val aClass = compileScript("fib_cp.kts", ScriptWithClassParam::class, null, runIsolated = false)
|
val aClass = compileScript("fib_cp.kts", ScriptWithClassParam::class, null, runIsolated = false)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(TestParamClass::class.java).newInstance(TestParamClass(4))
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(TestParamClass::class.java).newInstance(TestParamClass(4))
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithBaseClassWithParam() {
|
fun testScriptWithBaseClassWithParam() {
|
||||||
val aClass = compileScript("fib_dsl.kts", ScriptWithBaseClass::class, null, runIsolated = false)
|
val aClass = compileScript("fib_dsl.kts", ScriptWithBaseClass::class, null, runIsolated = false)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Integer.TYPE, Integer.TYPE).newInstance(4, 1)
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(Integer.TYPE, Integer.TYPE).newInstance(4, 1)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithDependsAnn() {
|
fun testScriptWithDependsAnn() {
|
||||||
val aClass = compileScript("fib_ext_ann.kts", ScriptWithIntParam::class, null)
|
val aClass = compileScript("fib_ext_ann.kts", ScriptWithIntParam::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithDependsAnn2() {
|
fun testScriptWithDependsAnn2() {
|
||||||
val aClass = compileScript("fib_ext_ann2.kts", ScriptWithIntParam::class, null)
|
val aClass = compileScript("fib_ext_ann2.kts", ScriptWithIntParam::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithoutParams() {
|
fun testScriptWithoutParams() {
|
||||||
val aClass = compileScript("without_params.kts", ScriptWithoutParams::class, null)
|
val aClass = compileScript("without_params.kts", ScriptWithoutParams::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||||
|
}
|
||||||
|
Assert.assertEquals("10\n", out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithOverridenParam() {
|
fun testScriptWithOverriddenParam() {
|
||||||
val aClass = compileScript("overridden_parameter.kts", ScriptBaseClassWithOverriddenProperty::class, null)
|
val aClass = compileScript("overridden_parameter.kts", ScriptBaseClassWithOverriddenProperty::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
val out = captureOut {
|
||||||
|
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||||
|
}
|
||||||
|
Assert.assertEquals("14\n", out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithArrayParam() {
|
fun testScriptWithArrayParam() {
|
||||||
val aClass = compileScript("array_parameter.kts", ScriptWithArrayParam::class, null)
|
val aClass = compileScript("array_parameter.kts", ScriptWithArrayParam::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf("one", "two"))
|
captureOut {
|
||||||
|
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf("one", "two"))
|
||||||
|
}.let {
|
||||||
|
Assert.assertEquals("one and two\n", it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithNullableParam() {
|
fun testScriptWithNullableParam() {
|
||||||
val aClass = compileScript("nullable_parameter.kts", ScriptWithNullableParam::class, null)
|
val aClass = compileScript("nullable_parameter.kts", ScriptWithNullableParam::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Int::class.javaObjectType).newInstance(null)
|
captureOut {
|
||||||
|
aClass!!.getConstructor(Int::class.javaObjectType).newInstance(null)
|
||||||
|
}.let {
|
||||||
|
Assert.assertEquals("Param is null\n", it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptVarianceParams() {
|
fun testScriptVarianceParams() {
|
||||||
val aClass = compileScript("variance_parameters.kts", ScriptVarianceParams::class, null)
|
val aClass = compileScript("variance_parameters.kts", ScriptVarianceParams::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Array<in Number>::class.java, Array<out Number>::class.java).newInstance(arrayOf("one"), arrayOf(1, 2))
|
captureOut {
|
||||||
|
aClass!!.getConstructor(Array<in Number>::class.java, Array<out Number>::class.java).newInstance(arrayOf("one"), arrayOf(1, 2))
|
||||||
|
}.let {
|
||||||
|
Assert.assertEquals("one and 1\n", it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithNullableProjection() {
|
fun testScriptWithNullableProjection() {
|
||||||
val aClass = compileScript("nullable_projection.kts", ScriptWithNullableProjection::class, null)
|
val aClass = compileScript("nullable_projection.kts", ScriptWithNullableProjection::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf<String?>(null))
|
captureOut {
|
||||||
|
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf<String?>(null))
|
||||||
|
}.let {
|
||||||
|
Assert.assertEquals("nullable\n", it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testScriptWithArray2DParam() {
|
fun testScriptWithArray2DParam() {
|
||||||
val aClass = compileScript("array2d_param.kts", ScriptWithArray2DParam::class, null)
|
val aClass = compileScript("array2d_param.kts", ScriptWithArray2DParam::class, null)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
aClass!!.getConstructor(Array<Array<in String>>::class.java).newInstance(arrayOf(arrayOf("one"), arrayOf("two")))
|
captureOut {
|
||||||
|
aClass!!.getConstructor(Array<Array<in String>>::class.java).newInstance(arrayOf(arrayOf("one"), arrayOf("two")))
|
||||||
|
}.let {
|
||||||
|
Assert.assertEquals("first: one, size: 1\n", it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -155,7 +198,7 @@ class ScriptTemplateTest {
|
|||||||
fun testScriptWithParamConversion() {
|
fun testScriptWithParamConversion() {
|
||||||
val aClass = compileScript("fib.kts", ScriptWithIntParam::class)
|
val aClass = compileScript("fib.kts", ScriptWithIntParam::class)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4"))
|
val anObj = tryConstructScriptClass(aClass!!, listOf("4"))
|
||||||
Assert.assertNotNull(anObj)
|
Assert.assertNotNull(anObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +208,7 @@ class ScriptTemplateTest {
|
|||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
var exceptionThrown = false
|
var exceptionThrown = false
|
||||||
try {
|
try {
|
||||||
KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
|
tryConstructScriptClass(aClass!!, emptyList())
|
||||||
}
|
}
|
||||||
catch (e: InvocationTargetException) {
|
catch (e: InvocationTargetException) {
|
||||||
Assert.assertTrue(e.cause is IllegalStateException)
|
Assert.assertTrue(e.cause is IllegalStateException)
|
||||||
@@ -224,7 +267,31 @@ class ScriptTemplateTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestKotlinScriptDependenciesResolver : ScriptDependenciesResolver {
|
open class TestKotlinScriptDummyDependenciesResolver : ScriptDependenciesResolver {
|
||||||
|
|
||||||
|
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
|
||||||
|
|
||||||
|
@AcceptedAnnotations(DependsOn::class, DependsOnTwo::class)
|
||||||
|
override fun resolve(script: ScriptContents,
|
||||||
|
environment: Map<String, Any?>?,
|
||||||
|
report: (ScriptDependenciesResolver.ReportSeverity, String, ScriptContents.Position?) -> Unit,
|
||||||
|
previousDependencies: KotlinScriptExternalDependencies?
|
||||||
|
): Future<KotlinScriptExternalDependencies?>
|
||||||
|
{
|
||||||
|
return object : KotlinScriptExternalDependencies {
|
||||||
|
override val classpath: Iterable<File> = classpathFromClassloader()
|
||||||
|
override val imports: Iterable<String> = listOf("org.jetbrains.kotlin.scripts.DependsOn", "org.jetbrains.kotlin.scripts.DependsOnTwo")
|
||||||
|
}.asFuture()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun classpathFromClassloader(): List<File> =
|
||||||
|
(TestKotlinScriptDependenciesResolver::class.java.classLoader as? URLClassLoader)?.urLs
|
||||||
|
?.mapNotNull(URL::toFile)
|
||||||
|
?.filter { it.path.contains("out") && it.path.contains("test") }
|
||||||
|
?: emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestKotlinScriptDependenciesResolver : TestKotlinScriptDummyDependenciesResolver() {
|
||||||
|
|
||||||
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
|
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
|
||||||
|
|
||||||
|
|||||||
@@ -42,16 +42,22 @@ class ScriptTest : KtUsefulTestCase() {
|
|||||||
fun testStandardScriptWithParams() {
|
fun testStandardScriptWithParams() {
|
||||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
|
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4", "comment"))
|
val out = captureOut {
|
||||||
Assert.assertNotNull(anObj)
|
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4", "comment"))
|
||||||
|
Assert.assertNotNull(anObj)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + " (comment)" + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testStandardScriptWithoutParams() {
|
fun testStandardScriptWithoutParams() {
|
||||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
|
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
|
val out = captureOut {
|
||||||
Assert.assertNotNull(anObj)
|
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
|
||||||
|
Assert.assertNotNull(anObj)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -60,13 +66,19 @@ class ScriptTest : KtUsefulTestCase() {
|
|||||||
tmpdir.mkdirs()
|
tmpdir.mkdirs()
|
||||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition, saveClassesDir = tmpdir)
|
val aClass = compileScript("fib_std.kts", StandardScriptDefinition, saveClassesDir = tmpdir)
|
||||||
Assert.assertNotNull(aClass)
|
Assert.assertNotNull(aClass)
|
||||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
|
val out1 = captureOut {
|
||||||
Assert.assertNotNull(anObj)
|
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
|
||||||
val savedClassLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), aClass.classLoader)
|
Assert.assertNotNull(anObj)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out1)
|
||||||
|
val savedClassLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), aClass!!.classLoader)
|
||||||
val aClassSaved = savedClassLoader.loadClass(aClass.name)
|
val aClassSaved = savedClassLoader.loadClass(aClass.name)
|
||||||
Assert.assertNotNull(aClassSaved)
|
Assert.assertNotNull(aClassSaved)
|
||||||
val anObjSaved = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClassSaved!!, emptyList())
|
val out2 = captureOut {
|
||||||
Assert.assertNotNull(anObjSaved)
|
val anObjSaved = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClassSaved!!, emptyList())
|
||||||
|
Assert.assertNotNull(anObjSaved)
|
||||||
|
}
|
||||||
|
Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out2)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun compileScript(
|
private fun compileScript(
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.scripts
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.PrintStream
|
||||||
|
|
||||||
|
internal const val NUM_4_LINE = "num: 4"
|
||||||
|
|
||||||
|
internal const val FIB_SCRIPT_OUTPUT_TAIL =
|
||||||
|
"""
|
||||||
|
fib(1)=1
|
||||||
|
fib(0)=1
|
||||||
|
fib(2)=2
|
||||||
|
fib(1)=1
|
||||||
|
fib(3)=3
|
||||||
|
fib(1)=1
|
||||||
|
fib(0)=1
|
||||||
|
fib(2)=2
|
||||||
|
fib(4)=5
|
||||||
|
"""
|
||||||
|
|
||||||
|
internal fun captureOut(body: () -> Unit): String {
|
||||||
|
val outStream = ByteArrayOutputStream()
|
||||||
|
val prevOut = System.out
|
||||||
|
System.setOut(PrintStream(outStream))
|
||||||
|
body()
|
||||||
|
System.out.flush()
|
||||||
|
System.setOut(prevOut)
|
||||||
|
return outStream.toString()
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user