Add zip helpers for Result and use in VaultRepository (#383)

This commit is contained in:
Brian Yencho
2023-12-13 08:49:56 -06:00
committed by GitHub
parent c7cd4c22be
commit 26aa60c2af
3 changed files with 205 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
package com.x8bit.bitwarden.data.platform.util
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
@@ -66,4 +67,120 @@ class ResultTest {
throwable.asFailure(),
)
}
@Test
fun `zip with two arguments should return a success when both are successes`() = runTest {
assertEquals(
("A" to 1).asSuccess(),
zip(
{ "A".asSuccess() },
{ 1.asSuccess() },
) { first, second ->
first to second
},
)
}
@Test
fun `zip with two arguments should return a failure when the first is a failure`() = runTest {
val throwable = Throwable()
assertEquals(
throwable.asFailure(),
zip(
{
@Suppress("USELESS_CAST")
throwable.asFailure() as Result<String>
},
{ 1.asSuccess() },
) { first, second ->
first to second
},
)
}
@Test
fun `zip with two arguments should return a failure when the second is a failure`() = runTest {
val throwable = Throwable()
assertEquals(
throwable.asFailure(),
zip(
{ "A".asSuccess() },
{
@Suppress("USELESS_CAST")
throwable.asFailure() as Result<Int>
},
) { first, second ->
first to second
},
)
}
@Test
fun `zip with three arguments should return a success when all are successes`() = runTest {
assertEquals(
Triple("A", 1, true).asSuccess(),
zip(
{ "A".asSuccess() },
{ 1.asSuccess() },
{ true.asSuccess() },
) { first, second, third ->
Triple(first, second, third)
},
)
}
@Test
fun `zip with three arguments should return a failure when the first is a failure`() = runTest {
val throwable = Throwable()
assertEquals(
throwable.asFailure(),
zip(
{
@Suppress("USELESS_CAST")
throwable.asFailure() as Result<String>
},
{ 1.asSuccess() },
{ true.asSuccess() },
) { first, second, third ->
Triple(first, second, third)
},
)
}
@Test
fun `zip with three arguments should return a failure when the second is a failure`() =
runTest {
val throwable = Throwable()
assertEquals(
throwable.asFailure(),
zip(
{ "A".asSuccess() },
{
@Suppress("USELESS_CAST")
throwable.asFailure() as Result<Int>
},
{ true.asSuccess() },
) { first, second, third ->
Triple(first, second, third)
},
)
}
@Test
fun `zip with three arguments should return a failure when the third is a failure`() = runTest {
val throwable = Throwable()
assertEquals(
throwable.asFailure(),
zip(
{ "A".asSuccess() },
{ 1.asSuccess() },
{
@Suppress("USELESS_CAST")
throwable.asFailure() as Result<Boolean>
},
) { first, second, third ->
Triple(first, second, third)
},
)
}
}