doc / ch.tutteli.atrium.api.fluent.en_GB.kotlin_1_3 / toBeASuccess

toBeASuccess

fun <E, T : Result<E>> Expect<T>.toBeASuccess(): Expect<E> (source)

Expects that the subject of this expectation (a Result) is a success (Result.isSuccess) and returns an Expect for the inner type E.

expect(Result.success(10))
    .toBeASuccess() // subject is now of type Int (actually 10)
    .toEqual(1)

fails { // because sub-expectation fails
    expect(Result.success(10))
        .toBeASuccess()      // subject is now of type Int (actually 10)
        .toBeLessThan(5)     // fails
        .toBeGreaterThan(12) // not evaluated/reported because `toBeLessThan` already fails
    //                          use `.toBeASuccess { ... }` if you want that all expectations are evaluated
}

fails { // because it was a Failure
    expect(Result.failure<Int>(ArithmeticException()))
        .toBeASuccess()      // fails
        .toBeGreaterThan(12) // not evaluated/reported because `toBeASuccess` already fails
    //                          use `.toBeASuccess { ... }` if you want that all expectations are evaluated
}

Return
The newly created Expect if the given assertion is a success.

Since
0.17.0

fun <E, T : Result<E>> Expect<T>.toBeASuccess(assertionCreator: Expect<E>.() -> Unit): Expect<T> (source)

Expects that the subject of this expectation (a Result) is a success (Result.isSuccess) and that it holds all assertions the given assertionCreator creates.

expect(Result.success(10)).toBeASuccess { // subject within this block is of type Int (actually 10)
    toEqual(10)
    toBeLessThan(15)
} // subject here is back to type Result<Int>

fails { // because sub-expectation fails
    expect(Result.success(10)).toBeASuccess {
        toBeGreaterThan(15) // fails
        toBeLessThan(5)     // still evaluated even though `toBeGreaterThan` already fails
        //                     use `.toBeASuccess.` if you want a fail fast behaviour
    }
}

fails { // because it was a Failure, but since we use a block...
    expect(Result.failure<Int>(ArithmeticException())).toBeASuccess {
        toBeGreaterThan(12) // ...reporting mentions that subject was expected `to be greater than: 12`
        //                     use `.toBeASuccess.` if you want a fail fast behaviour
    }

}

Return
an Expect for the subject of this expectation.

Since
0.17.0