Skip to content

Commit 0e53ad3

Browse files
committed
Temp encoding test
1 parent 2e736d4 commit 0e53ad3

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

firebase-common/src/commonTest/kotlin/dev/gitlive/firebase/EncodersTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import kotlinx.serialization.SerialName
88
import kotlinx.serialization.Serializable
99
import kotlinx.serialization.builtins.ListSerializer
1010
import kotlinx.serialization.builtins.MapSerializer
11+
import kotlinx.serialization.builtins.nullable
1112
import kotlinx.serialization.builtins.serializer
1213
import kotlinx.serialization.modules.SerializersModule
1314
import kotlinx.serialization.modules.polymorphic
1415
import kotlin.jvm.JvmInline
1516
import kotlin.test.Test
1617
import kotlin.test.assertEquals
18+
import kotlin.test.assertNull
1719

1820
@Serializable
1921
object TestObject {
@@ -71,6 +73,15 @@ data class NestedClass(
7173

7274
class EncodersTest {
7375

76+
@Test
77+
fun encodeDecodeNullableString() {
78+
val encoded = encode<String?>(null) { encodeDefaults = true }
79+
80+
nativeAssertEquals(null, encoded)
81+
82+
val decoded = decode(String.serializer().nullable, encoded)
83+
assertNull(decoded)
84+
}
7485
@Test
7586
fun encodeDecodeList() {
7687
val list = listOf("One", "Two", "Three")
@@ -82,6 +93,17 @@ class EncodersTest {
8293
assertEquals(listOf("One", "Two", "Three"), decoded)
8394
}
8495

96+
@Test
97+
fun encodeDecodeNullableList() {
98+
val list = listOf("One", "Two", null)
99+
val encoded = encode<List<String?>>(list) { encodeDefaults = true }
100+
101+
nativeAssertEquals(nativeListOf("One", "Two", null), encoded)
102+
103+
val decoded = decode(ListSerializer(String.serializer().nullable), encoded)
104+
assertEquals(listOf("One", "Two", null), decoded)
105+
}
106+
85107
@Test
86108
fun encodeDecodeMap() {
87109
val map = mapOf("key" to "value", "key2" to "value2", "key3" to "value3")
@@ -93,6 +115,17 @@ class EncodersTest {
93115
assertEquals(mapOf("key" to "value", "key2" to "value2", "key3" to "value3"), decoded)
94116
}
95117

118+
@Test
119+
fun encodeDecodeNullableMap() {
120+
val map = mapOf("key" to "value", "key2" to "value2", "key3" to null)
121+
val encoded = encode<Map<String, String?>>(map) { encodeDefaults = true }
122+
123+
nativeAssertEquals(nativeMapOf("key" to "value", "key2" to "value2", "key3" to null), encoded)
124+
125+
val decoded = decode(MapSerializer(String.serializer(), String.serializer().nullable), encoded)
126+
assertEquals(mapOf("key" to "value", "key2" to "value2", "key3" to null), decoded)
127+
}
128+
96129
@Test
97130
fun encodeDecodeObject() {
98131
val encoded = encode(TestObject.serializer(), TestObject) { encodeDefaults = false }

test-utils/src/androidMain/kotlin/dev/gitlive/firebase/TestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ actual fun runTest(test: suspend CoroutineScope.() -> Unit) = kotlinx.coroutines
1313
actual fun runBlockingTest(action: suspend CoroutineScope.() -> Unit) = runBlocking(block = action)
1414

1515
actual fun nativeMapOf(vararg pairs: Pair<Any, Any?>): Any = mapOf(*pairs)
16-
actual fun nativeListOf(vararg elements: Any): Any = listOf(*elements)
16+
actual fun nativeListOf(vararg elements: Any?): Any = listOf(*elements)
1717
actual fun nativeAssertEquals(expected: Any?, actual: Any?) {
1818
kotlin.test.assertEquals(expected, actual)
1919
}

test-utils/src/commonMain/kotlin/dev/gitlive/firebase/TestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ expect fun runTest(test: suspend CoroutineScope.() -> Unit): TestResult
1313
expect fun runBlockingTest(action: suspend CoroutineScope.() -> Unit)
1414

1515
expect fun nativeMapOf(vararg pairs: Pair<Any, Any?>): Any
16-
expect fun nativeListOf(vararg elements: Any): Any
16+
expect fun nativeListOf(vararg elements: Any?): Any
1717
expect fun nativeAssertEquals(expected: Any?, actual: Any?)

test-utils/src/iosMain/kotlin/dev/gitlive/firebase/TestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ actual fun runTest(test: suspend CoroutineScope.() -> Unit) = runBlocking {
2828
}
2929
actual fun runBlockingTest(action: suspend CoroutineScope.() -> Unit) = runBlocking(block = action)
3030
actual fun nativeMapOf(vararg pairs: Pair<Any, Any?>): Any = mapOf(*pairs)
31-
actual fun nativeListOf(vararg elements: Any): Any = listOf(*elements)
31+
actual fun nativeListOf(vararg elements: Any?): Any = listOf(*elements)
3232
actual fun nativeAssertEquals(expected: Any?, actual: Any?) {
3333
kotlin.test.assertEquals(expected, actual)
3434
}

test-utils/src/jsMain/kotlin/dev/gitlive/firebase/TestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ actual fun runBlockingTest(action: suspend CoroutineScope.() -> Unit) {
1515
}
1616

1717
actual fun nativeMapOf(vararg pairs: Pair<Any, Any?>): Any = json(*pairs.map { (key, value) -> ((key as? String) ?: JSON.stringify(key)) to value }.toTypedArray())
18-
actual fun nativeListOf(vararg elements: Any): Any = elements
18+
actual fun nativeListOf(vararg elements: Any?): Any = elements
1919
actual fun nativeAssertEquals(expected: Any?, actual: Any?) {
2020
kotlin.test.assertEquals(JSON.stringify(expected), JSON.stringify(actual))
2121
}

0 commit comments

Comments
 (0)