xref: /openbmc/phosphor-hwmon/test/sensor_unittest.cpp (revision 0e2d68d2af9b4fbb67319fcfd41112d83e990986)
1*0e2d68d2SPatrick Venture #include "env_mock.hpp"
2*0e2d68d2SPatrick Venture #include "hwmonio_mock.hpp"
3*0e2d68d2SPatrick Venture #include "sensor.hpp"
4*0e2d68d2SPatrick Venture 
5*0e2d68d2SPatrick Venture #include <memory>
6*0e2d68d2SPatrick Venture #include <utility>
7*0e2d68d2SPatrick Venture 
8*0e2d68d2SPatrick Venture #include <gmock/gmock.h>
9*0e2d68d2SPatrick Venture #include <gtest/gtest.h>
10*0e2d68d2SPatrick Venture 
11*0e2d68d2SPatrick Venture class SensorTest : public ::testing::Test
12*0e2d68d2SPatrick Venture {
13*0e2d68d2SPatrick Venture   protected:
14*0e2d68d2SPatrick Venture     void SetUp() override
15*0e2d68d2SPatrick Venture     {
16*0e2d68d2SPatrick Venture         envIntf = nullptr;
17*0e2d68d2SPatrick Venture     }
18*0e2d68d2SPatrick Venture };
19*0e2d68d2SPatrick Venture 
20*0e2d68d2SPatrick Venture using ::testing::Eq;
21*0e2d68d2SPatrick Venture using ::testing::Return;
22*0e2d68d2SPatrick Venture using ::testing::StrictMock;
23*0e2d68d2SPatrick Venture 
24*0e2d68d2SPatrick Venture TEST_F(SensorTest, BasicConstructorTest)
25*0e2d68d2SPatrick Venture {
26*0e2d68d2SPatrick Venture     /* Constructor test with nothing in an rcList or GPIO chip. */
27*0e2d68d2SPatrick Venture 
28*0e2d68d2SPatrick Venture     StrictMock<EnvMock> eMock;
29*0e2d68d2SPatrick Venture     envIntf = &eMock;
30*0e2d68d2SPatrick Venture 
31*0e2d68d2SPatrick Venture     auto sensorKey = std::make_pair(std::string("temp"), std::string("5"));
32*0e2d68d2SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
33*0e2d68d2SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
34*0e2d68d2SPatrick Venture     std::string path = "/";
35*0e2d68d2SPatrick Venture 
36*0e2d68d2SPatrick Venture     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
37*0e2d68d2SPatrick Venture     EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey)))
38*0e2d68d2SPatrick Venture         .WillOnce(Return(""));
39*0e2d68d2SPatrick Venture     EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return(""));
40*0e2d68d2SPatrick Venture 
41*0e2d68d2SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
42*0e2d68d2SPatrick Venture     EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return(""));
43*0e2d68d2SPatrick Venture     EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey)))
44*0e2d68d2SPatrick Venture         .WillOnce(Return(""));
45*0e2d68d2SPatrick Venture     EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey)))
46*0e2d68d2SPatrick Venture         .WillOnce(Return(""));
47*0e2d68d2SPatrick Venture 
48*0e2d68d2SPatrick Venture     auto sensor =
49*0e2d68d2SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
50*0e2d68d2SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
51*0e2d68d2SPatrick Venture }
52