10e2d68d2SPatrick Venture #include "env_mock.hpp" 299b95815SPatrick Venture #include "gpio_mock.hpp" 30e2d68d2SPatrick Venture #include "hwmonio_mock.hpp" 40e2d68d2SPatrick Venture #include "sensor.hpp" 50e2d68d2SPatrick Venture 699b95815SPatrick Venture #include <gpioplus/test/handle.hpp> 70e2d68d2SPatrick Venture #include <memory> 80e2d68d2SPatrick Venture #include <utility> 90e2d68d2SPatrick Venture 100e2d68d2SPatrick Venture #include <gmock/gmock.h> 110e2d68d2SPatrick Venture #include <gtest/gtest.h> 120e2d68d2SPatrick Venture 130e2d68d2SPatrick Venture class SensorTest : public ::testing::Test 140e2d68d2SPatrick Venture { 150e2d68d2SPatrick Venture protected: 160e2d68d2SPatrick Venture void SetUp() override 170e2d68d2SPatrick Venture { 180e2d68d2SPatrick Venture envIntf = nullptr; 1999b95815SPatrick Venture gpioIntf = nullptr; 200e2d68d2SPatrick Venture } 210e2d68d2SPatrick Venture }; 220e2d68d2SPatrick Venture 230e2d68d2SPatrick Venture using ::testing::Eq; 2499b95815SPatrick Venture using ::testing::Invoke; 250e2d68d2SPatrick Venture using ::testing::Return; 260e2d68d2SPatrick Venture using ::testing::StrictMock; 270e2d68d2SPatrick Venture 280e2d68d2SPatrick Venture TEST_F(SensorTest, BasicConstructorTest) 290e2d68d2SPatrick Venture { 300e2d68d2SPatrick Venture /* Constructor test with nothing in an rcList or GPIO chip. */ 310e2d68d2SPatrick Venture 320e2d68d2SPatrick Venture StrictMock<EnvMock> eMock; 330e2d68d2SPatrick Venture envIntf = &eMock; 340e2d68d2SPatrick Venture 350e2d68d2SPatrick Venture auto sensorKey = std::make_pair(std::string("temp"), std::string("5")); 360e2d68d2SPatrick Venture std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = 370e2d68d2SPatrick Venture std::make_unique<hwmonio::HwmonIOMock>(); 380e2d68d2SPatrick Venture std::string path = "/"; 390e2d68d2SPatrick Venture 400e2d68d2SPatrick Venture /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ 410e2d68d2SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey))) 420e2d68d2SPatrick Venture .WillOnce(Return("")); 430e2d68d2SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("")); 440e2d68d2SPatrick Venture 450e2d68d2SPatrick Venture /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ 460e2d68d2SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return("")); 470e2d68d2SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey))) 480e2d68d2SPatrick Venture .WillOnce(Return("")); 490e2d68d2SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey))) 500e2d68d2SPatrick Venture .WillOnce(Return("")); 510e2d68d2SPatrick Venture 520e2d68d2SPatrick Venture auto sensor = 530e2d68d2SPatrick Venture std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); 540e2d68d2SPatrick Venture EXPECT_FALSE(sensor == nullptr); 550e2d68d2SPatrick Venture } 5699b95815SPatrick Venture 5799b95815SPatrick Venture TEST_F(SensorTest, SensorRequiresGpio) 5899b95815SPatrick Venture { 5999b95815SPatrick Venture /* Constructor test with only the GPIO chip set. */ 6099b95815SPatrick Venture 6199b95815SPatrick Venture StrictMock<EnvMock> eMock; 6299b95815SPatrick Venture envIntf = &eMock; 6399b95815SPatrick Venture 6499b95815SPatrick Venture StrictMock<GpioHandleMock> gMock; 6599b95815SPatrick Venture gpioIntf = &gMock; 6699b95815SPatrick Venture 6799b95815SPatrick Venture /* The following piece of code can probably be copied above once it's 6899b95815SPatrick Venture * working. 6999b95815SPatrick Venture */ 7099b95815SPatrick Venture auto handleMock = std::make_unique<gpioplus::test::HandleMock>(); 7199b95815SPatrick Venture 7299b95815SPatrick Venture auto sensorKey = std::make_pair(std::string("temp"), std::string("5")); 7399b95815SPatrick Venture std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = 7499b95815SPatrick Venture std::make_unique<hwmonio::HwmonIOMock>(); 7599b95815SPatrick Venture std::string path = "/"; 7699b95815SPatrick Venture 7799b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey))) 7899b95815SPatrick Venture .WillOnce(Return("chipA")); 7999b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("5")); 8099b95815SPatrick Venture 8199b95815SPatrick Venture EXPECT_CALL(gMock, build(Eq("chipA"), Eq("5"))) 8299b95815SPatrick Venture .WillOnce(Invoke([&](const std::string& chip, const std::string& line) { 8399b95815SPatrick Venture return std::move(handleMock); 8499b95815SPatrick Venture })); 8599b95815SPatrick Venture 8699b95815SPatrick Venture /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ 8799b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return("")); 8899b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey))) 8999b95815SPatrick Venture .WillOnce(Return("")); 9099b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey))) 9199b95815SPatrick Venture .WillOnce(Return("")); 9299b95815SPatrick Venture 9399b95815SPatrick Venture auto sensor = 9499b95815SPatrick Venture std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); 9599b95815SPatrick Venture EXPECT_FALSE(sensor == nullptr); 9699b95815SPatrick Venture } 97*cd40c881SPatrick Venture 98*cd40c881SPatrick Venture TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue) 99*cd40c881SPatrick Venture { 100*cd40c881SPatrick Venture /* Construct a sensor that has a gain and offset, then verify they are used 101*cd40c881SPatrick Venture * when adjusting the value. 102*cd40c881SPatrick Venture */ 103*cd40c881SPatrick Venture 104*cd40c881SPatrick Venture StrictMock<EnvMock> eMock; 105*cd40c881SPatrick Venture envIntf = &eMock; 106*cd40c881SPatrick Venture 107*cd40c881SPatrick Venture auto sensorKey = std::make_pair(std::string("temp"), std::string("5")); 108*cd40c881SPatrick Venture std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = 109*cd40c881SPatrick Venture std::make_unique<hwmonio::HwmonIOMock>(); 110*cd40c881SPatrick Venture std::string path = "/"; 111*cd40c881SPatrick Venture 112*cd40c881SPatrick Venture /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ 113*cd40c881SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey))) 114*cd40c881SPatrick Venture .WillOnce(Return("")); 115*cd40c881SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("")); 116*cd40c881SPatrick Venture 117*cd40c881SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))) 118*cd40c881SPatrick Venture .WillOnce(Return("10")); 119*cd40c881SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey))) 120*cd40c881SPatrick Venture .WillOnce(Return("15")); 121*cd40c881SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey))) 122*cd40c881SPatrick Venture .WillOnce(Return("")); 123*cd40c881SPatrick Venture 124*cd40c881SPatrick Venture auto sensor = 125*cd40c881SPatrick Venture std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); 126*cd40c881SPatrick Venture EXPECT_FALSE(sensor == nullptr); 127*cd40c881SPatrick Venture 128*cd40c881SPatrick Venture double startingValue = 1.0; 129*cd40c881SPatrick Venture double resultValue = sensor->adjustValue(startingValue); 130*cd40c881SPatrick Venture EXPECT_DOUBLE_EQ(resultValue, 25.0); 131*cd40c881SPatrick Venture } 132