Minor: reformat and cleanup NashornJsTestChecker.kt
This commit is contained in:
@@ -13,8 +13,8 @@ import javax.script.Invocable
|
|||||||
import javax.script.ScriptEngine
|
import javax.script.ScriptEngine
|
||||||
|
|
||||||
fun createScriptEngine(): ScriptEngine =
|
fun createScriptEngine(): ScriptEngine =
|
||||||
// TODO use "-strict"
|
// TODO use "-strict"
|
||||||
NashornScriptEngineFactory().getScriptEngine("--language=es5", "--no-java", "--no-syntax-extensions")
|
NashornScriptEngineFactory().getScriptEngine("--language=es5", "--no-java", "--no-syntax-extensions")
|
||||||
|
|
||||||
fun ScriptEngine.overrideAsserter() {
|
fun ScriptEngine.overrideAsserter() {
|
||||||
eval("this['kotlin-test'].kotlin.test.overrideAsserter_wbnzx$(new this['kotlin-test'].kotlin.test.DefaultAsserter());")
|
eval("this['kotlin-test'].kotlin.test.overrideAsserter_wbnzx$(new this['kotlin-test'].kotlin.test.DefaultAsserter());")
|
||||||
@@ -27,25 +27,25 @@ fun ScriptEngine.runTestFunction(
|
|||||||
withModuleSystem: Boolean
|
withModuleSystem: Boolean
|
||||||
): Any? {
|
): Any? {
|
||||||
val testModule =
|
val testModule =
|
||||||
when {
|
when {
|
||||||
withModuleSystem ->
|
withModuleSystem ->
|
||||||
eval(BasicBoxTest.Companion.KOTLIN_TEST_INTERNAL + ".require('" + testModuleName!! + "')")
|
eval(BasicBoxTest.KOTLIN_TEST_INTERNAL + ".require('" + testModuleName!! + "')")
|
||||||
testModuleName === null ->
|
testModuleName === null ->
|
||||||
eval("this")
|
eval("this")
|
||||||
else ->
|
else ->
|
||||||
get(testModuleName)
|
get(testModuleName)
|
||||||
}
|
}
|
||||||
testModule as ScriptObjectMirror
|
testModule as ScriptObjectMirror
|
||||||
|
|
||||||
val testPackage =
|
val testPackage =
|
||||||
when {
|
when {
|
||||||
testPackageName === null ->
|
testPackageName === null ->
|
||||||
testModule
|
testModule
|
||||||
testPackageName.contains(".") ->
|
testPackageName.contains(".") ->
|
||||||
testPackageName.split(".").fold(testModule) { p, part -> p[part] as ScriptObjectMirror }
|
testPackageName.split(".").fold(testModule) { p, part -> p[part] as ScriptObjectMirror }
|
||||||
else ->
|
else ->
|
||||||
testModule[testPackageName]!!
|
testModule[testPackageName]!!
|
||||||
}
|
}
|
||||||
|
|
||||||
return (this as Invocable).invokeMethod(testPackage, testFunctionName)
|
return (this as Invocable).invokeMethod(testPackage, testFunctionName)
|
||||||
}
|
}
|
||||||
@@ -55,21 +55,20 @@ fun ScriptEngine.loadFile(path: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ScriptEngine.runAndRestoreContext(
|
fun ScriptEngine.runAndRestoreContext(
|
||||||
f: ScriptEngine.() -> Any?
|
f: ScriptEngine.() -> Any?
|
||||||
): Any? {
|
): Any? {
|
||||||
val globalObject = eval("this") as ScriptObjectMirror
|
val globalObject = eval("this") as ScriptObjectMirror
|
||||||
val before = globalObject.toMapWithAllMembers()
|
val before = globalObject.toMapWithAllMembers()
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
this.f()
|
this.f()
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
val after = globalObject.toMapWithAllMembers()
|
val after = globalObject.toMapWithAllMembers()
|
||||||
val diff = after.entries - before.entries
|
val diff = after.entries - before.entries
|
||||||
|
|
||||||
|
|
||||||
diff.forEach {
|
diff.forEach {
|
||||||
globalObject.put(it.key, before[it.key] ?: ScriptRuntime.UNDEFINED)
|
globalObject[it.key] = before[it.key] ?: ScriptRuntime.UNDEFINED
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,12 +85,12 @@ abstract class AbstractNashornJsTestChecker {
|
|||||||
get() = engineCache ?: createScriptEngineForTest().also { engineCache = it }
|
get() = engineCache ?: createScriptEngineForTest().also { engineCache = it }
|
||||||
|
|
||||||
fun check(
|
fun check(
|
||||||
files: List<String>,
|
files: List<String>,
|
||||||
testModuleName: String?,
|
testModuleName: String?,
|
||||||
testPackageName: String?,
|
testPackageName: String?,
|
||||||
testFunctionName: String,
|
testFunctionName: String,
|
||||||
expectedResult: String,
|
expectedResult: String,
|
||||||
withModuleSystem: Boolean
|
withModuleSystem: Boolean
|
||||||
) {
|
) {
|
||||||
val actualResult = run(files, testModuleName, testPackageName, testFunctionName, withModuleSystem)
|
val actualResult = run(files, testModuleName, testPackageName, testFunctionName, withModuleSystem)
|
||||||
Assert.assertEquals(expectedResult, actualResult)
|
Assert.assertEquals(expectedResult, actualResult)
|
||||||
@@ -102,11 +101,11 @@ abstract class AbstractNashornJsTestChecker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun run(
|
private fun run(
|
||||||
files: List<String>,
|
files: List<String>,
|
||||||
testModuleName: String?,
|
testModuleName: String?,
|
||||||
testPackageName: String?,
|
testPackageName: String?,
|
||||||
testFunctionName: String,
|
testFunctionName: String,
|
||||||
withModuleSystem: Boolean
|
withModuleSystem: Boolean
|
||||||
) = run(files) {
|
) = run(files) {
|
||||||
runTestFunction(testModuleName, testPackageName, testFunctionName, withModuleSystem)
|
runTestFunction(testModuleName, testPackageName, testFunctionName, withModuleSystem)
|
||||||
}
|
}
|
||||||
@@ -114,8 +113,8 @@ abstract class AbstractNashornJsTestChecker {
|
|||||||
protected open fun beforeRun() {}
|
protected open fun beforeRun() {}
|
||||||
|
|
||||||
private fun run(
|
private fun run(
|
||||||
files: List<String>,
|
files: List<String>,
|
||||||
f: ScriptEngine.() -> Any?
|
f: ScriptEngine.() -> Any?
|
||||||
): Any? {
|
): Any? {
|
||||||
// Recreate the engine once in a while
|
// Recreate the engine once in a while
|
||||||
if (engineUsageCnt++ > 100) {
|
if (engineUsageCnt++ > 100) {
|
||||||
@@ -131,12 +130,12 @@ abstract class AbstractNashornJsTestChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract protected fun createScriptEngineForTest(): ScriptEngine
|
protected abstract fun createScriptEngineForTest(): ScriptEngine
|
||||||
}
|
}
|
||||||
|
|
||||||
object NashornJsTestChecker: AbstractNashornJsTestChecker() {
|
object NashornJsTestChecker : AbstractNashornJsTestChecker() {
|
||||||
val SETUP_KOTLIN_OUTPUT = "kotlin.kotlin.io.output = new kotlin.kotlin.io.BufferedOutput();"
|
const val SETUP_KOTLIN_OUTPUT = "kotlin.kotlin.io.output = new kotlin.kotlin.io.BufferedOutput();"
|
||||||
private val GET_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.buffer;"
|
private const val GET_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.buffer;"
|
||||||
|
|
||||||
override fun beforeRun() {
|
override fun beforeRun() {
|
||||||
engine.eval(SETUP_KOTLIN_OUTPUT)
|
engine.eval(SETUP_KOTLIN_OUTPUT)
|
||||||
@@ -163,7 +162,7 @@ object NashornJsTestChecker: AbstractNashornJsTestChecker() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object NashornIrJsTestChecker: AbstractNashornJsTestChecker() {
|
object NashornIrJsTestChecker : AbstractNashornJsTestChecker() {
|
||||||
override fun createScriptEngineForTest(): ScriptEngine {
|
override fun createScriptEngineForTest(): ScriptEngine {
|
||||||
val engine = createScriptEngine()
|
val engine = createScriptEngine()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user