Skip to content

Commit

Permalink
Allow should(Not)Throw on nullable lambdas Kluent MarkusAmshove#63
Browse files Browse the repository at this point in the history
  • Loading branch information
gregwoodfill committed Sep 16, 2017
1 parent df1d432 commit 7e90b3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/amshove/kluent/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.amshove.kluent
import org.junit.ComparisonFailure
import kotlin.reflect.KClass

infix fun <T : Throwable> (() -> Any).shouldThrow(expectedException: KClass<T>): ExceptionResult<T> {
infix fun <T : Throwable> (() -> Any?).shouldThrow(expectedException: KClass<T>): ExceptionResult<T> {
try {
this.invoke()
fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None")
Expand All @@ -15,7 +15,7 @@ infix fun <T : Throwable> (() -> Any).shouldThrow(expectedException: KClass<T>):
}
}

infix fun <T : Throwable> (() -> Any).shouldNotThrow(expectedException: KClass<T>) {
infix fun <T : Throwable> (() -> Any?).shouldNotThrow(expectedException: KClass<T>) {
try {
this.invoke()
} catch (e: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,19 @@ class ShouldThrowTests : Spek({
}
}
}

given("a lambda that returns null") {
val func = { null }
it("should not throw exception") {
func shouldNotThrow AnyException
}
}

given("a lambda that can return null") {
val func = { if (0 == 1) null else throw IllegalStateException() }
it("should throw exception") {
func shouldThrow IllegalStateException::class
}
}
}
})

0 comments on commit 7e90b3c

Please sign in to comment.