Skip to content

Commit

Permalink
Add Task awaitResult converter
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Dec 20, 2023
1 parent d2d3a69 commit bc3d286
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions dachlatten-google/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id("android-library-base")
id("android-library-unit-test")
id("android-library-release")
}

dependencies {
compileOnly(libs.coroutines.playServices)

testImplementation(libs.coroutines.playServices)
testImplementation(libs.bundles.mockk)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.sipgate.dachlatten.google

import com.google.android.gms.tasks.Task
import kotlinx.coroutines.tasks.await

suspend fun <T> Task<T>.awaitResult(): Result<T> {
return try {
Result.success(await())
} catch (e: Throwable) {
Result.failure(e)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.sipgate.dachlatten.google

import com.google.android.gms.tasks.Task
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class TaskAwaitResultTest {
@Test
@Disabled
fun resultHandlesSuccessCorrectly() = runTest {
val successfulTask = mockk<Task<String>> {
coEvery { await() } returns "success"
}

val result = successfulTask.awaitResult()
assertTrue(result.isSuccess)
assertEquals("success", result.getOrNull())
assertFalse(result.isFailure)
assertNull(result.exceptionOrNull())
}

@Test
@Disabled
fun resultHandlesErrorCorrectly() = runTest {
val failingTask = mockk<Task<String>> {
coEvery { await() } throws Exception("you shall not pass!")
}

val result = failingTask.awaitResult()
assertFalse(result.isSuccess)
assertNull(result.getOrNull())
assertTrue(result.isFailure)
assertInstanceOf(Exception::class.java, result.exceptionOrNull())
}
}
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ compose = "1.5.4"
compose-compiler = "1.5.5"
kotlinx-datetime = "0.5.0"
kover = "0.7.5"
mockk = "1.13.8"

[libraries]
coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" }
coroutines-playServices = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-play-services", version.ref = "coroutines" }
coroutinesTest = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
support-annotations = { module = "com.android.support:support-annotations", version.ref = "support-annotations" }
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }
Expand All @@ -32,8 +34,12 @@ kover-gradlePlugin = { group = "org.jetbrains.kotlinx.kover", name = "org.jetbra
annotation-jvm = { group = "androidx.annotation", name = "annotation-jvm", version.ref = "annotation-jvm" }
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime-jvm", version.ref = "kotlinx-datetime" }

mockk-agent = { group = "io.mockk", name = "mockk-agent", version.ref = "mockk" }
mockk-android = { group = "io.mockk", name = "mockk-android", version.ref = "mockk" }

[bundles]
compose-ui-test = ["compose-ui-test-junit4", "compose-ui-test-manifest"]
mockk = ["mockk-agent", "mockk-android"]

[plugins]
androidLibrary = { id = "com.android.library", version.ref = "agp" }
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ include(":dachlatten-compose")
include(":dachlatten-datetime")
include(":dachlatten-debug")
include(":dachlatten-flow")
include(":dachlatten-google")
include(":dachlatten-primitives")

0 comments on commit bc3d286

Please sign in to comment.