1 #include "env_mock.hpp"
2 #include "gpio_mock.hpp"
3 #include "hwmonio_mock.hpp"
4 #include "sensor.hpp"
5
6 #include <gpioplus/test/handle.hpp>
7
8 #include <memory>
9 #include <utility>
10
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13
14 namespace env
15 {
16
17 // Delegate all calls to getEnv() to the mock
get(const char * key) const18 const char* EnvImpl::get(const char* key) const
19 {
20 return mockEnv.get(key);
21 }
22
23 EnvImpl env_impl;
24
25 } // namespace env
26
27 class SensorTest : public ::testing::Test
28 {
29 protected:
SetUp()30 void SetUp() override
31 {
32 gpioIntf = nullptr;
33 }
34
35 std::string temp = "temp";
36 std::string five = "5";
37 };
38
39 using ::testing::Eq;
40 using ::testing::Pair;
41 using ::testing::Return;
42 using ::testing::StrEq;
43 using ::testing::StrictMock;
44
TEST_F(SensorTest,BasicConstructorTest)45 TEST_F(SensorTest, BasicConstructorTest)
46 {
47 /* Constructor test with nothing in an rcList or GPIO chip. */
48 auto sensorKey = std::make_pair(temp, five);
49 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
50 std::make_unique<hwmonio::HwmonIOMock>();
51 std::string path = "/";
52
53 /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
54 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
55 .WillOnce(Return(""));
56 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
57
58 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
59 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
60 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
61 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
62 .WillOnce(Return(""));
63
64 auto sensor =
65 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
66 EXPECT_FALSE(sensor == nullptr);
67 }
68
TEST_F(SensorTest,SensorRequiresGpio)69 TEST_F(SensorTest, SensorRequiresGpio)
70 {
71 /* Constructor test with only the GPIO chip set. */
72
73 StrictMock<GpioHandleMock> gMock;
74 gpioIntf = &gMock;
75
76 /* The following piece of code can probably be copied above once it's
77 * working.
78 */
79 auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
80
81 auto sensorKey = std::make_pair(temp, five);
82 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
83 std::make_unique<hwmonio::HwmonIOMock>();
84 std::string path = "/";
85
86 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
87 .WillOnce(Return("chipA"));
88 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
89
90 EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
91 .WillOnce([&](const std::string&, const std::string&) {
92 return std::move(handleMock);
93 });
94
95 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
96 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
97 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
98 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
99 .WillOnce(Return(""));
100
101 auto sensor =
102 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
103 EXPECT_FALSE(sensor == nullptr);
104 }
105
TEST_F(SensorTest,SensorHasGainAndOffsetAdjustValue)106 TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue)
107 {
108 /* Construct a sensor that has a gain and offset, then verify they are used
109 * when adjusting the value.
110 */
111
112 auto sensorKey = std::make_pair(temp, five);
113 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
114 std::make_unique<hwmonio::HwmonIOMock>();
115 std::string path = "/";
116
117 /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
118 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
119 .WillOnce(Return(""));
120 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
121
122 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
123 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
124 .WillOnce(Return("15"));
125 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
126 .WillOnce(Return(""));
127
128 auto sensor =
129 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
130 EXPECT_FALSE(sensor == nullptr);
131
132 double startingValue = 1.0;
133 double resultValue = sensor->adjustValue(startingValue);
134 EXPECT_DOUBLE_EQ(resultValue, 25.0);
135 }
136