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
18#[test]
19fn read_line() {
20 let mut buf = Cursor::new(b"12");
21 let mut v = String::new();
22 assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 2);
23 assert_eq!(v, "12");
24
25 let mut buf = Cursor::new(b"12\n\n");
26 let mut v = String::new();
27 assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 3);
28 assert_eq!(v, "12\n");
29 v.clear();
30 assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 1);
31 assert_eq!(v, "\n");
32 v.clear();
33 assert_eq!(block_on(buf.read_line(&mut v)).unwrap(), 0);
34 assert_eq!(v, "");
35}
36
37#[test]
38fn maybe_pending() {
39 let mut buf = b"12".interleave_pending();
40 let mut v = String::new();
41 assert_eq!(run(buf.read_line(&mut v)).unwrap(), 2);
42 assert_eq!(v, "12");
43
44 let mut buf =
45 stream::iter(vec![&b"12"[..], &b"\n\n"[..]]).map(Ok).into_async_read().interleave_pending();
46 let mut v = String::new();
47 assert_eq!(run(buf.read_line(&mut v)).unwrap(), 3);
48 assert_eq!(v, "12\n");
49 v.clear();
50 assert_eq!(run(buf.read_line(&mut v)).unwrap(), 1);
51 assert_eq!(v, "\n");
52 v.clear();
53 assert_eq!(run(buf.read_line(&mut v)).unwrap(), 0);
54 assert_eq!(v, "");
55 v.clear();
56 assert_eq!(run(buf.read_line(&mut v)).unwrap(), 0);
57 assert_eq!(v, "");
58}
59