diff --git a/src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt b/src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt index cfb0eed6..2b594da1 100644 --- a/src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt +++ b/src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt @@ -1,9 +1,8 @@ package org.amshove.kluent +import org.junit.Assert.* import org.junit.ComparisonFailure import kotlin.reflect.KClass -import org.junit.Assert.* -import java.util.* infix fun Any.`should equal`(theOther: Any) = assertEquals(theOther, this) infix fun Any.`should not equal`(theOther: Any) = assertNotEquals(theOther, this) @@ -25,12 +24,12 @@ infix fun (() -> Unit).`should throw`(expectedException: KClass< this.invoke() fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None") } catch (e: Exception) { - if (expectedException.javaObjectType == AnyException::class.javaObjectType) { + if (expectedException.isAnyException()) { return } - if (e.javaClass == expectedException.javaObjectType) { - Unit - } else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}") + if (e.javaClass !== expectedException.javaObjectType) { + throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}") + } } } @@ -48,15 +47,13 @@ infix fun (() -> Unit).`should throw the Exception`(expectedExce infix fun (() -> Unit).`should not throw`(expectedException: KClass) { try { this.invoke() - Unit } catch (e: Exception) { - if (expectedException.javaObjectType == AnyException::class.javaObjectType) { + if (expectedException.isAnyException()) { fail("Expected no Exception to be thrown", "No Exception", "${e.javaClass}") } if (e.javaClass == expectedException.javaObjectType) { fail("Expected ${expectedException.javaObjectType} to not be thrown", "${e.javaClass}", "${expectedException.javaObjectType}") } - Unit } } @@ -65,7 +62,7 @@ infix fun (() -> Unit).`should not throw the Exception`(expected this.invoke() return NotThrowExceptionResult(noException) } catch (e: Exception) { - if (expectedException.javaObjectType == AnyException::class.javaObjectType) { + if (expectedException.isAnyException()) { fail("Expected no Exception to be thrown", "No Exception", "${e.javaClass}") } return NotThrowExceptionResult(e) @@ -85,6 +82,7 @@ val AnyException = AnyException::class class AnyException : Exception() {} private val noException = Exception("None") +private fun KClass.isAnyException() = this.javaObjectType == AnyException.javaObjectType private fun fail(message: String, expected: String, actual: String): Nothing = throw ComparisonFailure(message, expected, actual) private fun join(theArray: Array): String = theArray.joinToString(", ") private fun join(theIterable: Iterable): String = theIterable.joinToString(", ") \ No newline at end of file