blob: 25bad756952b3c526f26b9ae27e61684f4f049fb [file] [log] [blame]
William A. Kennington IIIbd1e0352023-12-22 16:44:55 -08001#include "util.hpp"
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
William A. Kennington III9a512c92022-08-12 15:15:55 -07003#include <stdplus/exception.hpp>
4#include <stdplus/fd/gmock.hpp>
5#include <stdplus/fd/line.hpp>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05006
William A. Kennington IIIbd1e0352023-12-22 16:44:55 -08007#include <string>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05008
9#include <gtest/gtest.h>
William A. Kennington III9a512c92022-08-12 15:15:55 -070010
11namespace stdplus
12{
13namespace fd
14{
15
William A. Kennington III9a512c92022-08-12 15:15:55 -070016TEST(LineReader, Empty)
17{
18 auto fd = makeMemfd("");
19 LineReader reader(fd);
20 EXPECT_EQ("", *reader.readLine());
21 EXPECT_THROW(reader.readLine(), exception::Eof);
22 EXPECT_THROW(reader.readLine(), exception::Eof);
23}
24
25TEST(LineReader, SingleLine)
26{
27 auto fd = makeMemfd("\n");
28 LineReader reader(fd);
29 EXPECT_EQ("", *reader.readLine());
30 EXPECT_EQ("", *reader.readLine());
31 EXPECT_THROW(reader.readLine(), exception::Eof);
32 EXPECT_THROW(reader.readLine(), exception::Eof);
33}
34
35TEST(LineReader, SomeData)
36{
37 auto fd = makeMemfd("A\nbcd\n\ne");
38 LineReader reader(fd);
39 EXPECT_EQ("A", *reader.readLine());
40 EXPECT_EQ("bcd", *reader.readLine());
41 EXPECT_EQ("", *reader.readLine());
42 EXPECT_EQ("e", *reader.readLine());
43 EXPECT_THROW(reader.readLine(), exception::Eof);
44 EXPECT_THROW(reader.readLine(), exception::Eof);
45}
46
47TEST(LineReader, LargerThanBuf)
48{
49 std::string big(LineReader::buf_size + 10, 'a');
50 auto fd = makeMemfd(std::string("alpha\n") + big + "\ndef");
51 LineReader reader(fd);
52 EXPECT_EQ("alpha", *reader.readLine());
53 EXPECT_EQ(big, *reader.readLine());
54 EXPECT_EQ("def", *reader.readLine());
55 EXPECT_THROW(reader.readLine(), exception::Eof);
56 EXPECT_THROW(reader.readLine(), exception::Eof);
57}
58
59using testing::_;
60
William A. Kennington III9a512c92022-08-12 15:15:55 -070061TEST(LineReader, Nonblock)
62{
63 FdMock fd;
64 {
65 testing::InSequence seq;
66 EXPECT_CALL(fd, read(_)).WillOnce(readSv("alph"));
67 EXPECT_CALL(fd, read(_)).WillOnce(readSv({}));
68 EXPECT_CALL(fd, read(_)).WillOnce(readSv("a"));
69 EXPECT_CALL(fd, read(_))
70 .WillRepeatedly(testing::Throw(stdplus::exception::Eof("test")));
71 }
72
73 LineReader reader(fd);
74 EXPECT_EQ(nullptr, reader.readLine());
75 EXPECT_EQ("alpha", *reader.readLine());
76 EXPECT_THROW(reader.readLine(), exception::Eof);
77 EXPECT_THROW(reader.readLine(), exception::Eof);
78}
79
80} // namespace fd
81} // namespace stdplus