diff options
| author | Patrick Venture <venture@google.com> | 2018-06-08 18:49:27 -0700 |
|---|---|---|
| committer | Patrick Venture <venture@google.com> | 2018-06-14 09:26:42 -0700 |
| commit | e31ee6da03ace3030ad54976a4001ee25aedad6b (patch) | |
| tree | e0a7c213fe5c6f8498bd08a3691503cfde19f56c /test | |
| parent | bd6b476a0c080bd1aca4c963f09c3f08a87c83d8 (diff) | |
| download | phosphor-pid-control-e31ee6da03ace3030ad54976a4001ee25aedad6b.tar.gz phosphor-pid-control-e31ee6da03ace3030ad54976a4001ee25aedad6b.zip | |
test: sensors: pluggable
Tests for pluggable sensors. Pluggable sensors themselves do nothing
but hold min/max values, and call into Read and Write interfaces.
Change-Id: I8991d22d77172fb9b2f897c02603e5ab5747ac12
Signed-off-by: Patrick Venture <venture@google.com>
Diffstat (limited to 'test')
| -rw-r--r-- | test/Makefile.am | 5 | ||||
| -rw-r--r-- | test/sensor_pluggable_unittest.cpp | 84 |
2 files changed, 88 insertions, 1 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index 738ab91..c6b3375 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -12,9 +12,12 @@ AM_LDFLAGS = $(GTEST_LIBS) \ $(PHOSPHOR_DBUS_INTERFACES_LIBS) # Run all 'check' test programs -check_PROGRAMS = sensor_manager_unittest +check_PROGRAMS = sensor_manager_unittest sensor_pluggable_unittest TESTS = $(check_PROGRAMS) # Until libconfig is mocked out or replaced, include it. sensor_manager_unittest_SOURCES = sensor_manager_unittest.cpp sensor_manager_unittest_LDADD = $(top_builddir)/sensors/manager.o + +sensor_pluggable_unittest_SOURCES = sensor_pluggable_unittest.cpp +sensor_pluggable_unittest_LDADD = $(top_builddir)/sensors/pluggable.o diff --git a/test/sensor_pluggable_unittest.cpp b/test/sensor_pluggable_unittest.cpp new file mode 100644 index 0000000..1e32230 --- /dev/null +++ b/test/sensor_pluggable_unittest.cpp @@ -0,0 +1,84 @@ +#include "sensors/pluggable.hpp" + +#include <chrono> +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +#include "test/readinterface_mock.hpp" +#include "test/writeinterface_mock.hpp" + +using ::testing::Invoke; + +TEST(PluggableSensorTest, BoringConstructorTest) { + // Build a boring Pluggable Sensor. + + int64_t min = 0; + int64_t max = 255; + + std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>(); + std::unique_ptr<WriteInterface> wi = + std::make_unique<WriteInterfaceMock>(min, max); + + std::string name = "name"; + int64_t timeout = 1; + + PluggableSensor p(name, timeout, std::move(ri), std::move(wi)); + // Successfully created it. +} + +TEST(PluggableSensorTest, TryReadingTest) { + // Verify calling read, calls the ReadInterface. + + int64_t min = 0; + int64_t max = 255; + + std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>(); + std::unique_ptr<WriteInterface> wi = + std::make_unique<WriteInterfaceMock>(min, max); + + std::string name = "name"; + int64_t timeout = 1; + + ReadInterfaceMock *rip = reinterpret_cast<ReadInterfaceMock *>(ri.get()); + + PluggableSensor p(name, timeout, std::move(ri), std::move(wi)); + + ReadReturn r; + r.value = 0.1; + r.updated = std::chrono::high_resolution_clock::now(); + + EXPECT_CALL(*rip, read()) + .WillOnce( + Invoke([&](void) { + return r; + }) + ); + + // TODO(venture): Implement comparison operator for ReadReturn. + ReadReturn v = p.read(); + EXPECT_EQ(r.value, v.value); + EXPECT_EQ(r.updated, v.updated); +} + +TEST(PluggableSensorTest, TryWritingTest) { + // Verify calling write, calls the WriteInterface. + + int64_t min = 0; + int64_t max = 255; + + std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>(); + std::unique_ptr<WriteInterface> wi = + std::make_unique<WriteInterfaceMock>(min, max); + + std::string name = "name"; + int64_t timeout = 1; + + WriteInterfaceMock *wip = reinterpret_cast<WriteInterfaceMock *>(wi.get()); + + PluggableSensor p(name, timeout, std::move(ri), std::move(wi)); + + double value = 0.303; + + EXPECT_CALL(*wip, write(value)); + p.write(value); +} |

