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