Skip to content

Commit

Permalink
Increase coroutines module coverage. (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Sep 10, 2024
1 parent d2d205a commit ab84f7d
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 2 deletions.
1 change: 1 addition & 0 deletions formula-coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ dependencies {
testImplementation(libs.truth)
testImplementation(libs.junit)
testImplementation(libs.coroutines.test)
testImplementation(libs.turbine)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object FlowRuntime {
formula: IFormula<Input, Output>,
config: RuntimeConfig?,
): Flow<Output> {
return callbackFlow<Output> {
val callbackFlow = callbackFlow {
val runtime = FormulaRuntime(
formula = formula,
onOutput = this::trySendBlocking,
Expand All @@ -33,6 +33,7 @@ object FlowRuntime {
awaitClose {
runtime.terminate()
}
}.distinctUntilChanged()
}
return callbackFlow.distinctUntilChanged()
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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<Unit, String>() {
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<String> {
return Evaluation("output")
}
}

runTest {
formula.toFlow().test {
Truth.assertThat(awaitItem()).isEqualTo("output")
}
}
}

@Test
fun `toFlow with unit input and config`() {
val formula = object : StatelessFormula<Unit, String>() {
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<String> {
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<String, String>() {
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> {
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<String, String>() {
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> {
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<String, String>() {
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> {
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<String, String>() {
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> {
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<String, String>() {
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> {
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)
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit ab84f7d

Please sign in to comment.