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

get

fun <E, T : List<E>> Expect<T>.get(index: Int): Expect<E> (source)

Expects that the given index is within the bounds of the subject of this expectation (a List) and returns an Expect for the element at that position.

val list = listOf(1, 2, 3)

expect(list)
    .get(0)             // subject is now of type Int (actually 1)
    .toBeGreaterThan(0) // subject is still of type Int (still 1)
    .toBeLessThan(2)

fails {
    expect(list)
        .get(0)              // subject is now of type Int (actually 1)
        .toBeGreaterThan(2)  // fails
        .toBeLessThan(0)     // not evaluated/reported because `toBeGreaterThan` already fails
    //                          use `.get(index) { ... }` if you want that all expectations are evaluated
}

fails {
    expect(list)
        .get(3)          // fails because index 3 is out of bound
        .toBeLessThan(0) // not evaluated/reported because `get` already fails
    //                      use `.get(index) { ... }` if you want that all expectations are evaluated
}

Return
The newly created Expect for the element at position index.

fun <E, T : List<E>> Expect<T>.get(index: Int, assertionCreator: Expect<E>.() -> Unit): Expect<T> (source)

Expects that the given index is within the bounds of the subject of this expectation (a List) and that the element at that position holds all assertions the given assertionCreator creates for it.

val list = listOf(1, 2, 3)

expect(list)
    .get(0) { // subject inside this block is of type Int (actually 1)
        toBeGreaterThan(0)
        toBeLessThan(2)
    } // subject here is back to type List<Int>
    .get(1) { // subject inside this block is of type Int (actually 2)
        toBeGreaterThan(1)
        toBeLessThan(3)
    }

fails {
    // all expectations are evaluated inside an expectation group block; for more details:
    // https://github.com/robstoll/atrium#define-single-assertions-or-assertion-groups

    expect(list).get(0) {
        toBeGreaterThan(2) // fails
        toBeLessThan(0)    // still evaluated even though `toBeGreaterThan` already fails,
        //                    use `.get(index).` if you want a fail fast behaviour
    }
}


fails { // because index 3 is out of bound, but since we use a block...
    expect(list).get(3) {
        toBeLessThan(0) // ...reporting mentions that the element at index 3 was expected `to be less than: 0`
    }
}

Return
an Expect for the subject of this expectation.