Skip to content

Commit

Permalink
assertInstanceOf methods accept nullable values
Browse files Browse the repository at this point in the history
  • Loading branch information
awelless committed Aug 25, 2023
1 parent 544b4e1 commit 437d29e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Expand Up @@ -244,7 +244,7 @@ fun assertNotNull(actual: Any?, messageSupplier: () -> String) {
* @see Assertions.assertInstanceOf
*/
@API(since = "5.11", status = EXPERIMENTAL)
inline fun <reified T : Any> assertInstanceOf(actual: Any) {
inline fun <reified T : Any> assertInstanceOf(actual: Any?) {
contract {
returns() implies (actual is T)
}
Expand All @@ -265,7 +265,7 @@ inline fun <reified T : Any> assertInstanceOf(actual: Any) {
* @see Assertions.assertInstanceOf
*/
@API(since = "5.11", status = EXPERIMENTAL)
inline fun <reified T : Any> assertInstanceOf(actual: Any, message: String) {
inline fun <reified T : Any> assertInstanceOf(actual: Any?, message: String) {
contract {
returns() implies (actual is T)
}
Expand All @@ -286,7 +286,7 @@ inline fun <reified T : Any> assertInstanceOf(actual: Any, message: String) {
* @see Assertions.assertInstanceOf
*/
@API(since = "5.11", status = EXPERIMENTAL)
inline fun <reified T : Any> assertInstanceOf(actual: Any, noinline messageSupplier: () -> String) {
inline fun <reified T : Any> assertInstanceOf(actual: Any?, noinline messageSupplier: () -> String) {
contract {
returns() implies (actual is T)

Expand Down
Expand Up @@ -236,6 +236,15 @@ class KotlinAssertionsTests {
assertFalse(string.isEmpty()) // smart cast to a String object
}

@Test
fun `assertInstanceOf with a null value`() {
val error = assertThrows<AssertionFailedError> {
assertInstanceOf<String>(null)
}

assertMessageStartsWith(error, "Unexpected null value")
}

@Test
fun `assertDoesNotThrow with value initialization in lambda`() {
val value: Int
Expand Down

0 comments on commit 437d29e

Please sign in to comment.