Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added inline version for intanceOf alias #1838

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlin.reflect.KClass
// alias for beInstanceOf
fun instanceOf(expected: KClass<*>): Matcher<Any?> = beInstanceOf(expected)

inline fun <reified T : Any> instanceOf(): Matcher<Any?> = instanceOf(T::class)

fun beInstanceOf(expected: KClass<*>): Matcher<Any?> = neverNullMatcher { value ->
MatcherResult(
expected.isInstance(value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,19 +53,27 @@ class TypeMatchersTest : WordSpec() {
val arrayList: List<Int> = arrayListOf(1, 2, 3)

arrayList should beInstanceOf(ArrayList::class)
arrayList should beInstanceOf<ArrayList<*>>()
arrayList should instanceOf(ArrayList::class)
arrayList should instanceOf<ArrayList<*>>()
arrayList.shouldBeInstanceOf<ArrayList<*>>()

arrayList should beInstanceOf(List::class)

shouldThrow<AssertionError> {
arrayList should beInstanceOf(LinkedList::class)
}
arrayList should beInstanceOf<List<*>>()
arrayList should instanceOf(List::class)
arrayList should instanceOf<List<*>>()
arrayList.shouldBeInstanceOf<List<*>>()

shouldThrow<AssertionError> { arrayList should beInstanceOf(LinkedList::class) }
shouldThrow<AssertionError> { arrayList should beInstanceOf(LinkedList::class) }
shouldThrow<AssertionError> { arrayList should beInstanceOf<LinkedList<*>>() }
shouldThrow<AssertionError> { arrayList should instanceOf(LinkedList::class) }
shouldThrow<AssertionError> { arrayList should instanceOf<LinkedList<*>>() }
shouldThrow<AssertionError> { arrayList.shouldBeInstanceOf<LinkedList<*>>() }

arrayList.shouldNotBeInstanceOf<LinkedList<*>>()

shouldThrow<AssertionError> {
arrayList.shouldNotBeInstanceOf<ArrayList<*>>()
}
shouldThrow<AssertionError> { arrayList.shouldNotBeInstanceOf<ArrayList<*>>() }
}

"use smart contracts to cast" {
Expand Down