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 | } |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 21 | |
| 22 | std::string temp = "temp"; |
| 23 | std::string five = "5"; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | using ::testing::Eq; |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 27 | using ::testing::Invoke; |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 28 | using ::testing::Pair; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 29 | using ::testing::Return; |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 30 | using ::testing::StrEq; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 31 | using ::testing::StrictMock; |
| 32 | |
| 33 | TEST_F(SensorTest, BasicConstructorTest) |
| 34 | { |
| 35 | /* Constructor test with nothing in an rcList or GPIO chip. */ |
| 36 | |
| 37 | StrictMock<EnvMock> eMock; |
| 38 | envIntf = &eMock; |
| 39 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 40 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 41 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 42 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 43 | std::string path = "/"; |
| 44 | |
| 45 | /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 46 | EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 47 | .WillOnce(Return("")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 48 | EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five))) |
| 49 | .WillOnce(Return("")); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 50 | |
| 51 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 52 | EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 53 | .WillOnce(Return("")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 54 | EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five))) |
| 55 | .WillOnce(Return("")); |
| 56 | EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 57 | .WillOnce(Return("")); |
| 58 | |
| 59 | auto sensor = |
| 60 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 61 | EXPECT_FALSE(sensor == nullptr); |
| 62 | } |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 63 | |
| 64 | TEST_F(SensorTest, SensorRequiresGpio) |
| 65 | { |
| 66 | /* Constructor test with only the GPIO chip set. */ |
| 67 | |
| 68 | StrictMock<EnvMock> eMock; |
| 69 | envIntf = &eMock; |
| 70 | |
| 71 | StrictMock<GpioHandleMock> gMock; |
| 72 | gpioIntf = &gMock; |
| 73 | |
| 74 | /* The following piece of code can probably be copied above once it's |
| 75 | * working. |
| 76 | */ |
| 77 | auto handleMock = std::make_unique<gpioplus::test::HandleMock>(); |
| 78 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 79 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 80 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 81 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 82 | std::string path = "/"; |
| 83 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 84 | EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 85 | .WillOnce(Return("chipA")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 86 | EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five))) |
| 87 | .WillOnce(Return("5")); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 88 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 89 | EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5"))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 90 | .WillOnce(Invoke([&](const std::string& chip, const std::string& line) { |
| 91 | return std::move(handleMock); |
| 92 | })); |
| 93 | |
| 94 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 95 | EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 96 | .WillOnce(Return("")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 97 | EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five))) |
| 98 | .WillOnce(Return("")); |
| 99 | EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 100 | .WillOnce(Return("")); |
| 101 | |
| 102 | auto sensor = |
| 103 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 104 | EXPECT_FALSE(sensor == nullptr); |
| 105 | } |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 106 | |
| 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 | StrictMock<EnvMock> eMock; |
| 114 | envIntf = &eMock; |
| 115 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 116 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 117 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 118 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 119 | std::string path = "/"; |
| 120 | |
| 121 | /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 122 | EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 123 | .WillOnce(Return("")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 124 | EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five))) |
| 125 | .WillOnce(Return("")); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 126 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 127 | EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 128 | .WillOnce(Return("10")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 129 | EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 130 | .WillOnce(Return("15")); |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame^] | 131 | EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 132 | .WillOnce(Return("")); |
| 133 | |
| 134 | auto sensor = |
| 135 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 136 | EXPECT_FALSE(sensor == nullptr); |
| 137 | |
| 138 | double startingValue = 1.0; |
| 139 | double resultValue = sensor->adjustValue(startingValue); |
| 140 | EXPECT_DOUBLE_EQ(resultValue, 25.0); |
| 141 | } |