Skip to content

Commit 23dd5ea

Browse files
committed
add JavaScriptEngineTest (not working), review fixes
1 parent fd0171f commit 23dd5ea

File tree

7 files changed

+81
-18
lines changed

7 files changed

+81
-18
lines changed

buildSrc/src/main/kotlin/Deps.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ object Deps {
1313

1414
private const val coroutinesVersion = "1.4.2"
1515
private const val kotlinxSerializationVersion = "1.1.0"
16-
private const val mokoTestVersion = "0.2.0"
16+
private const val mokoTestVersion = "0.3.0"
17+
1718
const val mokoJavascriptVersion = "0.1.0"
1819

1920
object Android {
2021
const val compileSdk = 30
2122
const val targetSdk = 30
22-
const val minSdk = 16
23+
const val minSdk = 18
2324
}
2425

2526
object Libs {
@@ -37,7 +38,13 @@ object Deps {
3738
const val kotlinSerialization =
3839
"org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion"
3940

40-
const val mokoTest = "dev.icerock.moko:test:$mokoTestVersion"
41+
const val kotlinTest =
42+
"org.jetbrains.kotlin:kotlin-test-common:$kotlinTestVersion"
43+
const val kotlinTestAnnotations =
44+
"org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinTestVersion"
45+
const val mokoTest = "dev.icerock.moko:test-core:$mokoTestVersion"
46+
const val mokoTestRobolectric = "dev.icerock.moko:test-roboelectric:$mokoTestVersion"
47+
4148
const val mokoJavascript = "dev.icerock.moko:javascript:$mokoJavascriptVersion"
4249
}
4350
}

javascript/build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@ plugins {
77
id("publication-convention")
88
}
99

10+
android {
11+
testOptions.unitTests.isIncludeAndroidResources = true
12+
defaultConfig {
13+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
14+
}
15+
}
16+
1017
dependencies {
1118
androidMainImplementation(Deps.Libs.Android.quickjs)
1219

1320
commonMainImplementation(Deps.Libs.MultiPlatform.kotlinSerialization)
1421

22+
commonTestImplementation(Deps.Libs.MultiPlatform.kotlinTest)
23+
commonTestImplementation(Deps.Libs.MultiPlatform.kotlinTestAnnotations)
1524
commonTestImplementation(Deps.Libs.MultiPlatform.mokoTest)
25+
26+
androidTestImplementation("androidx.test:runner:1.3.0")
27+
androidTestImplementation("androidx.test:rules:1.3.0")
28+
androidTestImplementation("androidx.test.ext:junit:1.1.2")
29+
androidTestImplementation("androidx.test.ext:junit-ktx:1.1.2")
1630
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.icerock.moko.javascript
6+
7+
import org.junit.After
8+
import org.junit.Before
9+
import org.junit.Test
10+
11+
class JavaScriptEngineTest {
12+
private var _javaScriptEngine: JavaScriptEngine? = null
13+
14+
private val javaScriptEngine: JavaScriptEngine
15+
get() = _javaScriptEngine!!
16+
17+
@Before
18+
fun init() {
19+
_javaScriptEngine = JavaScriptEngine()
20+
}
21+
22+
@After
23+
fun dispose() {
24+
javaScriptEngine.close()
25+
}
26+
27+
@Test
28+
fun equalityTest() {
29+
val jsScript = """
30+
var a = 1 + 2;
31+
a
32+
""".trimIndent()
33+
val result = javaScriptEngine.evaluate(emptyMap(), jsScript)
34+
35+
println("DBG $result")
36+
37+
//assertTrue { result is JsType.IntNum }
38+
//assertEquals(expected = 3, actual = result.intValue())
39+
}
40+
}

javascript/src/androidMain/kotlin/dev/icerock/moko/javascript/JavaScriptEngine.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,20 @@ actual class JavaScriptEngine actual constructor(){
4949
}
5050

5151
private fun handleQuickJsResult(result: Any?): JsType {
52-
if (result is String) {
53-
return try {
54-
JsType.Json(json.encodeToJsonElement(result))
55-
} catch (ex: SerializationException) {
56-
JsType.Str(result)
57-
}
58-
}
59-
6052
return when (result) {
6153
result == null -> JsType.Null
6254
is Boolean -> JsType.Bool(result)
6355
is Int -> JsType.IntNum(result)
6456
is Double -> JsType.DoubleNum(result)
6557
is Float -> JsType.DoubleNum(result.toDouble())
66-
else -> throw IllegalStateException("Impossible JavaScriptEngine handler state")
58+
is String -> try {
59+
JsType.Json(json.encodeToJsonElement(result))
60+
} catch (ex: SerializationException) {
61+
JsType.Str(result)
62+
}
63+
else -> throw JavaScriptEvaluationException(
64+
message = "Impossible JavaScriptEngine handler state with result [$result]"
65+
)
6766
}
6867
}
6968
}

javascript/src/commonMain/kotlin/dev/icerock/moko/javascript/JsType.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ sealed class JsType {
1818
*/
1919
object Null : JsType()
2020
}
21+
22+
fun JsType.boolValue(): Boolean = (this as JsType.Bool).value
23+
fun JsType.stringValue(): String = (this as JsType.Str).value
24+
fun JsType.intValue(): Int = (this as JsType.IntNum).value
25+
fun JsType.doubleValue(): Double = (this as JsType.DoubleNum).value
26+
fun JsType.jsonValue(): JsonElement = (this as JsType.Json).value
27+
fun JsType.nullValue(): Any? = (this as JsType.Null).let { null }

sample/android-app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<uses-permission android:name="android.permission.INTERNET" />
77

88
<application
9-
android:label="moko-fields test app"
9+
android:label="moko-javascript test app"
1010
android:theme="@style/Theme.AppCompat.DayNight"
1111
tools:ignore="GoogleAppIndexingWarning">
1212

sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/Calculator.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
package com.icerockdev.library
66

7-
import dev.icerock.moko.javascript.multiply
8-
97
class Calculator {
10-
fun run() {
11-
println(multiply(a = 2, b = 3))
12-
}
8+
fun run() = Unit
139
}

0 commit comments

Comments
 (0)