Buk*_*gey 2 junit functional-programming either kotlin arrow-kt
我想使用Either测试获得的结果。假设我有一个简单的示例,没有一个
@Test
fun `test arithmetic`() {
val simpleResult = 2 + 2
Assertions.assertEquals(4, simpleResult)
}
Run Code Online (Sandbox Code Playgroud)
现在我包装了结果:
@Test
fun `test arithmetic with either`() {
val result : Either<Nothing, Int> = (2 + 2).right()
Assertions.assertTrue(result.isRight())
result.map { Assertions.assertEquals(4, it) }
}
Run Code Online (Sandbox Code Playgroud)
我想它看起来有点丑陋,因为如果我们得到了最后的断言,Either.Left而不是Either.Right
如何执行,该如何断言如何以函数样式正确地测试结果?
kotlintest提供了一个kotest-assertions-arrow可用于测试Arrow类型的模块。
它基本上公开了Either和其他数据类型的匹配器。看看这个。
@Test
fun `test arithmetic with either`() {
val result : Either<Nothing, Int> = (2 + 2).right()
result.shouldBeRight(4)
}
Run Code Online (Sandbox Code Playgroud)
Either双方的实现都是数据类,因此您可以执行以下操作:
check(result == 4.right())
Run Code Online (Sandbox Code Playgroud)
或者可以使用与equals用于断言相等性的任何其他断言库类似的东西。
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |