Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a withData() overload that accepts one element #3901

Open
Virtlink opened this issue Feb 29, 2024 · 4 comments
Open

Add a withData() overload that accepts one element #3901

Virtlink opened this issue Feb 29, 2024 · 4 comments
Labels
blocked 🚫 Issues that are blocked on external projects, for example a Kotlin bug data-driven-testing 🗃️ Related to the data driven testing mechanisms within the testing framework.

Comments

@Virtlink
Copy link

Virtlink commented Feb 29, 2024

In the process of writing tests, I spent half an hour messing with types and type signatures trying to figure out why the following test gave me type errors:

import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData

class Tests: FunSpec({
    withData(
        Triple("a", 2, "aa"),
    ) { (input, count, expected) ->
        // ... //
    }
})

It gave me two errors. On (input, count, expected) I got:

Cannot infer a type for this parameter. Please specify it explicitly.
value-parameter `<name for destructuring parameter 0>`: [Error type: Cannot infer a lambda parameter type]

And on withData I got:

None of the following functions can be called with the arguments supplied.
RootScope.withData(Collection<TypeVariable(T)>, suspend ContainerScope.(TypeVariable(T)) → Unit)   where T = TypeVariable(T) for    fun <T> RootScope.withData(ts: Collection<T>, test: suspend ContainerScope.(T) → Unit): Unit defined in io.kotest.datatest
RootScope.withData(Map<String, TypeVariable(T)>, suspend ContainerScope.(TypeVariable(T)) → Unit)   where T = TypeVariable(T) for    fun <T> RootScope.withData(data: Map<String, T>, test: suspend ContainerScope.(T) → Unit): Unit defined in io.kotest.datatest
RootScope.withData(Sequence<TypeVariable(T)>, suspend ContainerScope.(TypeVariable(T)) → Unit)   where T = TypeVariable(T) for    fun <T> RootScope.withData(ts: Sequence<T>, test: suspend ContainerScope.(T) → Unit): Unit defined in io.kotest.datatest

Of course, maybe its obvious to you, dear reader, but it was not at all obvious to me what the issue was: there is no overload of withData() that accepts one element. You have to specify a minimum of two tests to withData(). And the error messages led me astray. So therefore my feature request:

Could you add an overload to withData() that accepts one element?

@kshired
Copy link
Contributor

kshired commented Mar 1, 2024

You can write a test use list as follows.

import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData

class Tests: FunSpec({
    withData(
        listOf(Triple("a", 2, "aa"))
    ) { (input, count, expected) ->
        // ... //
    }
})

@Virtlink
Copy link
Author

Virtlink commented Mar 1, 2024

Thank you, that is a good workaround once one is aware of the issue when using one element. My main issue is that I found it unexpected (and unintuitive and undocumented) that the vararg-overload of withData expects at least two elements. So I propose to add an overload that accepts one element:

fun <T> RootScope.withData(first: T, test: suspend ContainerScope.(T) -> Unit) =
    withData(listOf(first), test)

fun <T> RootScope.withData(nameFn: (T) -> String, first: T, test: suspend ContainerScope.(T) -> Unit) =
    withData(nameFn, listOf(first), test)

@kshired
Copy link
Contributor

kshired commented Mar 1, 2024

@Virtlink

There's some reason why we cannot add overloaded function that accept only one element.

) = // we need first and second to help the compiler disambiguate

@LeoColman LeoColman added question ❓ Inquiries or clarifications needed about the project. data-driven-testing 🗃️ Related to the data driven testing mechanisms within the testing framework. labels Mar 3, 2024
@sksamuel
Copy link
Member

Broken on the following issue in kotlin:
https://youtrack.jetbrains.com/issue/KT-66470/False-overload-resolution-ambiguity-when-both-vararg-and-collection-match-with-a-trailing-function-with-a-parameter

@sksamuel sksamuel added blocked 🚫 Issues that are blocked on external projects, for example a Kotlin bug and removed question ❓ Inquiries or clarifications needed about the project. labels Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked 🚫 Issues that are blocked on external projects, for example a Kotlin bug data-driven-testing 🗃️ Related to the data driven testing mechanisms within the testing framework.
Projects
None yet
Development

No branches or pull requests

4 participants