William A. Kennington III | bd1e035 | 2023-12-22 16:44:55 -0800 | [diff] [blame] | 1 | #include "util.hpp" |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 2 | |
William A. Kennington III | 9a512c9 | 2022-08-12 15:15:55 -0700 | [diff] [blame] | 3 | #include <stdplus/exception.hpp> |
| 4 | #include <stdplus/fd/gmock.hpp> |
| 5 | #include <stdplus/fd/line.hpp> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 6 | |
William A. Kennington III | bd1e035 | 2023-12-22 16:44:55 -0800 | [diff] [blame] | 7 | #include <string> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 8 | |
| 9 | #include <gtest/gtest.h> |
William A. Kennington III | 9a512c9 | 2022-08-12 15:15:55 -0700 | [diff] [blame] | 10 | |
| 11 | namespace stdplus |
| 12 | { |
| 13 | namespace fd |
| 14 | { |
| 15 | |
William A. Kennington III | 9a512c9 | 2022-08-12 15:15:55 -0700 | [diff] [blame] | 16 | TEST(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 | |
| 25 | TEST(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 | |
| 35 | TEST(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 | |
| 47 | TEST(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 | |
| 59 | using testing::_; |
| 60 | |
William A. Kennington III | 9a512c9 | 2022-08-12 15:15:55 -0700 | [diff] [blame] | 61 | TEST(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 |