Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 1 | #include "env_mock.hpp" |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 2 | #include "gpio_mock.hpp" |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 3 | #include "hwmonio_mock.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 6 | #include <gpioplus/test/handle.hpp> |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | class SensorTest : public ::testing::Test |
| 14 | { |
| 15 | protected: |
| 16 | void SetUp() override |
| 17 | { |
| 18 | envIntf = nullptr; |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 19 | gpioIntf = nullptr; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 20 | } |
| 21 | }; |
| 22 | |
| 23 | using ::testing::Eq; |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 24 | using ::testing::Invoke; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 25 | using ::testing::Return; |
| 26 | using ::testing::StrictMock; |
| 27 | |
| 28 | TEST_F(SensorTest, BasicConstructorTest) |
| 29 | { |
| 30 | /* Constructor test with nothing in an rcList or GPIO chip. */ |
| 31 | |
| 32 | StrictMock<EnvMock> eMock; |
| 33 | envIntf = &eMock; |
| 34 | |
| 35 | auto sensorKey = std::make_pair(std::string("temp"), std::string("5")); |
| 36 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 37 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 38 | std::string path = "/"; |
| 39 | |
| 40 | /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ |
| 41 | EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey))) |
| 42 | .WillOnce(Return("")); |
| 43 | EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("")); |
| 44 | |
| 45 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
| 46 | EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return("")); |
| 47 | EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey))) |
| 48 | .WillOnce(Return("")); |
| 49 | EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey))) |
| 50 | .WillOnce(Return("")); |
| 51 | |
| 52 | auto sensor = |
| 53 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 54 | EXPECT_FALSE(sensor == nullptr); |
| 55 | } |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 56 | |
| 57 | TEST_F(SensorTest, SensorRequiresGpio) |
| 58 | { |
| 59 | /* Constructor test with only the GPIO chip set. */ |
| 60 | |
| 61 | StrictMock<EnvMock> eMock; |
| 62 | envIntf = &eMock; |
| 63 | |
| 64 | StrictMock<GpioHandleMock> gMock; |
| 65 | gpioIntf = &gMock; |
| 66 | |
| 67 | /* The following piece of code can probably be copied above once it's |
| 68 | * working. |
| 69 | */ |
| 70 | auto handleMock = std::make_unique<gpioplus::test::HandleMock>(); |
| 71 | |
| 72 | auto sensorKey = std::make_pair(std::string("temp"), std::string("5")); |
| 73 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 74 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 75 | std::string path = "/"; |
| 76 | |
| 77 | EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey))) |
| 78 | .WillOnce(Return("chipA")); |
| 79 | EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("5")); |
| 80 | |
| 81 | EXPECT_CALL(gMock, build(Eq("chipA"), Eq("5"))) |
| 82 | .WillOnce(Invoke([&](const std::string& chip, const std::string& line) { |
| 83 | return std::move(handleMock); |
| 84 | })); |
| 85 | |
| 86 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
| 87 | EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return("")); |
| 88 | EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey))) |
| 89 | .WillOnce(Return("")); |
| 90 | EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey))) |
| 91 | .WillOnce(Return("")); |
| 92 | |
| 93 | auto sensor = |
| 94 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 95 | EXPECT_FALSE(sensor == nullptr); |
| 96 | } |