summaryrefslogtreecommitdiffstats
path: root/test/helpers.hpp
blob: 931a39ef5ef9c3e67ea04ff2e9181185eb06fef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged.
#pragma once

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <sdbusplus/test/sdbus_mock.hpp>
#include <string>
#include <vector>

using ::testing::Invoke;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::_;

/** @brief Setup the expectations for sdbus-based object creation.
 *
 * Objects created that inherit a composition from sdbusplus will all
 * require at least these expectations.
 *
 * If you have future sd_bus_emit_properties_changed_strv calls expected,
 * you'll need to add those calls into your test.  This only captures the
 * property updates you tell it to expect initially.
 *
 * TODO: Make it support more cases, as I'm sure there are more.
 *
 * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
 *     the sdbusplus::bus::bus you created.
 * @param[in] defer - Whether object announcement is deferred.
 * @param[in] path - the dbus path passed to the object
 * @param[in] intf - the dbus interface
 * @param[in] properties - an ordered list of expected property updates.
 * @param[in] index - a pointer to a valid integer in a surviving scope.
 */
void SetupDbusObject(sdbusplus::SdBusMock *sdbus_mock, bool defer,
                     const std::string &path, const std::string &intf,
                     const std::vector<std::string> &properties, int *index)
{
    EXPECT_CALL(*sdbus_mock,
                sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
                                         StrEq(intf), NotNull(), NotNull()))
        .WillOnce(Return(0));

    if (!defer)
    {
        EXPECT_CALL(*sdbus_mock,
                    sd_bus_emit_object_added(IsNull(), StrEq(path)))
            .WillOnce(Return(0));
    }

    if (properties.empty())
    {
        // We always expect one, but in this case we're not concerned with the
        // output.  If there is more than one property update, we should care
        // and the test writer can add the code to trigger the below case.
        EXPECT_CALL(*sdbus_mock,
                    sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
                                                        StrEq(intf), NotNull()))
            .WillOnce(Return(0));
    }
    else
    {
        (*index) = 0;
        EXPECT_CALL(*sdbus_mock,
                    sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
                                                        StrEq(intf), NotNull()))
            .Times(properties.size())
            .WillRepeatedly(Invoke([=](sd_bus *bus, const char *path,
                                       const char *interface, char **names) {
                EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
                return 0;
            }));
    }

    return;
}
OpenPOWER on IntegriCloud