1#![warn(rust_2018_idioms)]
2#![cfg(feature = "full")]
3
4use std::io::ErrorKind;
5use tokio::io::{AsyncBufReadExt, BufReader, Error};
6use tokio_test::{assert_ok, io::Builder};
7
8#[tokio::test]
9async fn read_until() {
10 let mut buf = vec![];
11 let mut rd: &[u8] = b"hello world";
12
13 let n = assert_ok!(rd.read_until(b' ', &mut buf).await);
14 assert_eq!(n, 6);
15 assert_eq!(buf, b"hello ");
16 buf.clear();
17 let n = assert_ok!(rd.read_until(b' ', &mut buf).await);
18 assert_eq!(n, 5);
19 assert_eq!(buf, b"world");
20 buf.clear();
21 let n = assert_ok!(rd.read_until(b' ', &mut buf).await);
22 assert_eq!(n, 0);
23 assert_eq!(buf, []);
24}
25
26#[tokio::test]
27async fn read_until_not_all_ready() {
28 let mock = Builder::new()
29 .read(b"Hello Wor")
30 .read(b"ld#Fizz\xffBuz")
31 .read(b"z#1#2")
32 .build();
33
34 let mut read = BufReader::new(mock);
35
36 let mut chunk = b"We say ".to_vec();
37 let bytes = read.read_until(b'#', &mut chunk).await.unwrap();
38 assert_eq!(bytes, b"Hello World#".len());
39 assert_eq!(chunk, b"We say Hello World#");
40
41 chunk = b"I solve ".to_vec();
42 let bytes = read.read_until(b'#', &mut chunk).await.unwrap();
43 assert_eq!(bytes, b"Fizz\xffBuzz\n".len());
44 assert_eq!(chunk, b"I solve Fizz\xffBuzz#");
45
46 chunk.clear();
47 let bytes = read.read_until(b'#', &mut chunk).await.unwrap();
48 assert_eq!(bytes, 2);
49 assert_eq!(chunk, b"1#");
50
51 chunk.clear();
52 let bytes = read.read_until(b'#', &mut chunk).await.unwrap();
53 assert_eq!(bytes, 1);
54 assert_eq!(chunk, b"2");
55}
56
57#[tokio::test]
58async fn read_until_fail() {
59 let mock = Builder::new()
60 .read(b"Hello \xffWor")
61 .read_error(Error::new(ErrorKind::Other, "The world has no end"))
62 .build();
63
64 let mut read = BufReader::new(mock);
65
66 let mut chunk = b"Foo".to_vec();
67 let err = read
68 .read_until(b'#', &mut chunk)
69 .await
70 .expect_err("Should fail");
71 assert_eq!(err.kind(), ErrorKind::Other);
72 assert_eq!(err.to_string(), "The world has no end");
73 assert_eq!(chunk, b"FooHello \xffWor");
74}
75