pepe
06/17/2019, 11:49 AMfun testStuff([...], type: ?????) {
[...]
assert((foo as Either.Left).a is type)
[...]
}
Should type
be KClass
or KType
?? I'm a bit lostSergey Chelombitko
06/17/2019, 11:59 AMClass<T>.isAssignableFrom(otherClass)
There is a similar API for KClass
but it requires kotlin-reflect.pepe
06/17/2019, 12:05 PMkarelpeeters
06/17/2019, 12:27 PMis T
then.Mike
06/17/2019, 1:10 PMfun verifyLeftUsage() {
val subject = "something".left()
verifyLeft<String>(subject) {
it shouldEqual "something"
}
}
/**
* For testing a function that returns an Either, and want to verify that a Left was returned
*
* If a Right is returned, an exception will be thrown with a descriptive message.
*
* The validation function will recieve the Left of the Either cast to class EE, where EE is the specific error
* class on the Left side.
*
* If the Left is not of the correct type, a class cast exception will be thrown.
*
* The validation can then focus on validating the specific properties of the error.
*
* @sample com.bns.cbttdsshrd.testhelper.assertj.ArrowKtSamples.verifyLeftUsage
* @param EE The type of the Notification that is expected
* @param subject The Either item to confirm is a `Left`
* @param validation The Lambda that will receive the Left as a parameter so further verifications can be performed on
* the object
*/
inline fun <reified EE> verifyLeft(subject: Either<*, *>, validation: (EE) -> Unit) {
subject.fold({
if (it is EE) {
validation(it)
} else {
throw AssertionError(
MESSAGE_LEFT_CORRECT_CLASS.format(
it?.javaClass?.simpleName ?: "NULL",
EE::class.simpleName
)
)
}
}, { fail(MESSAGE_IS_NOT_LEFT) })
}
pepe
06/18/2019, 7:20 AM