event: Add mock for clients
Makes it easier for client libraries to test their gpio logic.
Change-Id: I34d4372ff57698873ad449e02b78c8fa826d383d
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/Makefile.am b/src/Makefile.am
index 7bf0aa2..40bb24a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,7 @@
nobase_include_HEADERS += gpioplus/internal/sys.hpp
libgpioplus_la_SOURCES += gpioplus/internal/sys.cpp
+nobase_include_HEADERS += gpioplus/test/event.hpp
nobase_include_HEADERS += gpioplus/test/sys.hpp
nobase_include_HEADERS += gpioplus/test/handle.hpp
diff --git a/src/gpioplus/event.hpp b/src/gpioplus/event.hpp
index ce90f64..0bfe19c 100644
--- a/src/gpioplus/event.hpp
+++ b/src/gpioplus/event.hpp
@@ -26,11 +26,32 @@
uint32_t toInt() const;
};
-/** @class EVent
+/** @class EventInterface
+ * @brief Interface used for providing gpio events
+ */
+class EventInterface
+{
+ public:
+ virtual ~EventInterface() = default;
+
+ /** @brief Event data read from the gpio line */
+ struct Data
+ {
+ /** @brief The estimate of the time the event occurred */
+ std::chrono::duration<uint64_t, std::nano> timestamp;
+ /** @brief The identifier of the event */
+ uint32_t id;
+ };
+
+ virtual std::optional<Data> read() const = 0;
+ virtual uint8_t getValue() const = 0;
+};
+
+/** @class Event
* @brief Handle to a gpio line event
* @details Provides a c++ interface for gpio event operations
*/
-class Event
+class Event : public EventInterface
{
public:
/** @brief Creates a new gpio line event handler
@@ -54,15 +75,6 @@
*/
const internal::Fd& getFd() const;
- /** @brief Event data read from the gpio line */
- struct Data
- {
- /** @brief The estimate of the time the event occurred */
- std::chrono::duration<uint64_t, std::nano> timestamp;
- /** @brief The identifier of the event */
- uint32_t id;
- };
-
/** @brief Reads an event from the event file descriptor
* Follows the read(2) semantics of the underyling file descriptor
*
@@ -70,14 +82,14 @@
* @return The value of the event or std::nullopt if the file descriptor
* is non-blocking and no event has occurred
*/
- std::optional<Data> read() const;
+ std::optional<Data> read() const override;
/** @brief Get the current value of the associated line
*
* @throws std::system_error for underlying syscall failures
* @return The value of the gpio line
*/
- uint8_t getValue() const;
+ uint8_t getValue() const override;
private:
internal::Fd fd;
diff --git a/src/gpioplus/test/event.hpp b/src/gpioplus/test/event.hpp
new file mode 100644
index 0000000..64c3613
--- /dev/null
+++ b/src/gpioplus/test/event.hpp
@@ -0,0 +1,18 @@
+#pragma once
+#include <gmock/gmock.h>
+#include <gpioplus/event.hpp>
+
+namespace gpioplus
+{
+namespace test
+{
+
+class EventMock : public EventInterface
+{
+ public:
+ MOCK_CONST_METHOD0(read, std::optional<Data>());
+ MOCK_CONST_METHOD0(getValue, uint8_t());
+};
+
+} // namespace test
+} // namespace gpioplus
diff --git a/test/mocks.cpp b/test/mocks.cpp
index 2bc1f61..c7d7392 100644
--- a/test/mocks.cpp
+++ b/test/mocks.cpp
@@ -1,3 +1,4 @@
+#include <gpioplus/test/event.hpp>
#include <gpioplus/test/handle.hpp>
namespace gpioplus
@@ -7,6 +8,7 @@
TEST(Mocks, Compile)
{
+ test::EventMock event;
test::HandleMock handle;
}