last

fun <E : Comparable<E>, T : Iterable<E>> Expect<T>.last(assertionCreator: Expect<E>.() -> Unit): Expect<T>(source)

Expects that the subject of this expectation (an Iterable) is not empty and that the last element in it holds all assertions the given assertionCreator creates for it.

Return

an Expect for the subject of this expectation.

Since

1.2.0

Samples

val iterable = sequenceOf(1, 2, 3).asIterable()

expect(iterable)
    .last {
        toEqual(3) // subject is 3
    }
    .last { // subject is 3
        toBeGreaterThan(2) // subject is still 3
        toBeLessThan(4) // subject is still 3
    }

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

    expect(iterable).last { // subject within this expectation-group is of type Int (actually 3)
        toBeGreaterThan(3)   // fails
        toBeLessThan (4)     // still evaluated, even though  `toBeGreaterThan` already fails,
        //                      use `.last.` if you want a fail fast behaviour
    } // subject here is back to type Iterable<Int>
}

fails {
    expect(emptyList<Int>()).last {
        toEqual(3) // fails, because list is empty
    }
}

Expects that Iterable is not empty and returns an Expect for the last element in it.

Return

The newly created Expect for last element.

Since

1.2.0

Samples

val iterable = sequenceOf(1, 2, 3).asIterable()

expect(iterable).last.toEqual(3) // subject is 3

expect(iterable).last // Subject is 3
    .toBeGreaterThan(2) // subject is still 3
    .toBeLessThan(4) // subject is still 3

fails {
    expect(iterable).last.toBeGreaterThan(3).toBeLessThan (4) // subject is 3, fails on first expectation, second is skipped
}

fails {
    expect(emptyList<Int>()).last.toEqual(3) // fails, because list is empty
}