doc / ch.tutteli.atrium.api.fluent.en_GB / toBePresent

toBePresent

fun <E, T : Optional<E>> Expect<T>.toBePresent(): Expect<E> (source)

Expects that the subject of this expectation (an Optional) is present and returns an Expect for the inner type E.

Shortcut for more or less something like feature(Optional<T>::get) but with error handling; yet it depends on the underlying implementation though.

expect(Optional.of(1))
    .toBePresent() // subject is now of type Int (actually 1)
    .toBeGreaterThan(0)


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

fails { // because it was empty
    expect(Optional.empty<Int>())
        .toBePresent()       // fails
        .toBeGreaterThan(0)  // not evaluated/reported because `toBePresent` already fails
    //                          use `.toBePresent { ... }` if you want that all expectations are evaluated
}

Return
The newly created Expect for the inner type E.

Since
0.17.0

fun <E, T : Optional<E>> Expect<T>.toBePresent(assertionCreator: Expect<E>.() -> Unit): Expect<T> (source)

Expects that the subject of this expectation (an Optional) is present and that the wrapped value of type E holds all assertions the given assertionCreator creates.

expect(Optional.of(10)).toBePresent {  // subject within this block is of type Int (actually 10)
    toBeGreaterThan(0)
    toBeLessThan(11)
}

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

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

}

Return
an Expect for the subject of this expectation.

Since
0.17.0