Fix all small and medium-sized issues after review

This commit is contained in:
Ilya Chernikov
2016-10-06 18:46:46 +02:00
parent 9617fc7d6b
commit c2b5c11781
32 changed files with 195 additions and 312 deletions
@@ -22,14 +22,14 @@ import junit.framework.TestCase
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
import org.jetbrains.kotlin.cli.common.repl.*
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase
import org.jetbrains.kotlin.script.KotlinScriptDefinition
import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate
import org.jetbrains.kotlin.utils.PathUtil
import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.TestJdkKind
import org.junit.Test
import java.io.File
import java.net.URLClassLoader
@@ -48,7 +48,7 @@ class GenericReplTest : TestCase() {
val res1 = repl.replCompiler?.check(ReplCodeLine(0, "val x ="), emptyList())
TestCase.assertTrue("Unexpected check results: $res1", res1 is ReplCheckResult.Incomplete)
val codeLine0 = ReplCodeLine(0, "1 + 2")
val codeLine0 = ReplCodeLine(0, "val l1 = listOf(1 + 2)\nl1.first()")
val res2 = repl.replCompiler?.compile(codeLine0, emptyList())
val res2c = res2 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res2", res2c)
@@ -89,12 +89,10 @@ internal class TestRepl(
templateClasspath: List<File>,
templateClassName: String
) {
private val configuration = CompilerConfiguration().apply {
addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
addJvmClasspathRoots(PathUtil.getKotlinPathsForCompiler().let { listOf(it.runtimePath, it.reflectPath) })
addJvmClasspathRoots(templateClasspath)
private val configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, *templateClasspath.toTypedArray()).apply {
put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script")
}
private fun makeScriptDefinition(templateClasspath: List<File>, templateClassName: String): KotlinScriptDefinition {
val classloader = URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), this.javaClass.classLoader)
val cls = classloader.loadClass(templateClassName)
@@ -510,7 +510,7 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
val res0 = repl.check(ReplCodeLine(0, "val x ="), emptyList())
TestCase.assertTrue("Unexpected check results: $res0", res0 is ReplCheckResult.Incomplete)
val codeLine1 = ReplCodeLine(1, "val x = 5")
val codeLine1 = ReplCodeLine(1, "val lst = listOf(1)\nval x = 5")
val res1 = repl.compile(codeLine1, emptyList())
val res1c = res1 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res1", res1c)
@@ -181,29 +181,45 @@ class ScriptTemplateTest {
fun testScriptWithStandardTemplate() {
val aClass = compileScript("fib_std.kts", StandardScriptTemplate::class, runIsolated = false)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf("4", "other"))
captureOut {
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf("4", "other"))
}.let {
assertEqualsTrimmed(NUM_4_LINE + " (other)" + FIB_SCRIPT_OUTPUT_TAIL, it)
}
}
@Test
fun testScriptWithPackage() {
val aClass = compileScript("fib.pkg.kts", ScriptWithIntParam::class)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
captureOut {
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}.let {
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, it)
}
}
@Test
fun testScriptWithScriptDefinition() {
val aClass = compileScript("fib.kts", ScriptWithIntParam::class)
Assert.assertNotNull(aClass)
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
captureOut {
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
}.let {
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, it)
}
}
@Test
fun testScriptWithParamConversion() {
val aClass = compileScript("fib.kts", ScriptWithIntParam::class)
Assert.assertNotNull(aClass)
val anObj = tryConstructScriptClass(aClass!!, listOf("4"))
Assert.assertNotNull(anObj)
captureOut {
val anObj = tryConstructScriptClass(aClass!!, listOf("4"))
Assert.assertNotNull(anObj)
}.let {
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, it)
}
}
@Test
@@ -275,8 +291,6 @@ class ScriptTemplateTest {
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?>?,
@@ -39,9 +39,13 @@ internal fun captureOut(body: () -> Unit): String {
val outStream = ByteArrayOutputStream()
val prevOut = System.out
System.setOut(PrintStream(outStream))
body()
System.out.flush()
System.setOut(prevOut)
try {
body()
}
finally {
System.out.flush()
System.setOut(prevOut)
}
return outStream.toString()
}