sensor: add gpio handle test variation

Add a sensor constructor test where the gpio handle is required.

Change-Id: I05050fb71a6287183ca94f974e965e3f58de9499
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 8573194..2682d45 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -30,5 +30,5 @@
 	$(GPIOPLUS_LIBS) \
 	$(top_builddir)/sensor.o \
 	$(top_builddir)/hwmon.o \
-	$(top_builddir)/gpio_handle.o \
-	env.o
+	env.o \
+	gpio.o
diff --git a/test/sensor_unittest.cpp b/test/sensor_unittest.cpp
index 905b011..a80e252 100644
--- a/test/sensor_unittest.cpp
+++ b/test/sensor_unittest.cpp
@@ -1,7 +1,9 @@
 #include "env_mock.hpp"
+#include "gpio_mock.hpp"
 #include "hwmonio_mock.hpp"
 #include "sensor.hpp"
 
+#include <gpioplus/test/handle.hpp>
 #include <memory>
 #include <utility>
 
@@ -14,10 +16,12 @@
     void SetUp() override
     {
         envIntf = nullptr;
+        gpioIntf = nullptr;
     }
 };
 
 using ::testing::Eq;
+using ::testing::Invoke;
 using ::testing::Return;
 using ::testing::StrictMock;
 
@@ -49,3 +53,44 @@
         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
     EXPECT_FALSE(sensor == nullptr);
 }
+
+TEST_F(SensorTest, SensorRequiresGpio)
+{
+    /* Constructor test with only the GPIO chip set. */
+
+    StrictMock<EnvMock> eMock;
+    envIntf = &eMock;
+
+    StrictMock<GpioHandleMock> gMock;
+    gpioIntf = &gMock;
+
+    /* The following piece of code can probably be copied above once it's
+     * working.
+     */
+    auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
+
+    auto sensorKey = std::make_pair(std::string("temp"), std::string("5"));
+    std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
+        std::make_unique<hwmonio::HwmonIOMock>();
+    std::string path = "/";
+
+    EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey)))
+        .WillOnce(Return("chipA"));
+    EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return("5"));
+
+    EXPECT_CALL(gMock, build(Eq("chipA"), Eq("5")))
+        .WillOnce(Invoke([&](const std::string& chip, const std::string& line) {
+            return std::move(handleMock);
+        }));
+
+    /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
+    EXPECT_CALL(eMock, getEnv(Eq("GAIN"), Eq(sensorKey))).WillOnce(Return(""));
+    EXPECT_CALL(eMock, getEnv(Eq("OFFSET"), Eq(sensorKey)))
+        .WillOnce(Return(""));
+    EXPECT_CALL(eMock, getEnv(Eq("REMOVERCS"), Eq(sensorKey)))
+        .WillOnce(Return(""));
+
+    auto sensor =
+        std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
+    EXPECT_FALSE(sensor == nullptr);
+}