我想使用 Either 测试获得的结果。假设我有一个没有 Either 的简单示例
@Test
fun `test arithmetic`() {
val simpleResult = 2 + 2
Assertions.assertEquals(4, simpleResult)
}
现在我已经包装了结果:
@Test
fun `test arithmetic with either`() {
val result : Either<Nothing, Int> = (2 + 2).right()
Assertions.assertTrue(result.isRight())
result.map { Assertions.assertEquals(4, it) }
}
我想它看起来有点难看,因为如果我们得到了最后的断言,Either.Left
而不是Either.Right
如何以函数式正确测试结果,则不会执行最后一个断言?