diff options
Diffstat (limited to 'src/google/protobuf/util/message_differencer_unittest.cc')
-rw-r--r-- | src/google/protobuf/util/message_differencer_unittest.cc | 68 |
1 files changed, 54 insertions, 14 deletions
diff --git a/src/google/protobuf/util/message_differencer_unittest.cc b/src/google/protobuf/util/message_differencer_unittest.cc index a263d983..848290b1 100644 --- a/src/google/protobuf/util/message_differencer_unittest.cc +++ b/src/google/protobuf/util/message_differencer_unittest.cc @@ -35,6 +35,7 @@ // TODO(ksroka): Move some of these tests to field_comparator_test.cc. #include <algorithm> +#include <random> #include <string> #include <vector> @@ -906,8 +907,9 @@ TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllShuffledTest) { msg1.GetReflection()->ListFields(msg1, &fields1); msg2.GetReflection()->ListFields(msg2, &fields2); - std::random_shuffle(fields1.begin(), fields1.end()); - std::random_shuffle(fields2.begin(), fields2.end()); + std::default_random_engine rng; + std::shuffle(fields1.begin(), fields1.end(), rng); + std::shuffle(fields2.begin(), fields2.end(), rng); util::MessageDifferencer differencer; EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2)); @@ -1936,6 +1938,45 @@ TEST(MessageDifferencerTest, IgnoreField_TrumpsCompareWithFields) { EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields)); } +TEST(MessageDifferencerTest, IgnoreField_SetReportIgnoresFalse) { + protobuf_unittest::TestField msg1; + protobuf_unittest::TestField msg2; + + msg1.set_a(1); + msg1.set_b(2); + msg1.set_c(3); + msg1.add_rc(1); + msg1.add_rc(2); + + msg2.set_a(1); + msg2.set_b(1); + + const FieldDescriptor* a = GetFieldDescriptor(msg1, "a"); + const FieldDescriptor* b = GetFieldDescriptor(msg1, "b"); + const FieldDescriptor* c = GetFieldDescriptor(msg1, "c"); + const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc"); + + std::vector<const FieldDescriptor*> fields; + fields.push_back(a); + fields.push_back(b); + fields.push_back(c); + fields.push_back(rc); + + string diff_report; + util::MessageDifferencer differencer; + differencer.set_report_ignores(false); + differencer.set_report_matches(true); + differencer.ReportDifferencesToString(&diff_report); + differencer.IgnoreField(c); + differencer.IgnoreField(rc); + differencer.set_scope(util::MessageDifferencer::FULL); + EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields, fields)); + + EXPECT_EQ(diff_report, + "matched: a : 1\n" + "modified: b: 2 -> 1\n"); +} + // Test class to save a copy of the last field_context.parent_fields() vector // passed to the comparison function. @@ -1943,12 +1984,11 @@ class ParentSavingFieldComparator : public util::FieldComparator { public: ParentSavingFieldComparator() {} - virtual ComparisonResult Compare( - const google::protobuf::Message& message_1, - const google::protobuf::Message& message_2, - const google::protobuf::FieldDescriptor* field, - int index_1, int index_2, - const google::protobuf::util::FieldContext* field_context) { + virtual ComparisonResult Compare(const Message& message_1, + const Message& message_2, + const FieldDescriptor* field, int index_1, + int index_2, + const util::FieldContext* field_context) { if (field_context) parent_fields_ = *(field_context->parent_fields()); if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { @@ -1958,12 +1998,12 @@ class ParentSavingFieldComparator : public util::FieldComparator { } } - std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_fields() { + std::vector<util::MessageDifferencer::SpecificField> parent_fields() { return parent_fields_; } private: - std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_fields_; + std::vector<util::MessageDifferencer::SpecificField> parent_fields_; }; // Tests if MessageDifferencer sends the parent fields in the FieldContext @@ -2894,7 +2934,7 @@ TEST_F(ComparisonTest, MapIgnoreKeyTest) { } TEST_F(ComparisonTest, MapRoundTripSyncTest) { - google::protobuf::TextFormat::Parser parser; + TextFormat::Parser parser; unittest::TestMap map_reflection1; // By setting via reflection, data exists in repeated field. @@ -2910,7 +2950,7 @@ TEST_F(ComparisonTest, MapRoundTripSyncTest) { } TEST_F(ComparisonTest, MapEntryPartialTest) { - google::protobuf::TextFormat::Parser parser; + TextFormat::Parser parser; unittest::TestMap map1; unittest::TestMap map2; @@ -2932,7 +2972,7 @@ TEST_F(ComparisonTest, MapEntryPartialTest) { } TEST_F(ComparisonTest, MapEntryPartialEmptyKeyTest) { - google::protobuf::TextFormat::Parser parser; + TextFormat::Parser parser; unittest::TestMap map1; unittest::TestMap map2; ASSERT_TRUE(parser.ParseFromString("map_int32_foreign_message {}", &map1)); @@ -2961,7 +3001,7 @@ class LengthMapKeyComparator }; TEST_F(ComparisonTest, MapEntryCustomMapKeyComparator) { - google::protobuf::TextFormat::Parser parser; + TextFormat::Parser parser; protobuf_unittest::TestMap msg1; protobuf_unittest::TestMap msg2; |