1 // THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged.
2 #pragma once
3
4 #include <sdbusplus/test/sdbus_mock.hpp>
5
6 #include <string>
7 #include <vector>
8
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11
12 namespace pid_control
13 {
14
15 using ::testing::_;
16 using ::testing::Invoke;
17 using ::testing::IsNull;
18 using ::testing::NotNull;
19 using ::testing::Return;
20 using ::testing::StrEq;
21
22 /** @brief Setup the expectations for sdbus-based object creation.
23 *
24 * Objects created that inherit a composition from sdbusplus will all
25 * require at least these expectations.
26 *
27 * If you have future sd_bus_emit_properties_changed_strv calls expected,
28 * you'll need to add those calls into your test. This only captures the
29 * property updates you tell it to expect initially.
30 *
31 * TODO: Make it support more cases, as I'm sure there are more.
32 *
33 * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
34 * the sdbusplus::bus_t you created.
35 * @param[in] defer - Whether object announcement is deferred.
36 * @param[in] path - the dbus path passed to the object
37 * @param[in] intf - the dbus interface
38 * @param[in] properties - an ordered list of expected property updates.
39 * @param[in] index - a pointer to a valid double in a surviving scope.
40 */
SetupDbusObject(sdbusplus::SdBusMock * sdbus_mock,bool defer,const std::string & path,const std::string & intf,const std::vector<std::string> & properties,double * index)41 void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer,
42 const std::string& path, const std::string& intf,
43 const std::vector<std::string>& properties, double* index)
44 {
45 if (!defer)
46 {
47 EXPECT_CALL(*sdbus_mock,
48 sd_bus_emit_object_added(IsNull(), StrEq(path)))
49 .WillOnce(Return(0));
50 }
51
52 if (!properties.empty())
53 {
54 (*index) = 0;
55 EXPECT_CALL(*sdbus_mock,
56 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
57 StrEq(intf), NotNull()))
58 .Times(properties.size())
59 .WillRepeatedly(Invoke([=]([[maybe_unused]] sd_bus* bus,
60 [[maybe_unused]] const char* path,
61 [[maybe_unused]] const char* interface,
62 const char** names) {
63 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
64 return 0;
65 }));
66 }
67
68 return;
69 }
70
71 } // namespace pid_control
72