Eric
05/31/2023, 2:11 PM.isA<Ok<Foo>>().get { value }
with .isSuccess<Foo>()
but I'm having trouble w/ the generic signatures.
fun <V> Assertion.Builder<Result<V, *>>.isSuccess(): Assertion.Builder<V> = isA<Ok<V>>().get { value }
fun <E> Assertion.Builder<Result<*, E>>.isFailure(): Assertion.Builder<E> = isA<Err<E>>().get { error }
IntelliJ is yelling at me. Any ideas on what these ext funs should look like? Thanks.Eric
05/31/2023, 2:30 PMResult
fun <V> Assertion.Builder<*>.isOk(): Assertion.Builder<V> = isA<Ok<V>>().get { value }
fun <E> Assertion.Builder<*>.isErr(): Assertion.Builder<E> = isA<Err<E>>().get { error }
robfletcher
05/31/2023, 5:14 PMfun <V> Assertion.Builder<Result<*, *>>.isSuccess(): Assertion.Builder<V> = isA<Ok<V>>().get { value }
fun <E> Assertion.Builder<Result<*, *>>.isFailure(): Assertion.Builder<E> = isA<Err<E>>().get { error }
robfletcher
05/31/2023, 5:15 PMisA
robfletcher
05/31/2023, 5:15 PMEric
05/31/2023, 5:19 PMrobfletcher
05/31/2023, 5:22 PMresult
as Result<*, *>
. If I change that to a concrete type, or let it use inference I see the same as yourobfletcher
05/31/2023, 5:23 PMimport com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import strikt.api.Assertion
import strikt.api.expect
import strikt.assertions.isA
import strikt.assertions.isEqualTo
fun <V> Assertion.Builder<Result<*, *>>.isSuccess(): Assertion.Builder<V> = isA<Ok<V>>().get { value }
fun <E> Assertion.Builder<Result<*, *>>.isFailure(): Assertion.Builder<E> = isA<Err<E>>().get { error }
val result: Result<*, *> = Err(IllegalStateException("o noes"))
try {
expect {
that(result).isSuccess<String>().isEqualTo("hi")
}
} catch (e: Throwable) {
println (e)
}
robfletcher
05/31/2023, 5:24 PMrobfletcher
05/31/2023, 5:28 PMthat(result).isA<Ok<String>>().get { value }.isEqualTo("hi")
Eric
05/31/2023, 5:29 PMrobfletcher
05/31/2023, 5:29 PMOk
/ Err
robfletcher
05/31/2023, 5:29 PMisSuccess
function rather than being specified directlyrobfletcher
05/31/2023, 5:34 PMkotlin.Result
was a painrobfletcher
05/31/2023, 5:35 PM(that(result) as Assertion.Builder<Result<String, *>>).isSuccess<String>().isEqualTo("hi")
and it worksrobfletcher
05/31/2023, 5:37 PMresult
as Result<String, *>
robfletcher
05/31/2023, 5:39 PMEric
05/31/2023, 5:42 PMfun <V : Any, E : Any> Assertion.Builder<Result<V, E>>.isOk(): Assertion.Builder<V> = isA<Ok<V>>().get { value }
fun <V : Any, E : Any> Assertion.Builder<Result<V, E>>.isErr(): Assertion.Builder<E> = isA<Err<E>>().get { error }
but then the call site doesn't have a type param... not sure if that's an issue
that(result)._isOk_()._isEqualTo_(lead)
Eric
05/31/2023, 5:43 PMEric
05/31/2023, 5:44 PMrobfletcher
05/31/2023, 5:44 PMrobfletcher
05/31/2023, 5:45 PMEric
05/31/2023, 5:45 PMrobfletcher
05/31/2023, 5:47 PMResult<Superclass, E>
you don’t get to narrow it to a subclassEric
05/31/2023, 5:47 PMrobfletcher
05/31/2023, 5:47 PMrobfletcher
05/31/2023, 5:48 PMEric
05/31/2023, 5:48 PMrobfletcher
05/31/2023, 5:49 PMEric
05/31/2023, 5:50 PMrobfletcher
05/31/2023, 5:50 PMResult
typeEric
05/31/2023, 5:51 PMrobfletcher
05/31/2023, 5:51 PMrobfletcher
05/31/2023, 5:51 PMEric
05/31/2023, 5:52 PM: Result<Lead, RecaptchaVerificationFailure> { ... }
Eric
05/31/2023, 5:53 PMEric
07/26/2023, 1:23 PMinline fun <reified E : Any> Assertion.Builder<*>.isErrResult(): Assertion.Builder<E> =
isA<Result<*, E>>().isA<Err<E>>().get { error }.isA<E>()
// these could have a nice description
inline fun <reified V : Any> Assertion.Builder<*>.isOkResult(): Assertion.Builder<V> =
isA<Result<V, *>>().isA<Ok<V>>().get { value }.isA<V>()
but, it doesn't save me anything where I'm doing the assertion:
expectThat(result).isErrResult<InvalidToken>()
// is the same as this:
expectThat(result).isErr().isA<InvalidToken>()
So, I might not use it after all.