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::Invoke;
41 using ::testing::Pair;
42 using ::testing::Return;
43 using ::testing::StrEq;
44 using ::testing::StrictMock;
45 
TEST_F(SensorTest,BasicConstructorTest)46 TEST_F(SensorTest, BasicConstructorTest)
47 {
48     /* Constructor test with nothing in an rcList or GPIO chip. */
49     auto sensorKey = std::make_pair(temp, five);
50     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
51         std::make_unique<hwmonio::HwmonIOMock>();
52     std::string path = "/";
53 
54     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
55     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
56         .WillOnce(Return(""));
57     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
58 
59     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
60     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
61     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
62     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
63         .WillOnce(Return(""));
64 
65     auto sensor =
66         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
67     EXPECT_FALSE(sensor == nullptr);
68 }
69 
TEST_F(SensorTest,SensorRequiresGpio)70 TEST_F(SensorTest, SensorRequiresGpio)
71 {
72     /* Constructor test with only the GPIO chip set. */
73 
74     StrictMock<GpioHandleMock> gMock;
75     gpioIntf = &gMock;
76 
77     /* The following piece of code can probably be copied above once it's
78      * working.
79      */
80     auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
81 
82     auto sensorKey = std::make_pair(temp, five);
83     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
84         std::make_unique<hwmonio::HwmonIOMock>();
85     std::string path = "/";
86 
87     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
88         .WillOnce(Return("chipA"));
89     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
90 
91     EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
92         .WillOnce(Invoke([&](const std::string&, const std::string&) {
93             return std::move(handleMock);
94         }));
95 
96     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
97     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
98     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
99     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
100         .WillOnce(Return(""));
101 
102     auto sensor =
103         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
104     EXPECT_FALSE(sensor == nullptr);
105 }
106 
TEST_F(SensorTest,SensorHasGainAndOffsetAdjustValue)107 TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue)
108 {
109     /* Construct a sensor that has a gain and offset, then verify they are used
110      * when adjusting the value.
111      */
112 
113     auto sensorKey = std::make_pair(temp, five);
114     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
115         std::make_unique<hwmonio::HwmonIOMock>();
116     std::string path = "/";
117 
118     /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
119     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
120         .WillOnce(Return(""));
121     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
122 
123     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
124     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
125         .WillOnce(Return("15"));
126     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
127         .WillOnce(Return(""));
128 
129     auto sensor =
130         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
131     EXPECT_FALSE(sensor == nullptr);
132 
133     double startingValue = 1.0;
134     double resultValue = sensor->adjustValue(startingValue);
135     EXPECT_DOUBLE_EQ(resultValue, 25.0);
136 }
137