Patrick Venture | caaebd1 | 2019-06-21 14:56:07 -0700 | [diff] [blame] | 1 | #include "filesystem_mock.hpp" |
| 2 | #include "hwmonio.hpp" |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <string> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace hwmonio |
| 11 | { |
| 12 | namespace |
| 13 | { |
| 14 | |
| 15 | using ::testing::_; |
| 16 | using ::testing::Return; |
| 17 | |
| 18 | class HwmonIOTest : public ::testing::Test |
| 19 | { |
| 20 | protected: |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 21 | HwmonIOTest() : _hwmonio(_path, &_mock) {} |
Patrick Venture | caaebd1 | 2019-06-21 14:56:07 -0700 | [diff] [blame] | 22 | |
| 23 | const int64_t _value = 12; |
| 24 | const std::string _path = "abcd"; |
| 25 | const std::string _type = "fan"; |
| 26 | const std::string _id = "a"; |
| 27 | const std::string _sensor = "1"; |
| 28 | const size_t _retries = 1; |
| 29 | const std::chrono::milliseconds _delay = std::chrono::milliseconds{10}; |
| 30 | |
| 31 | FileSystemMock _mock; |
| 32 | HwmonIO _hwmonio; |
| 33 | }; |
| 34 | |
| 35 | TEST_F(HwmonIOTest, ReadReturnsValue) |
| 36 | { |
| 37 | EXPECT_CALL(_mock, read(_)).WillOnce(Return(_value)); |
| 38 | EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value); |
| 39 | } |
| 40 | |
| 41 | int64_t SetErrnoExcept(const std::string&) |
| 42 | { |
| 43 | errno = ETIMEDOUT; |
| 44 | throw std::runtime_error("bad times"); |
| 45 | } |
| 46 | |
| 47 | TEST_F(HwmonIOTest, ReadExceptsRetryable) |
| 48 | { |
| 49 | EXPECT_CALL(_mock, read(_)) |
| 50 | .WillOnce(&SetErrnoExcept) |
| 51 | .WillOnce(Return(_value)); |
| 52 | EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value); |
| 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | } // namespace hwmonio |