首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotest: PythagTriple错误函数调用失败的例子

Kotest: PythagTriple错误函数调用失败的例子
EN

Stack Overflow用户
提问于 2022-10-16 10:11:50
回答 1查看 29关注 0票数 0

我正在使用IntelliJ和Maven,下载了Kotest插件,并将依赖项添加到pom.xml (kotest-Runn-Junit5-jvm、kotest-断言-core-jvm、kotest-property,所有版本5.5.0)。

下面的基本示例正在运行:

代码语言:javascript
复制
class MyFirstTestClass : FunSpec({
    test("my first test") {
        1 + 2 shouldBe 3
    }
})

但是我不能让另一个例子工作,PythagTriple:

代码语言:javascript
复制
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.spec.style.StringSpec
import io.kotest.data.forAll
import io.kotest.matchers.shouldBe

data class PythagoreanTriple(
    val a: Int,
    val b: Int,
    val c: Int
)

class MyTests : FunSpec({
    context("Pythagorean triples tests") {
        forAll(
            PythagoreanTriple(3, 4, 5),
            PythagoreanTriple(6, 8, 10),
            PythagoreanTriple(8, 15, 17),
            PythagoreanTriple(7, 24, 25)
        ) { (a, b, c) ->
            isPythagoreanTriple(a, b, c) shouldBe true
        }
    }
})

fun isPythagoreanTriple(a: Int, b: Int, c: Int): Boolean = a * a + b * b == c * c

我可以找到这个例子的两个变体,一个使用forAll,另一个使用withData。两者都不起作用。

似乎有两个问题:

(1)

代码语言:javascript
复制
Kotlin: None of the following functions can be called with the arguments supplied: 
public suspend fun <A> forAll(vararg rows: Row1<TypeVariable(A)>, testfn: suspend (TypeVariable(A)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B> forAll(vararg rows: Row2<TypeVariable(A), TypeVariable(B)>, testfn: suspend (TypeVariable(A), TypeVariable(B)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C> forAll(vararg rows: Row3<TypeVariable(A), TypeVariable(B), TypeVariable(C)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D> forAll(vararg rows: Row4<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E> forAll(vararg rows: Row5<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F> forAll(vararg rows: Row6<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F, G> forAll(vararg rows: Row7<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F, G, H> forAll(vararg rows: Row8<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H)) -> Unit): Unit defined in io.kotest.data
public suspend fun <A, B, C, D, E, F, G, H, I> forAll(vararg rows: Row9<TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H), TypeVariable(I)>, testfn: suspend (TypeVariable(A), TypeVariable(B), TypeVariable(C), TypeVariable(D), TypeVariable(E), TypeVariable(F), TypeVariable(G), TypeVariable(H), TypeVariable(I)) -> Unit): Unit defined in io.kotest.data

使用withData只会导致未解决的引用: withData,而且我还没有找到一个导入。

(2)

代码语言:javascript
复制
Kotlin: Cannot infer a type for this parameter. Please specify it explicitly.

这似乎指的是:

代码语言:javascript
复制
        { **(a, b, c)** ->
            isPythagoreanTriple(a, b, c) shouldBe true
        }

考虑到我是个新手,这些肯定是我无法解决的一些基本问题。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-11-21 19:15:03

有三个错误使你的测试不起作用:

  1. 您的测试设置不包含测试,而只包含上下文。这可以通过将test.
  2. You更改为context来解决,而不应该为forAll-rows使用您自己的数据类型,但是正如(1)中的消息所暗示的那样,forAll的不同变体是为数据类型Row1Row2Row3等定义的。它们中的每一个都可以用row(...)创建。(从1到14之间的每个数据都有一个row函数。)
  3. forEach的变体并不指定一个带有一个RowX参数的函数,而是一个带有作为RowX参数类型的X参数的函数。因此,参数a, b, c被括号包围是不正确的--这是一种将一个参数分解为其组件的符号。

当您以下列方式更改测试时,您的测试就可以工作了:

代码语言:javascript
复制
class PythagoreanTripleTest : FunSpec({
    test("Pythagorean triples tests") {
        forAll(
            row(3, 4, 5),
            row(6, 8, 10),
            row(8, 15, 17),
            row(7, 24, 25)
        ) { a, b, c ->
            isPythagoreanTriple(a, b, c) shouldBe true
        }
    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74086204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档