summaryrefslogtreecommitdiffstats
path: root/tokio/tests/io_read_line.rs
blob: 15841c9b49d2afa2dae4338f6585e8d5e05c3764 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]

use std::io::ErrorKind;
use tokio::io::{AsyncBufReadExt, BufReader, Error};
use tokio_test::{assert_ok, io::Builder};

use std::io::Cursor;

#[tokio::test]
async fn read_line() {
    let mut buf = String::new();
    let mut rd = Cursor::new(b"hello\nworld\n\n");

    let n = assert_ok!(rd.read_line(&mut buf).await);
    assert_eq!(n, 6);
    assert_eq!(buf, "hello\n");
    buf.clear();
    let n = assert_ok!(rd.read_line(&mut buf).await);
    assert_eq!(n, 6);
    assert_eq!(buf, "world\n");
    buf.clear();
    let n = assert_ok!(rd.read_line(&mut buf).await);
    assert_eq!(n, 1);
    assert_eq!(buf, "\n");
    buf.clear();
    let n = assert_ok!(rd.read_line(&mut buf).await);
    assert_eq!(n, 0);
    assert_eq!(buf, "");
}

#[tokio::test]
async fn read_line_not_all_ready() {
    let mock = Builder::new()
        .read(b"Hello Wor")
        .read(b"ld\nFizzBuz")
        .read(b"z\n1\n2")
        .build();

    let mut read = BufReader::new(mock);

    let mut line = "We say ".to_string();
    let bytes = read.read_line(&mut line).await.unwrap();
    assert_eq!(bytes, "Hello World\n".len());
    assert_eq!(line.as_str(), "We say Hello World\n");

    line = "I solve ".to_string();
    let bytes = read.read_line(&mut line).await.unwrap();
    assert_eq!(bytes, "FizzBuzz\n".len());
    assert_eq!(line.as_str(), "I solve FizzBuzz\n");

    line.clear();
    let bytes = read.read_line(&mut line).await.unwrap();
    assert_eq!(bytes, 2);
    assert_eq!(line.as_str(), "1\n");

    line.clear();
    let bytes = read.read_line(&mut line).await.unwrap();
    assert_eq!(bytes, 1);
    assert_eq!(line.as_str(), "2");
}

#[tokio::test]
async fn read_line_invalid_utf8() {
    let mock = Builder::new().read(b"Hello Wor\xffld.\n").build();

    let mut read = BufReader::new(mock);

    let mut line = "Foo".to_string();
    let err = read.read_line(&mut line).await.expect_err("Should fail");
    assert_eq!(err.kind(), ErrorKind::InvalidData);
    assert_eq!(err.to_string(), "stream did not contain valid UTF-8");
    assert_eq!(line.as_str(), "Foo");
}

#[tokio::test]
async fn read_line_fail() {
    let mock = Builder::new()
        .read(b"Hello Wor")
        .read_error(Error::new(ErrorKind::Other, "The world has no end"))
        .build();

    let mut read = BufReader::new(mock);

    let mut line = "Foo".to_string();
    let err = read.read_line(&mut line).await.expect_err("Should fail");
    assert_eq!(err.kind(), ErrorKind::Other);
    assert_eq!(err.to_string(), "The world has no end");
    assert_eq!(line.as_str(), "FooHello Wor");
}

#[tokio::test]
async fn read_line_fail_and_utf8_fail() {
    let mock = Builder::new()
        .read(b"Hello Wor")
        .read(b"\xff\xff\xff")
        .read_error(Error::new(ErrorKind::Other, "The world has no end"))
        .build();

    let mut read = BufReader::new(mock);

    let mut line = "Foo".to_string();
    let err = read.read_line(&mut line).await.expect_err("Should fail");
    assert_eq!(err.kind(), ErrorKind::Other);
    assert_eq!(err.to_string(), "The world has no end");
    assert_eq!(line.as_str(), "Foo");
}