Tidy up MapTest

This commit is contained in:
Michael Bull 2024-03-05 20:08:20 +00:00
parent f05ce6e0c7
commit f7c9f605f1

View File

@ -2,7 +2,6 @@ package com.github.michaelbull.result
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNull
class MapTest { class MapTest {
private sealed interface MapErr { private sealed interface MapErr {
@ -27,7 +26,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Ok(30), expected = Ok(30),
actual = value.map { it + 20 } actual = value.map { it + 20 },
) )
} }
@ -37,7 +36,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Err(MapErr.HelloError), expected = Err(MapErr.HelloError),
actual = value.map { "hello $it" } actual = value.map { "hello $it" },
) )
} }
} }
@ -50,7 +49,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Ok("hello"), expected = Ok("hello"),
actual = result.flatten() actual = result.flatten(),
) )
} }
@ -60,7 +59,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Err(6), expected = Err(6),
actual = result.flatten() actual = result.flatten(),
) )
} }
@ -70,7 +69,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Err(6), expected = Err(6),
actual = result.flatten() actual = result.flatten(),
) )
} }
@ -80,12 +79,12 @@ class MapTest {
assertEquals( assertEquals(
expected = Ok(Ok("hello")), expected = Ok(Ok("hello")),
actual = result.flatten() actual = result.flatten(),
) )
assertEquals( assertEquals(
expected = Ok("hello"), expected = Ok("hello"),
actual = result.flatten().flatten() actual = result.flatten().flatten(),
) )
} }
} }
@ -98,7 +97,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Ok(70), expected = Ok(70),
actual = value.mapError { MapErr.WorldError } actual = value.mapError { MapErr.WorldError },
) )
} }
@ -108,7 +107,7 @@ class MapTest {
assertEquals( assertEquals(
expected = Err(MapErr.WorldError), expected = Err(MapErr.WorldError),
actual = value.mapError { MapErr.WorldError } actual = value.mapError { MapErr.WorldError },
) )
} }
} }
@ -121,7 +120,7 @@ class MapTest {
assertEquals( assertEquals(
expected = 3, expected = 3,
actual = value.mapOr(42, String::length) actual = value.mapOr(42, String::length),
) )
} }
@ -131,7 +130,7 @@ class MapTest {
assertEquals( assertEquals(
expected = 42, expected = 42,
actual = value.mapOr(42, String::length) actual = value.mapOr(42, String::length),
) )
} }
} }
@ -145,7 +144,7 @@ class MapTest {
assertEquals( assertEquals(
expected = 3, expected = 3,
actual = value.mapOrElse({ k * 2 }, String::length) actual = value.mapOrElse({ k * 2 }, String::length),
) )
} }
@ -155,7 +154,7 @@ class MapTest {
assertEquals( assertEquals(
expected = 42, expected = 42,
actual = value.mapOrElse({ k * 2 }, String::length) actual = value.mapOrElse({ k * 2 }, String::length),
) )
} }
} }
@ -168,12 +167,12 @@ class MapTest {
val result: String = value.mapBoth( val result: String = value.mapBoth(
success = { "good $it" }, success = { "good $it" },
failure = { "bad $it" } failure = { "bad $it" },
) )
assertEquals( assertEquals(
expected = "good 50", expected = "good 50",
actual = result actual = result,
) )
} }
@ -183,12 +182,12 @@ class MapTest {
val result: String = value.mapBoth( val result: String = value.mapBoth(
success = { "good $it" }, success = { "good $it" },
failure = { "bad $it" } failure = { "bad $it" },
) )
assertEquals( assertEquals(
expected = "bad 20", expected = "bad 20",
actual = result actual = result,
) )
} }
} }
@ -201,12 +200,12 @@ class MapTest {
val result: Result<String, Long> = value.flatMapBoth( val result: Result<String, Long> = value.flatMapBoth(
success = { Ok("good $it") }, success = { Ok("good $it") },
failure = { Err(100L) } failure = { Err(100L) },
) )
assertEquals( assertEquals(
expected = Ok("good 50"), expected = Ok("good 50"),
actual = result actual = result,
) )
} }
@ -216,12 +215,12 @@ class MapTest {
val value: Result<String, Long> = result.flatMapBoth( val value: Result<String, Long> = result.flatMapBoth(
success = { Ok("good $it") }, success = { Ok("good $it") },
failure = { Err(100L) } failure = { Err(100L) },
) )
assertEquals( assertEquals(
expected = Err(100L), expected = Err(100L),
actual = value actual = value,
) )
} }
} }
@ -234,12 +233,12 @@ class MapTest {
val result: Result<Long, MapErr.CustomError> = value.mapEither( val result: Result<Long, MapErr.CustomError> = value.mapEither(
success = { it + 500L }, success = { it + 500L },
failure = { MapErr.CustomError("$it") } failure = { MapErr.CustomError("$it") },
) )
assertEquals( assertEquals(
expected = Ok(1000L), expected = Ok(1000L),
actual = result actual = result,
) )
} }
@ -249,12 +248,12 @@ class MapTest {
val result: Result<Long, MapErr.CustomError> = value.mapEither( val result: Result<Long, MapErr.CustomError> = value.mapEither(
success = { it + 500L }, success = { it + 500L },
failure = { MapErr.CustomError("bad") } failure = { MapErr.CustomError("bad") },
) )
assertEquals( assertEquals(
expected = Err(MapErr.CustomError("bad")), expected = Err(MapErr.CustomError("bad")),
actual = result actual = result,
) )
} }
} }
@ -267,12 +266,12 @@ class MapTest {
val result: Result<Long, MapErr.CustomError> = value.flatMapEither( val result: Result<Long, MapErr.CustomError> = value.flatMapEither(
success = { Ok(it + 500L) }, success = { Ok(it + 500L) },
failure = { Err(MapErr.CustomError("$it")) } failure = { Err(MapErr.CustomError("$it")) },
) )
assertEquals( assertEquals(
expected = Ok(1000L), expected = Ok(1000L),
actual = result actual = result,
) )
} }
@ -282,12 +281,12 @@ class MapTest {
val result: Result<Long, MapErr.CustomError> = value.flatMapEither( val result: Result<Long, MapErr.CustomError> = value.flatMapEither(
success = { Ok(it + 500L) }, success = { Ok(it + 500L) },
failure = { Err(MapErr.CustomError("bad")) } failure = { Err(MapErr.CustomError("bad")) },
) )
assertEquals( assertEquals(
expected = Err(MapErr.CustomError("bad")), expected = Err(MapErr.CustomError("bad")),
actual = result actual = result,
) )
} }
} }
@ -296,37 +295,25 @@ class MapTest {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
val result = Ok("a").toErrorIfNull { "b" }
result as Ok
assertEquals( assertEquals(
expected = "a", expected = Ok("a"),
actual = result.value actual = Ok("a").toErrorIfNull { "b" },
) )
} }
@Test @Test
fun returnsTransformedErrorIfNull() { fun returnsTransformedErrorIfNull() {
val result: Result<Nothing, String> = Ok(null).toErrorIfNull { "a" }
result as Err
assertEquals( assertEquals(
expected = "a", expected = Err("a"),
actual = result.error actual = Ok(null).toErrorIfNull { "a" },
) )
} }
@Test @Test
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
val result = Err("a").toErrorIfNull { "b" }
result as Err
assertEquals( assertEquals(
expected = "a", expected = Err("a"),
actual = result.error actual = Err("a").toErrorIfNull { "b" },
) )
} }
} }
@ -335,34 +322,25 @@ class MapTest {
@Test @Test
fun returnsTransformedErrorIfNotNull() { fun returnsTransformedErrorIfNotNull() {
val result = Ok("a").toErrorUnlessNull { "b" }
result as Err
assertEquals( assertEquals(
expected = "b", expected = Err("b"),
actual = result.error actual = Ok("a").toErrorUnlessNull { "b" },
) )
} }
@Test @Test
fun returnsValueIfNull() { fun returnsValueIfNull() {
val result = Ok(null).toErrorUnlessNull { "a" } assertEquals(
expected = Ok(null),
result as Ok actual = Ok(null).toErrorUnlessNull { "a" },
)
assertNull(result.value)
} }
@Test @Test
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
val result = Err("a").toErrorUnlessNull { "b" }
result as Err
assertEquals( assertEquals(
expected = "b", expected = Err("b"),
actual = result.error actual = Err("a").toErrorUnlessNull { "b" },
) )
} }
} }