-
Notifications
You must be signed in to change notification settings - Fork 624
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
withData in StringSpec tests #3869
Comments
Re: 1st issue. I believe you should be unable to invoke You could switch to class DataTest : FreeSpec({
"what this test tests" - {
withData(
"a" to 1,
"b" to 2,
"c" to 3,
) { (first, second) ->
}
}
} IMO we should try to add a Re 2nd issue: Will have to read up what that does.. I'm not sure it's a good idea to force opt-in to use withData in root scope? class DataTest : StringSpec({
withData(
nameFn = { (first, second) -> "$first <> $second" },
"a" to 1,
"b" to 2,
"c" to 3,
) { (first, second) ->
}
} |
This is what I added to my fixtures: // https://github.com/kotest/kotest/issues/3869
@file:OptIn(ExperimentalTypeInference::class)
package com.fleshgrinder.kotest
import io.kotest.common.ExperimentalKotest
import io.kotest.core.names.TestName
import io.kotest.core.spec.style.StringSpec
import io.kotest.core.spec.style.scopes.ContainerScope
import io.kotest.core.spec.style.scopes.addTest
import io.kotest.core.test.TestType.Dynamic
import kotlin.experimental.ExperimentalTypeInference
@BuilderInference
@OptIn(ExperimentalKotest::class)
fun <T> StringSpec.withData(
name: (T) -> String,
params: Array<out T>,
test: suspend ContainerScope.(T) -> Unit,
) = params.forEach { addTest(TestName(name(it)), false, null, Dynamic) { test(it) } }
@BuilderInference
inline fun <reified T> StringSpec.withData(
noinline name: (T) -> String,
param: T,
vararg params: T,
noinline test: suspend ContainerScope.(T) -> Unit,
) = withData(name, arrayOf(param, *params), test)
The Having a And, yes, using |
I'm more afraid of the experimental type inference to be dropped / broken in some upcoming Kotlin version, at which point we would have to drop the feature (breaking change) or lock our users to a certain Kotlin version. Or could that not happen? Do you know how long it's been around and if there are any fopen YouTrack tickets discussing it? |
It's used in the standard library, I even added functions to it using it. The annotation is for sure going to be dropped at some point, but only because K2 is going to be intelligent enough on its own without it. |
Ok, I think we could add it. Wdyt @sksamuel ? |
I'm in favor, if we can get it to work. I tried adding
To all the withData functions and could not get the 2nd example to compile. |
Might be related to the ticket I filed at #3901 |
It seems as if
withData
cannot be used inStringSpec
tests, unless one accepts that each individual case gets its name assigned automatically. The following, which should work, doesn't compile, because Kotlin cannot infer the correct types.The
@BuilderInference
would fix the second issue:The text was updated successfully, but these errors were encountered: