https://kotlinlang.org logo
Title
b

Big Chungus

06/10/2021, 1:18 PM
Is it possible to evaluate test config lazily? Here's my use-case:
abstract class SomeParameterBasedTest<T>(abstract val data: List<T>) {
  // Junit example here, but wonder how that would look in kotest
  @Test
  fun execute() {
    data.forEach {
     // Testing code using parseResult<T>()
    }
  }

  abstract fun <T> parseResult(res: String): T
}

class OneKindOfTest: SomeParameterBasedTest<Int>(listOf(1,2,3)) {
  override fun parseResult(res: String): Int = res.toInt()
}
e

Emil Kantis

06/10/2021, 2:50 PM
b

Big Chungus

06/10/2021, 2:54 PM
Kinda, although that still requires the extending (as opposed to abstract) class to include it
e

Emil Kantis

06/10/2021, 4:49 PM
not sure I see the difference.. I think this is what it would look like.
class OneKindOfTest : FunSpec({
   include(
      someParameterBasedTest(
         listOf(1, 2, 3),
         { res -> res.toInt() }
      )
   )
})

fun <T> someParameterBasedTest(
   val data: List<T>,
   val parseResult: (res: String) -> T
) = funSpec {
   test("execute") {
      // Testing code using parseResult
   }
}
b

Big Chungus

06/10/2021, 4:50 PM
The difference is that this way doesn't force the implementing class to override that function
Also doesn't add the tests automatically
e

Emil Kantis

06/10/2021, 4:51 PM
you must pass the function
If. you want to rely on the type system to find implementations of
somePArameterBasedTest
I guess that makes a difference