1 | use futures::future; |
2 | use futures::stream; |
3 | use futures_test::future::FutureTestExt; |
4 | use futures_test::{assert_stream_done, assert_stream_next, assert_stream_pending}; |
5 | |
6 | #[test] |
7 | fn unfold1() { |
8 | let mut stream = stream::unfold(0, |state| { |
9 | if state <= 2 { |
10 | future::ready(Some((state * 2, state + 1))).pending_once() |
11 | } else { |
12 | future::ready(None).pending_once() |
13 | } |
14 | }); |
15 | |
16 | // Creates the future with the closure |
17 | // Not ready (delayed future) |
18 | assert_stream_pending!(stream); |
19 | // Future is ready, yields the item |
20 | assert_stream_next!(stream, 0); |
21 | |
22 | // Repeat |
23 | assert_stream_pending!(stream); |
24 | assert_stream_next!(stream, 2); |
25 | |
26 | assert_stream_pending!(stream); |
27 | assert_stream_next!(stream, 4); |
28 | |
29 | // No more items |
30 | assert_stream_pending!(stream); |
31 | assert_stream_done!(stream); |
32 | } |
33 | |