Just thinking: how feasible would it be to impleme...
# test
h
Just thinking: how feasible would it be to implement the syntax and major features of Spock in Kotlin? http://spockframework.org/spock/docs/1.3/all_in_one.html
r
there are various test frameworks, #kotlintest and #spek to name just two (in case you should not know them)
s
I think kotlintest is better than Spock (well I would say that). People talk about Spock all the time, but the only real missing feature is the way it shows: stack.size() == 2 | | | | 1 false [push me] Is there something else I'm missing ?
h
I obviously know kotlintest and spek, but I do fall back to Spock regardless. The syntax is just so much cleaner, and their use of e.g. input parameter combinations with
where:
is super nice.
s
It's nice yeah, you can do the same thing in KT though,
Copy code
forall(
      row(2, 3, 3),
      row(0, 0, 0),
      row(4, -1, 4),
      row(-2, -1, -1)
  ) { a, b, max ->
    Math.max(a, b) shouldBe max
  }
I suppose it's a bit more verbose needing the row wrapper
Any other examples of The syntax is just so much cleaner, I'd love to know so KT can improve if needed.
h
"a bit more verbose" is quite an understatement. The Spock syntax is pretty readable, that's why I pondered if a Kotlin DSL could mimic it
Copy code
@Unroll
def "maximum of two numbers, given #a and #b"() {
    expect:
      Math.max(a, b) == c

    where:
      a | b | c
      1 | 3 | 3
      7 | 4 | 4
      0 | 0 | 0
}
s
That would be like this:
Copy code
test("maximum of two numbers, given #a and #b") {
    forall(
        row(1, 3, 3),
        row(7, 4, 4),
        row(0, 0, 0)
    ) { a, b, max ->
      Math.max(a, b) shouldBe max
    }
  }
I don't think it's hugely different, but the spock syntax is definitely nicer as it has less ceremony. Let me see what we can come up with to improve and get closer to spock.
h
That would be awesome 😀
I believe Spock uses a groovy compiler plugin to achieve the syntax. And kotlin has compiler plugins as well, I think?
s
Yeah it does. Not sure if you can change syntax with it though
h
/cc: @stigkj FYI
s
Best I have come up with so far for KotlinTest 4.0 is this:
Copy code
expect(
      { max(it.a, it.c) shouldBe it.c },
      where(
        row(1, 2, 3),
        row(4, 5, 9),
        row(7, 0, 7)
      )
    )
Or you can ditch the "where"
Copy code
expect(
      { max(it.a, it.c) shouldBe it.c },
      row(1, 2, 3),
      row(4, 5, 9),
      row(7, 0, 7)
    )
Finally, there's this, but I can't get the type inference to work with this style
Copy code
expect<Int, Int, Int> {
      max(it.a, it.c) shouldBe it.c
    }.where(
      row(1, 2, 3),
      row(4, 5, 9),
      row(7, 0, 7)
    )
h
Not too bad. Still not quite as elegant as Spock 😉
s
lol indeed