Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 1 | #include <systemd/sd-bus-protocol.h> |
| 2 | |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame] | 3 | #include <sdbusplus/message.hpp> |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 5 | |
| 6 | #include <array> |
| 7 | #include <map> |
Ed Tanous | 28dc36d | 2018-02-21 12:22:54 -0800 | [diff] [blame] | 8 | #include <set> |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 9 | #include <span> |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 10 | #include <string> |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 11 | #include <tuple> |
| 12 | #include <unordered_map> |
Patrick Williams | b98bdc6 | 2020-06-16 16:15:40 -0500 | [diff] [blame] | 13 | #include <unordered_set> |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 14 | #include <variant> |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 15 | #include <vector> |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 16 | |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 20 | namespace |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 21 | { |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 22 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 23 | using testing::Eq; |
| 24 | using testing::MatcherCast; |
| 25 | using testing::Pointee; |
| 26 | using testing::Return; |
| 27 | using testing::SafeMatcherCast; |
| 28 | using testing::StrEq; |
| 29 | |
Ed Tanous | 640a7d5 | 2023-01-04 13:17:55 -0800 | [diff] [blame] | 30 | MATCHER_P(iovec_equal, match_string, "") |
| 31 | { |
| 32 | const char* start = std::bit_cast<char*>(arg->iov_base); |
| 33 | return std::string(start, arg->iov_len) == match_string; |
| 34 | } |
| 35 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 36 | class AppendTest : public testing::Test |
| 37 | { |
| 38 | protected: |
| 39 | testing::StrictMock<sdbusplus::SdBusMock> mock; |
| 40 | |
| 41 | void SetUp() override |
| 42 | { |
| 43 | EXPECT_CALL(mock, sd_bus_message_new_method_call(testing::_, testing::_, |
| 44 | nullptr, nullptr, |
| 45 | nullptr, nullptr)) |
| 46 | .WillRepeatedly(Return(0)); |
Ed Tanous | cb2fbeb | 2023-01-06 13:16:19 -0800 | [diff] [blame] | 47 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 48 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 49 | sdbusplus::message_t new_message() |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 50 | { |
| 51 | return sdbusplus::get_mocked_new(&mock).new_method_call( |
| 52 | nullptr, nullptr, nullptr, nullptr); |
| 53 | } |
| 54 | |
Patrick Venture | 2b238af | 2018-08-31 12:45:01 -0700 | [diff] [blame] | 55 | template <typename T> |
| 56 | void expect_basic(char type, T val) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 57 | { |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 58 | EXPECT_CALL(mock, sd_bus_message_append_basic( |
| 59 | nullptr, type, |
| 60 | MatcherCast<const void*>( |
| 61 | SafeMatcherCast<const T*>(Pointee(Eq(val)))))) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 62 | .WillOnce(Return(0)); |
| 63 | } |
| 64 | |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 65 | void expect_basic_string(char type, const char* str) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 66 | { |
| 67 | EXPECT_CALL(mock, sd_bus_message_append_basic( |
| 68 | nullptr, type, |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 69 | MatcherCast<const void*>( |
| 70 | SafeMatcherCast<const char*>(StrEq(str))))) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 71 | .WillOnce(Return(0)); |
| 72 | } |
Ed Tanous | 640a7d5 | 2023-01-04 13:17:55 -0800 | [diff] [blame] | 73 | void expect_basic_string_iovec(const char* str, size_t size) |
| 74 | { |
| 75 | std::string tmp = {str, size}; |
| 76 | EXPECT_CALL(mock, sd_bus_message_append_string_iovec( |
| 77 | nullptr, iovec_equal(tmp), 1)) |
| 78 | .WillOnce(Return(0)); |
| 79 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 80 | |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 81 | void expect_open_container(char type, const char* contents) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 82 | { |
| 83 | EXPECT_CALL( |
| 84 | mock, sd_bus_message_open_container(nullptr, type, StrEq(contents))) |
| 85 | .WillOnce(Return(0)); |
| 86 | } |
| 87 | |
| 88 | void expect_close_container() |
| 89 | { |
| 90 | EXPECT_CALL(mock, sd_bus_message_close_container(nullptr)) |
| 91 | .WillOnce(Return(0)); |
| 92 | } |
Ed Tanous | 242677a | 2024-03-27 17:56:21 -0700 | [diff] [blame^] | 93 | |
| 94 | static int on_array_append(sd_bus_message*, char, const void*, size_t) |
| 95 | { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | void expect_append_array(char type, size_t sz) |
| 100 | { |
| 101 | EXPECT_CALL(mock, |
| 102 | sd_bus_message_append_array(nullptr, type, testing::_, sz)) |
| 103 | .WillOnce(testing::Invoke(on_array_append)); |
| 104 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | TEST_F(AppendTest, RValueInt) |
| 108 | { |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 109 | static_assert( |
| 110 | sdbusplus::message::details::can_append_multiple_v<decltype(1)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 111 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 112 | new_message().append(1); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 113 | } |
| 114 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 115 | TEST_F(AppendTest, LValueInt) |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 116 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 117 | const int a = 1; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 118 | static_assert( |
| 119 | sdbusplus::message::details::can_append_multiple_v<decltype(a)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 120 | expect_basic<int>(SD_BUS_TYPE_INT32, a); |
| 121 | new_message().append(a); |
| 122 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 123 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 124 | TEST_F(AppendTest, XValueInt) |
| 125 | { |
| 126 | int a = 1; |
| 127 | expect_basic<int>(SD_BUS_TYPE_INT32, a); |
| 128 | new_message().append(std::move(a)); |
| 129 | } |
| 130 | |
| 131 | TEST_F(AppendTest, RValueBool) |
| 132 | { |
| 133 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, true); |
| 134 | new_message().append(true); |
| 135 | } |
| 136 | |
| 137 | TEST_F(AppendTest, LValueBool) |
| 138 | { |
| 139 | const bool a = false; |
| 140 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 141 | new_message().append(a); |
| 142 | } |
| 143 | |
| 144 | TEST_F(AppendTest, XValueBool) |
| 145 | { |
| 146 | bool a = false; |
| 147 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 148 | new_message().append(std::move(a)); |
| 149 | } |
| 150 | |
| 151 | TEST_F(AppendTest, RValueDouble) |
| 152 | { |
| 153 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, 1.1); |
| 154 | new_message().append(1.1); |
| 155 | } |
| 156 | |
| 157 | TEST_F(AppendTest, LValueDouble) |
| 158 | { |
| 159 | const double a = 1.1; |
| 160 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, a); |
| 161 | new_message().append(a); |
| 162 | } |
| 163 | |
| 164 | TEST_F(AppendTest, XValueDouble) |
| 165 | { |
| 166 | double a = 1.1; |
| 167 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, a); |
| 168 | new_message().append(std::move(a)); |
| 169 | } |
| 170 | |
| 171 | TEST_F(AppendTest, RValueCString) |
| 172 | { |
| 173 | expect_basic_string(SD_BUS_TYPE_STRING, "asdf"); |
| 174 | new_message().append("asdf"); |
| 175 | } |
| 176 | |
| 177 | TEST_F(AppendTest, LValueCString) |
| 178 | { |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 179 | const char* const s = "asdf"; |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 180 | expect_basic_string(SD_BUS_TYPE_STRING, s); |
| 181 | new_message().append(s); |
| 182 | } |
| 183 | |
| 184 | TEST_F(AppendTest, XValueCString) |
| 185 | { |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 186 | const char* s = "asdf"; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 187 | static_assert( |
| 188 | sdbusplus::message::details::can_append_multiple_v<decltype(s)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 189 | expect_basic_string(SD_BUS_TYPE_STRING, s); |
| 190 | new_message().append(std::move(s)); |
| 191 | } |
| 192 | |
| 193 | TEST_F(AppendTest, RValueString) |
| 194 | { |
| 195 | expect_basic_string(SD_BUS_TYPE_STRING, "asdf"); |
| 196 | new_message().append(std::string{"asdf"}); |
| 197 | } |
| 198 | |
| 199 | TEST_F(AppendTest, LValueString) |
| 200 | { |
| 201 | std::string s{"asdf"}; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 202 | static_assert( |
| 203 | !sdbusplus::message::details::can_append_multiple_v<decltype(s)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 204 | expect_basic_string(SD_BUS_TYPE_STRING, s.c_str()); |
| 205 | new_message().append(s); |
| 206 | } |
| 207 | |
| 208 | TEST_F(AppendTest, XValueString) |
| 209 | { |
| 210 | std::string s{"asdf"}; |
| 211 | expect_basic_string(SD_BUS_TYPE_STRING, s.c_str()); |
| 212 | new_message().append(std::move(s)); |
| 213 | } |
| 214 | |
Ed Tanous | 640a7d5 | 2023-01-04 13:17:55 -0800 | [diff] [blame] | 215 | TEST_F(AppendTest, LValueStringView) |
| 216 | { |
| 217 | std::string_view s{"asdf"}; |
| 218 | expect_basic_string_iovec(s.data(), s.size()); |
| 219 | new_message().append(s); |
| 220 | } |
| 221 | |
| 222 | TEST_F(AppendTest, RValueStringView) |
| 223 | { |
| 224 | std::string_view s{"asdf"}; |
| 225 | expect_basic_string_iovec(s.data(), s.size()); |
| 226 | new_message().append(std::string_view{"asdf"}); |
| 227 | } |
| 228 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 229 | TEST_F(AppendTest, ObjectPath) |
| 230 | { |
| 231 | sdbusplus::message::object_path o{"/asdf"}; |
| 232 | expect_basic_string(SD_BUS_TYPE_OBJECT_PATH, o.str.c_str()); |
| 233 | new_message().append(o); |
| 234 | } |
| 235 | |
| 236 | TEST_F(AppendTest, Signature) |
| 237 | { |
| 238 | sdbusplus::message::signature g{"ii"}; |
| 239 | expect_basic_string(SD_BUS_TYPE_SIGNATURE, g.str.c_str()); |
| 240 | new_message().append(g); |
| 241 | } |
| 242 | |
| 243 | TEST_F(AppendTest, CombinedBasic) |
| 244 | { |
| 245 | const int c = 3; |
| 246 | const std::string s1{"fdsa"}; |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 247 | const char* const s2 = "asdf"; |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 248 | |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 249 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 250 | testing::InSequence seq; |
| 251 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 252 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, 2.2); |
| 253 | expect_basic<int>(SD_BUS_TYPE_INT32, c); |
| 254 | expect_basic_string(SD_BUS_TYPE_STRING, s1.c_str()); |
| 255 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 256 | expect_basic_string(SD_BUS_TYPE_STRING, s2); |
| 257 | } |
| 258 | new_message().append(1, 2.2, c, s1, false, s2); |
| 259 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 260 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 261 | TEST_F(AppendTest, Array) |
| 262 | { |
| 263 | const std::array<double, 4> a{1.1, 2.2, 3.3, 4.4}; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 264 | static_assert( |
| 265 | !sdbusplus::message::details::can_append_multiple_v<decltype(a)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 266 | |
| 267 | { |
Ed Tanous | 242677a | 2024-03-27 17:56:21 -0700 | [diff] [blame^] | 268 | expect_append_array(SD_BUS_TYPE_DOUBLE, a.size() * sizeof(double)); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 269 | } |
| 270 | new_message().append(a); |
| 271 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 272 | |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 273 | TEST_F(AppendTest, Span) |
| 274 | { |
| 275 | const std::array<double, 4> a{1.1, 2.2, 3.3, 4.4}; |
| 276 | auto s = std::span{a}; |
| 277 | static_assert( |
| 278 | !sdbusplus::message::details::can_append_multiple_v<decltype(s)>); |
| 279 | |
| 280 | { |
Ed Tanous | 242677a | 2024-03-27 17:56:21 -0700 | [diff] [blame^] | 281 | expect_append_array(SD_BUS_TYPE_DOUBLE, a.size() * sizeof(double)); |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 282 | } |
| 283 | new_message().append(s); |
| 284 | } |
| 285 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 286 | TEST_F(AppendTest, Vector) |
| 287 | { |
Ed Tanous | f4265a7 | 2024-03-04 21:07:05 -0800 | [diff] [blame] | 288 | const std::vector<std::string> v{"a", "b", "c", "d"}; |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 289 | |
| 290 | { |
| 291 | testing::InSequence seq; |
Ed Tanous | f4265a7 | 2024-03-04 21:07:05 -0800 | [diff] [blame] | 292 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 293 | for (const auto& i : v) |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 294 | { |
Ed Tanous | f4265a7 | 2024-03-04 21:07:05 -0800 | [diff] [blame] | 295 | expect_basic_string(SD_BUS_TYPE_STRING, i.c_str()); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 296 | } |
| 297 | expect_close_container(); |
| 298 | } |
| 299 | new_message().append(v); |
| 300 | } |
| 301 | |
Ed Tanous | 242677a | 2024-03-27 17:56:21 -0700 | [diff] [blame^] | 302 | TEST_F(AppendTest, VectorIntegral) |
| 303 | { |
| 304 | const std::vector<int32_t> v{1, 2, 3, 4}; |
| 305 | expect_append_array(SD_BUS_TYPE_INT32, v.size() * sizeof(int32_t)); |
| 306 | new_message().append(v); |
| 307 | } |
| 308 | |
| 309 | TEST_F(AppendTest, VectorNestIntegral) |
| 310 | { |
| 311 | const std::vector<std::array<int32_t, 3>> v{ |
| 312 | {1, 2, 3}, {3, 4, 5}, {6, 7, 8}}; |
| 313 | |
| 314 | { |
| 315 | testing::InSequence seq; |
| 316 | expect_open_container(SD_BUS_TYPE_ARRAY, "ai"); |
| 317 | for (long unsigned int i = 0; i < v.size(); i++) |
| 318 | { |
| 319 | expect_append_array(SD_BUS_TYPE_INT32, |
| 320 | v[i].size() * sizeof(int32_t)); |
| 321 | } |
| 322 | expect_close_container(); |
| 323 | } |
| 324 | new_message().append(v); |
| 325 | } |
| 326 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 327 | TEST_F(AppendTest, Set) |
| 328 | { |
| 329 | const std::set<std::string> s{"one", "two", "eight"}; |
| 330 | |
| 331 | { |
| 332 | testing::InSequence seq; |
| 333 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 334 | for (const auto& i : s) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 335 | { |
| 336 | expect_basic_string(SD_BUS_TYPE_STRING, i.c_str()); |
| 337 | } |
| 338 | expect_close_container(); |
| 339 | } |
| 340 | new_message().append(s); |
| 341 | } |
| 342 | |
Patrick Williams | b98bdc6 | 2020-06-16 16:15:40 -0500 | [diff] [blame] | 343 | TEST_F(AppendTest, UnorderedSet) |
| 344 | { |
| 345 | const std::unordered_set<std::string> s{"one", "two", "eight"}; |
| 346 | |
| 347 | { |
| 348 | testing::InSequence seq; |
| 349 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
| 350 | for (const auto& i : s) |
| 351 | { |
| 352 | expect_basic_string(SD_BUS_TYPE_STRING, i.c_str()); |
| 353 | } |
| 354 | expect_close_container(); |
| 355 | } |
| 356 | new_message().append(s); |
| 357 | } |
| 358 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 359 | TEST_F(AppendTest, Map) |
| 360 | { |
| 361 | const std::map<int, std::string> m{ |
| 362 | {1, "a"}, |
| 363 | {2, "bc"}, |
| 364 | {3, "def"}, |
| 365 | {4, "ghij"}, |
| 366 | }; |
| 367 | |
| 368 | { |
| 369 | testing::InSequence seq; |
| 370 | expect_open_container(SD_BUS_TYPE_ARRAY, "{is}"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 371 | for (const auto& i : m) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 372 | { |
| 373 | expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "is"); |
| 374 | expect_basic<int>(SD_BUS_TYPE_INT32, i.first); |
| 375 | expect_basic_string(SD_BUS_TYPE_STRING, i.second.c_str()); |
| 376 | expect_close_container(); |
| 377 | } |
| 378 | expect_close_container(); |
| 379 | } |
| 380 | new_message().append(m); |
| 381 | } |
| 382 | |
| 383 | TEST_F(AppendTest, UnorderedMap) |
| 384 | { |
| 385 | const std::unordered_map<int, bool> m{ |
| 386 | {1, false}, |
| 387 | {2, true}, |
| 388 | {3, true}, |
| 389 | {4, false}, |
| 390 | }; |
| 391 | |
| 392 | { |
| 393 | testing::InSequence seq; |
| 394 | expect_open_container(SD_BUS_TYPE_ARRAY, "{ib}"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 395 | for (const auto& i : m) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 396 | { |
| 397 | expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "ib"); |
| 398 | expect_basic<int>(SD_BUS_TYPE_INT32, i.first); |
| 399 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, i.second); |
| 400 | expect_close_container(); |
| 401 | } |
| 402 | expect_close_container(); |
| 403 | } |
| 404 | new_message().append(m); |
| 405 | } |
| 406 | |
| 407 | TEST_F(AppendTest, Tuple) |
| 408 | { |
| 409 | const std::tuple<int, std::string, bool> t{5, "asdf", false}; |
| 410 | |
| 411 | { |
| 412 | testing::InSequence seq; |
| 413 | expect_open_container(SD_BUS_TYPE_STRUCT, "isb"); |
| 414 | expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(t)); |
| 415 | expect_basic_string(SD_BUS_TYPE_STRING, std::get<1>(t).c_str()); |
| 416 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(t)); |
| 417 | expect_close_container(); |
| 418 | } |
| 419 | new_message().append(t); |
| 420 | } |
| 421 | |
| 422 | TEST_F(AppendTest, Variant) |
| 423 | { |
| 424 | const bool b1 = false; |
| 425 | const std::string s2{"asdf"}; |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 426 | const std::variant<int, std::string, bool> v1{b1}, v2{s2}; |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 427 | |
| 428 | { |
| 429 | testing::InSequence seq; |
| 430 | expect_open_container(SD_BUS_TYPE_VARIANT, "b"); |
| 431 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1); |
| 432 | expect_close_container(); |
| 433 | expect_open_container(SD_BUS_TYPE_VARIANT, "s"); |
| 434 | expect_basic_string(SD_BUS_TYPE_STRING, s2.c_str()); |
| 435 | expect_close_container(); |
| 436 | } |
| 437 | new_message().append(v1, v2); |
| 438 | } |
| 439 | |
| 440 | TEST_F(AppendTest, LargeCombo) |
| 441 | { |
| 442 | std::vector<std::array<std::string, 3>> vas{{"a", "b", "c"}, |
| 443 | {"d", "", "e"}}; |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 444 | std::map<std::string, std::variant<int, double>> msv = { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 445 | {"a", 3.3}, {"b", 1}, {"c", 4.4}}; |
| 446 | |
| 447 | { |
| 448 | testing::InSequence seq; |
| 449 | |
| 450 | expect_open_container(SD_BUS_TYPE_ARRAY, "as"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 451 | for (const auto& as : vas) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 452 | { |
| 453 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 454 | for (const auto& s : as) |
Patrick Williams | 07e1d6f | 2016-09-08 15:54:09 -0500 | [diff] [blame] | 455 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 456 | expect_basic_string(SD_BUS_TYPE_STRING, s.c_str()); |
| 457 | } |
| 458 | expect_close_container(); |
| 459 | } |
| 460 | expect_close_container(); |
| 461 | |
| 462 | expect_open_container(SD_BUS_TYPE_ARRAY, "{sv}"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 463 | for (const auto& sv : msv) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 464 | { |
| 465 | expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "sv"); |
| 466 | expect_basic_string(SD_BUS_TYPE_STRING, sv.first.c_str()); |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 467 | if (std::holds_alternative<int>(sv.second)) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 468 | { |
| 469 | expect_open_container(SD_BUS_TYPE_VARIANT, "i"); |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 470 | expect_basic<int>(SD_BUS_TYPE_INT32, std::get<int>(sv.second)); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 471 | expect_close_container(); |
Patrick Williams | 07e1d6f | 2016-09-08 15:54:09 -0500 | [diff] [blame] | 472 | } |
| 473 | else |
| 474 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 475 | expect_open_container(SD_BUS_TYPE_VARIANT, "d"); |
| 476 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 477 | std::get<double>(sv.second)); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 478 | expect_close_container(); |
Patrick Williams | 07e1d6f | 2016-09-08 15:54:09 -0500 | [diff] [blame] | 479 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 480 | expect_close_container(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 481 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 482 | expect_close_container(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 483 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 484 | new_message().append(vas, msv); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 485 | } |
| 486 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 487 | } // namespace |