优化
This commit is contained in:
@@ -5,9 +5,26 @@ import io.legado.app.constant.SCRIPT_ENGINE
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.mozilla.javascript.Context
|
||||
|
||||
class JsTest {
|
||||
|
||||
@Language("js")
|
||||
private val printJs = """
|
||||
function print(str, newline) {
|
||||
if (typeof(str) == 'undefined') {
|
||||
str = 'undefined';
|
||||
} else if (str == null) {
|
||||
str = 'null';
|
||||
}
|
||||
java.lang.System.out.print(String(str));
|
||||
if (newline) java.lang.System.out.print("\n");
|
||||
}
|
||||
function println(str) {
|
||||
print(str, true);
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
@Test
|
||||
fun testMap() {
|
||||
val map = hashMapOf("id" to "3242532321")
|
||||
@@ -20,9 +37,11 @@ class JsTest {
|
||||
Assert.assertEquals("12314123", result)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testFor() {
|
||||
val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings())
|
||||
val scope = SCRIPT_ENGINE.getRuntimeScope(context)
|
||||
Context.enter().evaluateString(scope, printJs, "print", 1, null)
|
||||
@Language("js")
|
||||
val jsFor = """
|
||||
let result = 0
|
||||
@@ -30,16 +49,19 @@ class JsTest {
|
||||
let l=a.length
|
||||
for (let i = 0;i<l;i++){
|
||||
result = result + a[i]
|
||||
println(i)
|
||||
}
|
||||
for (let o of a){
|
||||
result = result + o
|
||||
println(o)
|
||||
}
|
||||
for (let o in a){
|
||||
result = result + o
|
||||
println(o)
|
||||
}
|
||||
result
|
||||
""".trimIndent()
|
||||
val result = SCRIPT_ENGINE.eval(jsFor).toString()
|
||||
val result = SCRIPT_ENGINE.eval(jsFor, scope).toString()
|
||||
Assert.assertEquals("12012", result)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user