1 #include "dbus/dbusutil.hpp"
2 
3 #include <string>
4 #include <tuple>
5 #include <unordered_map>
6 #include <utility>
7 #include <vector>
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
12 namespace pid_control
13 {
14 namespace
15 {
16 
17 using ::testing::StrEq;
18 using ::testing::UnorderedElementsAreArray;
19 
20 class GetSensorPathTest :
21     public ::testing::TestWithParam<
22         std::tuple<std::string, std::string, std::string>>
23 {};
24 
25 TEST_P(GetSensorPathTest, ReturnsExpectedValue)
26 {
27     // type, id, output
28     const auto& params = GetParam();
29     EXPECT_THAT(getSensorPath(std::get<0>(params), std::get<1>(params)),
30                 StrEq(std::get<2>(params)));
31 }
32 
33 INSTANTIATE_TEST_CASE_P(
34     GetSensorPathTests, GetSensorPathTest,
35     ::testing::Values(
36         std::make_tuple("fan", "0", "/xyz/openbmc_project/sensors/fan_tach/0"),
37         std::make_tuple("as", "we", "/xyz/openbmc_project/sensors/unknown/we"),
38         std::make_tuple("margin", "9",
39                         "/xyz/openbmc_project/sensors/temperature/9"),
40         std::make_tuple("temp", "123",
41                         "/xyz/openbmc_project/sensors/temperature/123")));
42 
43 class FindSensorsTest : public ::testing::Test
44 {
45   protected:
46     const std::unordered_map<std::string, std::string> sensors = {
47         {"path_a", "b"},
48         {"apple", "juice"},
49         {"other_le", "thing"},
50     };
51 
52     std::vector<std::pair<std::string, std::string>> results;
53 };
54 
55 TEST_F(FindSensorsTest, NoMatches)
56 {
57     const std::string target = "abcd";
58 
59     EXPECT_FALSE(findSensors(sensors, target, results));
60 }
61 
62 TEST_F(FindSensorsTest, OneMatches)
63 {
64     const std::string target = "a";
65 
66     EXPECT_TRUE(findSensors(sensors, target, results));
67 
68     std::vector<std::pair<std::string, std::string>> expected_results = {
69         {"path_a", "b"},
70     };
71 
72     EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
73 }
74 
75 TEST_F(FindSensorsTest, MultipleMatches)
76 {
77     const std::string target = "le";
78     EXPECT_TRUE(findSensors(sensors, target, results));
79 
80     std::vector<std::pair<std::string, std::string>> expected_results = {
81         {"apple", "juice"},
82         {"other_le", "thing"},
83     };
84 
85     EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
86 }
87 
88 } // namespace
89 } // namespace pid_control
90