https://kotlinlang.org logo
#kotest
Title
# kotest
h

holgerbrandl

12/03/2023, 10:45 AM
Hey, why is shouldBe (and its friends) not generically typed? Isn't the same the type the prerequisite for the values being equal? This would ease writing tests I think. Currently I need to figure type mismatches at runtime and not compile time.
e

Emil Kantis

12/03/2023, 10:49 AM
IIRC it's due to some corner cases, like
listOf(1,2,3) shouldBe intArrayOf(1,2,3)
I tinkered with a code inspection in the Kotest IntelliJ plugin way back, which identified cases where incompatible types were being passed to
shouldBe
https://github.com/kotest/kotest-intellij-plugin/pull/146
h

holgerbrandl

12/03/2023, 11:02 AM
Even if this fails for collections, what about simple objects? I'm thinking about:
val foo= 1.3
foo shouldBe 2
Does this really require a plugin? I was hoping for some simple (possibly reified) generic flavor of shouldBe.
From what I understand after some discussion with chatgpt, this currently not possible because of type earsure.
e

Emil Kantis

12/03/2023, 11:37 AM
Type erasure only occurs during runtime. I don’t think that’s a factor here
👍 2
s

sam

12/03/2023, 3:59 PM
It doesn't work for:
apple shouldBe fruit
and
fruit shouldBe apple
b

Ben Woodworth

12/11/2023, 7:49 PM
I think kotlin.test solves that with
@OnlyInputTypes T
, though the annotation's internal to Kotlin at the moment. (here)
s

sam

12/11/2023, 8:23 PM
Would that work for A, B if A <: B as well as A >: B because that's the usecase we need.
If it just locks in the types, it won't work where one is a subtype of another.
👍 1