diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableAssertions.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableAssertions.kt index a22019cfc5..5805f71549 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableAssertions.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableAssertions.kt @@ -14,6 +14,8 @@ import kotlin.reflect.KClass * creates an [Expect] for it and returns it. * * @return The newly created [Expect] for the property [Throwable.message] of the subject of `this` expectation. + * + * @sample ch.tutteli.atrium.api.infix.en_GB.samples.ThrowableExpectationSamples.messageFeature */ val Expect.message: Expect get() = it feature Throwable::message notToEqualNull o @@ -25,6 +27,8 @@ val Expect.message: Expect * returns an [Expect] for the current subject of `this` expectation. * * @return an [Expect] for the subject of `this` expectation. + * + * @sample ch.tutteli.atrium.api.infix.en_GB.samples.ThrowableExpectationSamples.message */ infix fun Expect.message(assertionCreator: Expect.() -> Unit): Expect = it feature of(Throwable::message) { it notToEqualNull assertionCreator } @@ -78,6 +82,8 @@ infix fun Expect.messageContains(values: Values): Expect * * @return The newly created [Expect] for the property [Throwable.cause] of the subject of `this` expectation * + * @sample ch.tutteli.atrium.api.infix.en_GB.samples.ThrowableExpectationSamples.causeFeature + * * @since 0.12.0 */ inline fun Expect.cause(): Expect = @@ -101,6 +107,8 @@ internal fun Expect.causeIsA( * * @return an [Expect] for the subject of `this` expectation. * + * @sample ch.tutteli.atrium.api.infix.en_GB.samples.ThrowableExpectationSamples.cause + * * @since 0.12.0 */ inline infix fun Expect.cause( diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableExpectations.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableExpectations.kt index 5c60d6bd1b..6a8872d530 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableExpectations.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/throwableExpectations.kt @@ -18,6 +18,8 @@ import kotlin.reflect.KClass * * @return an [Expect] for the subject of `this` expectation. * + * @sample ch.tutteli.atrium.api.infix.en_GB.samples.ThrowableExpectationSamples.messageToContain + * * @since 0.17.0 */ infix fun Expect.messageToContain(expected: CharSequenceOrNumberOrChar): Expect = @@ -35,6 +37,8 @@ infix fun Expect.messageToContain(expected: CharSequenceOrNum * * @return an [Expect] for the subject of `this` expectation. * + * @sample ch.tutteli.atrium.api.infix.en_GB.samples.ThrowableExpectationSamples.messageFeature + * * @since 0.17.0 */ infix fun Expect.messageToContain(values: Values): Expect = diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/samples/ThrowableExpectationSamples.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/samples/ThrowableExpectationSamples.kt new file mode 100644 index 0000000000..689b3d5ce8 --- /dev/null +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/samples/ThrowableExpectationSamples.kt @@ -0,0 +1,68 @@ +package ch.tutteli.atrium.api.infix.en_GB.samples + +import ch.tutteli.atrium.api.infix.en_GB.* +import ch.tutteli.atrium.api.verbs.internal.expect +import kotlin.test.Test + +class ThrowableExpectationSamples { + + @Test + fun messageToContain() { + expect(RuntimeException("abc")) messageToContain "b" + + fails { + expect(RuntimeException("abc")) messageToContain "d" + } + } + + @Test + fun messageFeature() { + expect(RuntimeException("abc")).message toContain "a" + // | subject is now of type String + + fails { + expect(RuntimeException("abc")).message toContain "d" + // | subject is now of type String + } + } + + @Test + fun message() { + expect(RuntimeException("abc")) message { // subject inside this block is of type String (actually "abc") + toContain("a") + } + + fails { + expect(RuntimeException("abc")) message { // subject inside this block is of type String (actually "abc") + toContain("d") + } + } + } + + @Test + fun causeFeature() { + expect(IllegalStateException(IndexOutOfBoundsException("abc"))) + .cause() messageToContain "b" + // | subject is now of type IndexOutOfBoundsException + + fails { + expect(IllegalStateException(IndexOutOfBoundsException("abc"))) + .cause() messageToContain "d" // not shown in reporting as `cause()` already fails + + } + } + + @Test + fun cause() { + expect(IllegalStateException(IndexOutOfBoundsException("abc"))) + .cause { // subject is now of type IndexOutOfBoundsException + it messageToContain "b" + } + + fails { // because wrong type expected (IllegalStateException instead of IndexOutOfBoundsException), but since we use a block... + expect(IllegalStateException(IndexOutOfBoundsException("abc"))).cause { + it messageToContain "b" // ... reporting mentions that subject's message was expected `to contain: "b"` + } + } + } +}