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 | } |
| 93 | }; |
| 94 | |
| 95 | TEST_F(AppendTest, RValueInt) |
| 96 | { |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 97 | static_assert( |
| 98 | sdbusplus::message::details::can_append_multiple_v<decltype(1)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 99 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 100 | new_message().append(1); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 101 | } |
| 102 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 103 | TEST_F(AppendTest, LValueInt) |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 104 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 105 | const int a = 1; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 106 | static_assert( |
| 107 | sdbusplus::message::details::can_append_multiple_v<decltype(a)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 108 | expect_basic<int>(SD_BUS_TYPE_INT32, a); |
| 109 | new_message().append(a); |
| 110 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 111 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 112 | TEST_F(AppendTest, XValueInt) |
| 113 | { |
| 114 | int a = 1; |
| 115 | expect_basic<int>(SD_BUS_TYPE_INT32, a); |
| 116 | new_message().append(std::move(a)); |
| 117 | } |
| 118 | |
| 119 | TEST_F(AppendTest, RValueBool) |
| 120 | { |
| 121 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, true); |
| 122 | new_message().append(true); |
| 123 | } |
| 124 | |
| 125 | TEST_F(AppendTest, LValueBool) |
| 126 | { |
| 127 | const bool a = false; |
| 128 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 129 | new_message().append(a); |
| 130 | } |
| 131 | |
| 132 | TEST_F(AppendTest, XValueBool) |
| 133 | { |
| 134 | bool a = false; |
| 135 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 136 | new_message().append(std::move(a)); |
| 137 | } |
| 138 | |
| 139 | TEST_F(AppendTest, RValueDouble) |
| 140 | { |
| 141 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, 1.1); |
| 142 | new_message().append(1.1); |
| 143 | } |
| 144 | |
| 145 | TEST_F(AppendTest, LValueDouble) |
| 146 | { |
| 147 | const double a = 1.1; |
| 148 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, a); |
| 149 | new_message().append(a); |
| 150 | } |
| 151 | |
| 152 | TEST_F(AppendTest, XValueDouble) |
| 153 | { |
| 154 | double a = 1.1; |
| 155 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, a); |
| 156 | new_message().append(std::move(a)); |
| 157 | } |
| 158 | |
| 159 | TEST_F(AppendTest, RValueCString) |
| 160 | { |
| 161 | expect_basic_string(SD_BUS_TYPE_STRING, "asdf"); |
| 162 | new_message().append("asdf"); |
| 163 | } |
| 164 | |
| 165 | TEST_F(AppendTest, LValueCString) |
| 166 | { |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 167 | const char* const s = "asdf"; |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 168 | expect_basic_string(SD_BUS_TYPE_STRING, s); |
| 169 | new_message().append(s); |
| 170 | } |
| 171 | |
| 172 | TEST_F(AppendTest, XValueCString) |
| 173 | { |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 174 | const char* s = "asdf"; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 175 | static_assert( |
| 176 | sdbusplus::message::details::can_append_multiple_v<decltype(s)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 177 | expect_basic_string(SD_BUS_TYPE_STRING, s); |
| 178 | new_message().append(std::move(s)); |
| 179 | } |
| 180 | |
| 181 | TEST_F(AppendTest, RValueString) |
| 182 | { |
| 183 | expect_basic_string(SD_BUS_TYPE_STRING, "asdf"); |
| 184 | new_message().append(std::string{"asdf"}); |
| 185 | } |
| 186 | |
| 187 | TEST_F(AppendTest, LValueString) |
| 188 | { |
| 189 | std::string s{"asdf"}; |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 190 | static_assert( |
| 191 | !sdbusplus::message::details::can_append_multiple_v<decltype(s)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 192 | expect_basic_string(SD_BUS_TYPE_STRING, s.c_str()); |
| 193 | new_message().append(s); |
| 194 | } |
| 195 | |
| 196 | TEST_F(AppendTest, XValueString) |
| 197 | { |
| 198 | std::string s{"asdf"}; |
| 199 | expect_basic_string(SD_BUS_TYPE_STRING, s.c_str()); |
| 200 | new_message().append(std::move(s)); |
| 201 | } |
| 202 | |
Ed Tanous | 640a7d5 | 2023-01-04 13:17:55 -0800 | [diff] [blame] | 203 | TEST_F(AppendTest, LValueStringView) |
| 204 | { |
| 205 | std::string_view s{"asdf"}; |
| 206 | expect_basic_string_iovec(s.data(), s.size()); |
| 207 | new_message().append(s); |
| 208 | } |
| 209 | |
| 210 | TEST_F(AppendTest, RValueStringView) |
| 211 | { |
| 212 | std::string_view s{"asdf"}; |
| 213 | expect_basic_string_iovec(s.data(), s.size()); |
| 214 | new_message().append(std::string_view{"asdf"}); |
| 215 | } |
| 216 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 217 | TEST_F(AppendTest, ObjectPath) |
| 218 | { |
| 219 | sdbusplus::message::object_path o{"/asdf"}; |
| 220 | expect_basic_string(SD_BUS_TYPE_OBJECT_PATH, o.str.c_str()); |
| 221 | new_message().append(o); |
| 222 | } |
| 223 | |
| 224 | TEST_F(AppendTest, Signature) |
| 225 | { |
| 226 | sdbusplus::message::signature g{"ii"}; |
| 227 | expect_basic_string(SD_BUS_TYPE_SIGNATURE, g.str.c_str()); |
| 228 | new_message().append(g); |
| 229 | } |
| 230 | |
| 231 | TEST_F(AppendTest, CombinedBasic) |
| 232 | { |
| 233 | const int c = 3; |
| 234 | const std::string s1{"fdsa"}; |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 235 | const char* const s2 = "asdf"; |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 236 | |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 237 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 238 | testing::InSequence seq; |
| 239 | expect_basic<int>(SD_BUS_TYPE_INT32, 1); |
| 240 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, 2.2); |
| 241 | expect_basic<int>(SD_BUS_TYPE_INT32, c); |
| 242 | expect_basic_string(SD_BUS_TYPE_STRING, s1.c_str()); |
| 243 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, false); |
| 244 | expect_basic_string(SD_BUS_TYPE_STRING, s2); |
| 245 | } |
| 246 | new_message().append(1, 2.2, c, s1, false, s2); |
| 247 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 248 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 249 | TEST_F(AppendTest, Array) |
| 250 | { |
| 251 | 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] | 252 | static_assert( |
| 253 | !sdbusplus::message::details::can_append_multiple_v<decltype(a)>); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 254 | |
| 255 | { |
| 256 | testing::InSequence seq; |
| 257 | expect_open_container(SD_BUS_TYPE_ARRAY, "d"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 258 | for (const auto& i : a) |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 259 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 260 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, i); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 261 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 262 | expect_close_container(); |
| 263 | } |
| 264 | new_message().append(a); |
| 265 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 266 | |
Patrick Williams | b7329a9 | 2022-04-28 09:57:23 -0500 | [diff] [blame] | 267 | TEST_F(AppendTest, Span) |
| 268 | { |
| 269 | const std::array<double, 4> a{1.1, 2.2, 3.3, 4.4}; |
| 270 | auto s = std::span{a}; |
| 271 | static_assert( |
| 272 | !sdbusplus::message::details::can_append_multiple_v<decltype(s)>); |
| 273 | |
| 274 | { |
| 275 | testing::InSequence seq; |
| 276 | expect_open_container(SD_BUS_TYPE_ARRAY, "d"); |
| 277 | for (const auto& i : s) |
| 278 | { |
| 279 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, i); |
| 280 | } |
| 281 | expect_close_container(); |
| 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 | { |
| 288 | const std::vector<int> v{1, 2, 3, 4}; |
| 289 | |
| 290 | { |
| 291 | testing::InSequence seq; |
| 292 | expect_open_container(SD_BUS_TYPE_ARRAY, "i"); |
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 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 295 | expect_basic<int>(SD_BUS_TYPE_INT32, i); |
| 296 | } |
| 297 | expect_close_container(); |
| 298 | } |
| 299 | new_message().append(v); |
| 300 | } |
| 301 | |
| 302 | TEST_F(AppendTest, Set) |
| 303 | { |
| 304 | const std::set<std::string> s{"one", "two", "eight"}; |
| 305 | |
| 306 | { |
| 307 | testing::InSequence seq; |
| 308 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 309 | for (const auto& i : s) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 310 | { |
| 311 | expect_basic_string(SD_BUS_TYPE_STRING, i.c_str()); |
| 312 | } |
| 313 | expect_close_container(); |
| 314 | } |
| 315 | new_message().append(s); |
| 316 | } |
| 317 | |
Patrick Williams | b98bdc6 | 2020-06-16 16:15:40 -0500 | [diff] [blame] | 318 | TEST_F(AppendTest, UnorderedSet) |
| 319 | { |
| 320 | const std::unordered_set<std::string> s{"one", "two", "eight"}; |
| 321 | |
| 322 | { |
| 323 | testing::InSequence seq; |
| 324 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
| 325 | for (const auto& i : s) |
| 326 | { |
| 327 | expect_basic_string(SD_BUS_TYPE_STRING, i.c_str()); |
| 328 | } |
| 329 | expect_close_container(); |
| 330 | } |
| 331 | new_message().append(s); |
| 332 | } |
| 333 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 334 | TEST_F(AppendTest, Map) |
| 335 | { |
| 336 | const std::map<int, std::string> m{ |
| 337 | {1, "a"}, |
| 338 | {2, "bc"}, |
| 339 | {3, "def"}, |
| 340 | {4, "ghij"}, |
| 341 | }; |
| 342 | |
| 343 | { |
| 344 | testing::InSequence seq; |
| 345 | expect_open_container(SD_BUS_TYPE_ARRAY, "{is}"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 346 | for (const auto& i : m) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 347 | { |
| 348 | expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "is"); |
| 349 | expect_basic<int>(SD_BUS_TYPE_INT32, i.first); |
| 350 | expect_basic_string(SD_BUS_TYPE_STRING, i.second.c_str()); |
| 351 | expect_close_container(); |
| 352 | } |
| 353 | expect_close_container(); |
| 354 | } |
| 355 | new_message().append(m); |
| 356 | } |
| 357 | |
| 358 | TEST_F(AppendTest, UnorderedMap) |
| 359 | { |
| 360 | const std::unordered_map<int, bool> m{ |
| 361 | {1, false}, |
| 362 | {2, true}, |
| 363 | {3, true}, |
| 364 | {4, false}, |
| 365 | }; |
| 366 | |
| 367 | { |
| 368 | testing::InSequence seq; |
| 369 | expect_open_container(SD_BUS_TYPE_ARRAY, "{ib}"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 370 | for (const auto& i : m) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 371 | { |
| 372 | expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "ib"); |
| 373 | expect_basic<int>(SD_BUS_TYPE_INT32, i.first); |
| 374 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, i.second); |
| 375 | expect_close_container(); |
| 376 | } |
| 377 | expect_close_container(); |
| 378 | } |
| 379 | new_message().append(m); |
| 380 | } |
| 381 | |
| 382 | TEST_F(AppendTest, Tuple) |
| 383 | { |
| 384 | const std::tuple<int, std::string, bool> t{5, "asdf", false}; |
| 385 | |
| 386 | { |
| 387 | testing::InSequence seq; |
| 388 | expect_open_container(SD_BUS_TYPE_STRUCT, "isb"); |
| 389 | expect_basic<int>(SD_BUS_TYPE_INT32, std::get<0>(t)); |
| 390 | expect_basic_string(SD_BUS_TYPE_STRING, std::get<1>(t).c_str()); |
| 391 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, std::get<2>(t)); |
| 392 | expect_close_container(); |
| 393 | } |
| 394 | new_message().append(t); |
| 395 | } |
| 396 | |
| 397 | TEST_F(AppendTest, Variant) |
| 398 | { |
| 399 | const bool b1 = false; |
| 400 | const std::string s2{"asdf"}; |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 401 | 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] | 402 | |
| 403 | { |
| 404 | testing::InSequence seq; |
| 405 | expect_open_container(SD_BUS_TYPE_VARIANT, "b"); |
| 406 | expect_basic<int>(SD_BUS_TYPE_BOOLEAN, b1); |
| 407 | expect_close_container(); |
| 408 | expect_open_container(SD_BUS_TYPE_VARIANT, "s"); |
| 409 | expect_basic_string(SD_BUS_TYPE_STRING, s2.c_str()); |
| 410 | expect_close_container(); |
| 411 | } |
| 412 | new_message().append(v1, v2); |
| 413 | } |
| 414 | |
| 415 | TEST_F(AppendTest, LargeCombo) |
| 416 | { |
| 417 | std::vector<std::array<std::string, 3>> vas{{"a", "b", "c"}, |
| 418 | {"d", "", "e"}}; |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 419 | std::map<std::string, std::variant<int, double>> msv = { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 420 | {"a", 3.3}, {"b", 1}, {"c", 4.4}}; |
| 421 | |
| 422 | { |
| 423 | testing::InSequence seq; |
| 424 | |
| 425 | expect_open_container(SD_BUS_TYPE_ARRAY, "as"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 426 | for (const auto& as : vas) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 427 | { |
| 428 | expect_open_container(SD_BUS_TYPE_ARRAY, "s"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 429 | for (const auto& s : as) |
Patrick Williams | 07e1d6f | 2016-09-08 15:54:09 -0500 | [diff] [blame] | 430 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 431 | expect_basic_string(SD_BUS_TYPE_STRING, s.c_str()); |
| 432 | } |
| 433 | expect_close_container(); |
| 434 | } |
| 435 | expect_close_container(); |
| 436 | |
| 437 | expect_open_container(SD_BUS_TYPE_ARRAY, "{sv}"); |
William A. Kennington III | e0d6965 | 2018-08-31 13:09:47 -0700 | [diff] [blame] | 438 | for (const auto& sv : msv) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 439 | { |
| 440 | expect_open_container(SD_BUS_TYPE_DICT_ENTRY, "sv"); |
| 441 | 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] | 442 | if (std::holds_alternative<int>(sv.second)) |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 443 | { |
| 444 | expect_open_container(SD_BUS_TYPE_VARIANT, "i"); |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 445 | 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] | 446 | expect_close_container(); |
Patrick Williams | 07e1d6f | 2016-09-08 15:54:09 -0500 | [diff] [blame] | 447 | } |
| 448 | else |
| 449 | { |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 450 | expect_open_container(SD_BUS_TYPE_VARIANT, "d"); |
| 451 | expect_basic<double>(SD_BUS_TYPE_DOUBLE, |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 452 | std::get<double>(sv.second)); |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 453 | expect_close_container(); |
Patrick Williams | 07e1d6f | 2016-09-08 15:54:09 -0500 | [diff] [blame] | 454 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 455 | expect_close_container(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 456 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 457 | expect_close_container(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 458 | } |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 459 | new_message().append(vas, msv); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 460 | } |
| 461 | |
William A. Kennington III | a1e9e2a | 2018-06-23 13:33:25 -0700 | [diff] [blame] | 462 | } // namespace |