mirror of
https://github.com/bitwarden/android.git
synced 2026-05-31 09:46:08 -05:00
Add zip helpers for Result and use in VaultRepository (#383)
This commit is contained in:
@@ -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)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user