assertFailsWith: set unexpected exception as a cause of assertion error

#KT-23514
This commit is contained in:
Ilya Gorbunov
2020-03-26 00:19:53 +03:00
parent 2bb36899da
commit 96ed87d81b
3 changed files with 9 additions and 5 deletions
@@ -35,9 +35,11 @@ class BasicAssertionsTest {
assertTrue(true) // at least one assertion required for qunit assertTrue(true) // at least one assertion required for qunit
withDefaultAsserter run@{ withDefaultAsserter run@{
val rootCause = IllegalArgumentException()
try { try {
assertFailsWith<IllegalStateException> { throw IllegalArgumentException() } assertFailsWith<IllegalStateException> { throw rootCause }
} catch (e: AssertionError) { } catch (e: AssertionError) {
if (e.cause !== rootCause) throw AssertionError("Expected to fail with correct cause")
return@run return@run
} }
throw AssertionError("Expected to fail") throw AssertionError("Expected to fail")
@@ -62,9 +64,11 @@ class BasicAssertionsTest {
@Test @Test
fun testAssertFailsWithClassFails() { fun testAssertFailsWithClassFails() {
checkFailedAssertion { val rootCause = IllegalStateException()
assertFailsWith(IllegalArgumentException::class) { throw IllegalStateException() } val actual = checkFailedAssertion {
assertFailsWith(IllegalArgumentException::class) { throw rootCause }
} }
assertSame(rootCause, actual.cause, "Expected to fail with correct cause")
checkFailedAssertion { checkFailedAssertion {
assertFailsWith(Exception::class) { } assertFailsWith(Exception::class) { }
@@ -34,7 +34,7 @@ internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return e as T return e as T
} }
asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e") asserter.fail(messagePrefix(message) + "Expected an exception of $exceptionClass to be thrown, but was $e", e)
} }
) )
} }
@@ -25,7 +25,7 @@ internal actual fun <T : Throwable> checkResultIsFailure(exceptionClass: KClass<
return e as T return e as T
} }
asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.java} to be thrown, but was $e") asserter.fail(messagePrefix(message) + "Expected an exception of ${exceptionClass.java} to be thrown, but was $e", e)
} }
) )
} }