tests: move module_from_xpath into a common sysrepo-helpers library
This looks like something that can be reused. And it will be.
Change-Id: I34ec27e1e0a38685ad4ccef213a881de17addc87
diff --git a/tests/mock/sysrepo/events.cpp b/tests/mock/sysrepo/events.cpp
index 562cf7f..0431f55 100644
--- a/tests/mock/sysrepo/events.cpp
+++ b/tests/mock/sysrepo/events.cpp
@@ -6,20 +6,10 @@
*/
#include "events.h"
-
-namespace {
-std::string module_from_xpath(const std::string& xpath)
-{
- auto pos = xpath.find(":");
- if (pos == 0 || pos == std::string::npos || xpath[0] != '/') {
- throw std::logic_error{"NotificationWatcher: Malformed XPath " + xpath};
- }
- return xpath.substr(1, pos - 1);
-}
-}
+#include "sysrepo-helpers/common.h"
NotificationWatcher::NotificationWatcher(sysrepo::Session& session, const std::string& xpath)
- : m_sub{session.onNotification(module_from_xpath(xpath),
+ : m_sub{session.onNotification(moduleFromXpath(xpath),
[this, xpath](sysrepo::Session, uint32_t, const sysrepo::NotificationType type, const std::optional<libyang::DataNode> tree, const sysrepo::NotificationTimeStamp) {
if (type != sysrepo::NotificationType::Realtime) {
return;
diff --git a/tests/sysrepo-helpers/common.cpp b/tests/sysrepo-helpers/common.cpp
index fd6b4d7..262a5aa 100644
--- a/tests/sysrepo-helpers/common.cpp
+++ b/tests/sysrepo-helpers/common.cpp
@@ -64,3 +64,12 @@
velia::utils::ScopedDatastoreSwitch s(session, datastore);
return dataFromSysrepo(session, xpath);
}
+
+std::string moduleFromXpath(const std::string& xpath)
+{
+ auto pos = xpath.find(":");
+ if (pos == 0 || pos == std::string::npos || xpath[0] != '/') {
+ throw std::logic_error{"module_from_xpath: malformed XPath " + xpath};
+ }
+ return xpath.substr(1, pos - 1);
+}
diff --git a/tests/sysrepo-helpers/common.h b/tests/sysrepo-helpers/common.h
index 0cffbf2..917126a 100644
--- a/tests/sysrepo-helpers/common.h
+++ b/tests/sysrepo-helpers/common.h
@@ -18,6 +18,8 @@
Values rpcFromSysrepo(sysrepo::Session session, const std::string& rpcPath, std::map<std::string, std::string> input);
Values dataFromSysrepo(sysrepo::Session session, const std::string& xpath, sysrepo::Datastore datastore);
+std::string moduleFromXpath(const std::string& xpath);
+
#define TEST_SYSREPO_INIT \
auto srConn = sysrepo::Connection{}; \
auto srSess = srConn.sessionStart();