blob: 233bf56ee265c98f27b7fe33eb843773533dd238 [file] [log] [blame]
Patrick Venturecaaebd12019-06-21 14:56:07 -07001#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
10namespace hwmonio
11{
12namespace
13{
14
15using ::testing::_;
16using ::testing::Return;
17
18class 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
37TEST_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
43int64_t SetErrnoExcept(const std::string&)
44{
45 errno = ETIMEDOUT;
46 throw std::runtime_error("bad times");
47}
48
49TEST_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