next

fun <E, T : Iterator<E>> Expect<T>.next(): Expect<E>(source)

Expects that this expectation (an Iterator) has a next element and returns an Expect for the next element.

Return

The newly created Expect for the next element.

Since

1.3.0

Samples

val iterator = listOf(1, 2, 3).iterator()

expect(iterator)
    .next()                    // subject is now of type Int (actually 1)
    .toBeGreaterThan(0)
    .toBeLessThan(2)

expect(iterator)
    .next()                    // subject is now of type Int (actually 2)
    .toBeGreaterThan(1)
    .toBeLessThan(3)

fails {
    expect(iterator)
        .next()                // subject is now of type Int (actually 3)
        .notToEqual(3)         // fails as subject is exactly 3
}

fails {
    expect(iterator)
        .next()                // fails as list has only 3 elements
        .toEqual(4)            // not evaluated/reported because `next` already fails
    //                            use `.next { ... }` if you want that all expectations are evaluated
}

fun <E, T : Iterator<E>> Expect<T>.next(assertionCreator: Expect<E>.() -> Unit): Expect<T>(source)

Expects that this expectation (an Iterator) has a next element and that the next element holds all assertions the given assertionCreator creates for it.

Return

an Expect for the subject of the next expectation.

Since

1.3.0

Samples

val iterator = listOf(1, 2, 3).iterator()

expect(iterator)
    .next {                    // subject inside this expectation-group is of type Int (actually 1)
        toBeGreaterThan(0)
        toBeLessThan(2)
    }                          // subject here is back to type Iterator<Int>
    .next {                    // subject inside this expectation-group is of type Int (actually 2)
        toBeGreaterThan(1)
        toBeLessThan(3)
    }

fails {
    expect(iterator)
        .next {                // subject inside this expectation-group is of type Int (actually 3)
            notToEqual(3)      // fails as subject is exactly 3
        }
}

fails {
    expect(iterator).next {    // fails as list has only 3 elements
        toEqual(4)             // not evaluated/reported because `next` already fails
    //                            use `.next()` if you want a fail fast behaviour
    }
}