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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
use enums_rust_proto::{test_map_with_nested_enum, TestMapWithNestedEnum};
use googletest::prelude::*;
use map_unittest_rust_proto::{MapEnum, TestMap, TestMapWithMessages};
use paste::paste;
use protobuf::ProtoString;
use std::collections::HashMap;
use unittest_rust_proto::TestAllTypes;
macro_rules! generate_map_primitives_tests {
(
$(($k_type:ty, $v_type:ty, $k_field:ident, $v_field:ident,
$k_nonzero:expr, $v_nonzero:expr $(,)?)),*
$(,)?
) => {
paste! { $(
#[gtest]
fn [< test_map_ $k_field _ $v_field >]() {
let mut msg = TestMap::new();
assert_that!(msg.[< map_ $k_field _ $v_field >]().len(), eq(0));
assert_that!(
msg.[< map_ $k_field _ $v_field >](),
elements_are![]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().keys().collect::<Vec<_>>(),
elements_are![]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().values().collect::<Vec<_>>(),
elements_are![]
);
let k = <$k_type>::default();
let v = <$v_type>::default();
assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k, v), eq(true));
assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k, v), eq(false));
assert_that!(msg.[< map_ $k_field _ $v_field >]().len(), eq(1));
assert_that!(
msg.[< map_ $k_field _ $v_field >](),
elements_are![eq((k, v))]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().keys().collect::<Vec<_>>(),
elements_are![eq(&k)]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().values().collect::<Vec<_>>(),
elements_are![eq(&v)]
);
let k2: $k_type = $k_nonzero;
let v2: $v_type = $v_nonzero;
assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k2, v2), eq(true));
assert_that!(msg.[< map_ $k_field _ $v_field >](), len(eq(2)));
assert_that!(
msg.[< map_ $k_field _ $v_field >](),
unordered_elements_are![
eq((k, v)),
eq((k2, v2)),
]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().keys().collect::<Vec<_>>(),
unordered_elements_are![eq(&k), eq(&k2)]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().values().collect::<Vec<_>>(),
unordered_elements_are![eq(&v), eq(&v2)]
);
}
)* }
};
}
generate_map_primitives_tests!(
(i32, i32, int32, int32, 1, 1),
(i64, i64, int64, int64, 1, 1),
(u32, u32, uint32, uint32, 1, 1),
(u64, u64, uint64, uint64, 1, 1),
(i32, i32, sint32, sint32, 1, 1),
(i64, i64, sint64, sint64, 1, 1),
(u32, u32, fixed32, fixed32, 1, 1),
(u64, u64, fixed64, fixed64, 1, 1),
(i32, i32, sfixed32, sfixed32, 1, 1),
(i64, i64, sfixed64, sfixed64, 1, 1),
(i32, f32, int32, float, 1, 1.),
(i32, f64, int32, double, 1, 1.),
(bool, bool, bool, bool, true, true),
(i32, &[u8], int32, bytes, 1, b"foo"),
(i32, MapEnum, int32, enum, 1, MapEnum::Baz),
);
#[gtest]
fn collect_as_hashmap() {
// Highlights conversion from protobuf map to hashmap.
let mut msg = TestMap::new();
msg.map_string_string_mut().insert("hello", "world");
msg.map_string_string_mut().insert("fizz", "buzz");
msg.map_string_string_mut().insert("boo", "blah");
let hashmap: HashMap<String, String> =
msg.map_string_string().iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
assert_that!(
hashmap,
unordered_elements_are![
(eq("hello"), eq("world")),
(eq("fizz"), eq("buzz")),
(eq("boo"), eq("blah")),
]
);
}
#[gtest]
fn test_string_maps() {
let mut msg = TestMap::new();
msg.map_string_string_mut().insert("hello", "world");
msg.map_string_string_mut().insert("fizz", "buzz");
assert_that!(msg.map_string_string().len(), eq(2));
assert_that!(msg.map_string_string().get("fizz").unwrap(), eq("buzz"));
assert_that!(msg.map_string_string().get("not found"), eq(None));
msg.map_string_string_mut().clear();
assert_that!(msg.map_string_string().len(), eq(0));
}
#[gtest]
fn test_nested_enum_maps() {
// Verify that C++ thunks are generated and are with the right name for strings
TestMapWithNestedEnum::new()
.string_map_mut()
.insert("foo", test_map_with_nested_enum::inner_nested::NestedEnum::Foo);
}
#[gtest]
fn test_bytes_and_string_copied() {
let mut msg = TestMap::new();
{
// Ensure val is dropped after inserting into the map.
let mut key = String::from("hello");
let mut val = String::from("world");
msg.map_string_string_mut().insert(key.as_str(), &val);
msg.map_int32_bytes_mut().insert(1, val.as_bytes());
// Validate that map keys are copied by mutating the originals.
key.replace_range(.., "ayo");
val.replace_range(.., "wOrld");
}
assert_that!(msg.map_string_string_mut().get("hello").unwrap(), eq("world"));
assert_that!(msg.map_string_string(), unordered_elements_are![(eq("hello"), eq("world"))]);
assert_that!(msg.map_int32_bytes_mut().get(1).unwrap(), eq(b"world"));
}
#[gtest]
fn test_map_setter() {
// Set Map
{
let mut msg = TestMap::new();
let mut map = protobuf::Map::<ProtoString, ProtoString>::new();
map.as_mut().copy_from([("hello", "world"), ("fizz", "buzz")]);
msg.set_map_string_string(map);
assert_that!(
msg.map_string_string(),
unordered_elements_are![
eq(("hello".into(), "world".into())),
eq(("fizz".into(), "buzz".into()))
]
);
}
// Set MapView
{
let mut msg = TestMap::new();
let mut map = protobuf::Map::<ProtoString, ProtoString>::new();
map.as_mut().copy_from([("hello", "world"), ("fizz", "buzz")]);
msg.set_map_string_string(map.as_view());
assert_that!(
msg.map_string_string(),
unordered_elements_are![
eq(("hello".into(), "world".into())),
eq(("fizz".into(), "buzz".into()))
]
);
}
// Set MapMut
{
let mut msg = TestMap::new();
let mut map = protobuf::Map::<ProtoString, ProtoString>::new();
map.as_mut().copy_from([("hello", "world"), ("fizz", "buzz")]);
msg.set_map_string_string(map.as_mut());
assert_that!(
msg.map_string_string(),
unordered_elements_are![
eq(("hello".into(), "world".into())),
eq(("fizz".into(), "buzz".into()))
]
);
// The original map should remain unchanged.
assert_that!(
map.as_view(),
unordered_elements_are![
eq(("hello".into(), "world".into())),
eq(("fizz".into(), "buzz".into()))
]
);
}
}
#[gtest]
fn test_message_map_mut_getter() {
let mut msg = TestAllTypes::new();
msg.set_optional_int32(5);
let mut map = protobuf::Map::<i32, TestAllTypes>::new();
map.as_mut().insert(1, msg);
assert_that!(map.as_view().len(), eq(1));
assert_that!(map.as_view().get(1).unwrap().optional_int32(), eq(5));
map.as_mut().get_mut(1).unwrap().set_optional_int32(10);
assert_that!(map.as_view().get(1).unwrap().optional_int32(), eq(10));
}
#[gtest]
fn test_map_creation_with_message_values() {
// Maps are usually created and owned by a parent message, but let's verify that
// we can successfully create and destroy them independently.
macro_rules! test_for_each_key {
($($key_t:ty, $key:expr;)*) => {
$(
let msg = TestAllTypes::new();
let mut map = protobuf::Map::<$key_t, TestAllTypes>::new();
map.as_mut().insert($key, msg);
assert_that!(map.as_view().len(), eq(1));
)*
}
}
test_for_each_key!(
i32, -5;
u32, 13u32;
i64, 7;
u64, 11u64;
bool, false;
ProtoString, "looooooooooooooooooooooooong string";
);
}
#[gtest]
fn test_map_clearing_with_message_values() {
macro_rules! test_for_each_key {
($($key_t:ty, $key:expr;)*) => {
$(
let msg = TestAllTypes::new();
let mut map = protobuf::Map::<$key_t, TestAllTypes>::new();
map.as_mut().insert($key, msg);
assert_that!(map.as_view().len(), eq(1));
map.as_mut().clear();
assert_that!(map.as_view().len(), eq(0));
)*
}
}
test_for_each_key!(
i32, -5;
u32, 13u32;
i64, 7;
u64, 11u64;
bool, false;
ProtoString, "looooooooooooooooooooooooong string";
);
}
macro_rules! generate_map_with_msg_values_tests {
(
$(($k_field:ident, $k_nonzero:expr, $k_other:expr $(,)?)),*
$(,)?
) => {
paste! { $(
#[gtest]
fn [< test_map_ $k_field _all_types >]() {
// We need to cover the following upb/c++ thunks:
// TODO - b/323883851: Add test once Map::new is public.
// * new
// * free (covered implicitly by drop)
// * clear, size, insert, get, remove, iter, iter_next (all covered below)
let mut msg = TestMapWithMessages::new();
assert_that!(msg.[< map_ $k_field _all_types >]().len(), eq(0));
assert_that!(msg.[< map_ $k_field _all_types >]().get($k_nonzero), none());
// this block makes sure `insert` copies/moves, not borrows.
{
let mut msg_val = TestAllTypes::new();
msg_val.set_optional_int32(1001);
assert_that!(
msg
.[< map_ $k_field _all_types_mut >]()
.insert($k_nonzero, msg_val.as_view()),
eq(true),
"`insert` should return true when key was inserted."
);
assert_that!(
msg
.[< map_ $k_field _all_types_mut >]()
.insert($k_nonzero, msg_val.as_view()),
eq(false),
"`insert` should return false when key was already present."
);
}
assert_that!(
msg.[< map_ $k_field _all_types >]().len(),
eq(1),
"`size` thunk should return correct len.");
assert_that!(
msg.[< map_ $k_field _all_types >]().get($k_nonzero),
some(anything()),
"`get` should return Some when key present.");
assert_that!(
msg.[< map_ $k_field _all_types >]().get($k_nonzero).unwrap().optional_int32(),
eq(1001));
assert_that!(
msg.[< map_ $k_field _all_types >]().get($k_other),
none(),
"`get` should return None when key missing.");
msg.[< map_ $k_field _all_types_mut >]().clear();
assert_that!(
msg.[< map_ $k_field _all_types >]().len(),
eq(0),
"`clear` should drop all elements.");
assert_that!(
msg.[< map_ $k_field _all_types_mut >]().insert($k_nonzero, TestAllTypes::new()),
eq(true));
assert_that!(
msg.[< map_ $k_field _all_types_mut >]().remove($k_nonzero),
eq(true),
"`remove` should return true when key was present.");
assert_that!(msg.[< map_ $k_field _all_types >](), empty());
assert_that!(
msg.[< map_ $k_field _all_types_mut >]().remove($k_nonzero),
eq(false),
"`remove` should return false when key was missing.");
// empty iter
// assert_that!(
// msg.[< map_ $k_field _all_types_mut >]().iter().collect::<Vec<_>>(),
// elements_are![],
// "`iter` should work when empty."
// );
assert_that!(
msg.[< map_ $k_field _all_types_mut >]().keys().count(),
eq(0),
"`iter` should work when empty."
);
assert_that!(
msg.[< map_ $k_field _all_types_mut >]().values().count(),
eq(0),
"`iter` should work when empty."
);
// single element iter
assert_that!(
msg.[< map_ $k_field _all_types_mut >]().insert($k_nonzero, TestAllTypes::new()),
eq(true));
// assert_that!(
// msg.[< map_ $k_field _all_types >]().iter().collect::<Vec<_>>(),
// unordered_elements_are![
// eq(($k_nonzero, anything())),
// ]
// );
assert_that!(
msg.[< map_ $k_field _all_types >]().keys().collect::<Vec<_>>(),
unordered_elements_are![eq(&$k_nonzero)]
);
assert_that!(
msg.[< map_ $k_field _all_types >]().values().count(),
eq(1));
// 2 element iter
assert_that!(
msg
.[< map_ $k_field _all_types_mut >]()
.insert($k_other, TestAllTypes::new()),
eq(true));
assert_that!(
msg.[< map_ $k_field _all_types >](),
len(eq(2))
);
assert_that!(
msg.[< map_ $k_field _all_types >]().keys().collect::<Vec<_>>(),
unordered_elements_are![eq(&$k_nonzero), eq(&$k_other)]
);
assert_that!(
msg.[< map_ $k_field _all_types >]().values().count(),
eq(2)
);
}
)* }
}
}
generate_map_with_msg_values_tests!(
(int32, 1i32, 2i32),
(int64, 1i64, 2i64),
(uint32, 1u32, 2u32),
(uint64, 1u64, 2u64),
(sint32, 1, 2),
(sint64, 1, 2),
(fixed32, 1u32, 2u32),
(fixed64, 1u64, 2u64),
(sfixed32, 1, 2),
(sfixed64, 1, 2),
(bool, true, false),
(string, "foo", "bar"),
);
|