OLD | NEW |
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 Loading... |
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 | |
183 TEST(FieldMaskUtilTest, TestIsVaildPath) { | 162 TEST(FieldMaskUtilTest, TestIsVaildPath) { |
184 EXPECT_TRUE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_int32")); | 163 EXPECT_TRUE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_int32")); |
185 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nonexist")); | 164 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nonexist")); |
186 EXPECT_TRUE( | 165 EXPECT_TRUE( |
187 FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nested_message.bb")); | 166 FieldMaskUtil::IsValidPath<TestAllTypes>("optional_nested_message.bb")); |
188 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>( | 167 EXPECT_FALSE(FieldMaskUtil::IsValidPath<TestAllTypes>( |
189 "optional_nested_message.nonexist")); | 168 "optional_nested_message.nonexist")); |
190 // FieldMask cannot be used to specify sub-fields of a repeated message. | 169 // FieldMask cannot be used to specify sub-fields of a repeated message. |
191 EXPECT_FALSE( | 170 EXPECT_FALSE( |
192 FieldMaskUtil::IsValidPath<TestAllTypes>("repeated_nested_message.bb")); | 171 FieldMaskUtil::IsValidPath<TestAllTypes>("repeated_nested_message.bb")); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 342 |
364 #define TEST_MERGE_ONE_PRIMITIVE_FIELD(field_name) \ | 343 #define TEST_MERGE_ONE_PRIMITIVE_FIELD(field_name) \ |
365 { \ | 344 { \ |
366 TestAllTypes tmp; \ | 345 TestAllTypes tmp; \ |
367 tmp.set_##field_name(src.field_name()); \ | 346 tmp.set_##field_name(src.field_name()); \ |
368 FieldMask mask; \ | 347 FieldMask mask; \ |
369 mask.add_paths(#field_name); \ | 348 mask.add_paths(#field_name); \ |
370 dst.Clear(); \ | 349 dst.Clear(); \ |
371 FieldMaskUtil::MergeMessageTo(src, mask, options, &dst); \ | 350 FieldMaskUtil::MergeMessageTo(src, mask, options, &dst); \ |
372 EXPECT_EQ(tmp.DebugString(), dst.DebugString()); \ | 351 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()); \ | |
377 } | 352 } |
378 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int32) | 353 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int32) |
379 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int64) | 354 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_int64) |
380 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint32) | 355 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint32) |
381 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint64) | 356 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_uint64) |
382 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint32) | 357 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint32) |
383 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint64) | 358 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sint64) |
384 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed32) | 359 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed32) |
385 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed64) | 360 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_fixed64) |
386 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sfixed32) | 361 TEST_MERGE_ONE_PRIMITIVE_FIELD(optional_sfixed32) |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 EXPECT_EQ(5678, nested_dst.payload().repeated_int32(0)); | 477 EXPECT_EQ(5678, nested_dst.payload().repeated_int32(0)); |
503 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(1)); | 478 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(1)); |
504 // Change the behavior to replace repeated fields. | 479 // Change the behavior to replace repeated fields. |
505 options.set_replace_repeated_fields(true); | 480 options.set_replace_repeated_fields(true); |
506 FieldMaskUtil::FromString("payload.repeated_int32", &mask); | 481 FieldMaskUtil::FromString("payload.repeated_int32", &mask); |
507 FieldMaskUtil::MergeMessageTo(nested_src, mask, options, &nested_dst); | 482 FieldMaskUtil::MergeMessageTo(nested_src, mask, options, &nested_dst); |
508 ASSERT_EQ(1, nested_dst.payload().repeated_int32_size()); | 483 ASSERT_EQ(1, nested_dst.payload().repeated_int32_size()); |
509 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(0)); | 484 EXPECT_EQ(1234, nested_dst.payload().repeated_int32(0)); |
510 } | 485 } |
511 | 486 |
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 | |
623 | 487 |
624 } // namespace | 488 } // namespace |
625 } // namespace util | 489 } // namespace util |
626 } // namespace protobuf | 490 } // namespace protobuf |
627 } // namespace google | 491 } // namespace google |
OLD | NEW |