https://kotlinlang.org logo
Title
k

Kirill Zhukov

05/09/2023, 6:36 AM
Is it expected for
shouldBeInstanceOf
not to work with generics? This test passes when I don’t think it should?
test("test") {
  listOf(1).shouldBeInstanceOf<List<String>>()
}
i

Ivan Pavlov

05/09/2023, 6:45 AM
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

Kirill Zhukov

05/09/2023, 6:47 AM
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

Adam S

05/09/2023, 6:47 AM
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
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

Ivan Pavlov

05/09/2023, 6:52 AM
@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

Kirill Zhukov

05/09/2023, 6:56 AM
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

Javier

05/09/2023, 10:29 AM
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 😕