I was experimenting with data-driven tests to chec...
# kotest
k
I was experimenting with data-driven tests to check that a number of alternative implementations of a function all work correctly. It works fine with I use
io.kotest.data.forAll
but it throws an exception when I use
withData
. I presume the latter is the preferred way for data-driven tests while the former is legacy (as I couldn't find it in the latest documentation). Full reproducible example in the thread.
Copy code
import io.kotest.core.spec.style.FreeSpec
import io.kotest.data.forAll
import io.kotest.data.row
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

fun foo() = 42
fun bar() = 42

class Test: FreeSpec({
    "forAll" {
        forAll(
            row(::foo),
            row(::bar)
        ) { f ->
            f() shouldBe 42
        }
    }
    "withData" {
        withData(
            ::foo,
            ::bar
        ) { f ->
            f() shouldBe 42
        }
    }
})
forAll
passes.
withData
throws
UnsupportedOperationException: This class is an internal synthetic class generated by the Kotlin compiler, such as an anonymous class for a lambda, a SAM wrapper, a callable reference, etc. It's not a Kotlin class or interface, so the reflection library has no idea what declarations it has. Please use Java reflection to inspect this class: class Test$1$2$1
e
I think there's some reflection going on which causes issues.. This variant passes as expected
Copy code
fun foo() = 42
fun bar() = 42

class Test : FreeSpec(
   {
      "forAll" {
         forAll(
            row(::foo),
            row(::bar),
         ) { f ->
            f() shouldBe 42
         }
      }

      "withData" - {
         withData(
            nameFn = { it.name },
            first = ::foo,
            second = ::bar,
         ) { f ->
            f() shouldBe 42
         }
      }
   },
)
Could be because you're passing a function reference to a top level function. I think top-level functions are put into a synthetic class, which might explain the error message you're seeing