last

infix fun <E : Comparable<E>, T : Iterable<E>> Expect<T>.last(o: o): Expect<E>(source)

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

Parameters

o

The filler object o.

Samples

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

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

expect(iterable) last o toBeGreaterThan(2) toBeLessThan(4) // subject is 3 and passes all expectations

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

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

infix 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 = listOf(1, 2, 3).asIterable()

expect(iterable) last {
    it toEqual 3 // subject is 3
} last {
    it toBeGreaterThan(2)
    it toBeLessThan(4) // subject is 3 and passes all expectations
}

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)
        it toBeGreaterThan (3)  // fails
        it toBeLessThan (4)     // still evaluated, even though  `toBeGreaterThan` already fails,
        //                      use `.last.` if you want a fail fast behaviour
    } // subject here is back type Iterable<Int>
}

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