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()
|
||||
|
||||
org.junit.Assert.assertTrue(false)
|
||||
org.junit.Assert.assertTrue(true)
|
||||
|
||||
System.out.println("$hdr: $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.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
|
||||
|
||||
@@ -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<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
|
||||
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<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
|
||||
fun testScriptWithNullableProjection() {
|
||||
val aClass = compileScript("nullable_projection.kts", ScriptWithNullableProjection::class, null)
|
||||
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
|
||||
fun testScriptWithArray2DParam() {
|
||||
val aClass = compileScript("array2d_param.kts", ScriptWithArray2DParam::class, null)
|
||||
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
|
||||
@@ -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<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() }
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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