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:
21     HwmonIOTest() : _hwmonio(_path, &_mock)
22     {
23     }
24 
25     const int64_t _value = 12;
26     const std::string _path = "abcd";
27     const std::string _type = "fan";
28     const std::string _id = "a";
29     const std::string _sensor = "1";
30     const size_t _retries = 1;
31     const std::chrono::milliseconds _delay = std::chrono::milliseconds{10};
32 
33     FileSystemMock _mock;
34     HwmonIO _hwmonio;
35 };
36 
37 TEST_F(HwmonIOTest, ReadReturnsValue)
38 {
39     EXPECT_CALL(_mock, read(_)).WillOnce(Return(_value));
40     EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value);
41 }
42 
43 int64_t SetErrnoExcept(const std::string&)
44 {
45     errno = ETIMEDOUT;
46     throw std::runtime_error("bad times");
47 }
48 
49 TEST_F(HwmonIOTest, ReadExceptsRetryable)
50 {
51     EXPECT_CALL(_mock, read(_))
52         .WillOnce(&SetErrnoExcept)
53         .WillOnce(Return(_value));
54     EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value);
55 }
56 
57 } // namespace
58 } // namespace hwmonio
59