1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3 
4 use std::io::ErrorKind;
5 use tokio::io::{AsyncBufReadExt, BufReader, Error};
6 use tokio_test::{assert_ok, io::Builder};
7 
8 use std::io::Cursor;
9 
10 #[tokio::test]
read_line()11 async fn read_line() {
12     let mut buf = String::new();
13     let mut rd = Cursor::new(b"hello\nworld\n\n");
14 
15     let n = assert_ok!(rd.read_line(&mut buf).await);
16     assert_eq!(n, 6);
17     assert_eq!(buf, "hello\n");
18     buf.clear();
19     let n = assert_ok!(rd.read_line(&mut buf).await);
20     assert_eq!(n, 6);
21     assert_eq!(buf, "world\n");
22     buf.clear();
23     let n = assert_ok!(rd.read_line(&mut buf).await);
24     assert_eq!(n, 1);
25     assert_eq!(buf, "\n");
26     buf.clear();
27     let n = assert_ok!(rd.read_line(&mut buf).await);
28     assert_eq!(n, 0);
29     assert_eq!(buf, "");
30 }
31 
32 #[tokio::test]
read_line_not_all_ready()33 async fn read_line_not_all_ready() {
34     let mock = Builder::new()
35         .read(b"Hello Wor")
36         .read(b"ld\nFizzBuz")
37         .read(b"z\n1\n2")
38         .build();
39 
40     let mut read = BufReader::new(mock);
41 
42     let mut line = "We say ".to_string();
43     let bytes = read.read_line(&mut line).await.unwrap();
44     assert_eq!(bytes, "Hello World\n".len());
45     assert_eq!(line.as_str(), "We say Hello World\n");
46 
47     line = "I solve ".to_string();
48     let bytes = read.read_line(&mut line).await.unwrap();
49     assert_eq!(bytes, "FizzBuzz\n".len());
50     assert_eq!(line.as_str(), "I solve FizzBuzz\n");
51 
52     line.clear();
53     let bytes = read.read_line(&mut line).await.unwrap();
54     assert_eq!(bytes, 2);
55     assert_eq!(line.as_str(), "1\n");
56 
57     line.clear();
58     let bytes = read.read_line(&mut line).await.unwrap();
59     assert_eq!(bytes, 1);
60     assert_eq!(line.as_str(), "2");
61 }
62 
63 #[tokio::test]
read_line_invalid_utf8()64 async fn read_line_invalid_utf8() {
65     let mock = Builder::new().read(b"Hello Wor\xffld.\n").build();
66 
67     let mut read = BufReader::new(mock);
68 
69     let mut line = "Foo".to_string();
70     let err = read.read_line(&mut line).await.expect_err("Should fail");
71     assert_eq!(err.kind(), ErrorKind::InvalidData);
72     assert_eq!(err.to_string(), "stream did not contain valid UTF-8");
73     assert_eq!(line.as_str(), "Foo");
74 }
75 
76 #[tokio::test]
read_line_fail()77 async fn read_line_fail() {
78     let mock = Builder::new()
79         .read(b"Hello Wor")
80         .read_error(Error::new(ErrorKind::Other, "The world has no end"))
81         .build();
82 
83     let mut read = BufReader::new(mock);
84 
85     let mut line = "Foo".to_string();
86     let err = read.read_line(&mut line).await.expect_err("Should fail");
87     assert_eq!(err.kind(), ErrorKind::Other);
88     assert_eq!(err.to_string(), "The world has no end");
89     assert_eq!(line.as_str(), "FooHello Wor");
90 }
91 
92 #[tokio::test]
read_line_fail_and_utf8_fail()93 async fn read_line_fail_and_utf8_fail() {
94     let mock = Builder::new()
95         .read(b"Hello Wor")
96         .read(b"\xff\xff\xff")
97         .read_error(Error::new(ErrorKind::Other, "The world has no end"))
98         .build();
99 
100     let mut read = BufReader::new(mock);
101 
102     let mut line = "Foo".to_string();
103     let err = read.read_line(&mut line).await.expect_err("Should fail");
104     assert_eq!(err.kind(), ErrorKind::Other);
105     assert_eq!(err.to_string(), "The world has no end");
106     assert_eq!(line.as_str(), "Foo");
107 }
108