rename throwableAssertion functions to new schema in api-fluent

This commit is contained in:
Robert Stoll
2021-05-19 22:34:18 +02:00
parent b17fa93436
commit 29e4c79fb7
4 changed files with 47 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ import ch.tutteli.atrium.logic.causeIsA
import ch.tutteli.atrium.logic.creating.transformers.SubjectChangerBuilder
import kotlin.reflect.KClass
//TODO move to throwableExpectations.kt with 0.18.0
/**
* Expects that the property [Throwable.message] of the subject of `this` expectation is not null,
* creates an [Expect] for it and returns it.
@@ -16,6 +17,7 @@ import kotlin.reflect.KClass
val <T : Throwable> Expect<T>.message: Expect<String>
get() = feature(Throwable::message).notToEqualNull()
//TODO move to throwableExpectations.kt with 0.18.0
/**
* Expects that the property [Throwable.message] of the subject of `this` expectation is not null and
* holds all assertions the given [assertionCreator] creates for it and
@@ -45,7 +47,7 @@ fun <T : Throwable> Expect<T>.messageContains(
vararg otherExpected: CharSequenceOrNumberOrChar
): Expect<T> = message { toContain(expected, *otherExpected) }
//TODO move to throwableExpectations.kt with 0.18.0
/**
* Expects that the property [Throwable.cause] of the subject *is a* [TExpected] (the same type or a sub-type),
* creates an [Expect] of the [TExpected] type for it and returns it.
@@ -57,12 +59,13 @@ fun <T : Throwable> Expect<T>.messageContains(
inline fun <reified TExpected : Throwable> Expect<out Throwable>.cause(): Expect<TExpected> =
causeIsA(TExpected::class).transform()
//TODO move to throwableExpectations.kt with 0.18.0 and rename to causeIsInstanceOf
@PublishedApi // in order that _logic does not become part of the API we have this extra function
internal fun <TExpected : Throwable> Expect<out Throwable>.causeIsA(
kClass: KClass<TExpected>
): SubjectChangerBuilder.ExecutionStep<Throwable?, TExpected> = _logic.causeIsA(kClass)
//TODO move to throwableExpectations.kt with 0.18.0
/**
*
* Expects that the property [Throwable.cause] of the subject *is a* [TExpected] (the same type or a sub-type) and

View File

@@ -0,0 +1,29 @@
package ch.tutteli.atrium.api.fluent.en_GB
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.creating.typeutils.CharSequenceOrNumberOrChar
import ch.tutteli.atrium.logic._logic
import ch.tutteli.atrium.logic.causeIsA
import ch.tutteli.atrium.logic.creating.transformers.SubjectChangerBuilder
import kotlin.reflect.KClass
/**
* Expects that the property [Throwable.message] of the subject of `this` expectation is not null and contains
* [expected]'s [toString] representation and the [toString] representation of the [otherExpected] (if given),
* using a non disjoint search.
*
* It is more or less a shortcut for `message { contains.atLeast(1).values(expected, otherExpected) }`, depending on
* the implementation though.
*
* Notice that a runtime check applies which assures that only [CharSequence], [Number] and [Char] are passed.
* This function expects [CharSequenceOrNumberOrChar] (which is a typealias for [Any]) for your convenience,
* so that you can mix [String] and [Int] for instance.
*
* @return an [Expect] for the subject of `this` expectation.
*
* @since 0.17.0
*/
fun <T : Throwable> Expect<T>.messageToContain(
expected: CharSequenceOrNumberOrChar,
vararg otherExpected: CharSequenceOrNumberOrChar
): Expect<T> = message { toContain(expected, *otherExpected) }

View File

@@ -6,7 +6,7 @@ import ch.tutteli.atrium.specs.*
class ThrowableExpectationsSpec : ch.tutteli.atrium.specs.integration.ThrowableExpectationsSpec(
property<Throwable, String>(Expect<Throwable>::message),
fun1<Throwable, Expect<String>.() -> Unit>(Expect<Throwable>::message),
fun2(Expect<Throwable>::messageContains),
fun2(Expect<Throwable>::messageToContain),
("cause" to Companion::causeFeature).withFeatureSuffix(),
"cause" to Companion::cause
) {

View File

@@ -17,7 +17,7 @@ import org.spekframework.spek2.style.specification.Suite
abstract class ThrowableExpectationsSpec(
messageFeature: Feature0<Throwable, String>,
message: Fun1<Throwable, Expect<String>.() -> Unit>,
messageContains: Fun2<Throwable, Any, Array<out Any>>,
messageToContain: Fun2<Throwable, Any, Array<out Any>>,
causeFeature: Feature0<Throwable, IllegalArgumentException>,
cause: Feature1<Throwable, Expect<IllegalArgumentException>.() -> Unit, IllegalArgumentException>,
@@ -29,7 +29,7 @@ abstract class ThrowableExpectationsSpec(
describePrefix,
messageFeature.forSubjectLess(),
message.forSubjectLess { toEqual("hello") },
messageContains.forSubjectLess("hello", arrayOf())
messageToContain.forSubjectLess("hello", arrayOf())
) {})
include(object : AssertionCreatorSpec<Throwable>(
@@ -40,9 +40,9 @@ abstract class ThrowableExpectationsSpec(
fun describeFun(vararg pairs: SpecPair<*>, body: Suite.() -> Unit) =
describeFunTemplate(describePrefix, pairs.map { it.name }.toTypedArray(), body = body)
describeFun(messageFeature, message, messageContains) {
describeFun(messageFeature, message, messageToContain) {
val messageFunctions = unifySignatures(messageFeature, message)
val messageContainsFun = messageContains.lambda
val messageContainsFun = messageToContain.lambda
context("Throwable.message is null") {
val throwable: Throwable = IllegalArgumentException()
@@ -61,7 +61,7 @@ abstract class ThrowableExpectationsSpec(
}
}
it("${messageContains.name} - throws an AssertionError which shows intended sub assertion") {
it("${messageToContain.name} - throws an AssertionError which shows intended sub assertion") {
expect {
expect(throwable).messageContainsFun(1, arrayOf(2.3, 'z', "hello"))
@@ -91,12 +91,12 @@ abstract class ThrowableExpectationsSpec(
expect(throwable).messageFun { toBeEmpty() }
}
}
it("${messageContains.name} - throws an AssertionError if the assertion does not hold") {
it("${messageToContain.name} - throws an AssertionError if the assertion does not hold") {
expect {
expect(throwable).messageContainsFun("nada", arrayOf())
}.toThrow<AssertionError> { messageContains(VALUE.getDefault() + ": \"nada\"") }
}
it("${messageContains.name} - throws IllegalArgumentException if empty string is passed") {
it("${messageToContain.name} - throws IllegalArgumentException if empty string is passed") {
expect {
expect(throwable).messageContainsFun("", arrayOf())
}.toThrow<IllegalArgumentException>()
@@ -116,12 +116,12 @@ abstract class ThrowableExpectationsSpec(
expect(throwable).messageFun { toEqual(" ") }
}
}
it("${messageContains.name} - throws an AssertionError if the assertion does not hold") {
it("${messageToContain.name} - throws an AssertionError if the assertion does not hold") {
expect {
expect(throwable).messageContainsFun("nada", arrayOf())
}.toThrow<AssertionError> { messageContains(VALUE.getDefault() + ": \"nada\"") }
}
it("${messageContains.name} - does not throw if the assertion holds") {
it("${messageToContain.name} - does not throw if the assertion holds") {
expect(throwable).messageContainsFun(" ", arrayOf())
}
}
@@ -141,17 +141,17 @@ abstract class ThrowableExpectationsSpec(
}
}
it("${messageContains.name} - throws an AssertionError if the assertion does not hold") {
it("${messageToContain.name} - throws an AssertionError if the assertion does not hold") {
expect {
expect(throwable).messageContainsFun("nada", arrayOf())
}.toThrow<AssertionError> { messageContains(VALUE.getDefault() + ": \"nada\"") }
}
it("${messageContains.name} - does not throw if the assertion holds") {
it("${messageToContain.name} - does not throw if the assertion holds") {
expect(throwable).messageContainsFun(1, arrayOf(2.3, 'z', "hello"))
}
}
it("${messageContains.name} - throws an IllegalArgumentException if an object is passed") {
it("${messageToContain.name} - throws an IllegalArgumentException if an object is passed") {
val throwable: Throwable = IndexOutOfBoundsException()
expect {
expect(throwable).messageContainsFun(Pair(1, 2), arrayOf())