-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New mechanism for testing thrown exceptions
- Loading branch information
1 parent
db1997d
commit 1b67725
Showing
9 changed files
with
99 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package strikt.api | ||
|
||
/** | ||
* Executes [action], catching and returning any exception that is thrown. If | ||
* no exception is thrown the method returns `null`. | ||
* | ||
* @return the exception thrown by [action] or `null` if no exception is thrown. | ||
*/ | ||
fun catching( | ||
action: () -> Unit | ||
): Throwable? { | ||
return try { | ||
action() | ||
null | ||
} catch (actual: Throwable) { | ||
actual | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
strikt-core/src/main/kotlin/strikt/assertions/Functions.kt
This file was deleted.
Oops, something went wrong.
26 changes: 24 additions & 2 deletions
26
strikt-core/src/main/kotlin/strikt/assertions/Throwable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
package strikt.assertions | ||
|
||
import strikt.api.Assertion.Builder | ||
import strikt.api.DescribeableBuilder | ||
|
||
/** | ||
* Maps an assertion on a [Throwable] to an assertion on its message. | ||
* Maps an assertion on a [Throwable] to an assertion on its | ||
* [Throwable.message]. | ||
* This mapping also asserts that the message is not `null`. | ||
* This is particularly useful after [throws]. | ||
* | ||
* @author [Xavier Hanin](https://github.com/xhanin) | ||
*/ | ||
val <T : Throwable> Builder<T>.message: Builder<String> | ||
get() = map(Throwable::message).isNotNull() | ||
|
||
/** | ||
* Maps an assertion on a [Throwable] to an assertion on its [Throwable.cause]. | ||
*/ | ||
val <T : Throwable> Builder<T>.cause: DescribeableBuilder<Throwable?> | ||
get() = map(Throwable::cause) | ||
|
||
/** | ||
* Asserts that an exception is an instance of the expected type. | ||
* The assertion fails if the subject is `null` or not an instance of [E]. | ||
* | ||
* This assertion is designed for use with the [strikt.api.catching] function. | ||
*/ | ||
inline fun <reified E : Throwable> Builder<Throwable?>.throws(): Builder<E> = | ||
assert("threw %s", E::class.java) { | ||
when (it) { | ||
null -> fail("nothing was thrown") | ||
is E -> pass() | ||
else -> fail(description = "%s was thrown", actual = it, cause = it) | ||
} | ||
} as Builder<E> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters