Skip to content

Commit

Permalink
Prevent zipOrAccumulate from executing lambdas twice
Browse files Browse the repository at this point in the history
Closes #112
  • Loading branch information
rhirai-line authored and michaelbull committed Nov 18, 2024
1 parent 144c58e commit 03704eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public inline fun <T1, T2, E, V> zipOrAccumulate(
val result2 = producer2()

val results = listOf(
producer1(),
producer2(),
result1,
result2,
)

return if (results.allOk()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,52 @@ class ZipTest {

class ZipOrAccumulate {

@Test
fun returnsTransformedValueIfAllOfTwoOk() {
val result = zipOrAccumulate(
{ Ok(10) },
{ Ok(20) },
Int::plus,
)

assertEquals(
expected = Ok(30),
actual = result,
)
}

@Test
fun returnsOneErrIfOneOfTwoErr() {
val result = zipOrAccumulate(
{ Ok(10) },
{ produce(20, "hello") },
Int::plus,
)

val expectedErrors = listOf("hello")

assertEquals(
expected = Err(expectedErrors),
actual = result,
)
}

@Test
fun returnsErrsIfAllOfTwoErr() {
val result = zipOrAccumulate(
{ produce(10, "foo") },
{ produce(20, "bar") },
Int::plus,
)

val expectedErrors = listOf("foo", "bar")

assertEquals(
expected = Err(expectedErrors),
actual = result,
)
}

@Test
fun returnsTransformedValueIfAllOk() {
val result = zipOrAccumulate(
Expand Down

0 comments on commit 03704eb

Please sign in to comment.