Skip to content

Commit 2feef98

Browse files
authored
pulled out EncodersTest to commonTest module (#96)
* pulled out EncodersTest to commonTest module * Update decoders.kt * make instrumented again for now
1 parent 268c6e2 commit 2feef98

File tree

5 files changed

+79
-40
lines changed

5 files changed

+79
-40
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase
6+
7+
actual fun nativeMapOf(vararg pairs: Pair<String, Any>): Any = mapOf(*pairs)
8+
actual fun nativeListOf(vararg elements: Any): Any = listOf(*elements)
9+
actual fun nativeAssertEquals(expected: Any?, actual: Any?) = kotlin.test.assertEquals(expected, actual)

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

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase
6+
7+
import kotlinx.serialization.Serializable
8+
import kotlinx.serialization.builtins.ListSerializer
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
expect fun nativeMapOf(vararg pairs: Pair<String, Any>): Any
13+
expect fun nativeListOf(vararg elements: Any): Any
14+
expect fun nativeAssertEquals(expected: Any?, actual: Any?): Unit
15+
16+
@Serializable
17+
data class TestData(val map: Map<String, String>, val bool: Boolean = false, val nullableBool: Boolean? = null)
18+
19+
class EncodersTest {
20+
@Test
21+
fun encodeMap() {
22+
val encoded = encode(mapOf("key" to "value"), shouldEncodeElementDefault = true)
23+
24+
nativeAssertEquals(nativeMapOf("key" to "value"), encoded)
25+
}
26+
27+
@Test
28+
fun encodeObject() {
29+
val encoded = encode<TestData>(TestData.serializer(), TestData(mapOf("key" to "value"), true), shouldEncodeElementDefault = false)
30+
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "bool" to true), encoded)
31+
}
32+
33+
@Test
34+
fun encodeObjectNullableValue() {
35+
val encoded = encode<TestData>(TestData.serializer(), TestData(mapOf("key" to "value"), true, nullableBool = true), shouldEncodeElementDefault = true)
36+
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "bool" to true, "nullableBool" to true), encoded)
37+
}
38+
39+
@Test
40+
fun decodeObject() {
41+
val decoded = decode<TestData>(TestData.serializer(), nativeMapOf("map" to nativeMapOf("key" to "value")))
42+
assertEquals(TestData(mapOf("key" to "value"), false), decoded)
43+
}
44+
45+
@Test
46+
fun decodeListOfObjects() {
47+
val decoded = decode(ListSerializer(TestData.serializer()), nativeListOf(nativeMapOf("map" to nativeMapOf("key" to "value"))))
48+
assertEquals(listOf(TestData(mapOf("key" to "value"), false)), decoded)
49+
}
50+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase
6+
7+
actual fun nativeMapOf(vararg pairs: Pair<String, Any>): Any = mapOf(*pairs)
8+
actual fun nativeListOf(vararg elements: Any): Any = listOf(*elements)
9+
actual fun nativeAssertEquals(expected: Any?, actual: Any?) = kotlin.test.assertEquals(expected, actual)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase
6+
7+
import kotlin.js.json
8+
9+
actual fun nativeMapOf(vararg pairs: Pair<String, Any>): Any = json(*pairs)
10+
actual fun nativeListOf(vararg elements: Any): Any = elements
11+
actual fun nativeAssertEquals(expected: Any?, actual: Any?) = kotlin.test.assertEquals(JSON.stringify(expected), JSON.stringify(actual))

0 commit comments

Comments
 (0)