10e2d68d2SPatrick Venture #include "env_mock.hpp" 2*99b95815SPatrick Venture #include "gpio_mock.hpp" 30e2d68d2SPatrick Venture #include "hwmonio_mock.hpp" 40e2d68d2SPatrick Venture #include "sensor.hpp" 50e2d68d2SPatrick Venture 6*99b95815SPatrick 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; 19*99b95815SPatrick Venture gpioIntf = nullptr; 200e2d68d2SPatrick Venture } 210e2d68d2SPatrick Venture }; 220e2d68d2SPatrick Venture 230e2d68d2SPatrick Venture using ::testing::Eq; 24*99b95815SPatrick 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 } 56*99b95815SPatrick Venture 57*99b95815SPatrick Venture TEST_F(SensorTest, SensorRequiresGpio) 58*99b95815SPatrick Venture { 59*99b95815SPatrick Venture /* Constructor test with only the GPIO chip set. */ 60*99b95815SPatrick Venture 61*99b95815SPatrick Venture StrictMock<EnvMock> eMock; 62*99b95815SPatrick Venture envIntf = &eMock; 63*99b95815SPatrick Venture 64*99b95815SPatrick Venture StrictMock<GpioHandleMock> gMock; 65*99b95815SPatrick Venture gpioIntf = &gMock; 66*99b95815SPatrick Venture 67*99b95815SPatrick Venture /* The following piece of code can probably be copied above once it's 68*99b95815SPatrick Venture * working. 69*99b95815SPatrick Venture */ 70*99b95815SPatrick Venture auto handleMock = std::make_unique<gpioplus::test::HandleMock>(); 71*99b95815SPatrick Venture 72*99b95815SPatrick Venture auto sensorKey = std::make_pair(std::string("temp"), std::string("5")); 73*99b95815SPatrick Venture std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = 74*99b95815SPatrick Venture std::make_unique<hwmonio::HwmonIOMock>(); 75*99b95815SPatrick Venture std::string path = "/"; 76*99b95815SPatrick Venture 77*99b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey))) 78*99b95815SPatrick Venture .WillOnce(Return("chipA")); 79*99b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("5")); 80*99b95815SPatrick Venture 81*99b95815SPatrick Venture EXPECT_CALL(gMock, build(Eq("chipA"), Eq("5"))) 82*99b95815SPatrick Venture .WillOnce(Invoke([&](const std::string& chip, const std::string& line) { 83*99b95815SPatrick Venture return std::move(handleMock); 84*99b95815SPatrick Venture })); 85*99b95815SPatrick Venture 86*99b95815SPatrick Venture /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ 87*99b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return("")); 88*99b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey))) 89*99b95815SPatrick Venture .WillOnce(Return("")); 90*99b95815SPatrick Venture EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey))) 91*99b95815SPatrick Venture .WillOnce(Return("")); 92*99b95815SPatrick Venture 93*99b95815SPatrick Venture auto sensor = 94*99b95815SPatrick Venture std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); 95*99b95815SPatrick Venture EXPECT_FALSE(sensor == nullptr); 96*99b95815SPatrick Venture } 97