dbus: Move findSensors to dbus util
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I2ba4d3c430200eb58cb4364dcce694a080531846
diff --git a/dbus/dbusconfiguration.cpp b/dbus/dbusconfiguration.cpp
index f9d7790..90c2472 100644
--- a/dbus/dbusconfiguration.cpp
+++ b/dbus/dbusconfiguration.cpp
@@ -30,7 +30,6 @@
#include <functional>
#include <iostream>
#include <list>
-#include <regex>
#include <set>
#include <unordered_map>
#include <variant>
@@ -88,22 +87,6 @@
return retString;
}
-bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
- const std::string& search,
- std::vector<std::pair<std::string, std::string>>& matches)
-{
- std::smatch match;
- std::regex reg(search + '$');
- for (const auto& sensor : sensors)
- {
- if (std::regex_search(sensor.first, match, reg))
- {
- matches.push_back(sensor);
- }
- }
- return matches.size() > 0;
-}
-
// this function prints the configuration into a form similar to the cpp
// generated code to help in verification, should be turned off during normal
// use
diff --git a/dbus/dbusutil.cpp b/dbus/dbusutil.cpp
index 7545eed..6cdad14 100644
--- a/dbus/dbusutil.cpp
+++ b/dbus/dbusutil.cpp
@@ -4,9 +4,13 @@
#include <cstdint>
#include <iostream>
#include <map>
+#include <regex>
#include <set>
#include <string>
+#include <unordered_map>
+#include <utility>
#include <variant>
+#include <vector>
using Property = std::string;
using Value = std::variant<int64_t, double, std::string, bool>;
@@ -15,6 +19,22 @@
namespace pid_control
{
+bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
+ const std::string& search,
+ std::vector<std::pair<std::string, std::string>>& matches)
+{
+ std::smatch match;
+ std::regex reg(search + '$');
+ for (const auto& sensor : sensors)
+ {
+ if (std::regex_search(sensor.first, match, reg))
+ {
+ matches.push_back(sensor);
+ }
+ }
+ return matches.size() > 0;
+}
+
std::string getSensorPath(const std::string& type, const std::string& id)
{
std::string layer = type;
diff --git a/dbus/dbusutil.hpp b/dbus/dbusutil.hpp
index 8bd2824..420c8fb 100644
--- a/dbus/dbusutil.hpp
+++ b/dbus/dbusutil.hpp
@@ -2,6 +2,9 @@
#include <stdexcept>
#include <string>
+#include <unordered_map>
+#include <utility>
+#include <vector>
namespace pid_control
{
@@ -28,4 +31,8 @@
void scaleSensorReading(const double min, const double max, double& value);
bool validType(const std::string& type);
+bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
+ const std::string& search,
+ std::vector<std::pair<std::string, std::string>>& matches);
+
} // namespace pid_control
diff --git a/test/dbus_util_unittest.cpp b/test/dbus_util_unittest.cpp
index b02e1d6..0780cb1 100644
--- a/test/dbus_util_unittest.cpp
+++ b/test/dbus_util_unittest.cpp
@@ -2,6 +2,9 @@
#include <string>
#include <tuple>
+#include <unordered_map>
+#include <utility>
+#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -12,6 +15,7 @@
{
using ::testing::StrEq;
+using ::testing::UnorderedElementsAreArray;
class GetSensorPathTest :
public ::testing::TestWithParam<
@@ -36,5 +40,50 @@
std::make_tuple("temp", "123",
"/xyz/openbmc_project/sensors/temperature/123")));
+class FindSensorsTest : public ::testing::Test
+{
+ protected:
+ const std::unordered_map<std::string, std::string> sensors = {
+ {"path_a", "b"},
+ {"apple", "juice"},
+ {"other_le", "thing"},
+ };
+
+ std::vector<std::pair<std::string, std::string>> results;
+};
+
+TEST_F(FindSensorsTest, NoMatches)
+{
+ const std::string target = "abcd";
+
+ EXPECT_FALSE(findSensors(sensors, target, results));
+}
+
+TEST_F(FindSensorsTest, OneMatches)
+{
+ const std::string target = "a";
+
+ EXPECT_TRUE(findSensors(sensors, target, results));
+
+ std::vector<std::pair<std::string, std::string>> expected_results = {
+ {"path_a", "b"},
+ };
+
+ EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
+}
+
+TEST_F(FindSensorsTest, MultipleMatches)
+{
+ const std::string target = "le";
+ EXPECT_TRUE(findSensors(sensors, target, results));
+
+ std::vector<std::pair<std::string, std::string>> expected_results = {
+ {"apple", "juice"},
+ {"other_le", "thing"},
+ };
+
+ EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
+}
+
} // namespace
} // namespace pid_control