diff options
author | Patrick Venture <venture@google.com> | 2018-12-19 07:53:45 -0800 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2019-01-02 11:56:28 -0800 |
commit | 0e2d68d2af9b4fbb67319fcfd41112d83e990986 (patch) | |
tree | 53ef7be5240db3d172c82360ecb2d7700e49ea44 | |
parent | a67a947007d598ad30b322c67fba6efa338d2156 (diff) | |
download | phosphor-hwmon-0e2d68d2af9b4fbb67319fcfd41112d83e990986.tar.gz phosphor-hwmon-0e2d68d2af9b4fbb67319fcfd41112d83e990986.zip |
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>
-rw-r--r-- | test/Makefile.am | 15 | ||||
-rw-r--r-- | test/sensor_unittest.cpp | 51 |
2 files changed, 65 insertions, 1 deletions
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 @@ AM_LDFLAGS = \ $(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 @@ hwmon_unittest_LDADD = $(top_builddir)/hwmon.o 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); +} |