Scoop API

scoop:scoop.test:0.1.0

src/exceptions.scoop

Selected production source · src/exceptions.scoop

1package scoop.test
3/**
4 * Runs the block and returns the thrown exception if it has the requested type. Normal return or
5 * another exception type produces [AssertionFailure]; a mismatched exception is retained as its
6 * cause.
7 */
8public fun <E : Throwable> assertFailsWith(
9 message: String? = None,
10 location: SourceLocation = currentSourceLocation(),
11 block: () -> Unit
12): E {
13 val caught: Throwable? = try {
14 block()
15 None
16 } catch (error: Throwable) {
17 Some(error)
18 }
19 when (caught) {
20 case Some(val error) -> {
21 if (error is E) return error
22 throw AssertionFailure("assertFailsWith", message, location, cause = Some(error))
23 }
24 case None -> throw AssertionFailure("assertFailsWith", message, location)
25 }