Implement Invocable on base evaluator and locally-evaluating JSR223 sample engines

fixes #KT-14707
This commit is contained in:
Ilya Chernikov
2016-12-01 12:30:30 +01:00
parent fbd4c6eb61
commit c82e91eafe
9 changed files with 166 additions and 46 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.script.jsr223
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import org.junit.Assert
import org.junit.Test
import javax.script.Invocable
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
@@ -65,4 +66,41 @@ class KotlinJsr223ScriptEngineIT {
val res2 = engine.eval("x + 2")
Assert.assertEquals(5, res2)
}
@Test
fun testInvocable() {
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val res1 = engine.eval("""
fun fn(x: Int) = x + 2
val obj = object {
fun fn1(x: Int) = x + 3
}
obj
""")
Assert.assertNotNull(res1)
val invocator = engine as? Invocable
Assert.assertNotNull(invocator)
assertThrows(NoSuchMethodException::class.java) {
invocator!!.invokeFunction("fn1", 3)
}
val res2 = invocator!!.invokeFunction("fn", 3)
Assert.assertEquals(5, res2)
assertThrows(NoSuchMethodException::class.java) {
invocator!!.invokeMethod(res1, "fn", 3)
}
val res3 = invocator!!.invokeMethod(res1, "fn1", 3)
Assert.assertEquals(6, res3)
}
}
fun assertThrows(exceptionClass: Class<*>, body: () -> Unit) {
try {
body()
Assert.fail("Expecting an exception of type ${exceptionClass.name}")
}
catch (e: Throwable) {
if (!exceptionClass.isAssignableFrom(e.javaClass)) {
Assert.fail("Expecting an exception of type ${exceptionClass.name} but got ${e.javaClass.name}")
}
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.script.jsr223
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import org.junit.Assert
import org.junit.Test
import javax.script.Invocable
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
@@ -65,4 +66,41 @@ class KotlinJsr223ScriptEngineIT {
val res2 = engine.eval("x + 2")
Assert.assertEquals(5, res2)
}
@Test
fun testInvocable() {
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val res1 = engine.eval("""
fun fn(x: Int) = x + 2
val obj = object {
fun fn1(x: Int) = x + 3
}
obj
""")
Assert.assertNotNull(res1)
val invocator = engine as? Invocable
Assert.assertNotNull(invocator)
assertThrows(NoSuchMethodException::class.java) {
invocator!!.invokeFunction("fn1", 3)
}
val res2 = invocator!!.invokeFunction("fn", 3)
Assert.assertEquals(5, res2)
assertThrows(NoSuchMethodException::class.java) {
invocator!!.invokeMethod(res1, "fn", 3)
}
val res3 = invocator!!.invokeMethod(res1, "fn1", 3)
Assert.assertEquals(6, res3)
}
}
fun assertThrows(exceptionClass: Class<*>, body: () -> Unit) {
try {
body()
Assert.fail("Expecting an exception of type ${exceptionClass.name}")
}
catch (e: Throwable) {
if (!exceptionClass.isAssignableFrom(e.javaClass)) {
Assert.fail("Expecting an exception of type ${exceptionClass.name} but got ${e.javaClass.name}")
}
}
}