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