gpt4 book ai didi

junit - 如何以功能样式从 Arrow 测试 Either

转载 作者:行者123 更新时间:2023-12-02 12:04:23 25 4
gpt4 key购买 nike

我想使用 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如何以功能样式正确测试结果?

最佳答案

kotlintest提供kotest-assertions-arrow可用于测试箭头类型的模块。

它基本上公开了 Either 和其他数据类型的匹配器。看看this .

@Test
fun `test arithmetic with either`() {
val result : Either<Nothing, Int> = (2 + 2).right()
result.shouldBeRight(4)
}

关于junit - 如何以功能样式从 Arrow 测试 Either,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54666162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com