Prepare JsTestChecker (but not introduce so far) for J2V8 adoption
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -671,7 +671,7 @@ abstract class BasicBoxTest(
|
||||
val result = engineForMinifier.runAndRestoreContext {
|
||||
runList.forEach(this::loadFile)
|
||||
overrideAsserter()
|
||||
eval(NashornJsTestChecker.SETUP_KOTLIN_OUTPUT)
|
||||
eval<String>(NashornJsTestChecker.SETUP_KOTLIN_OUTPUT)
|
||||
runTestFunction(testModuleName, testPackage, testFunction, withModuleSystem)
|
||||
}
|
||||
TestCase.assertEquals(expectedResult, result)
|
||||
|
||||
+23
-37
@@ -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<Any>(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<String, Any?> = 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<String, Any?>? = 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<String>, expectedResult: String) {
|
||||
run(files)
|
||||
val actualResult = engine.eval(GET_KOTLIN_OUTPUT)
|
||||
val actualResult = engine.eval<String>(GET_KOTLIN_OUTPUT)
|
||||
Assert.assertEquals(expectedResult, actualResult)
|
||||
}
|
||||
|
||||
@@ -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<String, Any?>
|
||||
@@ -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 <T> eval(script: String): T
|
||||
fun getGlobalContext(): GlobalRuntimeContext
|
||||
fun evalVoid(script: String)
|
||||
fun <T> callMethod(obj: Any, name: String, vararg args: Any?): T
|
||||
fun loadFile(path: String)
|
||||
}
|
||||
@@ -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 <T> 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 <T> 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('\\', '/')}');")
|
||||
}
|
||||
}
|
||||
@@ -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<String>("box()")
|
||||
|
||||
Assert.assertEquals("$fileName: box() function must return 'OK'", "OK", result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user