hwmonio: Add injection point to test internal behavior
With the complexities of the various behaviors within the read() method
of HwmonIO, introduce an injection point for testing. There is a
default available, and therefore this is a surgical change that only
impacts future tests.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I1ead56c7fe1a2f87ebf316488e68f435a41c9d19
diff --git a/test/Makefile.am b/test/Makefile.am
index 37aa977..016409f 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -21,7 +21,7 @@
$(CODE_COVERAGE_LIBS)
# Run all 'check' test programs
-check_PROGRAMS = hwmon_unittest fanpwm_unittest sensor_unittest
+check_PROGRAMS = hwmon_unittest fanpwm_unittest sensor_unittest hwmonio_default_unittest
TESTS = $(check_PROGRAMS)
hwmon_unittest_SOURCES = hwmon_unittest.cpp
@@ -42,3 +42,6 @@
$(top_builddir)/hwmon.o \
env.o \
gpio.o
+
+hwmonio_default_unittest_SOURCES = hwmonio_default_unittest.cpp
+hwmonio_default_unittest_LDADD = $(top_builddir)/hwmonio.o
diff --git a/test/filesystem_mock.hpp b/test/filesystem_mock.hpp
new file mode 100644
index 0000000..9d24d3c
--- /dev/null
+++ b/test/filesystem_mock.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "hwmonio.hpp"
+
+#include <string>
+
+#include <gmock/gmock.h>
+
+namespace hwmonio
+{
+
+class FileSystemMock : public FileSystemInterface
+{
+ public:
+ MOCK_CONST_METHOD1(read, int64_t(const std::string&));
+ MOCK_CONST_METHOD2(write, void(const std::string&, uint32_t));
+};
+
+} // namespace hwmonio
diff --git a/test/hwmonio_default_unittest.cpp b/test/hwmonio_default_unittest.cpp
new file mode 100644
index 0000000..233bf56
--- /dev/null
+++ b/test/hwmonio_default_unittest.cpp
@@ -0,0 +1,58 @@
+#include "filesystem_mock.hpp"
+#include "hwmonio.hpp"
+
+#include <chrono>
+#include <string>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace hwmonio
+{
+namespace
+{
+
+using ::testing::_;
+using ::testing::Return;
+
+class HwmonIOTest : public ::testing::Test
+{
+ protected:
+ HwmonIOTest() : _hwmonio(_path, &_mock)
+ {
+ }
+
+ const int64_t _value = 12;
+ const std::string _path = "abcd";
+ const std::string _type = "fan";
+ const std::string _id = "a";
+ const std::string _sensor = "1";
+ const size_t _retries = 1;
+ const std::chrono::milliseconds _delay = std::chrono::milliseconds{10};
+
+ FileSystemMock _mock;
+ HwmonIO _hwmonio;
+};
+
+TEST_F(HwmonIOTest, ReadReturnsValue)
+{
+ EXPECT_CALL(_mock, read(_)).WillOnce(Return(_value));
+ EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value);
+}
+
+int64_t SetErrnoExcept(const std::string&)
+{
+ errno = ETIMEDOUT;
+ throw std::runtime_error("bad times");
+}
+
+TEST_F(HwmonIOTest, ReadExceptsRetryable)
+{
+ EXPECT_CALL(_mock, read(_))
+ .WillOnce(&SetErrnoExcept)
+ .WillOnce(Return(_value));
+ EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value);
+}
+
+} // namespace
+} // namespace hwmonio