| 1 |
use futures::executor::block_on; |
| 2 |
use futures::future::{Future, FutureExt}; |
| 3 |
use futures::io::{AsyncBufReadExt, Cursor}; |
| 4 |
use futures::stream::{self, StreamExt, TryStreamExt}; |
| 5 |
use futures::task::Poll; |
| 6 |
use futures_test::io::AsyncReadTestExt; |
| 7 |
use futures_test::task::noop_context; |
| 8 |
|
| 9 |
fn 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 |
macro_rules! block_on_next { |
| 19 |
($expr:expr) => { |
| 20 |
block_on($expr.next()).unwrap().unwrap() |
| 21 |
}; |
| 22 |
} |
| 23 |
|
| 24 |
macro_rules! run_next { |
| 25 |
($expr:expr) => { |
| 26 |
run($expr.next()).unwrap().unwrap() |
| 27 |
}; |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn 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] |
| 45 |
fn 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 |
} |