This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- FindInitializationErrors
- TEST
- TEST
- TEST
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/unittest.pb.h>
#include <google/protobuf/test_util.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>
#include <google/protobuf/stubs/strutil.h>
namespace google {
namespace protobuf {
namespace internal {
namespace {
TEST(ReflectionOpsTest, SanityCheck) {
unittest::TestAllTypes message;
TestUtil::SetAllFields(&message);
TestUtil::ExpectAllFieldsSet(message);
}
TEST(ReflectionOpsTest, Copy) {
unittest::TestAllTypes message, message2;
TestUtil::SetAllFields(&message);
ReflectionOps::Copy(message, &message2);
TestUtil::ExpectAllFieldsSet(message2);
ReflectionOps::Copy(message2, &message2);
TestUtil::ExpectAllFieldsSet(message2);
}
TEST(ReflectionOpsTest, CopyExtensions) {
unittest::TestAllExtensions message, message2;
TestUtil::SetAllExtensions(&message);
ReflectionOps::Copy(message, &message2);
TestUtil::ExpectAllExtensionsSet(message2);
}
TEST(ReflectionOpsTest, Merge) {
unittest::TestAllTypes message, message2;
TestUtil::SetAllFields(&message);
message2.set_optional_int32(message.optional_int32());
message.clear_optional_int32();
message2.set_optional_string(message.optional_string());
message.set_optional_string("something else");
message2.add_repeated_int32(message.repeated_int32(1));
int32 i = message.repeated_int32(0);
message.clear_repeated_int32();
message.add_repeated_int32(i);
ReflectionOps::Merge(message2, &message);
TestUtil::ExpectAllFieldsSet(message);
}
TEST(ReflectionOpsTest, MergeExtensions) {
unittest::TestAllExtensions message, message2;
TestUtil::SetAllExtensions(&message);
message2.SetExtension(unittest::optional_int32_extension,
message.GetExtension(unittest::optional_int32_extension));
message.ClearExtension(unittest::optional_int32_extension);
message2.SetExtension(unittest::optional_string_extension,
message.GetExtension(unittest::optional_string_extension));
message.SetExtension(unittest::optional_string_extension, "something else");
message2.AddExtension(unittest::repeated_int32_extension,
message.GetExtension(unittest::repeated_int32_extension, 1));
int32 i = message.GetExtension(unittest::repeated_int32_extension, 0);
message.ClearExtension(unittest::repeated_int32_extension);
message.AddExtension(unittest::repeated_int32_extension, i);
ReflectionOps::Merge(message2, &message);
TestUtil::ExpectAllExtensionsSet(message);
}
TEST(ReflectionOpsTest, MergeUnknown) {
unittest::TestEmptyMessage message1, message2;
message1.mutable_unknown_fields()->AddVarint(1234, 1);
message2.mutable_unknown_fields()->AddVarint(1234, 2);
ReflectionOps::Merge(message2, &message1);
ASSERT_EQ(2, message1.unknown_fields().field_count());
ASSERT_EQ(UnknownField::TYPE_VARINT,
message1.unknown_fields().field(0).type());
EXPECT_EQ(1, message1.unknown_fields().field(0).varint());
ASSERT_EQ(UnknownField::TYPE_VARINT,
message1.unknown_fields().field(1).type());
EXPECT_EQ(2, message1.unknown_fields().field(1).varint());
}
#ifdef PROTOBUF_HAS_DEATH_TEST
TEST(ReflectionOpsTest, MergeFromSelf) {
unittest::TestAllTypes message;
EXPECT_DEATH(
ReflectionOps::Merge(message, &message),
"&from");
}
#endif
TEST(ReflectionOpsTest, Clear) {
unittest::TestAllTypes message;
TestUtil::SetAllFields(&message);
ReflectionOps::Clear(&message);
TestUtil::ExpectClear(message);
EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
&message.optionalgroup());
EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
&message.optional_nested_message());
EXPECT_NE(&unittest::ForeignMessage::default_instance(),
&message.optional_foreign_message());
EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
&message.optional_import_message());
}
TEST(ReflectionOpsTest, ClearExtensions) {
unittest::TestAllExtensions message;
TestUtil::SetAllExtensions(&message);
ReflectionOps::Clear(&message);
TestUtil::ExpectExtensionsClear(message);
EXPECT_NE(&unittest::OptionalGroup_extension::default_instance(),
&message.GetExtension(unittest::optionalgroup_extension));
EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
&message.GetExtension(unittest::optional_nested_message_extension));
EXPECT_NE(&unittest::ForeignMessage::default_instance(),
&message.GetExtension(
unittest::optional_foreign_message_extension));
EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
&message.GetExtension(unittest::optional_import_message_extension));
}
TEST(ReflectionOpsTest, ClearUnknown) {
unittest::TestEmptyMessage message;
message.mutable_unknown_fields()->AddVarint(1234, 1);
ReflectionOps::Clear(&message);
EXPECT_EQ(0, message.unknown_fields().field_count());
}
TEST(ReflectionOpsTest, DiscardUnknownFields) {
unittest::TestAllTypes message;
TestUtil::SetAllFields(&message);
message.mutable_unknown_fields()
->AddVarint(123456, 654321);
message.mutable_optional_nested_message()
->mutable_unknown_fields()
->AddVarint(123456, 654321);
message.mutable_repeated_nested_message(0)
->mutable_unknown_fields()
->AddVarint(123456, 654321);
EXPECT_EQ(1, message.unknown_fields().field_count());
EXPECT_EQ(1, message.optional_nested_message()
.unknown_fields().field_count());
EXPECT_EQ(1, message.repeated_nested_message(0)
.unknown_fields().field_count());
ReflectionOps::DiscardUnknownFields(&message);
TestUtil::ExpectAllFieldsSet(message);
EXPECT_EQ(0, message.unknown_fields().field_count());
EXPECT_EQ(0, message.optional_nested_message()
.unknown_fields().field_count());
EXPECT_EQ(0, message.repeated_nested_message(0)
.unknown_fields().field_count());
}
TEST(ReflectionOpsTest, DiscardUnknownExtensions) {
unittest::TestAllExtensions message;
TestUtil::SetAllExtensions(&message);
message.mutable_unknown_fields()
->AddVarint(123456, 654321);
message.MutableExtension(unittest::optional_nested_message_extension)
->mutable_unknown_fields()
->AddVarint(123456, 654321);
message.MutableExtension(unittest::repeated_nested_message_extension, 0)
->mutable_unknown_fields()
->AddVarint(123456, 654321);
EXPECT_EQ(1, message.unknown_fields().field_count());
EXPECT_EQ(1,
message.GetExtension(unittest::optional_nested_message_extension)
.unknown_fields().field_count());
EXPECT_EQ(1,
message.GetExtension(unittest::repeated_nested_message_extension, 0)
.unknown_fields().field_count());
ReflectionOps::DiscardUnknownFields(&message);
TestUtil::ExpectAllExtensionsSet(message);
EXPECT_EQ(0, message.unknown_fields().field_count());
EXPECT_EQ(0,
message.GetExtension(unittest::optional_nested_message_extension)
.unknown_fields().field_count());
EXPECT_EQ(0,
message.GetExtension(unittest::repeated_nested_message_extension, 0)
.unknown_fields().field_count());
}
TEST(ReflectionOpsTest, IsInitialized) {
unittest::TestRequired message;
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
message.set_a(1);
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
message.set_b(2);
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
message.set_c(3);
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
}
TEST(ReflectionOpsTest, ForeignIsInitialized) {
unittest::TestRequiredForeign message;
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
message.mutable_optional_message();
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
message.mutable_optional_message()->set_a(1);
message.mutable_optional_message()->set_b(2);
message.mutable_optional_message()->set_c(3);
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
unittest::TestRequired* sub_message = message.add_repeated_message();
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
sub_message->set_a(1);
sub_message->set_b(2);
sub_message->set_c(3);
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
}
TEST(ReflectionOpsTest, ExtensionIsInitialized) {
unittest::TestAllExtensions message;
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
message.MutableExtension(unittest::TestRequired::single);
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
message.MutableExtension(unittest::TestRequired::single)->set_a(1);
message.MutableExtension(unittest::TestRequired::single)->set_b(2);
message.MutableExtension(unittest::TestRequired::single)->set_c(3);
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
message.AddExtension(unittest::TestRequired::multi);
EXPECT_FALSE(ReflectionOps::IsInitialized(message));
message.MutableExtension(unittest::TestRequired::multi, 0)->set_a(1);
message.MutableExtension(unittest::TestRequired::multi, 0)->set_b(2);
message.MutableExtension(unittest::TestRequired::multi, 0)->set_c(3);
EXPECT_TRUE(ReflectionOps::IsInitialized(message));
}
static string FindInitializationErrors(const Message& message) {
vector<string> errors;
ReflectionOps::FindInitializationErrors(message, "", &errors);
return JoinStrings(errors, ",");
}
TEST(ReflectionOpsTest, FindInitializationErrors) {
unittest::TestRequired message;
EXPECT_EQ("a,b,c", FindInitializationErrors(message));
}
TEST(ReflectionOpsTest, FindForeignInitializationErrors) {
unittest::TestRequiredForeign message;
message.mutable_optional_message();
message.add_repeated_message();
message.add_repeated_message();
EXPECT_EQ("optional_message.a,"
"optional_message.b,"
"optional_message.c,"
"repeated_message[0].a,"
"repeated_message[0].b,"
"repeated_message[0].c,"
"repeated_message[1].a,"
"repeated_message[1].b,"
"repeated_message[1].c",
FindInitializationErrors(message));
}
TEST(ReflectionOpsTest, FindExtensionInitializationErrors) {
unittest::TestAllExtensions message;
message.MutableExtension(unittest::TestRequired::single);
message.AddExtension(unittest::TestRequired::multi);
message.AddExtension(unittest::TestRequired::multi);
EXPECT_EQ("(protobuf_unittest.TestRequired.single).a,"
"(protobuf_unittest.TestRequired.single).b,"
"(protobuf_unittest.TestRequired.single).c,"
"(protobuf_unittest.TestRequired.multi)[0].a,"
"(protobuf_unittest.TestRequired.multi)[0].b,"
"(protobuf_unittest.TestRequired.multi)[0].c,"
"(protobuf_unittest.TestRequired.multi)[1].a,"
"(protobuf_unittest.TestRequired.multi)[1].b,"
"(protobuf_unittest.TestRequired.multi)[1].c",
FindInitializationErrors(message));
}
}
}
}
}