This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/test_util.h>
#include <google/protobuf/unittest.pb.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>
namespace google {
namespace protobuf {
namespace {
const FieldDescriptor* F(const string& name) {
const FieldDescriptor* result =
unittest::TestAllTypes::descriptor()->FindFieldByName(name);
GOOGLE_CHECK(result != NULL);
return result;
}
TEST(GeneratedMessageReflectionTest, Defaults) {
unittest::TestAllTypes message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllTypes::descriptor());
reflection_tester.ExpectClearViaReflection(message);
const Reflection* reflection = message.GetReflection();
EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
&reflection->GetMessage(message, F("optionalgroup")));
EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
&reflection->GetMessage(message, F("optional_nested_message")));
EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
&reflection->GetMessage(message, F("optional_foreign_message")));
EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
&reflection->GetMessage(message, F("optional_import_message")));
}
TEST(GeneratedMessageReflectionTest, Accessors) {
unittest::TestAllTypes message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllTypes::descriptor());
reflection_tester.SetAllFieldsViaReflection(&message);
TestUtil::ExpectAllFieldsSet(message);
reflection_tester.ExpectAllFieldsSetViaReflection(message);
reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
TestUtil::ExpectRepeatedFieldsModified(message);
}
TEST(GeneratedMessageReflectionTest, GetStringReference) {
unittest::TestAllTypes message;
message.set_optional_string("foo");
message.add_repeated_string("foo");
const Reflection* reflection = message.GetReflection();
string scratch;
EXPECT_EQ(&message.optional_string(),
&reflection->GetStringReference(message, F("optional_string"), &scratch))
<< "For simple string fields, GetStringReference() should return a "
"reference to the underlying string.";
EXPECT_EQ(&message.repeated_string(0),
&reflection->GetRepeatedStringReference(message, F("repeated_string"),
0, &scratch))
<< "For simple string fields, GetRepeatedStringReference() should return "
"a reference to the underlying string.";
}
TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) {
unittest::TestAllTypes message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllTypes::descriptor());
TestUtil::SetAllFields(&message);
message.Clear();
const Reflection* reflection = message.GetReflection();
EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
&reflection->GetMessage(message, F("optionalgroup")));
EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
&reflection->GetMessage(message, F("optional_nested_message")));
EXPECT_NE(&unittest::ForeignMessage::default_instance(),
&reflection->GetMessage(message, F("optional_foreign_message")));
EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
&reflection->GetMessage(message, F("optional_import_message")));
}
TEST(GeneratedMessageReflectionTest, Swap) {
unittest::TestAllTypes message1;
unittest::TestAllTypes message2;
TestUtil::SetAllFields(&message1);
const Reflection* reflection = message1.GetReflection();
reflection->Swap(&message1, &message2);
TestUtil::ExpectClear(message1);
TestUtil::ExpectAllFieldsSet(message2);
}
TEST(GeneratedMessageReflectionTest, SwapWithBothSet) {
unittest::TestAllTypes message1;
unittest::TestAllTypes message2;
TestUtil::SetAllFields(&message1);
TestUtil::SetAllFields(&message2);
TestUtil::ModifyRepeatedFields(&message2);
const Reflection* reflection = message1.GetReflection();
reflection->Swap(&message1, &message2);
TestUtil::ExpectRepeatedFieldsModified(message1);
TestUtil::ExpectAllFieldsSet(message2);
message1.set_optional_int32(532819);
reflection->Swap(&message1, &message2);
EXPECT_EQ(532819, message2.optional_int32());
}
TEST(GeneratedMessageReflectionTest, SwapExtensions) {
unittest::TestAllExtensions message1;
unittest::TestAllExtensions message2;
TestUtil::SetAllExtensions(&message1);
const Reflection* reflection = message1.GetReflection();
reflection->Swap(&message1, &message2);
TestUtil::ExpectExtensionsClear(message1);
TestUtil::ExpectAllExtensionsSet(message2);
}
TEST(GeneratedMessageReflectionTest, SwapUnknown) {
unittest::TestEmptyMessage message1, message2;
message1.mutable_unknown_fields()->AddVarint(1234, 1);
EXPECT_EQ(1, message1.unknown_fields().field_count());
EXPECT_EQ(0, message2.unknown_fields().field_count());
const Reflection* reflection = message1.GetReflection();
reflection->Swap(&message1, &message2);
EXPECT_EQ(0, message1.unknown_fields().field_count());
EXPECT_EQ(1, message2.unknown_fields().field_count());
}
TEST(GeneratedMessageReflectionTest, RemoveLast) {
unittest::TestAllTypes message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllTypes::descriptor());
TestUtil::SetAllFields(&message);
reflection_tester.RemoveLastRepeatedsViaReflection(&message);
TestUtil::ExpectLastRepeatedsRemoved(message);
}
TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) {
unittest::TestAllExtensions message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllExtensions::descriptor());
TestUtil::SetAllExtensions(&message);
reflection_tester.RemoveLastRepeatedsViaReflection(&message);
TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
}
TEST(GeneratedMessageReflectionTest, ReleaseLast) {
unittest::TestAllTypes message;
const Descriptor* descriptor = message.GetDescriptor();
TestUtil::ReflectionTester reflection_tester(descriptor);
TestUtil::SetAllFields(&message);
reflection_tester.ReleaseLastRepeatedsViaReflection(&message, false);
TestUtil::ExpectLastRepeatedsReleased(message);
message.Clear();
TestUtil::SetAllFields(&message);
ASSERT_EQ(2, message.repeated_foreign_message_size());
const protobuf_unittest::ForeignMessage* expected =
message.mutable_repeated_foreign_message(1);
scoped_ptr<Message> released(message.GetReflection()->ReleaseLast(
&message, descriptor->FindFieldByName("repeated_foreign_message")));
EXPECT_EQ(expected, released.get());
}
TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) {
unittest::TestAllExtensions message;
const Descriptor* descriptor = message.GetDescriptor();
TestUtil::ReflectionTester reflection_tester(descriptor);
TestUtil::SetAllExtensions(&message);
reflection_tester.ReleaseLastRepeatedsViaReflection(&message, true);
TestUtil::ExpectLastRepeatedExtensionsReleased(message);
message.Clear();
TestUtil::SetAllExtensions(&message);
ASSERT_EQ(2, message.ExtensionSize(
unittest::repeated_foreign_message_extension));
const protobuf_unittest::ForeignMessage* expected = message.MutableExtension(
unittest::repeated_foreign_message_extension, 1);
scoped_ptr<Message> released(message.GetReflection()->ReleaseLast(
&message, descriptor->file()->FindExtensionByName(
"repeated_foreign_message_extension")));
EXPECT_EQ(expected, released.get());
}
TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) {
unittest::TestAllTypes message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllTypes::descriptor());
TestUtil::SetAllFields(&message);
reflection_tester.SwapRepeatedsViaReflection(&message);
TestUtil::ExpectRepeatedsSwapped(message);
reflection_tester.SwapRepeatedsViaReflection(&message);
TestUtil::ExpectAllFieldsSet(message);
}
TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) {
unittest::TestAllExtensions message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllExtensions::descriptor());
TestUtil::SetAllExtensions(&message);
reflection_tester.SwapRepeatedsViaReflection(&message);
TestUtil::ExpectRepeatedExtensionsSwapped(message);
reflection_tester.SwapRepeatedsViaReflection(&message);
TestUtil::ExpectAllExtensionsSet(message);
}
TEST(GeneratedMessageReflectionTest, Extensions) {
unittest::TestAllExtensions message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllExtensions::descriptor());
reflection_tester.SetAllFieldsViaReflection(&message);
TestUtil::ExpectAllExtensionsSet(message);
reflection_tester.ExpectAllFieldsSetViaReflection(message);
reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
TestUtil::ExpectRepeatedExtensionsModified(message);
}
TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
const Reflection* reflection =
unittest::TestAllExtensions::default_instance().GetReflection();
const FieldDescriptor* extension1 =
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
"optional_int32_extension");
const FieldDescriptor* extension2 =
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
"repeated_string_extension");
EXPECT_EQ(extension1,
reflection->FindKnownExtensionByNumber(extension1->number()));
EXPECT_EQ(extension2,
reflection->FindKnownExtensionByNumber(extension2->number()));
EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == NULL);
EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
FindKnownExtensionByNumber(extension1->number()) == NULL);
}
TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
const Reflection* reflection =
unittest::TestAllExtensions::default_instance().GetReflection();
const FieldDescriptor* extension1 =
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
"optional_int32_extension");
const FieldDescriptor* extension2 =
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
"repeated_string_extension");
EXPECT_EQ(extension1,
reflection->FindKnownExtensionByName(extension1->full_name()));
EXPECT_EQ(extension2,
reflection->FindKnownExtensionByName(extension2->full_name()));
EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == NULL);
EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
FindKnownExtensionByName(extension1->full_name()) == NULL);
}
TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) {
unittest::TestAllTypes message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllTypes::descriptor());
reflection_tester.ExpectMessagesReleasedViaReflection(
&message, TestUtil::ReflectionTester::IS_NULL);
reflection_tester.SetAllFieldsViaReflection(&message);
reflection_tester.ExpectMessagesReleasedViaReflection(
&message, TestUtil::ReflectionTester::NOT_NULL);
reflection_tester.SetAllFieldsViaReflection(&message);
message.Clear();
reflection_tester.ExpectMessagesReleasedViaReflection(
&message, TestUtil::ReflectionTester::CAN_BE_NULL);
TestUtil::SetAllFields(&message);
TestUtil::ExpectAllFieldsSet(message);
}
TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) {
unittest::TestAllExtensions message;
TestUtil::ReflectionTester reflection_tester(
unittest::TestAllExtensions::descriptor());
reflection_tester.ExpectMessagesReleasedViaReflection(
&message, TestUtil::ReflectionTester::IS_NULL);
reflection_tester.SetAllFieldsViaReflection(&message);
reflection_tester.ExpectMessagesReleasedViaReflection(
&message, TestUtil::ReflectionTester::NOT_NULL);
reflection_tester.SetAllFieldsViaReflection(&message);
message.Clear();
reflection_tester.ExpectMessagesReleasedViaReflection(
&message, TestUtil::ReflectionTester::CAN_BE_NULL);
TestUtil::SetAllExtensions(&message);
TestUtil::ExpectAllExtensionsSet(message);
}
#ifdef PROTOBUF_HAS_DEATH_TEST
TEST(GeneratedMessageReflectionTest, UsageErrors) {
unittest::TestAllTypes message;
const Reflection* reflection = message.GetReflection();
const Descriptor* descriptor = message.GetDescriptor();
#define f(NAME) descriptor->FindFieldByName(NAME)
EXPECT_DEATH(
reflection->GetInt32(
message, descriptor->FindFieldByName("optional_int64")),
"Protocol Buffer reflection usage error:\n"
" Method : google::protobuf::Reflection::GetInt32\n"
" Message type: protobuf_unittest\\.TestAllTypes\n"
" Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
" Problem : Field is not the right type for this message:\n"
" Expected : CPPTYPE_INT32\n"
" Field type: CPPTYPE_INT64");
EXPECT_DEATH(
reflection->GetInt32(
message, descriptor->FindFieldByName("repeated_int32")),
"Protocol Buffer reflection usage error:\n"
" Method : google::protobuf::Reflection::GetInt32\n"
" Message type: protobuf_unittest.TestAllTypes\n"
" Field : protobuf_unittest.TestAllTypes.repeated_int32\n"
" Problem : Field is repeated; the method requires a singular field.");
EXPECT_DEATH(
reflection->GetInt32(
message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
"Protocol Buffer reflection usage error:\n"
" Method : google::protobuf::Reflection::GetInt32\n"
" Message type: protobuf_unittest.TestAllTypes\n"
" Field : protobuf_unittest.ForeignMessage.c\n"
" Problem : Field does not match message type.");
EXPECT_DEATH(
reflection->HasField(
message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
"Protocol Buffer reflection usage error:\n"
" Method : google::protobuf::Reflection::HasField\n"
" Message type: protobuf_unittest.TestAllTypes\n"
" Field : protobuf_unittest.ForeignMessage.c\n"
" Problem : Field does not match message type.");
#undef f
}
#endif
}
}
}