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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 #include <gtest/gtest.h> | 59 #include <gtest/gtest.h> |
60 | 60 |
61 namespace google { | 61 namespace google { |
62 namespace protobuf { | 62 namespace protobuf { |
63 | 63 |
64 namespace { | 64 namespace { |
65 | 65 |
66 | 66 |
67 const FieldDescriptor* GetFieldDescriptor( | 67 const FieldDescriptor* GetFieldDescriptor( |
68 const Message& message, const string& field_name) { | 68 const Message& message, const string& field_name) { |
69 vector<string> field_path = | 69 std::vector<string> field_path = |
70 Split(field_name, ".", true); | 70 Split(field_name, ".", true); |
71 const Descriptor* descriptor = message.GetDescriptor(); | 71 const Descriptor* descriptor = message.GetDescriptor(); |
72 const FieldDescriptor* field = NULL; | 72 const FieldDescriptor* field = NULL; |
73 for (int i = 0; i < field_path.size(); i++) { | 73 for (int i = 0; i < field_path.size(); i++) { |
74 field = descriptor->FindFieldByName(field_path[i]); | 74 field = descriptor->FindFieldByName(field_path[i]); |
75 descriptor = field->message_type(); | 75 descriptor = field->message_type(); |
76 } | 76 } |
77 return field; | 77 return field; |
78 } | 78 } |
79 | 79 |
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 EXPECT_TRUE(differencer.Compare(empty1, empty2)); | 783 EXPECT_TRUE(differencer.Compare(empty1, empty2)); |
784 } | 784 } |
785 | 785 |
786 TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllTest) { | 786 TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllTest) { |
787 unittest::TestAllTypes msg1; | 787 unittest::TestAllTypes msg1; |
788 unittest::TestAllTypes msg2; | 788 unittest::TestAllTypes msg2; |
789 | 789 |
790 TestUtil::SetAllFields(&msg1); | 790 TestUtil::SetAllFields(&msg1); |
791 TestUtil::SetAllFields(&msg2); | 791 TestUtil::SetAllFields(&msg2); |
792 | 792 |
793 vector<const FieldDescriptor*> fields1; | 793 std::vector<const FieldDescriptor*> fields1; |
794 vector<const FieldDescriptor*> fields2; | 794 std::vector<const FieldDescriptor*> fields2; |
795 msg1.GetReflection()->ListFields(msg1, &fields1); | 795 msg1.GetReflection()->ListFields(msg1, &fields1); |
796 msg2.GetReflection()->ListFields(msg2, &fields2); | 796 msg2.GetReflection()->ListFields(msg2, &fields2); |
797 | 797 |
798 util::MessageDifferencer differencer; | 798 util::MessageDifferencer differencer; |
799 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); | 799 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); |
800 } | 800 } |
801 | 801 |
802 TEST(MessageDifferencerTest, SpecifiedFieldsInequalityAllTest) { | 802 TEST(MessageDifferencerTest, SpecifiedFieldsInequalityAllTest) { |
803 unittest::TestAllTypes msg1; | 803 unittest::TestAllTypes msg1; |
804 unittest::TestAllTypes msg2; | 804 unittest::TestAllTypes msg2; |
805 | 805 |
806 TestUtil::SetAllFields(&msg1); | 806 TestUtil::SetAllFields(&msg1); |
807 | 807 |
808 vector<const FieldDescriptor*> fields1; | 808 std::vector<const FieldDescriptor*> fields1; |
809 vector<const FieldDescriptor*> fields2; | 809 std::vector<const FieldDescriptor*> fields2; |
810 msg1.GetReflection()->ListFields(msg1, &fields1); | 810 msg1.GetReflection()->ListFields(msg1, &fields1); |
811 msg2.GetReflection()->ListFields(msg2, &fields2); | 811 msg2.GetReflection()->ListFields(msg2, &fields2); |
812 | 812 |
813 util::MessageDifferencer differencer; | 813 util::MessageDifferencer differencer; |
814 EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); | 814 EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); |
815 } | 815 } |
816 | 816 |
817 TEST(MessageDifferencerTest, SpecifiedFieldsEmptyListAlwaysSucceeds) { | 817 TEST(MessageDifferencerTest, SpecifiedFieldsEmptyListAlwaysSucceeds) { |
818 unittest::TestAllTypes msg1; | 818 unittest::TestAllTypes msg1; |
819 unittest::TestAllTypes msg2; | 819 unittest::TestAllTypes msg2; |
820 | 820 |
821 TestUtil::SetAllFields(&msg1); | 821 TestUtil::SetAllFields(&msg1); |
822 | 822 |
823 vector<const FieldDescriptor*> empty_fields; | 823 std::vector<const FieldDescriptor*> empty_fields; |
824 | 824 |
825 util::MessageDifferencer differencer; | 825 util::MessageDifferencer differencer; |
826 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, | 826 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, |
827 empty_fields, empty_fields)); | 827 empty_fields, empty_fields)); |
828 | 828 |
829 TestUtil::SetAllFields(&msg2); | 829 TestUtil::SetAllFields(&msg2); |
830 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, | 830 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, |
831 empty_fields, empty_fields)); | 831 empty_fields, empty_fields)); |
832 } | 832 } |
833 | 833 |
834 TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) { | 834 TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) { |
835 unittest::TestAllTypes msg1; | 835 unittest::TestAllTypes msg1; |
836 TestUtil::SetAllFields(&msg1); | 836 TestUtil::SetAllFields(&msg1); |
837 | 837 |
838 vector<const FieldDescriptor*> fields; | 838 std::vector<const FieldDescriptor*> fields; |
839 msg1.GetReflection()->ListFields(msg1, &fields); | 839 msg1.GetReflection()->ListFields(msg1, &fields); |
840 | 840 |
841 util::MessageDifferencer differencer; | 841 util::MessageDifferencer differencer; |
842 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, fields, fields)); | 842 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, fields, fields)); |
843 | 843 |
844 { | 844 { |
845 // Compare with a subset of fields. | 845 // Compare with a subset of fields. |
846 vector<const FieldDescriptor*> compare_fields; | 846 std::vector<const FieldDescriptor*> compare_fields; |
847 for (int i = 0; i < fields.size(); ++i) { | 847 for (int i = 0; i < fields.size(); ++i) { |
848 if (i % 2 == 0) { | 848 if (i % 2 == 0) { |
849 compare_fields.push_back(fields[i]); | 849 compare_fields.push_back(fields[i]); |
850 } | 850 } |
851 } | 851 } |
852 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, | 852 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, |
853 compare_fields, compare_fields)); | 853 compare_fields, compare_fields)); |
854 } | 854 } |
855 { | 855 { |
856 // Specify a different set of fields to compare, even though we're using the | 856 // Specify a different set of fields to compare, even though we're using the |
857 // same message. This should fail, since we are explicitly saying that the | 857 // same message. This should fail, since we are explicitly saying that the |
858 // set of fields are different. | 858 // set of fields are different. |
859 vector<const FieldDescriptor*> compare_fields1; | 859 std::vector<const FieldDescriptor*> compare_fields1; |
860 vector<const FieldDescriptor*> compare_fields2; | 860 std::vector<const FieldDescriptor*> compare_fields2; |
861 for (int i = 0; i < fields.size(); ++i) { | 861 for (int i = 0; i < fields.size(); ++i) { |
862 if (i % 2 == 0) { | 862 if (i % 2 == 0) { |
863 compare_fields1.push_back(fields[i]); | 863 compare_fields1.push_back(fields[i]); |
864 } else { | 864 } else { |
865 compare_fields2.push_back(fields[i]); | 865 compare_fields2.push_back(fields[i]); |
866 } | 866 } |
867 } | 867 } |
868 EXPECT_FALSE(differencer.CompareWithFields( | 868 EXPECT_FALSE(differencer.CompareWithFields( |
869 msg1, msg1, compare_fields1, compare_fields2)); | 869 msg1, msg1, compare_fields1, compare_fields2)); |
870 } | 870 } |
871 } | 871 } |
872 | 872 |
873 TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllShuffledTest) { | 873 TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllShuffledTest) { |
874 // This is a public function, so make sure there are no assumptions about the | 874 // This is a public function, so make sure there are no assumptions about the |
875 // list of fields. Randomly shuffle them to make sure that they are properly | 875 // list of fields. Randomly shuffle them to make sure that they are properly |
876 // ordered for comparison. | 876 // ordered for comparison. |
877 unittest::TestAllTypes msg1; | 877 unittest::TestAllTypes msg1; |
878 unittest::TestAllTypes msg2; | 878 unittest::TestAllTypes msg2; |
879 | 879 |
880 TestUtil::SetAllFields(&msg1); | 880 TestUtil::SetAllFields(&msg1); |
881 TestUtil::SetAllFields(&msg2); | 881 TestUtil::SetAllFields(&msg2); |
882 | 882 |
883 vector<const FieldDescriptor*> fields1; | 883 std::vector<const FieldDescriptor*> fields1; |
884 vector<const FieldDescriptor*> fields2; | 884 std::vector<const FieldDescriptor*> fields2; |
885 msg1.GetReflection()->ListFields(msg1, &fields1); | 885 msg1.GetReflection()->ListFields(msg1, &fields1); |
886 msg2.GetReflection()->ListFields(msg2, &fields2); | 886 msg2.GetReflection()->ListFields(msg2, &fields2); |
887 | 887 |
888 std::random_shuffle(fields1.begin(), fields1.end()); | 888 std::random_shuffle(fields1.begin(), fields1.end()); |
889 std::random_shuffle(fields2.begin(), fields2.end()); | 889 std::random_shuffle(fields2.begin(), fields2.end()); |
890 | 890 |
891 util::MessageDifferencer differencer; | 891 util::MessageDifferencer differencer; |
892 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); | 892 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); |
893 } | 893 } |
894 | 894 |
895 TEST(MessageDifferencerTest, SpecifiedFieldsSubsetEqualityTest) { | 895 TEST(MessageDifferencerTest, SpecifiedFieldsSubsetEqualityTest) { |
896 // Specify a set of fields to compare. All the fields are equal. | 896 // Specify a set of fields to compare. All the fields are equal. |
897 unittest::TestAllTypes msg1; | 897 unittest::TestAllTypes msg1; |
898 unittest::TestAllTypes msg2; | 898 unittest::TestAllTypes msg2; |
899 TestUtil::SetAllFields(&msg1); | 899 TestUtil::SetAllFields(&msg1); |
900 TestUtil::SetAllFields(&msg2); | 900 TestUtil::SetAllFields(&msg2); |
901 | 901 |
902 vector<const FieldDescriptor*> fields1; | 902 std::vector<const FieldDescriptor*> fields1; |
903 msg1.GetReflection()->ListFields(msg1, &fields1); | 903 msg1.GetReflection()->ListFields(msg1, &fields1); |
904 | 904 |
905 vector<const FieldDescriptor*> compare_fields; | 905 std::vector<const FieldDescriptor*> compare_fields; |
906 // Only compare the field descriptors with even indices. | 906 // Only compare the field descriptors with even indices. |
907 for (int i = 0; i < fields1.size(); ++i) { | 907 for (int i = 0; i < fields1.size(); ++i) { |
908 if (i % 2 == 0) { | 908 if (i % 2 == 0) { |
909 compare_fields.push_back(fields1[i]); | 909 compare_fields.push_back(fields1[i]); |
910 } | 910 } |
911 } | 911 } |
912 | 912 |
913 util::MessageDifferencer differencer; | 913 util::MessageDifferencer differencer; |
914 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, | 914 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, |
915 compare_fields, compare_fields)); | 915 compare_fields, compare_fields)); |
916 } | 916 } |
917 | 917 |
918 TEST(MessageDifferencerTest, | 918 TEST(MessageDifferencerTest, |
919 SpecifiedFieldsSubsetIgnoresOtherFieldDifferencesTest) { | 919 SpecifiedFieldsSubsetIgnoresOtherFieldDifferencesTest) { |
920 // Specify a set of fields to compare, but clear all the other fields in one | 920 // Specify a set of fields to compare, but clear all the other fields in one |
921 // of the messages. This should fail a regular compare, but CompareWithFields | 921 // of the messages. This should fail a regular compare, but CompareWithFields |
922 // should succeed. | 922 // should succeed. |
923 unittest::TestAllTypes msg1; | 923 unittest::TestAllTypes msg1; |
924 unittest::TestAllTypes msg2; | 924 unittest::TestAllTypes msg2; |
925 TestUtil::SetAllFields(&msg1); | 925 TestUtil::SetAllFields(&msg1); |
926 TestUtil::SetAllFields(&msg2); | 926 TestUtil::SetAllFields(&msg2); |
927 | 927 |
928 vector<const FieldDescriptor*> fields1; | 928 std::vector<const FieldDescriptor*> fields1; |
929 const Reflection* reflection = msg1.GetReflection(); | 929 const Reflection* reflection = msg1.GetReflection(); |
930 reflection->ListFields(msg1, &fields1); | 930 reflection->ListFields(msg1, &fields1); |
931 | 931 |
932 vector<const FieldDescriptor*> compare_fields; | 932 std::vector<const FieldDescriptor*> compare_fields; |
933 // Only compare the field descriptors with even indices. | 933 // Only compare the field descriptors with even indices. |
934 for (int i = 0; i < fields1.size(); ++i) { | 934 for (int i = 0; i < fields1.size(); ++i) { |
935 if (i % 2 == 0) { | 935 if (i % 2 == 0) { |
936 compare_fields.push_back(fields1[i]); | 936 compare_fields.push_back(fields1[i]); |
937 } else { | 937 } else { |
938 reflection->ClearField(&msg2, fields1[i]); | 938 reflection->ClearField(&msg2, fields1[i]); |
939 } | 939 } |
940 } | 940 } |
941 | 941 |
942 util::MessageDifferencer differencer; | 942 util::MessageDifferencer differencer; |
943 EXPECT_FALSE(differencer.Compare(msg1, msg2)); | 943 EXPECT_FALSE(differencer.Compare(msg1, msg2)); |
944 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, | 944 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, |
945 compare_fields, compare_fields)); | 945 compare_fields, compare_fields)); |
946 } | 946 } |
947 | 947 |
948 TEST(MessageDifferencerTest, SpecifiedFieldsDetectsDifferencesTest) { | 948 TEST(MessageDifferencerTest, SpecifiedFieldsDetectsDifferencesTest) { |
949 // Change all of the repeated fields in one of the messages, and use only | 949 // Change all of the repeated fields in one of the messages, and use only |
950 // those fields for comparison. | 950 // those fields for comparison. |
951 unittest::TestAllTypes msg1; | 951 unittest::TestAllTypes msg1; |
952 unittest::TestAllTypes msg2; | 952 unittest::TestAllTypes msg2; |
953 TestUtil::SetAllFields(&msg1); | 953 TestUtil::SetAllFields(&msg1); |
954 TestUtil::SetAllFields(&msg2); | 954 TestUtil::SetAllFields(&msg2); |
955 TestUtil::ModifyRepeatedFields(&msg2); | 955 TestUtil::ModifyRepeatedFields(&msg2); |
956 | 956 |
957 vector<const FieldDescriptor*> fields1; | 957 std::vector<const FieldDescriptor*> fields1; |
958 msg1.GetReflection()->ListFields(msg1, &fields1); | 958 msg1.GetReflection()->ListFields(msg1, &fields1); |
959 | 959 |
960 vector<const FieldDescriptor*> compare_fields; | 960 std::vector<const FieldDescriptor*> compare_fields; |
961 // Only compare the repeated field descriptors. | 961 // Only compare the repeated field descriptors. |
962 for (int i = 0; i < fields1.size(); ++i) { | 962 for (int i = 0; i < fields1.size(); ++i) { |
963 if (fields1[i]->is_repeated()) { | 963 if (fields1[i]->is_repeated()) { |
964 compare_fields.push_back(fields1[i]); | 964 compare_fields.push_back(fields1[i]); |
965 } | 965 } |
966 } | 966 } |
967 | 967 |
968 util::MessageDifferencer differencer; | 968 util::MessageDifferencer differencer; |
969 EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, | 969 EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, |
970 compare_fields, compare_fields)); | 970 compare_fields, compare_fields)); |
971 } | 971 } |
972 | 972 |
973 TEST(MessageDifferencerTest, SpecifiedFieldsEquivalenceAllTest) { | 973 TEST(MessageDifferencerTest, SpecifiedFieldsEquivalenceAllTest) { |
974 unittest::TestAllTypes msg1; | 974 unittest::TestAllTypes msg1; |
975 unittest::TestAllTypes msg2; | 975 unittest::TestAllTypes msg2; |
976 | 976 |
977 TestUtil::SetAllFields(&msg1); | 977 TestUtil::SetAllFields(&msg1); |
978 TestUtil::SetAllFields(&msg2); | 978 TestUtil::SetAllFields(&msg2); |
979 | 979 |
980 vector<const FieldDescriptor*> fields1; | 980 std::vector<const FieldDescriptor*> fields1; |
981 vector<const FieldDescriptor*> fields2; | 981 std::vector<const FieldDescriptor*> fields2; |
982 msg1.GetReflection()->ListFields(msg1, &fields1); | 982 msg1.GetReflection()->ListFields(msg1, &fields1); |
983 msg2.GetReflection()->ListFields(msg2, &fields2); | 983 msg2.GetReflection()->ListFields(msg2, &fields2); |
984 | 984 |
985 util::MessageDifferencer differencer; | 985 util::MessageDifferencer differencer; |
986 differencer.set_message_field_comparison( | 986 differencer.set_message_field_comparison( |
987 util::MessageDifferencer::EQUIVALENT); | 987 util::MessageDifferencer::EQUIVALENT); |
988 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); | 988 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); |
989 } | 989 } |
990 | 990 |
991 TEST(MessageDifferencerTest, | 991 TEST(MessageDifferencerTest, |
(...skipping 12 matching lines...) Expand all Loading... |
1004 ASSERT_TRUE(optional_int64_desc != NULL); | 1004 ASSERT_TRUE(optional_int64_desc != NULL); |
1005 ASSERT_TRUE(default_int64_desc != NULL); | 1005 ASSERT_TRUE(default_int64_desc != NULL); |
1006 msg1.set_optional_int32(0); | 1006 msg1.set_optional_int32(0); |
1007 msg2.set_optional_int64(0); | 1007 msg2.set_optional_int64(0); |
1008 msg1.set_default_int64(default_int64_desc->default_value_int64()); | 1008 msg1.set_default_int64(default_int64_desc->default_value_int64()); |
1009 | 1009 |
1010 // Set a field to a non-default value so we know that field selection is | 1010 // Set a field to a non-default value so we know that field selection is |
1011 // actually doing something. | 1011 // actually doing something. |
1012 msg2.set_optional_uint64(23); | 1012 msg2.set_optional_uint64(23); |
1013 | 1013 |
1014 vector<const FieldDescriptor*> fields1; | 1014 std::vector<const FieldDescriptor*> fields1; |
1015 vector<const FieldDescriptor*> fields2; | 1015 std::vector<const FieldDescriptor*> fields2; |
1016 fields1.push_back(optional_int32_desc); | 1016 fields1.push_back(optional_int32_desc); |
1017 fields1.push_back(default_int64_desc); | 1017 fields1.push_back(default_int64_desc); |
1018 | 1018 |
1019 fields2.push_back(optional_int64_desc); | 1019 fields2.push_back(optional_int64_desc); |
1020 | 1020 |
1021 util::MessageDifferencer differencer; | 1021 util::MessageDifferencer differencer; |
1022 EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); | 1022 EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); |
1023 differencer.set_message_field_comparison( | 1023 differencer.set_message_field_comparison( |
1024 util::MessageDifferencer::EQUIVALENT); | 1024 util::MessageDifferencer::EQUIVALENT); |
1025 EXPECT_FALSE(differencer.Compare(msg1, msg2)); | 1025 EXPECT_FALSE(differencer.Compare(msg1, msg2)); |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1279 // key => value: (2, {1, 3}) => "b" | 1279 // key => value: (2, {1, 3}) => "b" |
1280 item->set_a(2); | 1280 item->set_a(2); |
1281 item->add_ra(3); | 1281 item->add_ra(3); |
1282 item->add_ra(1); | 1282 item->add_ra(1); |
1283 item->set_b("b"); | 1283 item->set_b("b"); |
1284 | 1284 |
1285 // Compare | 1285 // Compare |
1286 util::MessageDifferencer differencer; | 1286 util::MessageDifferencer differencer; |
1287 differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.ra")); | 1287 differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.ra")); |
1288 EXPECT_FALSE(differencer.Compare(msg1, msg2)); | 1288 EXPECT_FALSE(differencer.Compare(msg1, msg2)); |
1289 vector<const FieldDescriptor*> key_fields; | 1289 std::vector<const FieldDescriptor*> key_fields; |
1290 key_fields.push_back(GetFieldDescriptor(msg1, "item.a")); | 1290 key_fields.push_back(GetFieldDescriptor(msg1, "item.a")); |
1291 key_fields.push_back(GetFieldDescriptor(msg1, "item.ra")); | 1291 key_fields.push_back(GetFieldDescriptor(msg1, "item.ra")); |
1292 differencer.TreatAsMapWithMultipleFieldsAsKey( | 1292 differencer.TreatAsMapWithMultipleFieldsAsKey( |
1293 GetFieldDescriptor(msg1, "item"), key_fields); | 1293 GetFieldDescriptor(msg1, "item"), key_fields); |
1294 EXPECT_TRUE(differencer.Compare(msg1, msg2)); | 1294 EXPECT_TRUE(differencer.Compare(msg1, msg2)); |
1295 | 1295 |
1296 // Introduce some differences. | 1296 // Introduce some differences. |
1297 msg1.clear_item(); | 1297 msg1.clear_item(); |
1298 msg2.clear_item(); | 1298 msg2.clear_item(); |
1299 item = msg1.add_item(); | 1299 item = msg1.add_item(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1356 // key => value: (2, {1, 3}) => "b" | 1356 // key => value: (2, {1, 3}) => "b" |
1357 item->mutable_m()->set_a(2); | 1357 item->mutable_m()->set_a(2); |
1358 item->mutable_m()->add_rc(3); | 1358 item->mutable_m()->add_rc(3); |
1359 item->mutable_m()->add_rc(1); | 1359 item->mutable_m()->add_rc(1); |
1360 item->set_b("b"); | 1360 item->set_b("b"); |
1361 | 1361 |
1362 // Compare | 1362 // Compare |
1363 util::MessageDifferencer differencer; | 1363 util::MessageDifferencer differencer; |
1364 differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.m.rc")); | 1364 differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.m.rc")); |
1365 EXPECT_FALSE(differencer.Compare(msg1, msg2)); | 1365 EXPECT_FALSE(differencer.Compare(msg1, msg2)); |
1366 vector<vector<const FieldDescriptor*> > key_field_paths; | 1366 std::vector<std::vector<const FieldDescriptor*> > key_field_paths; |
1367 vector<const FieldDescriptor*> key_field_path1; | 1367 std::vector<const FieldDescriptor*> key_field_path1; |
1368 key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m")); | 1368 key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m")); |
1369 key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m.a")); | 1369 key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m.a")); |
1370 vector<const FieldDescriptor*> key_field_path2; | 1370 std::vector<const FieldDescriptor*> key_field_path2; |
1371 key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m")); | 1371 key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m")); |
1372 key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m.rc")); | 1372 key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m.rc")); |
1373 key_field_paths.push_back(key_field_path1); | 1373 key_field_paths.push_back(key_field_path1); |
1374 key_field_paths.push_back(key_field_path2); | 1374 key_field_paths.push_back(key_field_path2); |
1375 differencer.TreatAsMapWithMultipleFieldPathsAsKey( | 1375 differencer.TreatAsMapWithMultipleFieldPathsAsKey( |
1376 GetFieldDescriptor(msg1, "item"), key_field_paths); | 1376 GetFieldDescriptor(msg1, "item"), key_field_paths); |
1377 EXPECT_TRUE(differencer.Compare(msg1, msg2)); | 1377 EXPECT_TRUE(differencer.Compare(msg1, msg2)); |
1378 | 1378 |
1379 // Introduce some differences. | 1379 // Introduce some differences. |
1380 msg1.clear_item(); | 1380 msg1.clear_item(); |
(...skipping 25 matching lines...) Expand all Loading... |
1406 protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item(); | 1406 protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item(); |
1407 item->set_a(1); | 1407 item->set_a(1); |
1408 item->add_ra(2); | 1408 item->add_ra(2); |
1409 item->set_b("hello"); | 1409 item->set_b("hello"); |
1410 item = msg2.add_item(); | 1410 item = msg2.add_item(); |
1411 item->set_a(1); | 1411 item->set_a(1); |
1412 item->add_ra(3); | 1412 item->add_ra(3); |
1413 item->set_b("world"); | 1413 item->set_b("world"); |
1414 // Compare | 1414 // Compare |
1415 util::MessageDifferencer differencer; | 1415 util::MessageDifferencer differencer; |
1416 vector<const FieldDescriptor*> key_fields; | 1416 std::vector<const FieldDescriptor*> key_fields; |
1417 key_fields.push_back(GetFieldDescriptor(msg1, "item.a")); | 1417 key_fields.push_back(GetFieldDescriptor(msg1, "item.a")); |
1418 key_fields.push_back(GetFieldDescriptor(msg1, "item.ra")); | 1418 key_fields.push_back(GetFieldDescriptor(msg1, "item.ra")); |
1419 differencer.TreatAsMapWithMultipleFieldsAsKey( | 1419 differencer.TreatAsMapWithMultipleFieldsAsKey( |
1420 GetFieldDescriptor(msg1, "item"), key_fields); | 1420 GetFieldDescriptor(msg1, "item"), key_fields); |
1421 string output; | 1421 string output; |
1422 differencer.ReportDifferencesToString(&output); | 1422 differencer.ReportDifferencesToString(&output); |
1423 EXPECT_FALSE(differencer.Compare(msg1, msg2)); | 1423 EXPECT_FALSE(differencer.Compare(msg1, msg2)); |
1424 EXPECT_EQ( | 1424 EXPECT_EQ( |
1425 "added: item[0]: { a: 1 ra: 3 b: \"world\" }\n" | 1425 "added: item[0]: { a: 1 ra: 3 b: \"world\" }\n" |
1426 "deleted: item[0]: { a: 1 ra: 2 b: \"hello\" }\n", | 1426 "deleted: item[0]: { a: 1 ra: 2 b: \"hello\" }\n", |
(...skipping 23 matching lines...) Expand all Loading... |
1450 output); | 1450 output); |
1451 } | 1451 } |
1452 | 1452 |
1453 static const char* const kIgnoredFields[] = {"rm.b", "rm.m.b"}; | 1453 static const char* const kIgnoredFields[] = {"rm.b", "rm.m.b"}; |
1454 | 1454 |
1455 class TestIgnorer : public util::MessageDifferencer::IgnoreCriteria { | 1455 class TestIgnorer : public util::MessageDifferencer::IgnoreCriteria { |
1456 public: | 1456 public: |
1457 virtual bool IsIgnored( | 1457 virtual bool IsIgnored( |
1458 const Message& message1, const Message& message2, | 1458 const Message& message1, const Message& message2, |
1459 const FieldDescriptor* field, | 1459 const FieldDescriptor* field, |
1460 const vector<util::MessageDifferencer::SpecificField>& parent_fields) { | 1460 const std::vector<util::MessageDifferencer::SpecificField>& |
| 1461 parent_fields) { |
1461 string name = ""; | 1462 string name = ""; |
1462 for (int i = 0; i < parent_fields.size(); ++i) { | 1463 for (int i = 0; i < parent_fields.size(); ++i) { |
1463 name += parent_fields[i].field->name() + "."; | 1464 name += parent_fields[i].field->name() + "."; |
1464 } | 1465 } |
1465 name += field->name(); | 1466 name += field->name(); |
1466 for (int i = 0; i < GOOGLE_ARRAYSIZE(kIgnoredFields); ++i) { | 1467 for (int i = 0; i < GOOGLE_ARRAYSIZE(kIgnoredFields); ++i) { |
1467 if (name.compare(kIgnoredFields[i]) == 0) { | 1468 if (name.compare(kIgnoredFields[i]) == 0) { |
1468 return true; | 1469 return true; |
1469 } | 1470 } |
1470 } | 1471 } |
(...skipping 24 matching lines...) Expand all Loading... |
1495 EXPECT_TRUE(differ.Compare(msg1, msg2)); | 1496 EXPECT_TRUE(differ.Compare(msg1, msg2)); |
1496 } | 1497 } |
1497 | 1498 |
1498 // Takes the product of all elements of item.ra as the key for key comparison. | 1499 // Takes the product of all elements of item.ra as the key for key comparison. |
1499 class ValueProductMapKeyComparator | 1500 class ValueProductMapKeyComparator |
1500 : public util::MessageDifferencer::MapKeyComparator { | 1501 : public util::MessageDifferencer::MapKeyComparator { |
1501 public: | 1502 public: |
1502 typedef util::MessageDifferencer::SpecificField SpecificField; | 1503 typedef util::MessageDifferencer::SpecificField SpecificField; |
1503 virtual bool IsMatch( | 1504 virtual bool IsMatch( |
1504 const Message &message1, const Message &message2, | 1505 const Message &message1, const Message &message2, |
1505 const vector<SpecificField>& parent_fields) const { | 1506 const std::vector<SpecificField>& parent_fields) const { |
1506 const Reflection* reflection1 = message1.GetReflection(); | 1507 const Reflection* reflection1 = message1.GetReflection(); |
1507 const Reflection* reflection2 = message2.GetReflection(); | 1508 const Reflection* reflection2 = message2.GetReflection(); |
1508 // FieldDescriptor for item.ra | 1509 // FieldDescriptor for item.ra |
1509 const FieldDescriptor* ra_field = | 1510 const FieldDescriptor* ra_field = |
1510 message1.GetDescriptor()->FindFieldByName("ra"); | 1511 message1.GetDescriptor()->FindFieldByName("ra"); |
1511 // Get the product of all elements in item.ra | 1512 // Get the product of all elements in item.ra |
1512 int result1 = 1, result2 = 1; | 1513 int result1 = 1, result2 = 1; |
1513 for (int i = 0; i < reflection1->FieldSize(message1, ra_field); ++i) { | 1514 for (int i = 0; i < reflection1->FieldSize(message1, ra_field); ++i) { |
1514 result1 *= reflection1->GetRepeatedInt32(message1, ra_field, i); | 1515 result1 *= reflection1->GetRepeatedInt32(message1, ra_field, i); |
1515 } | 1516 } |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1856 msg1.add_rc(1); | 1857 msg1.add_rc(1); |
1857 msg1.add_rc(2); | 1858 msg1.add_rc(2); |
1858 | 1859 |
1859 msg2.set_c(3); | 1860 msg2.set_c(3); |
1860 msg2.add_rc(1); | 1861 msg2.add_rc(1); |
1861 msg2.add_rc(3); | 1862 msg2.add_rc(3); |
1862 | 1863 |
1863 const FieldDescriptor* c = GetFieldDescriptor(msg1, "c"); | 1864 const FieldDescriptor* c = GetFieldDescriptor(msg1, "c"); |
1864 const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc"); | 1865 const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc"); |
1865 | 1866 |
1866 vector<const FieldDescriptor*> fields; | 1867 std::vector<const FieldDescriptor*> fields; |
1867 fields.push_back(c); | 1868 fields.push_back(c); |
1868 fields.push_back(rc); | 1869 fields.push_back(rc); |
1869 | 1870 |
1870 util::MessageDifferencer differencer; | 1871 util::MessageDifferencer differencer; |
1871 differencer.IgnoreField(rc); | 1872 differencer.IgnoreField(rc); |
1872 | 1873 |
1873 differencer.set_scope(util::MessageDifferencer::FULL); | 1874 differencer.set_scope(util::MessageDifferencer::FULL); |
1874 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields)); | 1875 EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields)); |
1875 | 1876 |
1876 differencer.set_scope(util::MessageDifferencer::PARTIAL); | 1877 differencer.set_scope(util::MessageDifferencer::PARTIAL); |
(...skipping 15 matching lines...) Expand all Loading... |
1892 const google::protobuf::util::FieldContext* field_context) { | 1893 const google::protobuf::util::FieldContext* field_context) { |
1893 if (field_context) | 1894 if (field_context) |
1894 parent_fields_ = *(field_context->parent_fields()); | 1895 parent_fields_ = *(field_context->parent_fields()); |
1895 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 1896 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
1896 return RECURSE; | 1897 return RECURSE; |
1897 } else { | 1898 } else { |
1898 return SAME; | 1899 return SAME; |
1899 } | 1900 } |
1900 } | 1901 } |
1901 | 1902 |
1902 vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_field
s() { | 1903 std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_
fields() { |
1903 return parent_fields_; | 1904 return parent_fields_; |
1904 } | 1905 } |
1905 | 1906 |
1906 private: | 1907 private: |
1907 vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_field
s_; | 1908 std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_
fields_; |
1908 }; | 1909 }; |
1909 | 1910 |
1910 // Tests if MessageDifferencer sends the parent fields in the FieldContext | 1911 // Tests if MessageDifferencer sends the parent fields in the FieldContext |
1911 // parameter. | 1912 // parameter. |
1912 TEST(MessageDifferencerTest, FieldContextParentFieldsTest) { | 1913 TEST(MessageDifferencerTest, FieldContextParentFieldsTest) { |
1913 protobuf_unittest::TestDiffMessage msg1; | 1914 protobuf_unittest::TestDiffMessage msg1; |
1914 msg1.add_rm()->set_c(1); | 1915 msg1.add_rm()->set_c(1); |
1915 protobuf_unittest::TestDiffMessage msg2; | 1916 protobuf_unittest::TestDiffMessage msg2; |
1916 msg2.add_rm()->set_c(1); | 1917 msg2.add_rm()->set_c(1); |
1917 | 1918 |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2342 item = msg2.add_item(); | 2343 item = msg2.add_item(); |
2343 item->CopyFrom(msg1.item(1)); | 2344 item->CopyFrom(msg1.item(1)); |
2344 item->add_rb("second"); | 2345 item->add_rb("second"); |
2345 | 2346 |
2346 EXPECT_EQ("added: item[0].rb[1]: \"second\"\n" | 2347 EXPECT_EQ("added: item[0].rb[1]: \"second\"\n" |
2347 "deleted: item[0]: { rm { c: 2 rc: 10 rc: 10 } }\n", | 2348 "deleted: item[0]: { rm { c: 2 rc: 10 rc: 10 } }\n", |
2348 Run(msg1, msg2)); | 2349 Run(msg1, msg2)); |
2349 } | 2350 } |
2350 | 2351 |
2351 TEST_F(ComparisonTest, RepeatedSetOptionTest_Unknown) { | 2352 TEST_F(ComparisonTest, RepeatedSetOptionTest_Unknown) { |
2352 // Currently, as_set option doens't have affects on unknown field. | 2353 // Currently, as_set option doesn't have affects on unknown field. |
2353 // If needed, this feature will be added by request. | 2354 // If needed, this feature will be added by request. |
2354 repeated_field_as_set(); | 2355 repeated_field_as_set(); |
2355 unknown1_->AddGroup(245)->AddFixed32(248, 1); | 2356 unknown1_->AddGroup(245)->AddFixed32(248, 1); |
2356 unknown2_->AddGroup(245)->AddFixed32(248, 3); | 2357 unknown2_->AddGroup(245)->AddFixed32(248, 3); |
2357 unknown2_->AddGroup(245)->AddFixed32(248, 1); | 2358 unknown2_->AddGroup(245)->AddFixed32(248, 1); |
2358 | 2359 |
2359 // We expect it behaves the same as normal comparison. | 2360 // We expect it behaves the same as normal comparison. |
2360 EXPECT_EQ("modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n" | 2361 EXPECT_EQ("modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n" |
2361 "added: 245[1]: { ... }\n", | 2362 "added: 245[1]: { ... }\n", |
2362 RunUn()); | 2363 RunUn()); |
(...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3142 | 3143 |
3143 util::MessageDifferencer message_differencer; | 3144 util::MessageDifferencer message_differencer; |
3144 message_differencer.TreatAsSet(GetFieldDescriptor(m1, "repeated_any_value")); | 3145 message_differencer.TreatAsSet(GetFieldDescriptor(m1, "repeated_any_value")); |
3145 EXPECT_TRUE(message_differencer.Compare(m1, m2)); | 3146 EXPECT_TRUE(message_differencer.Compare(m1, m2)); |
3146 } | 3147 } |
3147 | 3148 |
3148 | 3149 |
3149 } // namespace | 3150 } // namespace |
3150 } // namespace protobuf | 3151 } // namespace protobuf |
3151 } // namespace google | 3152 } // namespace google |
OLD | NEW |