test: add tests for creating sensor objects

Add a variety of tests for different sensor constructor scenarios.

Change-Id: I859a9473e3c80fb06b0ae15f0eeb8217ee390b11
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index b9a3c49..8573194 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -11,7 +11,7 @@
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
 
 # Run all 'check' test programs
-check_PROGRAMS = hwmon_unittest fanpwm_unittest
+check_PROGRAMS = hwmon_unittest fanpwm_unittest sensor_unittest
 TESTS = $(check_PROGRAMS)
 
 hwmon_unittest_SOURCES = hwmon_unittest.cpp
@@ -19,3 +19,16 @@
 
 fanpwm_unittest_SOURCES = fanpwm_unittest.cpp
 fanpwm_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) $(top_builddir)/fan_pwm.o
+
+sensor_unittest_SOURCES = sensor_unittest.cpp
+sensor_unittest_CXXFLAGS = \
+	$(PHOSPHOR_LOGGING_CFLAGS) \
+	$(GPIOPLUS_CFLAGS)
+sensor_unittest_LDADD = \
+	-lstdc++fs \
+	$(PHOSPHOR_LOGGING_LIBS) \
+	$(GPIOPLUS_LIBS) \
+	$(top_builddir)/sensor.o \
+	$(top_builddir)/hwmon.o \
+	$(top_builddir)/gpio_handle.o \
+	env.o
diff --git a/test/sensor_unittest.cpp b/test/sensor_unittest.cpp
new file mode 100644
index 0000000..905b011
--- /dev/null
+++ b/test/sensor_unittest.cpp
@@ -0,0 +1,51 @@
+#include "env_mock.hpp"
+#include "hwmonio_mock.hpp"
+#include "sensor.hpp"
+
+#include <memory>
+#include <utility>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+class SensorTest : public ::testing::Test
+{
+  protected:
+    void SetUp() override
+    {
+        envIntf = nullptr;
+    }
+};
+
+using ::testing::Eq;
+using ::testing::Return;
+using ::testing::StrictMock;
+
+TEST_F(SensorTest, BasicConstructorTest)
+{
+    /* Constructor test with nothing in an rcList or GPIO chip. */
+
+    StrictMock<EnvMock> eMock;
+    envIntf = &eMock;
+
+    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 = "/";
+
+    /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
+    EXPECT_CALL(eMock, getEnv(Eq("GPIOCHIP"), Eq(sensorKey)))
+        .WillOnce(Return(""));
+    EXPECT_CALL(eMock, getEnv(Eq("GPIO"), Eq(sensorKey))).WillOnce(Return(""));
+
+    /* 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);
+}