Trying to write matchers for <https://github.com/m...
# strikt
d
Trying to write matchers for https://github.com/michaelbull/kotlin-result, but I'm having a bit of trouble understanding how to return things... I have:
Copy code
fun <V, E> Assertion.Builder<Result<V, E>>.isFailure(): Assertion.Builder<E> =
    assert("is failure") {
        when {
            it is Err -> pass(
                description = "threw %s",
                actual = it.error
            )
            else -> fail(
                description = "returned %s",
                actual = it.get()
            )
        }
    }
        .get("exception") {
            unwrapError()
        }

fun <V, E> Assertion.Builder<Result<V, E>>.isSuccess(): Assertion.Builder<V> =
    assert("is success") {
        when {
            it is Ok -> pass()
            else -> fail(
                description = "was error ${it.unwrapError()}",
            )
        }
    }
        .get("value") {
            unwrap()
        }

inline fun <reified E> Assertion.Builder<Result<Any?, E>>.failedWith() =
    isFailure().isA<E>()
But when I try `
Copy code
expectThat(result).isFailure().isA<SomeDomainError>()
I get:
Copy code
org.opentest4j.AssertionFailedError: ▼ Expect that Ok(SomeStub(id=1,...)):
  ✗ is failure
    returned SomeStub(id=1, ...)
r
That looks like it’s hitting the exact same bug as the IR compiler seems to provoke in the standard
kotlin.Result
extensions
d
But Result in that library isn't an inline/value class at all...?
r
oh, I was misinterpreting your error message there, sorry
only 7am here and I’m on my first coffee of the day, gimme a moment 😉
d
It looks like that's the right thing, since actual is the
%s
... so that's ok... but I still have a problem with the
Copy code
inline fun <reified E> Assertion.Builder<Result<Any?, E>>.failedWith() =
    isFailure().isA<E>()
not working, it has a problem with receiver types not matching...
Also, it should have given me a more descriptive error when I chained the
Copy code
.isA<SomeDomainError>()
but it just cut off early when using isFailure()... Actually I was kind of expecting the isA to help return the failure type expected in isFailure()... but we don't have that yet if it didn't fail 🙃.
Even
failedWith()
wouldn't help with that...