From ab84f7d9cba8850c7cf0db6a5afd4d51920a0cd6 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Tue, 10 Sep 2024 13:06:15 -0400 Subject: [PATCH] Increase coroutines module coverage. (#386) --- formula-coroutines/build.gradle.kts | 1 + .../formula/coroutines/FlowRuntime.kt | 5 +- .../formula/coroutines/FlowActionTest.kt | 20 +++ .../formula/coroutines/FlowRuntimeTest.kt | 136 ++++++++++++++++++ gradle/libs.versions.toml | 1 + 5 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowActionTest.kt create mode 100644 formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowRuntimeTest.kt diff --git a/formula-coroutines/build.gradle.kts b/formula-coroutines/build.gradle.kts index 9b44e0c8..17a95ee7 100644 --- a/formula-coroutines/build.gradle.kts +++ b/formula-coroutines/build.gradle.kts @@ -17,4 +17,5 @@ dependencies { testImplementation(libs.truth) testImplementation(libs.junit) testImplementation(libs.coroutines.test) + testImplementation(libs.turbine) } \ No newline at end of file diff --git a/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt b/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt index 6535c9e5..79e854e9 100644 --- a/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt +++ b/formula-coroutines/src/main/java/com/instacart/formula/coroutines/FlowRuntime.kt @@ -20,7 +20,7 @@ object FlowRuntime { formula: IFormula, config: RuntimeConfig?, ): Flow { - return callbackFlow { + val callbackFlow = callbackFlow { val runtime = FormulaRuntime( formula = formula, onOutput = this::trySendBlocking, @@ -33,6 +33,7 @@ object FlowRuntime { awaitClose { runtime.terminate() } - }.distinctUntilChanged() + } + return callbackFlow.distinctUntilChanged() } } \ No newline at end of file diff --git a/formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowActionTest.kt b/formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowActionTest.kt new file mode 100644 index 00000000..47cbcd71 --- /dev/null +++ b/formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowActionTest.kt @@ -0,0 +1,20 @@ +package com.instacart.formula.coroutines + +import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.flow.flowOf +import org.junit.Test + +class FlowActionTest { + + @Test + fun `default key is null`() { + val action = FlowAction.fromFlow { flowOf("") } + assertThat(action.key()).isNull() + } + + @Test + fun `specified key`() { + val action = FlowAction.fromFlow("unique-key") { flowOf("") } + assertThat(action.key()).isEqualTo("unique-key") + } +} \ No newline at end of file diff --git a/formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowRuntimeTest.kt b/formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowRuntimeTest.kt new file mode 100644 index 00000000..2bf0e85d --- /dev/null +++ b/formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowRuntimeTest.kt @@ -0,0 +1,136 @@ +package com.instacart.formula.coroutines + +import app.cash.turbine.test +import com.google.common.truth.Truth +import com.instacart.formula.Evaluation +import com.instacart.formula.RuntimeConfig +import com.instacart.formula.Snapshot +import com.instacart.formula.StatelessFormula +import com.instacart.formula.test.CountingInspector +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.junit.Test + +class FlowRuntimeTest { + + @Test + fun `toFlow with unit input and no config`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation("output") + } + } + + runTest { + formula.toFlow().test { + Truth.assertThat(awaitItem()).isEqualTo("output") + } + } + } + + @Test + fun `toFlow with unit input and config`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation("output") + } + } + + val inspector = CountingInspector() + runTest { + formula.toFlow(config = RuntimeConfig(inspector = inspector)).test { + Truth.assertThat(awaitItem()).isEqualTo("output") + } + } + inspector.assertEvaluationCount(1) + } + + @Test + fun `toFlow with input value and no config`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation(input) + } + } + + runTest { + formula.toFlow("output").test { + Truth.assertThat(awaitItem()).isEqualTo("output") + } + } + } + + @Test + fun `toFlow with input value and config`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation(input) + } + } + + val inspector = CountingInspector() + runTest { + formula.toFlow("output", RuntimeConfig(inspector = inspector)).test { + Truth.assertThat(awaitItem()).isEqualTo("output") + } + } + inspector.assertEvaluationCount(1) + } + + @Test + fun `toFlow with input flow and no config`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation(input) + } + } + + runTest { + formula.toFlow(flowOf("output", "output-2")).test { + Truth.assertThat(awaitItem()).isEqualTo("output") + Truth.assertThat(awaitItem()).isEqualTo("output-2") + } + } + } + + @Test + fun `toFlow with input flow and config`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation(input) + } + } + + val inspector = CountingInspector() + runTest { + formula.toFlow( + input = flowOf("output", "output-2"), + config = RuntimeConfig(inspector = inspector), + ).test { + Truth.assertThat(awaitItem()).isEqualTo("output") + Truth.assertThat(awaitItem()).isEqualTo("output-2") + } + } + inspector.assertEvaluationCount(2) + } + + @Test + fun `toFlow with input flow with duplicate values`() { + val formula = object : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation(input) + } + } + + val inspector = CountingInspector() + runTest { + formula.toFlow( + input = flowOf("output", "output", "output"), + config = RuntimeConfig(inspector = inspector), + ).test { + Truth.assertThat(awaitItem()).isEqualTo("output") + } + } + inspector.assertEvaluationCount(1) + } +} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f73bf636..b0e83cb8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -32,6 +32,7 @@ kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } +turbine = { module = "app.cash.turbine:turbine", version="1.1.0" } compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" } compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }