test: add gpiohandle mock impl
Add gpio handle mock implementation for use in testing.
Change-Id: I0fb2d1af4739ad700f62a4e7cbbe62db528bb93e
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/gpio_handle.cpp b/gpio_handle.cpp
index 2a46ae2..d99a031 100644
--- a/gpio_handle.cpp
+++ b/gpio_handle.cpp
@@ -12,8 +12,8 @@
using namespace phosphor::logging;
-std::unique_ptr<gpioplus::Handle> BuildGpioHandle(const std::string& gpiochip,
- const std::string& line)
+std::unique_ptr<gpioplus::HandleInterface>
+ BuildGpioHandle(const std::string& gpiochip, const std::string& line)
{
char *gpioEnd, *lineEnd;
unsigned long chipId = std::strtoul(gpiochip.c_str(), &gpioEnd, 10);
diff --git a/gpio_handle.hpp b/gpio_handle.hpp
index f7d4a5e..f48a34d 100644
--- a/gpio_handle.hpp
+++ b/gpio_handle.hpp
@@ -12,9 +12,9 @@
*
* @param[in] gpiochip - gpiochip id as string, e.g. "0", or "1"
* @param[in] line - gpio line offset as string.
- * @return A gpioplus::Handle on success nullptr on failure.
+ * @return A gpioplus::HandleInterface on success nullptr on failure.
*/
-std::unique_ptr<gpioplus::Handle> BuildGpioHandle(const std::string& gpiochip,
- const std::string& line);
+std::unique_ptr<gpioplus::HandleInterface>
+ BuildGpioHandle(const std::string& gpiochip, const std::string& line);
} // namespace gpio
diff --git a/sensor.hpp b/sensor.hpp
index 3bcfc82..bf98724 100644
--- a/sensor.hpp
+++ b/sensor.hpp
@@ -138,7 +138,7 @@
valueAdjust sensorAdjusts;
/** @brief Optional pointer to GPIO handle. */
- std::unique_ptr<gpioplus::Handle> handle;
+ std::unique_ptr<gpioplus::HandleInterface> handle;
/** @brief default pause after unlocking gpio. */
static constexpr std::chrono::milliseconds pause{500};
diff --git a/test/gpio.cpp b/test/gpio.cpp
new file mode 100644
index 0000000..cf01a0a
--- /dev/null
+++ b/test/gpio.cpp
@@ -0,0 +1,18 @@
+#include "gpio_mock.hpp"
+
+#include <memory>
+#include <string>
+
+// Set this before each test that hits a call to getEnv().
+GpioHandleInterface* gpioIntf;
+
+namespace gpio
+{
+
+std::unique_ptr<gpioplus::HandleInterface>
+ BuildGpioHandle(const std::string& gpiochip, const std::string& line)
+{
+ return (gpioIntf) ? gpioIntf->build(gpiochip, line) : nullptr;
+}
+
+} // namespace gpio
diff --git a/test/gpio_mock.hpp b/test/gpio_mock.hpp
new file mode 100644
index 0000000..d1acfef
--- /dev/null
+++ b/test/gpio_mock.hpp
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <gpioplus/handle.hpp>
+#include <memory>
+#include <string>
+
+#include <gmock/gmock.h>
+
+class GpioHandleInterface
+{
+ public:
+ virtual ~GpioHandleInterface() = default;
+ virtual std::unique_ptr<gpioplus::HandleInterface>
+ build(const std::string& gpiochip, const std::string& line) const = 0;
+};
+
+class GpioHandleMock : public GpioHandleInterface
+{
+ public:
+ virtual ~GpioHandleMock() = default;
+ MOCK_CONST_METHOD2(build, std::unique_ptr<gpioplus::HandleInterface>(
+ const std::string&, const std::string&));
+};
+
+// Set this before each test that hits a call to getEnv().
+extern GpioHandleInterface* gpioIntf;