diff --git a/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/types/TypeMatchers.kt b/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/types/TypeMatchers.kt index 5e2e39d32da..0496ed630a5 100644 --- a/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/types/TypeMatchers.kt +++ b/kotest-assertions/kotest-assertions-core/src/commonMain/kotlin/io/kotest/matchers/types/TypeMatchers.kt @@ -9,6 +9,8 @@ import kotlin.reflect.KClass // alias for beInstanceOf fun instanceOf(expected: KClass<*>): Matcher = beInstanceOf(expected) +inline fun instanceOf(): Matcher = instanceOf(T::class) + fun beInstanceOf(expected: KClass<*>): Matcher = neverNullMatcher { value -> MatcherResult( expected.isInstance(value), diff --git a/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/types/TypeMatchersTest.kt b/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/types/TypeMatchersTest.kt index 0e19d67a17a..590da7bccce 100644 --- a/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/types/TypeMatchersTest.kt +++ b/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/types/TypeMatchersTest.kt @@ -12,6 +12,7 @@ import io.kotest.matchers.should import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNot import io.kotest.matchers.types.haveAnnotation +import io.kotest.matchers.types.instanceOf import io.kotest.matchers.types.shouldBeInstanceOf import io.kotest.matchers.types.shouldBeSameInstanceAs import io.kotest.matchers.types.shouldBeTypeOf @@ -52,19 +53,27 @@ class TypeMatchersTest : WordSpec() { val arrayList: List = arrayListOf(1, 2, 3) arrayList should beInstanceOf(ArrayList::class) + arrayList should beInstanceOf>() + arrayList should instanceOf(ArrayList::class) + arrayList should instanceOf>() arrayList.shouldBeInstanceOf>() arrayList should beInstanceOf(List::class) - - shouldThrow { - arrayList should beInstanceOf(LinkedList::class) - } + arrayList should beInstanceOf>() + arrayList should instanceOf(List::class) + arrayList should instanceOf>() + arrayList.shouldBeInstanceOf>() + + shouldThrow { arrayList should beInstanceOf(LinkedList::class) } + shouldThrow { arrayList should beInstanceOf(LinkedList::class) } + shouldThrow { arrayList should beInstanceOf>() } + shouldThrow { arrayList should instanceOf(LinkedList::class) } + shouldThrow { arrayList should instanceOf>() } + shouldThrow { arrayList.shouldBeInstanceOf>() } arrayList.shouldNotBeInstanceOf>() - shouldThrow { - arrayList.shouldNotBeInstanceOf>() - } + shouldThrow { arrayList.shouldNotBeInstanceOf>() } } "use smart contracts to cast" {