From bc3d2863dad0ff19dfa4ee466dd7b01ba686e7c0 Mon Sep 17 00:00:00 2001 From: Jan Seeger Date: Wed, 20 Dec 2023 16:48:27 +0100 Subject: [PATCH] Add Task awaitResult converter --- .idea/gradle.xml | 1 + dachlatten-google/build.gradle.kts | 12 +++++ .../de/sipgate/dachlatten/google/TaskExt.kt | 12 +++++ .../dachlatten/google/TaskAwaitResultTest.kt | 44 +++++++++++++++++++ gradle/libs.versions.toml | 6 +++ settings.gradle.kts | 1 + 6 files changed, 76 insertions(+) create mode 100644 dachlatten-google/build.gradle.kts create mode 100644 dachlatten-google/src/main/kotlin/de/sipgate/dachlatten/google/TaskExt.kt create mode 100644 dachlatten-google/src/test/kotlin/de/sipgate/dachlatten/google/TaskAwaitResultTest.kt diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 86a6024..69ac663 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -28,6 +28,7 @@ diff --git a/dachlatten-google/build.gradle.kts b/dachlatten-google/build.gradle.kts new file mode 100644 index 0000000..40dac43 --- /dev/null +++ b/dachlatten-google/build.gradle.kts @@ -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) +} diff --git a/dachlatten-google/src/main/kotlin/de/sipgate/dachlatten/google/TaskExt.kt b/dachlatten-google/src/main/kotlin/de/sipgate/dachlatten/google/TaskExt.kt new file mode 100644 index 0000000..1681ce8 --- /dev/null +++ b/dachlatten-google/src/main/kotlin/de/sipgate/dachlatten/google/TaskExt.kt @@ -0,0 +1,12 @@ +package de.sipgate.dachlatten.google + +import com.google.android.gms.tasks.Task +import kotlinx.coroutines.tasks.await + +suspend fun Task.awaitResult(): Result { + return try { + Result.success(await()) + } catch (e: Throwable) { + Result.failure(e) + } +} diff --git a/dachlatten-google/src/test/kotlin/de/sipgate/dachlatten/google/TaskAwaitResultTest.kt b/dachlatten-google/src/test/kotlin/de/sipgate/dachlatten/google/TaskAwaitResultTest.kt new file mode 100644 index 0000000..b3167e1 --- /dev/null +++ b/dachlatten-google/src/test/kotlin/de/sipgate/dachlatten/google/TaskAwaitResultTest.kt @@ -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> { + 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> { + 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()) + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e9a0753..6d99b9e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" } @@ -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" } diff --git a/settings.gradle.kts b/settings.gradle.kts index bc4aee6..4ebe18a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -26,4 +26,5 @@ include(":dachlatten-compose") include(":dachlatten-datetime") include(":dachlatten-debug") include(":dachlatten-flow") +include(":dachlatten-google") include(":dachlatten-primitives")