diff options
author | Patrick Venture <venture@google.com> | 2018-12-19 08:58:21 -0800 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2019-01-02 11:56:28 -0800 |
commit | e3e0f97d648d7251f6670289ae38284c79686e47 (patch) | |
tree | 48122b1f2363c636ca5a35b133698c25ff655b04 | |
parent | 0e2d68d2af9b4fbb67319fcfd41112d83e990986 (diff) | |
download | phosphor-hwmon-e3e0f97d648d7251f6670289ae38284c79686e47.tar.gz phosphor-hwmon-e3e0f97d648d7251f6670289ae38284c79686e47.zip |
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>
-rw-r--r-- | gpio_handle.cpp | 4 | ||||
-rw-r--r-- | gpio_handle.hpp | 6 | ||||
-rw-r--r-- | sensor.hpp | 2 | ||||
-rw-r--r-- | test/gpio.cpp | 18 | ||||
-rw-r--r-- | test/gpio_mock.hpp | 26 |
5 files changed, 50 insertions, 6 deletions
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 @@ namespace gpio 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 @@ namespace gpio * * @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 @@ -138,7 +138,7 @@ class Sensor 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; |