From 385aab841842eb853f58327cb8b2fb9e3d51cedd Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Wed, 7 Sep 2016 12:03:34 +0200 Subject: [PATCH] Add output checking to script tests --- compiler/testData/script/fib_ext.kts | 2 +- .../kotlin/daemon/CompilerDaemonTest.kt | 2 + .../kotlin/scripts/ScriptTemplateTest.kt | 99 ++++++++++++++++--- .../jetbrains/kotlin/scripts/ScriptTest.kt | 30 ++++-- .../kotlin/scripts/scriptTestsUtil.kt | 46 +++++++++ 5 files changed, 153 insertions(+), 26 deletions(-) create mode 100644 compiler/tests/org/jetbrains/kotlin/scripts/scriptTestsUtil.kt diff --git a/compiler/testData/script/fib_ext.kts b/compiler/testData/script/fib_ext.kts index 31ee5058bd3..c862588b224 100644 --- a/compiler/testData/script/fib_ext.kts +++ b/compiler/testData/script/fib_ext.kts @@ -9,7 +9,7 @@ fun fib(n: Int): Int { val hdr = "Num".decapitalize() -org.junit.Assert.assertTrue(false) +org.junit.Assert.assertTrue(true) System.out.println("$hdr: $num") val result = fib(num) diff --git a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt index b2178bbf3ce..43f7d324fcf 100644 --- a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt @@ -24,7 +24,9 @@ import org.jetbrains.kotlin.daemon.client.RemoteOutputStreamServer import org.jetbrains.kotlin.daemon.common.* import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase import org.jetbrains.kotlin.progress.CompilationCanceledStatus +import org.jetbrains.kotlin.script.* import org.jetbrains.kotlin.test.KotlinTestUtils +import org.jetbrains.kotlin.utils.PathUtil import java.io.ByteArrayOutputStream import java.io.File import java.rmi.server.UnicastRemoteObject diff --git a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt index a31faf9cf58..7bb87a3ae16 100644 --- a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt @@ -19,12 +19,14 @@ package org.jetbrains.kotlin.scripts import com.intellij.openapi.util.Disposer import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys 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.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler import org.jetbrains.kotlin.codegen.CompilationException import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.config.addKotlinSourceRoot +import org.jetbrains.kotlin.daemon.toFile import org.jetbrains.kotlin.script.* import org.jetbrains.kotlin.test.ConfigurationKind import org.jetbrains.kotlin.test.KotlinTestUtils @@ -50,84 +52,125 @@ class ScriptTemplateTest { fun testScriptWithParam() { val aClass = compileScript("fib.kts", ScriptWithIntParam::class, null) 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 fun testScriptWithClassParameter() { val aClass = compileScript("fib_cp.kts", ScriptWithClassParam::class, null, runIsolated = false) 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 fun testScriptWithBaseClassWithParam() { val aClass = compileScript("fib_dsl.kts", ScriptWithBaseClass::class, null, runIsolated = false) 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 fun testScriptWithDependsAnn() { val aClass = compileScript("fib_ext_ann.kts", ScriptWithIntParam::class, null) 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 fun testScriptWithDependsAnn2() { val aClass = compileScript("fib_ext_ann2.kts", ScriptWithIntParam::class, null) 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 fun testScriptWithoutParams() { val aClass = compileScript("without_params.kts", ScriptWithoutParams::class, null) Assert.assertNotNull(aClass) - aClass!!.getConstructor(Integer.TYPE).newInstance(4) + val out = captureOut { + aClass!!.getConstructor(Integer.TYPE).newInstance(4) + } + Assert.assertEquals("10\n", out) } @Test - fun testScriptWithOverridenParam() { + fun testScriptWithOverriddenParam() { val aClass = compileScript("overridden_parameter.kts", ScriptBaseClassWithOverriddenProperty::class, null) Assert.assertNotNull(aClass) - aClass!!.getConstructor(Integer.TYPE).newInstance(4) + val out = captureOut { + aClass!!.getConstructor(Integer.TYPE).newInstance(4) + } + Assert.assertEquals("14\n", out) } @Test fun testScriptWithArrayParam() { val aClass = compileScript("array_parameter.kts", ScriptWithArrayParam::class, null) Assert.assertNotNull(aClass) - aClass!!.getConstructor(Array::class.java).newInstance(arrayOf("one", "two")) + captureOut { + aClass!!.getConstructor(Array::class.java).newInstance(arrayOf("one", "two")) + }.let { + Assert.assertEquals("one and two\n", it) + } } @Test fun testScriptWithNullableParam() { val aClass = compileScript("nullable_parameter.kts", ScriptWithNullableParam::class, null) 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 fun testScriptVarianceParams() { val aClass = compileScript("variance_parameters.kts", ScriptVarianceParams::class, null) Assert.assertNotNull(aClass) - aClass!!.getConstructor(Array::class.java, Array::class.java).newInstance(arrayOf("one"), arrayOf(1, 2)) + captureOut { + aClass!!.getConstructor(Array::class.java, Array::class.java).newInstance(arrayOf("one"), arrayOf(1, 2)) + }.let { + Assert.assertEquals("one and 1\n", it) + } } @Test fun testScriptWithNullableProjection() { val aClass = compileScript("nullable_projection.kts", ScriptWithNullableProjection::class, null) Assert.assertNotNull(aClass) - aClass!!.getConstructor(Array::class.java).newInstance(arrayOf(null)) + captureOut { + aClass!!.getConstructor(Array::class.java).newInstance(arrayOf(null)) + }.let { + Assert.assertEquals("nullable\n", it) + } } @Test fun testScriptWithArray2DParam() { val aClass = compileScript("array2d_param.kts", ScriptWithArray2DParam::class, null) Assert.assertNotNull(aClass) - aClass!!.getConstructor(Array>::class.java).newInstance(arrayOf(arrayOf("one"), arrayOf("two"))) + captureOut { + aClass!!.getConstructor(Array>::class.java).newInstance(arrayOf(arrayOf("one"), arrayOf("two"))) + }.let { + Assert.assertEquals("first: one, size: 1\n", it) + } } @Test @@ -155,7 +198,7 @@ class ScriptTemplateTest { fun testScriptWithParamConversion() { val aClass = compileScript("fib.kts", ScriptWithIntParam::class) Assert.assertNotNull(aClass) - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4")) + val anObj = tryConstructScriptClass(aClass!!, listOf("4")) Assert.assertNotNull(anObj) } @@ -165,7 +208,7 @@ class ScriptTemplateTest { Assert.assertNotNull(aClass) var exceptionThrown = false try { - KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) + tryConstructScriptClass(aClass!!, emptyList()) } catch (e: InvocationTargetException) { 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?, + report: (ScriptDependenciesResolver.ReportSeverity, String, ScriptContents.Position?) -> Unit, + previousDependencies: KotlinScriptExternalDependencies? + ): Future + { + return object : KotlinScriptExternalDependencies { + override val classpath: Iterable = classpathFromClassloader() + override val imports: Iterable = listOf("org.jetbrains.kotlin.scripts.DependsOn", "org.jetbrains.kotlin.scripts.DependsOnTwo") + }.asFuture() + } + + protected fun classpathFromClassloader(): List = + (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() } diff --git a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt index 8d4ffac3400..555ec0a1f9c 100644 --- a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt @@ -42,16 +42,22 @@ class ScriptTest : KtUsefulTestCase() { fun testStandardScriptWithParams() { val aClass = compileScript("fib_std.kts", StandardScriptDefinition) Assert.assertNotNull(aClass) - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4", "comment")) - Assert.assertNotNull(anObj) + val out = captureOut { + val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4", "comment")) + Assert.assertNotNull(anObj) + } + Assert.assertEquals(NUM_4_LINE + " (comment)" + FIB_SCRIPT_OUTPUT_TAIL, out) } @Test fun testStandardScriptWithoutParams() { val aClass = compileScript("fib_std.kts", StandardScriptDefinition) Assert.assertNotNull(aClass) - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) - Assert.assertNotNull(anObj) + val out = captureOut { + val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) + Assert.assertNotNull(anObj) + } + Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out) } @Test @@ -60,13 +66,19 @@ class ScriptTest : KtUsefulTestCase() { tmpdir.mkdirs() val aClass = compileScript("fib_std.kts", StandardScriptDefinition, saveClassesDir = tmpdir) Assert.assertNotNull(aClass) - val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) - Assert.assertNotNull(anObj) - val savedClassLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), aClass.classLoader) + val out1 = captureOut { + val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList()) + 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) Assert.assertNotNull(aClassSaved) - val anObjSaved = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClassSaved!!, emptyList()) - Assert.assertNotNull(anObjSaved) + val out2 = captureOut { + val anObjSaved = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClassSaved!!, emptyList()) + Assert.assertNotNull(anObjSaved) + } + Assert.assertEquals(NUM_4_LINE + " (none)" + FIB_SCRIPT_OUTPUT_TAIL, out2) } private fun compileScript( diff --git a/compiler/tests/org/jetbrains/kotlin/scripts/scriptTestsUtil.kt b/compiler/tests/org/jetbrains/kotlin/scripts/scriptTestsUtil.kt new file mode 100644 index 00000000000..6e647987ae0 --- /dev/null +++ b/compiler/tests/org/jetbrains/kotlin/scripts/scriptTestsUtil.kt @@ -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() +} +