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 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 13 | namespace env |
| 14 | { |
| 15 | |
| 16 | // Delegate all calls to getEnv() to the mock |
| 17 | const char* EnvImpl::get(const char* key) const |
| 18 | { |
| 19 | return mockEnv.get(key); |
| 20 | } |
| 21 | |
| 22 | EnvImpl env_impl; |
| 23 | |
| 24 | } // namespace env |
| 25 | |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 26 | class SensorTest : public ::testing::Test |
| 27 | { |
| 28 | protected: |
| 29 | void SetUp() override |
| 30 | { |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 31 | gpioIntf = nullptr; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 32 | } |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 33 | |
| 34 | std::string temp = "temp"; |
| 35 | std::string five = "5"; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | using ::testing::Eq; |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 39 | using ::testing::Invoke; |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 40 | using ::testing::Pair; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 41 | using ::testing::Return; |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 42 | using ::testing::StrEq; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 43 | using ::testing::StrictMock; |
| 44 | |
| 45 | TEST_F(SensorTest, BasicConstructorTest) |
| 46 | { |
| 47 | /* Constructor test with nothing in an rcList or GPIO chip. */ |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 48 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 49 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 50 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 51 | std::string path = "/"; |
| 52 | |
| 53 | /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 54 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5"))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 55 | .WillOnce(Return("")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 56 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("")); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 57 | |
| 58 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 59 | EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("")); |
| 60 | EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return("")); |
| 61 | EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5"))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 62 | .WillOnce(Return("")); |
| 63 | |
| 64 | auto sensor = |
| 65 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 66 | EXPECT_FALSE(sensor == nullptr); |
| 67 | } |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 68 | |
| 69 | TEST_F(SensorTest, SensorRequiresGpio) |
| 70 | { |
| 71 | /* Constructor test with only the GPIO chip set. */ |
| 72 | |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 73 | StrictMock<GpioHandleMock> gMock; |
| 74 | gpioIntf = &gMock; |
| 75 | |
| 76 | /* The following piece of code can probably be copied above once it's |
| 77 | * working. |
| 78 | */ |
| 79 | auto handleMock = std::make_unique<gpioplus::test::HandleMock>(); |
| 80 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 81 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 82 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 83 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 84 | std::string path = "/"; |
| 85 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 86 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5"))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 87 | .WillOnce(Return("chipA")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 88 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5")); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 89 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 90 | EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5"))) |
Brad Bishop | 778f5c3 | 2019-10-07 15:48:15 -0400 | [diff] [blame] | 91 | .WillOnce(Invoke([&](const std::string&, const std::string&) { |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 92 | return std::move(handleMock); |
| 93 | })); |
| 94 | |
| 95 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 96 | EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("")); |
| 97 | EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return("")); |
| 98 | EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5"))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 99 | .WillOnce(Return("")); |
| 100 | |
| 101 | auto sensor = |
| 102 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 103 | EXPECT_FALSE(sensor == nullptr); |
| 104 | } |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 105 | |
| 106 | TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue) |
| 107 | { |
| 108 | /* Construct a sensor that has a gain and offset, then verify they are used |
| 109 | * when adjusting the value. |
| 110 | */ |
| 111 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 112 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 113 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 114 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 115 | std::string path = "/"; |
| 116 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 117 | /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */ |
| 118 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5"))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 119 | .WillOnce(Return("")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 120 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("")); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 121 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 122 | EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10")); |
| 123 | EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 124 | .WillOnce(Return("15")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 125 | EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5"))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 126 | .WillOnce(Return("")); |
| 127 | |
| 128 | auto sensor = |
| 129 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
| 130 | EXPECT_FALSE(sensor == nullptr); |
| 131 | |
| 132 | double startingValue = 1.0; |
| 133 | double resultValue = sensor->adjustValue(startingValue); |
| 134 | EXPECT_DOUBLE_EQ(resultValue, 25.0); |
| 135 | } |