Is it expected for `shouldBeInstanceOf` not to wor...
# kotest
k
Is it expected for
shouldBeInstanceOf
not to work with generics? This test passes when I don’t think it should?
Copy code
test("test") {
  listOf(1).shouldBeInstanceOf<List<String>>()
}
i
It's not possible due to type erasure. Generic type isn't stored in runtime, so List<Int> is the same type as List<String> in such case https://kotlinlang.org/docs/generics.html#type-erasure
k
When using shouldBeInstanceOf<T> or shouldBeTypeOf<T>, the assertions can now use generic contracts to smart case down to generic instances.
For example, consider the following example where we are given an Any. After invoking
shouldBeTypeOf
with a generic type, the type is smart casted if the assertion passes.
```val list: Any = arrayListOf(1, 2, 3)
list.shouldBeTypeOf<ArrayList<Int>>()
list[0] shouldBe 1 // can only work with a smart case```
a
type erasure would mean it’s not possible to assume that it’s a
List<Int>
, but it should be possible to verify all the elements are Strings
as an alternative, you could try this @Kirill Zhukov
Copy code
listOf(1, 2, 3).shouldForAll { it.shouldBeInstanceOf<String>() }
in that blog post it uses
shouldBeTypeOf<ArrayList<Int>>()
, not
shouldBeInstanceOf<ArrayList<Int>>()
- could you try both?
i
@Kirill Zhukov docs may be confusing in such case if you're not aware of type erasure 🙂 It actually can't check generic type, but can smart cast argument of this check
k
shouldBeTypeOf
doesn’t work either
I see, makes sense
In my use case I actually have a
Err<T>: Result
generic data class that I want to assert:
someResult.shouldBeOfType<Err<SomeError>>()
Since that doesn’t work, think will just create a custom extension:
someResult.shouldBeErrOfType<SomeError>()
j
this shouldn't compile directly, I think there is an issue about adding an annotation to the kotest matcher to force this check
don't remember the annotation name tho 😕
105 Views