Skip to content

Commit 78bf5cc

Browse files
committed
tests sharing and improvements of engine
1 parent 0de2cc2 commit 78bf5cc

File tree

10 files changed

+300
-244
lines changed

10 files changed

+300
-244
lines changed

buildSrc/src/main/kotlin/Deps.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ object Deps {
3030
const val material = "com.google.android.material:material:$materialDesignVersion"
3131
const val lifecycle = "androidx.lifecycle:lifecycle-extensions:$androidLifecycleVersion"
3232

33+
const val kotlinTestJUnit = "org.jetbrains.kotlin:kotlin-test-junit:$kotlinTestVersion"
3334
const val testRunner = "androidx.test:runner:$androidCoreTestingVersion"
3435
const val testRules = "androidx.test:rules:$androidCoreTestingVersion"
3536
const val testJUnitExt = "androidx.test.ext:junit:$testJUnitExtVersion"

javascript/build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ android {
1818
exclude("META-INF/*.kotlin_module")
1919
exclude("META-INF/AL2.0")
2020
exclude("META-INF/LGPL2.1")
21+
}
22+
}
23+
24+
kotlin {
25+
sourceSets {
26+
val mobileDeviceTest by creating
27+
28+
val commonTest by getting
29+
val iosTest by getting
30+
val androidAndroidTest by getting
2131

32+
mobileDeviceTest.dependsOn(commonTest)
33+
iosTest.dependsOn(mobileDeviceTest)
34+
androidAndroidTest.dependsOn(mobileDeviceTest)
2235
}
2336
}
2437

@@ -31,6 +44,7 @@ dependencies {
3144
commonTestImplementation(Deps.Libs.MultiPlatform.kotlinTestAnnotations)
3245
commonTestImplementation(Deps.Libs.MultiPlatform.mokoTest)
3346

47+
androidTestImplementation(Deps.Libs.Android.kotlinTestJUnit)
3448
androidTestImplementation(Deps.Libs.Android.testRunner)
3549
androidTestImplementation(Deps.Libs.Android.testRules)
3650
androidTestImplementation(Deps.Libs.Android.testJUnitExt)

javascript/src/androidAndroidTest/kotlin/dev/icerock/moko/javascript/JavaScriptEngineTest.kt

Lines changed: 0 additions & 161 deletions
This file was deleted.

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package dev.icerock.moko.javascript
77
import app.cash.quickjs.QuickJs
88
import app.cash.quickjs.QuickJsException
99
import kotlinx.serialization.SerializationException
10+
import kotlinx.serialization.builtins.serializer
1011
import kotlinx.serialization.json.Json
12+
import kotlinx.serialization.json.JsonElement
1113
import kotlinx.serialization.json.JsonObject
1214

1315
actual class JavaScriptEngine actual constructor() {
@@ -46,22 +48,39 @@ actual class JavaScriptEngine actual constructor() {
4648
return handleQuickJsResult(result)
4749
}
4850

51+
// TODO fix pass of arguments - now wrapping of string and json invalid and will be broken on multilined strings
4952
private fun convertContextMapToJsScript(context: Map<String, JsType>): String {
5053
if (context.isEmpty()) return ""
5154

5255
return context.mapNotNull { pair ->
53-
pair.value.value?.let { "var ${pair.key} = $it;" }
56+
prepareValueForJs(pair.value)?.let { "var ${pair.key} = $it;" }
5457
}.joinToString(separator = "")
5558
}
5659

60+
private fun prepareValueForJs(valueWrapper: JsType): String? {
61+
return when (valueWrapper) {
62+
is JsType.Bool -> valueWrapper.value.toString()
63+
is JsType.DoubleNum -> valueWrapper.value.toString()
64+
is JsType.Json -> valueWrapper.value.let {
65+
Json.encodeToString(JsonElement.serializer(), it)
66+
}.let {
67+
"JSON.parse(\"$it\")"
68+
}
69+
is JsType.Str -> valueWrapper.value.let {
70+
"\"$it\""
71+
}
72+
JsType.Null -> null
73+
}
74+
}
75+
5776
private fun handleQuickJsResult(result: Any?): JsType {
58-
return when {
59-
result == null -> JsType.Null
60-
result is Boolean -> JsType.Bool(result)
61-
result is Int -> JsType.IntNum(result)
62-
result is Double -> JsType.DoubleNum(result)
63-
result is Float -> JsType.DoubleNum(result.toDouble())
64-
result is String -> try {
77+
return when (result) {
78+
null -> JsType.Null
79+
is Boolean -> JsType.Bool(result)
80+
is Int -> JsType.DoubleNum(result.toDouble())
81+
is Double -> JsType.DoubleNum(result)
82+
is Float -> JsType.DoubleNum(result.toDouble())
83+
is String -> try {
6584
val serializeResult = json.parseToJsonElement(result)
6685
if (serializeResult is JsonObject) {
6786
JsType.Json(serializeResult)
@@ -70,6 +89,8 @@ actual class JavaScriptEngine actual constructor() {
7089
}
7190
} catch (ex: SerializationException) {
7291
JsType.Str(result)
92+
} catch (ex: IllegalStateException) {
93+
JsType.Str(result)
7394
}
7495
else -> throw JavaScriptEvaluationException(
7596
message = "Impossible JavaScriptEngine handler state with result [$result]"

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,23 @@ package dev.icerock.moko.javascript
77
import kotlinx.serialization.json.JsonElement
88

99
sealed class JsType {
10-
data class Bool(val value: Boolean): JsType()
11-
data class Str(val value: String): JsType()
12-
data class IntNum(val value: Int): JsType()
13-
data class DoubleNum(val value: Double): JsType()
14-
data class Json(val value: JsonElement): JsType()
10+
data class Bool(override val value: Boolean) : JsType()
11+
data class Str(override val value: String) : JsType()
12+
data class DoubleNum(override val value: Double) : JsType()
13+
data class Json(override val value: JsonElement) : JsType()
1514

1615
/**
1716
* For "undefined" and "null".
1817
*/
19-
object Null : JsType()
20-
}
21-
22-
val JsType.value: Any?
23-
get() {
24-
return when (this) {
25-
is JsType.Bool -> value
26-
is JsType.Str -> value
27-
is JsType.IntNum -> value
28-
is JsType.DoubleNum -> value
29-
is JsType.Json -> value.toString()
30-
is JsType.Null -> null
31-
}
18+
object Null : JsType() {
19+
override val value: Any? get() = null
3220
}
3321

22+
abstract val value: Any?
23+
}
24+
3425
fun JsType.boolValue(): Boolean = (this as JsType.Bool).value
3526
fun JsType.stringValue(): String = (this as JsType.Str).value
36-
fun JsType.intValue(): Int = (this as JsType.IntNum).value
3727
fun JsType.doubleValue(): Double = (this as JsType.DoubleNum).value
3828
fun JsType.jsonValue(): JsonElement = (this as JsType.Json).value
3929
fun JsType.nullValue(): Any? = (this as JsType.Null).let { null }

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ actual class JavaScriptEngine actual constructor() {
3333

3434
context.forEach {
3535
jsContext.setObject(
36-
`object` = it.value.getValue(),
36+
`object` = prepareValueForJsContext(it.value),
3737
forKeyedSubscript = NSString.create(string = it.key)
3838
)
3939
}
@@ -46,6 +46,11 @@ actual class JavaScriptEngine actual constructor() {
4646
actual fun close() {
4747
// Nothing to do here
4848
}
49+
50+
private fun prepareValueForJsContext(valueWrapper: JsType): Any? {
51+
return if (valueWrapper is JsType.Json) valueWrapper.value.getValue()
52+
else valueWrapper.value
53+
}
4954
}
5055

5156
private fun JsonObject.toNSDictionary(): NSDictionary {
@@ -74,17 +79,6 @@ private fun JsonElement.getValue(): Any? {
7479
?: (this as? JsonPrimitive)?.content
7580
}
7681

77-
private fun JsType.getValue(): Any? {
78-
return when (this) {
79-
is JsType.Bool -> value
80-
is JsType.Str -> value
81-
is JsType.IntNum -> value
82-
is JsType.DoubleNum -> value
83-
is JsType.Json -> value.getValue()
84-
is JsType.Null -> null
85-
}
86-
}
87-
8882
private fun JSValue.toMokoJSType(): JsType {
8983
return when {
9084
isBoolean -> JsType.Bool(toBool())

javascript/src/iosTest/kotlin/dev/icerock/moko/javascript/JavaScriptEngineTest.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)