Hey, why is shouldBe (and its friends) not generic...
# kotest
h
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
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
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
Type erasure only occurs during runtime. I don’t think that’s a factor here
👍 2
s
It doesn't work for:
apple shouldBe fruit
and
fruit shouldBe apple
b
I think kotlin.test solves that with
@OnlyInputTypes T
, though the annotation's internal to Kotlin at the moment. (here)
s
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