1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package test.exceptions
import kotlin.test.*
class ExceptionTest {
private val cause = Exception("cause")
@Test fun throwable() = testCreateException(::Throwable, ::Throwable, ::Throwable, ::Throwable)
@Test fun error() = testCreateException(::Error, ::Error, ::Error, ::Error)
@Test fun exception() = testCreateException(::Exception, ::Exception, ::Exception, ::Exception)
@Test fun runtimeException() = testCreateException(::RuntimeException, ::RuntimeException, ::RuntimeException, ::RuntimeException)
@Test fun illegalArgumentException() = testCreateException(::IllegalArgumentException, ::IllegalArgumentException, ::IllegalArgumentException, ::IllegalArgumentException)
@Test fun illegalStateException() = testCreateException(::IllegalStateException, ::IllegalStateException, ::IllegalStateException, ::IllegalStateException)
@Test fun indexOutOfBoundsException() = testCreateException(::IndexOutOfBoundsException, ::IndexOutOfBoundsException)
@Test fun unsupportedOperationException() = testCreateException(::UnsupportedOperationException, ::UnsupportedOperationException, ::UnsupportedOperationException, ::UnsupportedOperationException)
@Test fun numberFormatException() = testCreateException(::NumberFormatException, ::NumberFormatException)
@Test fun nullPointerException() = testCreateException(::NullPointerException, ::NullPointerException)
@Test fun classCastException() = testCreateException(::ClassCastException, ::ClassCastException)
@Test fun noSuchElementException() = testCreateException(::NoSuchElementException, ::NoSuchElementException)
@Test fun concurrentModificationException() = testCreateException(::ConcurrentModificationException, ::ConcurrentModificationException)
@Test fun arithmeticException() = testCreateException(::ArithmeticException, ::ArithmeticException)
@Test fun noWhenBranchMatchedException() = testCreateException(::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException)
@Test fun uninitializedPropertyAccessException() = testCreateException(::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException)
@Test fun assertionError() = testCreateException(::AssertionError, ::AssertionError, ::AssertionError)
private fun <T : Throwable> testCreateException(
noarg: () -> T,
fromMessage: (String?) -> T,
fromCause: ((Throwable?) -> T)? = null,
fromMessageCause: ((String?, Throwable?) -> T)? = null
) {
noarg().let { e ->
assertEquals(null, e.message)
assertEquals(null, e.cause)
}
fromMessage("message").let { e ->
assertEquals("message", e.message)
assertEquals(null, e.cause)
}
fromMessage(null).let { e ->
assertTrue(e.message == null || e.message == "null")
}
fromMessageCause?.run {
invoke("message", cause).let { e ->
assertEquals("message", e.message)
assertSame(cause, e.cause)
}
invoke(null, null).let { e ->
assertEquals(null, e.message)
assertEquals(null, e.cause)
}
}
fromCause?.invoke(cause)?.let { e ->
assertSame(cause, e.cause)
}
}
}
|