Refactor and fix script annotation processing
This commit is contained in:
@@ -19,7 +19,7 @@ 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.tryConstructClassFromStringArgs
|
||||
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
||||
@@ -35,6 +35,8 @@ import org.jetbrains.kotlin.utils.PathUtil
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
import java.io.OutputStream
|
||||
import java.io.PrintStream
|
||||
import java.lang.Exception
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.net.URL
|
||||
@@ -91,7 +93,14 @@ class ScriptTemplateTest {
|
||||
|
||||
@Test
|
||||
fun testScriptWithDependsAnn2() {
|
||||
Assert.assertNull(compileScript("fib_ext_ann2.kts", ScriptWithIntParamAndDummyResolver::class, null, includeKotlinRuntime = false))
|
||||
val savedErr = System.err
|
||||
try {
|
||||
System.setErr(PrintStream(NullOutputStream()))
|
||||
Assert.assertNull(compileScript("fib_ext_ann2.kts", ScriptWithIntParamAndDummyResolver::class, null, includeKotlinRuntime = false))
|
||||
}
|
||||
finally {
|
||||
System.setErr(savedErr)
|
||||
}
|
||||
|
||||
val aClass = compileScript("fib_ext_ann2.kts", ScriptWithIntParam::class, null, includeKotlinRuntime = false)
|
||||
Assert.assertNotNull(aClass)
|
||||
@@ -324,13 +333,13 @@ class TestKotlinScriptDependenciesResolver : TestKotlinScriptDummyDependenciesRe
|
||||
val cp = script.annotations.flatMap {
|
||||
when (it) {
|
||||
is DependsOn -> if (it.path == "@{runtime}") listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath) else listOf(File(it.path))
|
||||
is DependsOnTwo -> listOf(it.path1, it.path2).flatMap {
|
||||
is DependsOnTwo -> listOf(it.path1, it.path2).flatMap { it?.let {
|
||||
when {
|
||||
it.isBlank() -> emptyList()
|
||||
it == "@{runtime}" -> listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath)
|
||||
else -> listOf(File(it))
|
||||
}
|
||||
}
|
||||
}}
|
||||
is InvalidScriptResolverAnnotation -> throw Exception("Invalid annotation ${it.name}", it.error)
|
||||
else -> throw Exception("Unknown annotation ${it.javaClass}")
|
||||
}
|
||||
@@ -394,3 +403,9 @@ annotation class DependsOn(val path: String)
|
||||
@Target(AnnotationTarget.FILE)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class DependsOnTwo(val unused: String = "", val path1: String = "", val path2: String = "")
|
||||
|
||||
private class NullOutputStream : OutputStream() {
|
||||
override fun write(b: Int) { }
|
||||
override fun write(b: ByteArray) { }
|
||||
override fun write(b: ByteArray, off: Int, len: Int) { }
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ 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.tryConstructClassFromStringArgs
|
||||
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.util
|
||||
|
||||
import org.jetbrains.kotlin.utils.tryCreateCallableMappingFromNamedArgs
|
||||
import org.jetbrains.kotlin.utils.tryCreateCallableMappingFromStringArgs
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import kotlin.reflect.KParameter
|
||||
|
||||
class ArgsToParamsMatchingTest {
|
||||
|
||||
@Test
|
||||
fun testMatchFromStrings() {
|
||||
Assert.assertNull(tryCreateCallableMappingFromStringArgs(::foo, listOf()))
|
||||
Assert.assertNull(tryCreateCallableMappingFromStringArgs(::foo, listOf("1", "2")))
|
||||
|
||||
assertParamMapsEquals(tryCreateCallableMappingFromStringArgs(::foo, listOf("1", "2", "s", "0.1")),
|
||||
"i" to 1, "b" to 2.toByte(), "c" to 's', "d" to 0.1)
|
||||
|
||||
Assert.assertNull(tryCreateCallableMappingFromStringArgs(::foo, listOf("1", "258", "s", "0.1")))
|
||||
Assert.assertNull(tryCreateCallableMappingFromStringArgs(::foo, listOf("1", "258", "s", "0")))
|
||||
Assert.assertNull(tryCreateCallableMappingFromStringArgs(::foo, listOf("1", "258", "sss", "0.1")))
|
||||
|
||||
assertParamMapsEquals(tryCreateCallableMappingFromStringArgs(::foo, listOf("1", "2", "s", "0.1", "abc", "true", "1", "2", "3")),
|
||||
"i" to 1, "b" to 2.toByte(), "c" to 's', "d" to 0.1, "s" to "abc", "t" to true, "v" to arrayOf(1L, 2L, 3L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMatchNamed() {
|
||||
Assert.assertNull(tryCreateCallableMappingFromNamedArgs(::foo, listOf()))
|
||||
Assert.assertNull(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2)))
|
||||
|
||||
assertParamMapsEquals(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2.toByte(), null to 's', null to 0.1)),
|
||||
"i" to 1, "b" to 2.toByte(), "c" to 's', "d" to 0.1)
|
||||
|
||||
assertParamMapsEquals(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2.toByte(), "c" to 's', "d" to 0.1)),
|
||||
"i" to 1, "b" to 2.toByte(), "c" to 's', "d" to 0.1)
|
||||
|
||||
assertParamMapsEquals(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2.toByte(), "d" to 0.1, "c" to 's')),
|
||||
"i" to 1, "b" to 2.toByte(), "c" to 's', "d" to 0.1)
|
||||
|
||||
assertParamMapsEquals(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2.toByte(), null to 's', null to 0.1, "v" to arrayOf(1L, 2L, 3L))),
|
||||
"i" to 1, "b" to 2.toByte(), "c" to 's', "d" to 0.1, "v" to arrayOf(1L, 2L, 3L))
|
||||
|
||||
Assert.assertNull(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2.toByte(), null to 's', "x" to 0.1))) // wrong name
|
||||
Assert.assertNull(tryCreateCallableMappingFromNamedArgs(::foo, listOf(null to 1, null to 2.toByte(), "c" to 's', null to 0.1))) // unnamed after named
|
||||
}
|
||||
}
|
||||
|
||||
private fun assertParamMapsEquals(actuals: Map<KParameter, Any?>?, vararg expected: Pair<String, Any?>) {
|
||||
Assert.assertNotNull(actuals)
|
||||
val stringifiedActuals = actuals!!.mapKeys { it.key.name }
|
||||
val mappedExpected = expected.toMap()
|
||||
if (expected != stringifiedActuals) {
|
||||
Assert.assertEquals(stringifiedActuals.keys, mappedExpected.keys)
|
||||
mappedExpected.forEach { exp ->
|
||||
val actVal = stringifiedActuals[exp.key]
|
||||
if (exp.value != actVal) {
|
||||
val msg = "Unexpected value for key '${exp.key}'; expected: ${exp.value}, actual: $actVal"
|
||||
if ((exp.value?.javaClass?.isArray ?: false) && (actVal?.javaClass?.isArray ?: false )) {
|
||||
Assert.assertArrayEquals(msg, arrayOf(exp.value), arrayOf(actVal)) // tricking Array.deepEquals to compare single element arrays (instead of tedious casting to typed array)
|
||||
}
|
||||
else {
|
||||
Assert.assertEquals(msg, exp.value, actVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun foo(i: Int, b: Byte, c: Char, d: Double = 0.0, s: String = "", t: Boolean = true, vararg v: Long) {}
|
||||
Reference in New Issue
Block a user