diff --git a/strikt-core/src/main/kotlin/strikt/api/Assertion.kt b/strikt-core/src/main/kotlin/strikt/api/Assertion.kt index fe2f2e6f..f4fb55fe 100644 --- a/strikt-core/src/main/kotlin/strikt/api/Assertion.kt +++ b/strikt-core/src/main/kotlin/strikt/api/Assertion.kt @@ -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 @@ -26,6 +27,24 @@ internal constructor( fun describedAs(description: String): Assertion = 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.() -> Unit): Assertion = + Assertion(subject, COLLECT, negated) + .apply(block) + .also { + if (mode == FAIL_FAST) { + subject.throwOnFailure() + } + } + /** * Evaluates a condition that may pass or fail. * diff --git a/strikt-core/src/main/kotlin/strikt/api/Expect.kt b/strikt-core/src/main/kotlin/strikt/api/Expect.kt index 8fdbc8df..a5d47360 100644 --- a/strikt-core/src/main/kotlin/strikt/api/Expect.kt +++ b/strikt-core/src/main/kotlin/strikt/api/Expect.kt @@ -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]. @@ -11,31 +11,29 @@ import strikt.opentest4j.throwOnFailure * @param subject the subject of the chain of assertions. * @return an assertion for [subject]. */ -fun expect(subject: T): Assertion { - return Assertion(Subject(subject), Mode.FAIL_FAST) -} +fun expect(subject: T): Assertion = + 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 expect( subject: T, block: Assertion.() -> Unit -): Assertion { - return Subject(subject) - .let { - Assertion(it, Mode.COLLECT).apply { - block() - it.throwOnFailure() - } - } -} +): Assertion = + expect(subject).evaluate(block) /** * Asserts that [action] throws an exception of type [E] when executed.