Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: third_party/protobuf/src/google/protobuf/util/field_mask_util_test.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 EXPECT_EQ(0, mask.paths_size()); 152 EXPECT_EQ(0, mask.paths_size());
153 FieldMaskUtil::FromJsonString("fooBar", &mask); 153 FieldMaskUtil::FromJsonString("fooBar", &mask);
154 EXPECT_EQ(1, mask.paths_size()); 154 EXPECT_EQ(1, mask.paths_size());
155 EXPECT_EQ("foo_bar", mask.paths(0)); 155 EXPECT_EQ("foo_bar", mask.paths(0));
156 FieldMaskUtil::FromJsonString("fooBar,bazQuz", &mask); 156 FieldMaskUtil::FromJsonString("fooBar,bazQuz", &mask);
157 EXPECT_EQ(2, mask.paths_size()); 157 EXPECT_EQ(2, mask.paths_size());
158 EXPECT_EQ("foo_bar", mask.paths(0)); 158 EXPECT_EQ("foo_bar", mask.paths(0));
159 EXPECT_EQ("baz_quz", mask.paths(1)); 159 EXPECT_EQ("baz_quz", mask.paths(1));
160 } 160 }
161 161
162 TEST(FieldMaskUtilTest, GetFieldDescriptors) {
163 std::vector<const FieldDescriptor*> field_descriptors;
164 EXPECT_TRUE(FieldMaskUtil::GetFieldDescriptors(
165 TestAllTypes::descriptor(), "optional_int32", &field_descriptors));
166 EXPECT_EQ(1, field_descriptors.size());
167 EXPECT_EQ("optional_int32", field_descriptors[0]->name());
168 EXPECT_FALSE(FieldMaskUtil::GetFieldDescriptors(
169 TestAllTypes::descriptor(), "optional_nonexist", NULL));
170 EXPECT_TRUE(FieldMaskUtil::GetFieldDescriptors(TestAllTypes::descriptor(),
171 "optional_nested_message.bb",
172 &field_descriptors));
173 EXPECT_EQ(2, field_descriptors.size());
174 EXPECT_EQ("optional_nested_message", field_descriptors[0]->name());
175 EXPECT_EQ("bb", field_descriptors[1]->name());
176 EXPECT_FALSE(FieldMaskUtil::GetFieldDescriptors(
177 TestAllTypes::descriptor(), "optional_nested_message.nonexist", NULL));
178 // FieldMask cannot be used to specify sub-fields of a repeated message.
179 EXPECT_FALSE(FieldMaskUtil::GetFieldDescriptors(
180 TestAllTypes::descriptor(), "repeated_nested_message.bb", NULL));
181 }
182
162 TEST(FieldMaskUtilTest, TestIsVaildPath) { 183 TEST(FieldMaskUtilTest, TestIsVaildPath) {
163 EXPECT_TRUE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_int32")); 184 EXPECT_TRUE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_int32"));
164 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nonexist")); 185 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nonexist"));
165 EXPECT_TRUE( 186 EXPECT_TRUE(
166 FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nested_message.bb")); 187 FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nested_message.bb"));
167 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>( 188 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>(
168 "optional_nested_message.nonexist")); 189 "optional_nested_message.nonexist"));
169 // FieldMask cannot be used to specify sub-fields of a repeated message. 190 // FieldMask cannot be used to specify sub-fields of a repeated message.
170 EXPECT_FALSE( 191 EXPECT_FALSE(
171 FieldMaskUtil::IsValidPath<TestAllTypes>("repeated_nested_message.bb")); 192 FieldMaskUtil::IsValidPath<TestAllTypes>("repeated_nested_message.bb"));
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 363
343 #define TEST_MERGE_ONE_PRIMITIVE_FIELD(field_name) \ 364 #define TEST_MERGE_ONE_PRIMITIVE_FIELD(field_name) \
344 { \ 365 { \
345 TestAllTypes tmp; \ 366 TestAllTypes tmp; \
346 tmp.set_##field_name(src.field_name()); \ 367 tmp.set_##field_name(src.field_name()); \
347 FieldMask mask; \ 368 FieldMask mask; \
348 mask.add_paths(#field_name); \ 369 mask.add_paths(#field_name); \
349 dst.Clear(); \ 370 dst.Clear(); \
350 FieldMaskUtil::MergeMessageTo(src, mask, options, &dst); \ 371 FieldMaskUtil::MergeMessageTo(src, mask, options, &dst); \
351 EXPECT_EQ(tmp.DebugString(), dst.DebugString()); \ 372 EXPECT_EQ(tmp.DebugString(), dst.DebugString()); \
373 src.clear_##field_name(); \
374 tmp.clear_##field_name(); \
375 FieldMaskUtil::MergeMessageTo(src, mask, options, &dst); \
376 EXPECT_EQ(tmp.DebugString(), dst.DebugString()); \
352 } 377 }
353 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int32) 378 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int32)
354 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int64) 379 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int64)
355 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint32) 380 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint32)
356 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint64) 381 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint64)
357 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint32) 382 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint32)
358 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint64) 383 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint64)
359 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed32) 384 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed32)
360 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed64) 385 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed64)
361 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sfixed32) 386 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sfixed32)
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 EXPECT_EQ(5678, nested_dst.payload().repeated_int32(0)); 502 EXPECT_EQ(5678, nested_dst.payload().repeated_int32(0));
478 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(1)); 503 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(1));
479 // Change the behavior to replace repeated fields. 504 // Change the behavior to replace repeated fields.
480 options.set_replace_repeated_fields(true); 505 options.set_replace_repeated_fields(true);
481 FieldMaskUtil::FromString("payload.repeated_int32", &mask); 506 FieldMaskUtil::FromString("payload.repeated_int32", &mask);
482 FieldMaskUtil::MergeMessageTo(nested_src, mask, options, &nested_dst); 507 FieldMaskUtil::MergeMessageTo(nested_src, mask, options, &nested_dst);
483 ASSERT_EQ(1, nested_dst.payload().repeated_int32_size()); 508 ASSERT_EQ(1, nested_dst.payload().repeated_int32_size());
484 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(0)); 509 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(0));
485 } 510 }
486 511
512 TEST(FieldMaskUtilTest, TrimMessage) {
513 #define TEST_TRIM_ONE_PRIMITIVE_FIELD(field_name) \
514 { \
515 TestAllTypes msg; \
516 TestUtil::SetAllFields(&msg); \
517 TestAllTypes tmp; \
518 tmp.set_##field_name(msg.field_name()); \
519 FieldMask mask; \
520 mask.add_paths(#field_name); \
521 FieldMaskUtil::TrimMessage(mask, &msg); \
522 EXPECT_EQ(tmp.DebugString(), msg.DebugString()); \
523 }
524 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_int32)
525 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_int64)
526 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_uint32)
527 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_uint64)
528 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_sint32)
529 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_sint64)
530 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_fixed32)
531 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_fixed64)
532 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_sfixed32)
533 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_sfixed64)
534 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_float)
535 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_double)
536 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_bool)
537 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_string)
538 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_bytes)
539 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_nested_enum)
540 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_foreign_enum)
541 TEST_TRIM_ONE_PRIMITIVE_FIELD(optional_import_enum)
542 #undef TEST_TRIM_ONE_PRIMITIVE_FIELD
543
544 #define TEST_TRIM_ONE_FIELD(field_name) \
545 { \
546 TestAllTypes msg; \
547 TestUtil::SetAllFields(&msg); \
548 TestAllTypes tmp; \
549 *tmp.mutable_##field_name() = msg.field_name(); \
550 FieldMask mask; \
551 mask.add_paths(#field_name); \
552 FieldMaskUtil::TrimMessage(mask, &msg); \
553 EXPECT_EQ(tmp.DebugString(), msg.DebugString()); \
554 }
555 TEST_TRIM_ONE_FIELD(optional_nested_message)
556 TEST_TRIM_ONE_FIELD(optional_foreign_message)
557 TEST_TRIM_ONE_FIELD(optional_import_message)
558
559 TEST_TRIM_ONE_FIELD(repeated_int32)
560 TEST_TRIM_ONE_FIELD(repeated_int64)
561 TEST_TRIM_ONE_FIELD(repeated_uint32)
562 TEST_TRIM_ONE_FIELD(repeated_uint64)
563 TEST_TRIM_ONE_FIELD(repeated_sint32)
564 TEST_TRIM_ONE_FIELD(repeated_sint64)
565 TEST_TRIM_ONE_FIELD(repeated_fixed32)
566 TEST_TRIM_ONE_FIELD(repeated_fixed64)
567 TEST_TRIM_ONE_FIELD(repeated_sfixed32)
568 TEST_TRIM_ONE_FIELD(repeated_sfixed64)
569 TEST_TRIM_ONE_FIELD(repeated_float)
570 TEST_TRIM_ONE_FIELD(repeated_double)
571 TEST_TRIM_ONE_FIELD(repeated_bool)
572 TEST_TRIM_ONE_FIELD(repeated_string)
573 TEST_TRIM_ONE_FIELD(repeated_bytes)
574 TEST_TRIM_ONE_FIELD(repeated_nested_message)
575 TEST_TRIM_ONE_FIELD(repeated_foreign_message)
576 TEST_TRIM_ONE_FIELD(repeated_import_message)
577 TEST_TRIM_ONE_FIELD(repeated_nested_enum)
578 TEST_TRIM_ONE_FIELD(repeated_foreign_enum)
579 TEST_TRIM_ONE_FIELD(repeated_import_enum)
580 #undef TEST_TRIM_ONE_FIELD
581
582 // Test trim nested fields.
583 NestedTestAllTypes nested_msg;
584 nested_msg.mutable_child()->mutable_payload()->set_optional_int32(1234);
585 nested_msg.mutable_child()
586 ->mutable_child()
587 ->mutable_payload()
588 ->set_optional_int32(5678);
589 NestedTestAllTypes trimmed_msg(nested_msg);
590 FieldMask mask;
591 FieldMaskUtil::FromString("child.payload", &mask);
592 FieldMaskUtil::TrimMessage(mask, &trimmed_msg);
593 EXPECT_EQ(1234, trimmed_msg.child().payload().optional_int32());
594 EXPECT_EQ(0, trimmed_msg.child().child().payload().optional_int32());
595
596 trimmed_msg = nested_msg;
597 FieldMaskUtil::FromString("child.child.payload", &mask);
598 FieldMaskUtil::TrimMessage(mask, &trimmed_msg);
599 EXPECT_EQ(0, trimmed_msg.child().payload().optional_int32());
600 EXPECT_EQ(5678, trimmed_msg.child().child().payload().optional_int32());
601
602 trimmed_msg = nested_msg;
603 FieldMaskUtil::FromString("child", &mask);
604 FieldMaskUtil::TrimMessage(mask, &trimmed_msg);
605 EXPECT_EQ(1234, trimmed_msg.child().payload().optional_int32());
606 EXPECT_EQ(5678, trimmed_msg.child().child().payload().optional_int32());
607
608 trimmed_msg = nested_msg;
609 FieldMaskUtil::FromString("child.child", &mask);
610 FieldMaskUtil::TrimMessage(mask, &trimmed_msg);
611 EXPECT_EQ(0, trimmed_msg.child().payload().optional_int32());
612 EXPECT_EQ(5678, trimmed_msg.child().child().payload().optional_int32());
613
614 // Verify than an empty FieldMask trims nothing
615 TestAllTypes all_types_msg;
616 TestUtil::SetAllFields(&all_types_msg);
617 TestAllTypes trimmed_all_types(all_types_msg);
618 FieldMask empty_mask;
619 FieldMaskUtil::TrimMessage(empty_mask, &trimmed_all_types);
620 EXPECT_EQ(trimmed_all_types.DebugString(), all_types_msg.DebugString());
621 }
622
487 623
488 } // namespace 624 } // namespace
489 } // namespace util 625 } // namespace util
490 } // namespace protobuf 626 } // namespace protobuf
491 } // namespace google 627 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698