-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from robstoll/failIf
add failIf
- Loading branch information
Showing
3 changed files
with
44 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch.tutteli.kbox | ||
|
||
/** | ||
* Delegates to [check] but inverting the [predicate], i.e. throws an [IllegalStateException] if predicate is true. | ||
* | ||
* @param predicate the predicate which defines if the exception should be thrown or not. | ||
* @param errorMessage The message which as [IllegalStateException.message] | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
inline fun failIf(predicate: Boolean, errorMessage: () -> String) = check(predicate.not(), errorMessage) |
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,24 @@ | ||
package ch.tutteli.kbox | ||
|
||
import ch.tutteli.atrium.api.fluent.en_GB.messageToContain | ||
import ch.tutteli.atrium.api.fluent.en_GB.notToThrow | ||
import ch.tutteli.atrium.api.fluent.en_GB.toThrow | ||
import ch.tutteli.kbox.atrium.expect | ||
import kotlin.test.Test | ||
|
||
class FailIfTest { | ||
@Test | ||
fun it_throws_an_IllegalStateException_if_predicate_is_true() { | ||
expect { | ||
failIf(true) { "my message" } | ||
}.toThrow<IllegalStateException> { messageToContain("my message") } | ||
} | ||
|
||
@Test | ||
fun it_does_not_throw_if_predicate_holds() { | ||
expect { | ||
failIf(false) { throw UnsupportedOperationException("bla") } | ||
}.notToThrow() | ||
} | ||
} | ||
|