Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Merge pull request #127 from jcornaz/feature/improve-isInstanceOf
Browse files Browse the repository at this point in the history
Allow to call `isInstanceOf` and equivalent assertions on nullable types
  • Loading branch information
MarkusAmshove authored Dec 2, 2018
2 parents 4c10efb + 684dd92 commit 5c906fe
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
1. Fabrício Rissetto - [@fabriciorissetto](https://github.com/fabriciorissetto) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=fabriciorissetto))
1. Vitus Ortner - [@vitusortner](https://github.com/vitusortner) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=vitusortner))
1. Caleb Brinkman - [@floralvikings](https://github.com/floralvikings) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=floralvikings))

1. Jonathan Cornaz - [@jcornaz](https://github.com/jcornaz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=jcornaz))
4 changes: 3 additions & 1 deletion jvm/src/main/kotlin/org/amshove/kluent/BasicBacktick.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ infix fun Any?.`should be instance of`(className: Class<*>) = this.shouldBeInsta

infix fun Any?.`should be instance of`(className: KClass<*>) = this.shouldBeInstanceOf(className)

inline fun <reified T : Any> Any.`should be instance of`() = this.shouldBeInstanceOf<T>()
inline fun <reified T : Any> Any?.`should be instance of`() = this.shouldBeInstanceOf<T>()

infix fun Any?.`should not be instance of`(className: Class<*>) = this.shouldNotBeInstanceOf(className)

infix fun Any?.`should not be instance of`(className: KClass<*>) = this.shouldNotBeInstanceOf(className)

inline fun <reified T : Any> Any?.`should not be instance of`() = this.shouldNotBeInstanceOf<T>()

fun Any?.`should be null`() = this.shouldBeNull()

fun <T : Any> T?.`should not be null`(): T = this.shouldNotBeNull()
Expand Down
15 changes: 7 additions & 8 deletions jvm/src/main/kotlin/org/amshove/kluent/Reflection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ import org.amshove.kluent.internal.assertFalse
import org.amshove.kluent.internal.assertTrue
import org.amshove.kluent.internal.failExpectedActual
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf

infix fun Any?.shouldBeInstanceOf(className: Class<*>) = assertTrue("Expected $this to be an instance of $className", className.isInstance(this))

infix fun Any?.shouldBeInstanceOf(className: KClass<*>) = assertTrue("Expected $this to be an instance of $className", className.isInstance(this))

inline fun <reified T : Any> Any.shouldBeInstanceOf(): T = if (this::class.isInstance(T::class) || this::class.isSubclassOf(T::class)) this as T else throw AssertionError("Expected $this to be an instance or subclass of ${T::class.qualifiedName}")
inline fun <reified T> Any?.shouldBeInstanceOf(): T = if (this is T) this else throw AssertionError("Expected $this to be an instance or subclass of ${T::class.qualifiedName}")

infix fun Any?.shouldNotBeInstanceOf(className: Class<*>) = assertFalse("Expected $this to not be an instance of $className", className.isInstance(this))

infix fun Any?.shouldNotBeInstanceOf(className: KClass<*>) = assertFalse("Expected $this to not be an instance of $className", className.isInstance(this))

inline fun <reified T : Any> Any.shouldNotBeInstanceOf() = this.shouldNotBeInstanceOf(T::class)
inline fun <reified T> Any?.shouldNotBeInstanceOf() = if (this !is T) this else throw AssertionError("Expected $this to not be an instance or subclass of ${T::class.qualifiedName}")

infix fun <T : Any> T.shouldHaveTheSameClassAs(other: Any) = apply {
infix fun <T : Any> T?.shouldHaveTheSameClassAs(other: Any) = apply {
if (!haveSameClasses(this, other)) {
failExpectedActual("Expected objects to have the same class", this::class.qualifiedName!!, other::class.qualifiedName!!)
failExpectedActual("Expected objects to have the same class", this?.javaClass?.kotlin?.qualifiedName, other::class.qualifiedName!!)
}
}

infix fun <T : Any> T.shouldNotHaveTheSameClassAs(other: Any) = apply {
infix fun <T : Any> T?.shouldNotHaveTheSameClassAs(other: Any) = apply {
if(haveSameClasses(this, other)) {
failExpectedActual("Expected objects to not have the same class", this::class.qualifiedName!!, other::class.qualifiedName!!)
failExpectedActual("Expected objects to not have the same class", this?.javaClass?.kotlin?.qualifiedName, other::class.qualifiedName!!)
}
}

private fun haveSameClasses(first: Any, second: Any) = first::class == second::class
private fun haveSameClasses(first: Any?, second: Any) = first?.javaClass?.kotlin == second::class
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.amshove.kluent.tests.helpclasses.*
import org.junit.Test
import kotlin.test.assertFails


class ShouldBeInstanceOfShould {
@Test
fun passWhenTestingAnObjectWithThePassedClass() {
Expand Down Expand Up @@ -40,4 +39,24 @@ class ShouldBeInstanceOfShould {
val child = base.shouldBeInstanceOf<Child>()
}
}
}

@Test
fun failWhenTestingNullInstance() {
val base: Base? = null
assertFails {
base.shouldBeInstanceOf<Base>()
}
}

@Test
fun passWhenTestingNullInstanceAgainstNullableType() {
val base: Base? = null
base.shouldBeInstanceOf<Base?>()
}

@Test
fun passWhenTestingNonNullInstanceAgainstCompatibleNullableType() {
val base: Base = Base()
base.shouldBeInstanceOf<Base?>()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.amshove.kluent.tests.assertions.reflection

import org.amshove.kluent.shouldHaveTheSameClassAs
import org.amshove.kluent.tests.helpclasses.Base
import org.junit.Test
import kotlin.test.assertFails

Expand Down Expand Up @@ -45,6 +46,16 @@ class ShouldHaveTheSameClassAsShould {

assertFails { firstChild.shouldHaveTheSameClassAs(secondChild) }
}

@Test
fun failWhenTestingNullInstance() {
val base: Base? = null
val other = Base()

assertFails {
base.shouldHaveTheSameClassAs(other)
}
}
}

interface IInterface
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.amshove.kluent.tests.assertions.reflection

import org.amshove.kluent.shouldBeInstanceOf
import org.amshove.kluent.shouldNotBeInstanceOf
import org.amshove.kluent.tests.helpclasses.Base
import org.amshove.kluent.tests.helpclasses.Circle
import org.amshove.kluent.tests.helpclasses.Shape
import org.amshove.kluent.tests.helpclasses.Square
Expand Down Expand Up @@ -33,4 +35,22 @@ class ShouldNotBeInstanceOfShould {
val firstObject: Shape = Circle(10.0)
assertFails { firstObject.shouldNotBeInstanceOf<Circle>() }
}
}

@Test
fun passWhenTestingNullInstance() {
val base: Base? = null
base.shouldNotBeInstanceOf<Base>()
}

@Test
fun failWhenTestingNullInstanceAgainstNullableType() {
val base: Base? = null
assertFails { base.shouldNotBeInstanceOf<Base?>() }
}

@Test
fun failWhenTestingNonNullInstanceAgainstCompatibleNullableType() {
val base: Base = Base()
assertFails { base.shouldNotBeInstanceOf<Base?>() }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.amshove.kluent.tests.assertions.reflection

import org.amshove.kluent.shouldNotHaveTheSameClassAs
import org.amshove.kluent.tests.helpclasses.Base
import org.junit.Test
import kotlin.test.assertFails

Expand Down Expand Up @@ -45,4 +46,12 @@ class ShouldNotHaveTheSameClassAsShould {

assertFails { intList.shouldNotHaveTheSameClassAs(stringList) }
}

@Test
fun passWhenTestingNullInstance() {
val base: Base? = null
val other = Base()

base.shouldNotHaveTheSameClassAs(other)
}
}

0 comments on commit 5c906fe

Please sign in to comment.