robfletcher
10/05/2021, 4:55 PMT
to being a subtype of whatever the current subject is?christophsturm
10/05/2021, 4:59 PMchristophsturm
10/05/2021, 4:59 PMrobfletcher
10/05/2021, 4:59 PMchristophsturm
10/05/2021, 5:00 PMrobfletcher
10/05/2021, 5:02 PMisA
has 2 type parameters you can’t call it with isA<Whatever>
christophsturm
10/05/2021, 5:16 PMrobfletcher
10/05/2021, 5:17 PMchristophsturm
10/05/2021, 5:18 PMchristophsturm
10/05/2021, 5:19 PMrobfletcher
10/05/2021, 5:19 PMisA
in the assertion tree. Might be possible to optimize that out thodave08
01/26/2023, 4:58 PMinline fun <reified T, R> Assertion.Builder<*>.getFromA(noinline function: T.() -> R): Assertion.Builder<R> =
isA<T>().get(function.describe(), function)
fun <Receiver, Result> (Receiver.() -> Result).describe(): String =
when (this) {
is KProperty<*> ->
"value of property $name"
is KFunction<*> ->
"return value of $name"
is CallableReference -> "value of $propertyName"
else -> { "%s" }
}
val CallableReference.propertyName: String
get() = "^get(.+)$".toRegex().find(name).let { match ->
return when (match) {
null -> name
else -> match.groupValues[1].replaceFirstChar { it.lowercase(Locale.getDefault()) }
}
}
dave08
01/26/2023, 4:59 PMdave08
01/26/2023, 4:59 PMEric
01/26/2023, 4:59 PM@Suppress("UNCHECKED_CAST")
inline fun <reified T> Assertion.Builder<*>.isCollectionOf(): Assertion.Builder<Collection<T>> =
compose("is a Collection<${T::class.java.name}>") { subject ->
val collection = subject as? Collection<*>
assertThat("is a Collection") { collection != null }
if (collection == null) return@compose
assert("all elements are ${T::class.java.name}") {
val wronglyTypedElements = collection.mapIndexedNotNull { index, element ->
when (element) {
is T -> null
null -> "null @ index $index"
else -> "${element::class.java.name} @ index $index"
}
}
if (wronglyTypedElements.isEmpty()) {
pass()
} else {
fail(description = "incorrect elements: $wronglyTypedElements")
}
}
}.assertAllPassed() as Assertion.Builder<Collection<T>>
Eric
01/26/2023, 5:00 PMdave08
01/26/2023, 5:01 PM