1use futures::executor::block_on;
2use futures::future::{Future, FutureExt};
3use futures::io::{AsyncBufReadExt, Cursor};
4use futures::stream::{self, StreamExt, TryStreamExt};
5use futures::task::Poll;
6use futures_test::io::AsyncReadTestExt;
7use futures_test::task::noop_context;
8
9fn run<F: Future + Unpin>(mut f: F) -> F::Output {
10 let mut cx = noop_context();
11 loop {
12 if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
13 return x;
14 }
15 }
16}
17
18macro_rules! block_on_next {
19 ($expr:expr) => {
20 block_on($expr.next()).unwrap().unwrap()
21 };
22}
23
24macro_rules! run_next {
25 ($expr:expr) => {
26 run($expr.next()).unwrap().unwrap()
27 };
28}
29
30#[test]
31fn lines() {
32 let buf = Cursor::new(&b"12\r"[..]);
33 let mut s = buf.lines();
34 assert_eq!(block_on_next!(s), "12\r".to_string());
35 assert!(block_on(s.next()).is_none());
36
37 let buf = Cursor::new(&b"12\r\n\n"[..]);
38 let mut s = buf.lines();
39 assert_eq!(block_on_next!(s), "12".to_string());
40 assert_eq!(block_on_next!(s), "".to_string());
41 assert!(block_on(s.next()).is_none());
42}
43
44#[test]
45fn maybe_pending() {
46 let buf =
47 stream::iter(vec![&b"12"[..], &b"\r"[..]]).map(Ok).into_async_read().interleave_pending();
48 let mut s = buf.lines();
49 assert_eq!(run_next!(s), "12\r".to_string());
50 assert!(run(s.next()).is_none());
51
52 let buf = stream::iter(vec![&b"12"[..], &b"\r\n"[..], &b"\n"[..]])
53 .map(Ok)
54 .into_async_read()
55 .interleave_pending();
56 let mut s = buf.lines();
57 assert_eq!(run_next!(s), "12".to_string());
58 assert_eq!(run_next!(s), "".to_string());
59 assert!(run(s.next()).is_none());
60}
61