https://kotlinlang.org logo
Title
s

Sandymcp

10/25/2021, 3:11 PM
package sse

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.io.BufferedReader
import java.io.StringReader

data class Case<G, W>(val given: G, val want: W)

class SseReaderKoTest : FunSpec({

    context("read tag and payload test") {
        withData(
            mapOf(
                "empty marks end of message" to Case("\n", "" to ""),
                "event and payload" to Case("event: surprise\n", "event" to "surprise"),
                "data and payload" to Case("data: surprise\n", "data" to "surprise"),
                "id and payload" to Case("id: surprise\n", "id" to "surprise"),
                "comment" to Case(": surprise\n", ":" to ""),
                "blank comment" to Case(":\n", ":" to ""),
            )
        ) { (given, want) ->
            readTagAndPayload(BufferedReader(StringReader(given))) shouldBe want
        }
    }
})
d

dave08

10/25/2021, 3:21 PM
Did you try without the context()?
s

Sandymcp

10/25/2021, 3:22 PM
d

dave08

10/25/2021, 3:23 PM
When I had tried, it seems like nesting and certain InstancePerXXX modes didn't work well with this...
s

Sandymcp

10/25/2021, 3:24 PM
Well i can see the
withData
methods it looks like I should be using
@io.kotest.common.ExperimentalKotest @kotlin.jvm.JvmName public suspend fun <T : kotlin.Any> io.kotest.core.test.TestContext.withData(data: kotlin.collections.Map<kotlin.String, T>, test: suspend io.kotest.core.test.TestContext.(T) -> kotlin.Unit): kotlin.Unit { /* compiled code */ }
d

dave08

10/25/2021, 3:25 PM
Do other types of tests work?
Like a simple test("...") { ... }?
s

Sandymcp

10/25/2021, 3:26 PM
Yes I had all of this working using a property test and
exhaustive
looked like a simple change and improvement for naming
it could be it’s the wrong context being included i.e there’s an extension function somewhere which is not picked up correctly. The examples are not showing the imports
d

dave08

10/25/2021, 3:30 PM
Well, you could always resort to the regular listOf(...).forEach { test(...) { ... } } 🙃. Otherwise, I'd simplify thiings and get bare bones working, until the problem is pinpointed... I personally went back to the latter, and the old datatesting api with table and rows...
s

Sandymcp

10/25/2021, 3:37 PM
Yeah well, I could also just write a
@Test
fun t()
  mapOf().forEach {
  }
}
and just use Kotlin test - which was my starting point, and worked perfectly. But then one of my colleagues pointed kotest out. The only reason for using Kotest in the first place is to allow all the iterations to be run and get better naming. If I can’t get that, then there’s little point using all the other even clunkier mechanisms. There’s obviously some incantation I’m missing somewhere
d

dave08

10/25/2021, 3:40 PM
The better naming is possible:
mapOf(
                "empty marks end of message" to Case(, "\n", "" to ""),
                "event and payload" to Case("event: surprise\n", "event" to "surprise"),
                "data and payload" to Case("data: surprise\n", "data" to "surprise"),
                "id and payload" to Case("id: surprise\n", "id" to "surprise"),
                "comment" to Case(": surprise\n", ":" to ""),
                "blank comment" to Case(":\n", ":" to ""),
            ).forEach { (given, want) ->
    it(given) {
       readTagAndPayload(BufferedReader(StringReader(given))) shouldBe want  
               }
}
Also the
context("in case X") { /* previous code */ }
allows nested naming... which is another plus. And coroutines support, a bunch of plugins and extensions...
Property testing can be used separately ...
@Sandymcp I was looking over at the slack threads where I had problems... it seems like it was more with Kotest extension and nesting beforeTest in withData... maybe you have a compile or runtime error, JUnit is a bit notorious at hiding them.. but otherwise it should be working...
s

Sandymcp

10/26/2021, 8:18 AM
The problem is a compile time issue
d

dave08

10/26/2021, 9:22 AM
Glad you solved it 😃!