Skip to content

Commit

Permalink
make it possible to do a block assertion after describedAs
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Jun 9, 2018
1 parent 7ec0118 commit bf0a5b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 19 additions & 0 deletions strikt-core/src/main/kotlin/strikt/api/Assertion.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package strikt.api

import strikt.api.Mode.COLLECT
import strikt.api.Mode.FAIL_FAST
import strikt.api.reporting.Result
import strikt.api.reporting.Subject
Expand All @@ -26,6 +27,24 @@ internal constructor(
fun describedAs(description: String): Assertion<T> =
Assertion(subject.copy(description = description), mode, negated)

/**
* Evaluates multiple assertions against the subject.
*
* @param block a closure that can perform multiple assertions that will all
* be evaluated regardless of whether preceding ones pass or fail.
*
* @see expect
*/
// TODO: not 100% happy with the name
fun evaluate(block: Assertion<T>.() -> Unit): Assertion<T> =
Assertion(subject, COLLECT, negated)
.apply(block)
.also {
if (mode == FAIL_FAST) {
subject.throwOnFailure()
}
}

/**
* Evaluates a condition that may pass or fail.
*
Expand Down
24 changes: 11 additions & 13 deletions strikt-core/src/main/kotlin/strikt/api/Expect.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package strikt.api

import strikt.api.Mode.FAIL_FAST
import strikt.api.reporting.Subject
import strikt.assertions.throws
import strikt.opentest4j.throwOnFailure

/**
* Start a chain of assertions over [subject].
Expand All @@ -11,31 +11,29 @@ import strikt.opentest4j.throwOnFailure
* @param subject the subject of the chain of assertions.
* @return an assertion for [subject].
*/
fun <T> expect(subject: T): Assertion<T> {
return Assertion(Subject(subject), Mode.FAIL_FAST)
}
fun <T> expect(subject: T): Assertion<T> =
Assertion(Subject(subject), FAIL_FAST)

/**
* Evaluate a block of assertions over [subject].
* This is the entry-point for the assertion API.
*
* This is a shortcut for:
*
* expect(subject).evaluate(block)
*
* @param subject the subject of the block of assertions.
* @param block a closure that can perform multiple assertions that will all
* be evaluated regardless of whether preceding ones pass or fail.
* @return an assertion for [subject].
*
* @see Assertion.evaluate
*/
fun <T> expect(
subject: T,
block: Assertion<T>.() -> Unit
): Assertion<T> {
return Subject(subject)
.let {
Assertion(it, Mode.COLLECT).apply {
block()
it.throwOnFailure()
}
}
}
): Assertion<T> =
expect(subject).evaluate(block)

/**
* Asserts that [action] throws an exception of type [E] when executed.
Expand Down

0 comments on commit bf0a5b8

Please sign in to comment.