groostav
05/13/2016, 11:56 AMprivate fun <X : Throwable> doesntThrow(call : () -> Any): Boolean {
return try{ call(); true }
catch( ex : X ){ false }
}
@Test fun `when using type parameter for exception somehow type erasure doesnt screw us`(){
val resultOfNonThrowing = doesntThrow<IllegalArgumentException> { val x = 5; Unit }
val resultOfThrowingThing = doesntThrow<IllegalArgumentException> { throw IllegalArgumentException() }
val resultOfThrowingOtherThing = doesntThrow<IllegalArgumentException> { throw NullPointerException() }
assertThat(resultOfNonThrowing).isTrue();
assertThat(resultOfThrowingThing).isFalse();
assertThat(resultOfThrowingOtherThing).isFalse(); //but it does :(
}