Skip to content

Commit 87a5862

Browse files
extract test-utils module
1 parent 3bacb97 commit 87a5862

File tree

17 files changed

+176
-78
lines changed

17 files changed

+176
-78
lines changed

build.gradle.kts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,28 @@ subprojects {
6565
onlyIf { !project.gradle.startParameter.taskNames.contains("publishToMavenLocal") }
6666
}
6767

68+
val skipPublishing = project.name == "test-utils" // skip publishing for test utils
69+
6870
tasks {
71+
withType<Test> {
72+
testLogging {
73+
showExceptions = true
74+
exceptionFormat = TestExceptionFormat.FULL
75+
showStandardStreams = true
76+
showCauses = true
77+
showStackTraces = true
78+
events = setOf(
79+
TestLogEvent.STARTED,
80+
TestLogEvent.FAILED,
81+
TestLogEvent.PASSED,
82+
TestLogEvent.SKIPPED,
83+
TestLogEvent.STANDARD_OUT,
84+
TestLogEvent.STANDARD_ERROR
85+
)
86+
}
87+
}
88+
89+
if (skipPublishing) return@tasks
6990

7091
val updateVersion by registering(Exec::class) {
7192
commandLine("npm", "--allow-same-version", "--prefix", projectDir, "version", "${project.property("${project.name}.version")}")
@@ -136,24 +157,6 @@ subprojects {
136157
commandLine("npm", "publish")
137158
}
138159
}
139-
140-
withType<Test> {
141-
testLogging {
142-
showExceptions = true
143-
exceptionFormat = TestExceptionFormat.FULL
144-
showStandardStreams = true
145-
showCauses = true
146-
showStackTraces = true
147-
events = setOf(
148-
TestLogEvent.STARTED,
149-
TestLogEvent.FAILED,
150-
TestLogEvent.PASSED,
151-
TestLogEvent.SKIPPED,
152-
TestLogEvent.STANDARD_OUT,
153-
TestLogEvent.STANDARD_ERROR
154-
)
155-
}
156-
}
157160
}
158161

159162
afterEvaluate {
@@ -181,8 +184,10 @@ subprojects {
181184
}
182185
}
183186

184-
apply(plugin="maven-publish")
185-
apply(plugin="signing")
187+
if (skipPublishing) return@subprojects
188+
189+
apply(plugin = "maven-publish")
190+
apply(plugin = "signing")
186191

187192

188193
configure<PublishingExtension> {
@@ -235,6 +240,7 @@ subprojects {
235240
}
236241
}
237242
}
243+
238244
}
239245
}
240246

firebase-common/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ kotlin {
9090
}
9191
}
9292

93+
val commonTest by getting {
94+
dependencies {
95+
implementation(project(":test-utils"))
96+
}
97+
}
98+
9399
val androidMain by getting {
94100
dependencies {
95101
api("com.google.firebase:firebase-common")

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import kotlin.test.Test
1111
import kotlin.test.assertEquals
1212
import kotlin.test.assertNull
1313

14-
expect fun nativeMapOf(vararg pairs: Pair<String, Any?>): Any
15-
expect fun nativeListOf(vararg elements: Any): Any
16-
expect fun nativeAssertEquals(expected: Any?, actual: Any?): Unit
17-
1814
@Serializable
1915
data class TestData(val map: Map<String, String>, val bool: Boolean = false, val nullableBool: Boolean? = null)
2016

firebase-firestore/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ kotlin {
103103
}
104104
}
105105

106+
val commonTest by getting {
107+
dependencies {
108+
implementation(project(":test-utils"))
109+
}
110+
}
111+
106112
val androidMain by getting {
107113
dependencies {
108114
api("com.google.firebase:firebase-firestore")

firebase-firestore/src/androidAndroidTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,3 @@ actual val emulatorHost: String = "10.0.2.2"
1414
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
1515

1616
actual fun runTest(test: suspend CoroutineScope.() -> Unit) = runBlocking { test() }
17-
18-
@Suppress("UNCHECKED_CAST")
19-
actual fun encodedAsMap(encoded: Any?): Map<String, Any?> = encoded as Map<String, Any?>
20-
21-
actual fun Map<String, Any?>.asEncoded(): Any = this

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/GeoPointTests.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dev.gitlive.firebase.firestore
22

33
import dev.gitlive.firebase.decode
44
import dev.gitlive.firebase.encode
5+
import dev.gitlive.firebase.nativeAssertEquals
6+
import dev.gitlive.firebase.nativeMapOf
57
import kotlinx.serialization.Serializable
68
import kotlin.test.Test
79
import kotlin.test.assertEquals
@@ -19,19 +21,20 @@ class GeoPointTests {
1921
fun encodeGeoPointObject() = runTest {
2022
val geoPoint = GeoPoint(12.3, 45.6)
2123
val item = TestDataWithGeoPoint("123", geoPoint)
22-
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
23-
assertEquals("123", encoded["uid"])
2424
// check GeoPoint is encoded to a platform representation
25-
assertEquals(geoPoint.nativeValue, encoded["location"])
25+
nativeAssertEquals(
26+
nativeMapOf("uid" to "123", "location" to geoPoint.nativeValue),
27+
encode(item, shouldEncodeElementDefault = false)
28+
)
2629
}
2730

2831
@Test
2932
fun decodeGeoPointObject() = runTest {
3033
val geoPoint = GeoPoint(12.3, 45.6)
31-
val obj = mapOf(
34+
val obj = nativeMapOf(
3235
"uid" to "123",
3336
"location" to geoPoint.nativeValue
34-
).asEncoded()
37+
)
3538
val decoded: TestDataWithGeoPoint = decode(obj)
3639
assertEquals("123", decoded.uid)
3740
// check a platform GeoPoint is properly wrapped

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/TimestampTests.kt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ package dev.gitlive.firebase.firestore
33
import dev.gitlive.firebase.decode
44
import dev.gitlive.firebase.encode
55
import dev.gitlive.firebase.firebaseSerializer
6+
import dev.gitlive.firebase.nativeAssertEquals
7+
import dev.gitlive.firebase.nativeMapOf
68
import kotlinx.serialization.Serializable
79
import kotlin.test.*
10+
import kotlin.time.Duration.Companion.milliseconds
11+
import kotlin.time.DurationUnit
812

913
@Serializable
1014
data class TestData(
@@ -29,34 +33,41 @@ class TimestampTests {
2933
fun encodeTimestampObject() = runTest {
3034
val timestamp = Timestamp(123, 456)
3135
val item = TestData("uid123", timestamp, timestamp, null)
32-
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
33-
assertEquals("uid123", encoded["uid"])
34-
// NOTE: wrapping is required because JS does not override equals
35-
assertEquals(timestamp, Timestamp(encoded["createdAt"] as NativeTimestamp))
36-
assertEquals(timestamp, Timestamp(encoded["updatedAt"] as NativeTimestamp))
37-
assertNull(encoded["deletedAt"])
36+
nativeAssertEquals(
37+
nativeMapOf(
38+
"uid" to "uid123",
39+
"createdAt" to timestamp.nativeValue,
40+
"updatedAt" to timestamp.nativeValue,
41+
"deletedAt" to null
42+
),
43+
encode(item, shouldEncodeElementDefault = false)
44+
)
3845
}
3946

4047
@Test
4148
fun encodeServerTimestampObject() = runTest {
4249
val timestamp = Timestamp(123, 456)
4350
val item = TestData("uid123", timestamp, Timestamp.ServerTimestamp, Timestamp.ServerTimestamp)
44-
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
45-
assertEquals("uid123", encoded["uid"])
46-
assertEquals(timestamp, Timestamp(encoded["createdAt"] as NativeTimestamp))
47-
assertEquals(FieldValue.serverTimestamp, FieldValue(encoded["updatedAt"]!!))
48-
assertEquals(FieldValue.serverTimestamp, FieldValue(encoded["deletedAt"]!!))
51+
nativeAssertEquals(
52+
nativeMapOf(
53+
"uid" to "uid123",
54+
"createdAt" to timestamp.nativeValue,
55+
"updatedAt" to FieldValue.serverTimestamp.nativeValue,
56+
"deletedAt" to FieldValue.serverTimestamp.nativeValue
57+
),
58+
encode(item, shouldEncodeElementDefault = false)
59+
)
4960
}
5061

5162
@Test
5263
fun decodeTimestampObject() = runTest {
5364
val timestamp = Timestamp(123, 345)
54-
val obj = mapOf(
65+
val obj = nativeMapOf(
5566
"uid" to "uid123",
5667
"createdAt" to timestamp.nativeValue,
5768
"updatedAt" to timestamp.nativeValue,
5869
"deletedAt" to timestamp.nativeValue
59-
).asEncoded()
70+
)
6071
val decoded: TestData = decode(obj)
6172
assertEquals("uid123", decoded.uid)
6273
with(decoded.createdAt) {
@@ -78,12 +89,12 @@ class TimestampTests {
7889

7990
@Test
8091
fun decodeEmptyTimestampObject() = runTest {
81-
val obj = mapOf(
92+
val obj = nativeMapOf(
8293
"uid" to "uid123",
8394
"createdAt" to Timestamp.now().nativeValue,
8495
"updatedAt" to Timestamp.now().nativeValue,
8596
"deletedAt" to null
86-
).asEncoded()
97+
)
8798
val decoded: TestData = decode(obj)
8899
assertEquals("uid123", decoded.uid)
89100
assertNotNull(decoded.updatedAt)
@@ -100,21 +111,17 @@ class TimestampTests {
100111

101112
@Test
102113
fun timestampMillisecondsConversion() = runTest {
103-
val duration = 1666170858063.milliseconds
104-
val (seconds, nanoseconds) = ms.toComponents { seconds, nanoseconds -> seconds to nanoseconds }
105-
val ms = duration.toDouble(DurationUnit.MILLISECONDS)
114+
val ms = 1666170858063.0
106115

107116
val timestamp = Timestamp.fromMilliseconds(ms)
108-
assertEquals(seconds, timestamp.seconds)
109-
assertEquals(nanoseconds, timestamp.nanoseconds)
110117
assertEquals(ms, timestamp.toMilliseconds())
111118
}
112119

113120
@Test
114121
fun timestampDurationConversion() = runTest {
115122
val duration = 1666170858063.milliseconds
116-
val (seconds, nanoseconds) = ms.toComponents { seconds, nanoseconds -> seconds to nanoseconds }
117-
val timestamp = Timestamp.fromDuration(ms)
123+
val (seconds, nanoseconds) = duration.toComponents { seconds, nanoseconds -> seconds to nanoseconds }
124+
val timestamp = Timestamp.fromDuration(duration)
118125
assertEquals(seconds, timestamp.seconds)
119126
assertEquals(nanoseconds, timestamp.nanoseconds)
120127
assertEquals(duration, timestamp.toDuration())

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ expect val emulatorHost: String
2828
expect val context: Any
2929
expect fun runTest(test: suspend CoroutineScope.() -> Unit)
3030

31-
/** @return a map extracted from the encoded data. */
32-
expect fun encodedAsMap(encoded: Any?): Map<String, Any?>
33-
/** @return pairs as raw encoded data. */
34-
expect fun Map<String, Any?>.asEncoded(): Any
35-
3631
class FirebaseFirestoreTest {
3732

3833
@Serializable

firebase-firestore/src/iosTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,3 @@ actual fun runTest(test: suspend CoroutineScope.() -> Unit) = runBlocking {
2222
}
2323
testRun.await()
2424
}
25-
26-
@Suppress("UNCHECKED_CAST")
27-
actual fun encodedAsMap(encoded: Any?): Map<String, Any?> = encoded as Map<String, Any?>
28-
29-
actual fun Map<String, Any?>.asEncoded(): Any = this

firebase-firestore/src/jsTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,3 @@ actual val context: Any = Unit
1515
actual fun runTest(test: suspend CoroutineScope.() -> Unit) {
1616
runTest { test() }
1717
}
18-
19-
actual fun encodedAsMap(encoded: Any?): Map<String, Any?> {
20-
return (js("Object").entries(encoded) as Array<Array<Any>>).associate {
21-
it[0] as String to it[1]
22-
}
23-
}
24-
actual fun Map<String, Any?>.asEncoded(): Any =
25-
json(*entries.map { (key, value) -> key to value }.toTypedArray())

0 commit comments

Comments
 (0)