summaryrefslogtreecommitdiffstats
path: root/vendor/futures/tests/stream_unfold.rs
blob: 16b10813b131483afe1042b0be624c672522faff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use futures::future;
use futures::stream;
use futures_test::future::FutureTestExt;
use futures_test::{assert_stream_done, assert_stream_next, assert_stream_pending};

#[test]
fn unfold1() {
    let mut stream = stream::unfold(0, |state| {
        if state <= 2 {
            future::ready(Some((state * 2, state + 1))).pending_once()
        } else {
            future::ready(None).pending_once()
        }
    });

    // Creates the future with the closure
    // Not ready (delayed future)
    assert_stream_pending!(stream);
    // Future is ready, yields the item
    assert_stream_next!(stream, 0);

    // Repeat
    assert_stream_pending!(stream);
    assert_stream_next!(stream, 2);

    assert_stream_pending!(stream);
    assert_stream_next!(stream, 4);

    // No more items
    assert_stream_pending!(stream);
    assert_stream_done!(stream);
}