diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java index 6d68261bd79..4adace84090 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -107,7 +107,6 @@ public class KotlinTestUtils { private static final boolean DONT_IGNORE_TESTS_WORKING_ON_COMPATIBLE_BACKEND = Boolean.getBoolean("org.jetbrains.kotlin.dont.ignore.tests.working.on.compatible.backend"); - private static final boolean AUTOMATICALLY_UNMUTE_PASSED_TESTS = false; private static final boolean AUTOMATICALLY_MUTE_FAILED_TESTS = false; diff --git a/js/js.tests/build.gradle.kts b/js/js.tests/build.gradle.kts index c522dd7f9e9..e198da7ce8f 100644 --- a/js/js.tests/build.gradle.kts +++ b/js/js.tests/build.gradle.kts @@ -1,7 +1,6 @@ import com.moowork.gradle.node.NodeExtension -import com.moowork.gradle.node.exec.ExecRunner -import com.moowork.gradle.node.npm.NpmExecRunner import com.moowork.gradle.node.npm.NpmTask +import org.gradle.internal.os.OperatingSystem plugins { kotlin("jvm") @@ -41,7 +40,7 @@ dependencies { testRuntime(project(":kotlin-preloader")) // it's required for ant tests testRuntime(project(":compiler:backend-common")) testRuntime(commonDep("org.fusesource.jansi", "jansi")) - + antLauncherJar(commonDep("org.apache.ant", "ant")) antLauncherJar(files(toolsJar())) } @@ -65,7 +64,7 @@ projectTest { } val prefixForPpropertiesToForward = "fd." - for((key, value) in properties) { + for ((key, value) in properties) { if (key.startsWith(prefixForPpropertiesToForward)) { systemProperty(key.substring(prefixForPpropertiesToForward.length), value) } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt index 7b1d7cb5da0..7ae66e9d3ea 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt @@ -671,7 +671,7 @@ abstract class BasicBoxTest( val result = engineForMinifier.runAndRestoreContext { runList.forEach(this::loadFile) overrideAsserter() - eval(NashornJsTestChecker.SETUP_KOTLIN_OUTPUT) + eval(NashornJsTestChecker.SETUP_KOTLIN_OUTPUT) runTestFunction(testModuleName, testPackage, testFunction, withModuleSystem) } TestCase.assertEquals(expectedResult, result) diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/JsTestChecker.kt similarity index 70% rename from js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt rename to js/js.tests/test/org/jetbrains/kotlin/js/test/JsTestChecker.kt index 6c4b3b95f2a..92185a5db6d 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/JsTestChecker.kt @@ -5,19 +5,18 @@ package org.jetbrains.kotlin.js.test -import jdk.nashorn.api.scripting.NashornScriptEngineFactory -import jdk.nashorn.api.scripting.ScriptObjectMirror import jdk.nashorn.internal.runtime.ScriptRuntime +import org.jetbrains.kotlin.js.test.interop.GlobalRuntimeContext +import org.jetbrains.kotlin.js.test.interop.ScriptEngine +import org.jetbrains.kotlin.js.test.interop.ScriptEngineNashorn import org.junit.Assert -import javax.script.Invocable -import javax.script.ScriptEngine -fun createScriptEngine(): ScriptEngine = - // TODO use "-strict" - NashornScriptEngineFactory().getScriptEngine("--language=es5", "--no-java", "--no-syntax-extensions") +fun createScriptEngine(): ScriptEngine { + return ScriptEngineNashorn() +} fun ScriptEngine.overrideAsserter() { - eval("this['kotlin-test'].kotlin.test.overrideAsserter_wbnzx$(this['kotlin-test'].kotlin.test.DefaultAsserter);") + evalVoid("this['kotlin-test'].kotlin.test.overrideAsserter_wbnzx$(this['kotlin-test'].kotlin.test.DefaultAsserter);") } fun ScriptEngine.runTestFunction( @@ -25,37 +24,24 @@ fun ScriptEngine.runTestFunction( testPackageName: String?, testFunctionName: String, withModuleSystem: Boolean -): Any? { - val testModule = - when { - withModuleSystem -> - eval(BasicBoxTest.KOTLIN_TEST_INTERNAL + ".require('" + testModuleName!! + "')") - testModuleName === null -> - eval("this") - else -> - get(testModuleName) - } - testModule as ScriptObjectMirror +): String? { + var script = when { + withModuleSystem -> BasicBoxTest.KOTLIN_TEST_INTERNAL + ".require('" + testModuleName!! + "')" + testModuleName === null -> "this" + else -> testModuleName + } - val testPackage = - when { - testPackageName === null -> - testModule - testPackageName.contains(".") -> - testPackageName.split(".").fold(testModule) { p, part -> p[part] as ScriptObjectMirror } - else -> - testModule[testPackageName]!! - } + if (testPackageName !== null) { + script += ".$testPackageName" + } - return (this as Invocable).invokeMethod(testPackage, testFunctionName) + val testPackage = eval(script) + return callMethod(testPackage, testFunctionName) } -fun ScriptEngine.loadFile(path: String) { - eval("load('${path.replace('\\', '/')}');") -} fun ScriptEngine.runAndRestoreContext( - globalObject: ScriptObjectMirror = eval("this") as ScriptObjectMirror, + globalObject: GlobalRuntimeContext = getGlobalContext(), originalState: Map = globalObject.toMap(), f: ScriptEngine.() -> Any? ): Any? { @@ -73,13 +59,13 @@ abstract class AbstractNashornJsTestChecker { private var engineUsageCnt = 0 private var engineCache: ScriptEngine? = null - private var globalObject: ScriptObjectMirror? = null + private var globalObject: GlobalRuntimeContext? = null private var originalState: Map? = null protected val engine get() = engineCache ?: createScriptEngineForTest().also { engineCache = it - globalObject = it.eval("this") as ScriptObjectMirror + globalObject = it.getGlobalContext() originalState = globalObject?.toMap() } @@ -137,12 +123,12 @@ object NashornJsTestChecker : AbstractNashornJsTestChecker() { private const val GET_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.buffer;" override fun beforeRun() { - engine.eval(SETUP_KOTLIN_OUTPUT) + engine.evalVoid(SETUP_KOTLIN_OUTPUT) } fun checkStdout(files: List, expectedResult: String) { run(files) - val actualResult = engine.eval(GET_KOTLIN_OUTPUT) + val actualResult = engine.eval(GET_KOTLIN_OUTPUT) Assert.assertEquals(expectedResult, actualResult) } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/GlobalRuntimeContext.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/GlobalRuntimeContext.kt new file mode 100644 index 00000000000..61da2ce353f --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/GlobalRuntimeContext.kt @@ -0,0 +1,8 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.js.test.interop + +typealias GlobalRuntimeContext = MutableMap \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/ScriptEngine.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/ScriptEngine.kt new file mode 100644 index 00000000000..450bb6f74de --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/ScriptEngine.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.js.test.interop + +interface ScriptEngine { + fun eval(script: String): T + fun getGlobalContext(): GlobalRuntimeContext + fun evalVoid(script: String) + fun callMethod(obj: Any, name: String, vararg args: Any?): T + fun loadFile(path: String) +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/ScriptEngineNashorn.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/ScriptEngineNashorn.kt new file mode 100644 index 00000000000..a33be58982e --- /dev/null +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/interop/ScriptEngineNashorn.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.js.test.interop + +import jdk.nashorn.api.scripting.NashornScriptEngineFactory +import javax.script.Invocable + +class ScriptEngineNashorn : ScriptEngine { + // TODO use "-strict" + private val myEngine = NashornScriptEngineFactory().getScriptEngine("--language=es5", "--no-java", "--no-syntax-extensions") + + @Suppress("UNCHECKED_CAST") + override fun eval(script: String): T { + return myEngine.eval(script) as T + } + + override fun evalVoid(script: String) { + myEngine.eval(script) + } + + override fun getGlobalContext(): GlobalRuntimeContext { + return eval("this") + } + + @Suppress("UNCHECKED_CAST") + override fun callMethod(obj: Any, name: String, vararg args: Any?): T { + return (myEngine as Invocable).invokeMethod(obj, name, *args) as T + } + + override fun loadFile(path: String) { + evalVoid("load('${path.replace('\\', '/')}');") + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt index f0a7c4379c5..900de61b370 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/BasicOptimizerTest.kt @@ -128,8 +128,8 @@ abstract class BasicOptimizerTest(private var basePath: String) { private fun runScript(fileName: String, code: String) { val engine = createScriptEngine() - engine.eval(code) - val result = engine.eval("box()") + engine.evalVoid(code) + val result = engine.eval("box()") Assert.assertEquals("$fileName: box() function must return 'OK'", "OK", result) }