1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
17
// Author: kenton@google.com (Kenton Varda)
18
// Based on original Protocol Buffers design by
19
// Sanjay Ghemawat, Jeff Dean, and others.
21
// To test GeneratedMessageReflection, we actually let the protocol compiler
22
// generate a full protocol message implementation and then test its
23
// reflection interface. This is much easier and more maintainable than
24
// trying to create our own Message class for GeneratedMessageReflection
27
// The tests here closely mirror some of the tests in
28
// compiler/cpp/unittest, except using the reflection interface
29
// rather than generated accessors.
31
#include <google/protobuf/generated_message_reflection.h>
32
#include <google/protobuf/descriptor.h>
33
#include <google/protobuf/test_util.h>
34
#include <google/protobuf/unittest.pb.h>
36
#include <google/protobuf/stubs/common.h>
37
#include <google/protobuf/testing/googletest.h>
38
#include <gtest/gtest.h>
45
// Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
46
const FieldDescriptor* F(const string& name) {
47
const FieldDescriptor* result =
48
unittest::TestAllTypes::descriptor()->FindFieldByName(name);
49
GOOGLE_CHECK(result != NULL);
53
TEST(GeneratedMessageReflectionTest, Defaults) {
54
// Check that all default values are set correctly in the initial message.
55
unittest::TestAllTypes message;
56
TestUtil::ReflectionTester reflection_tester(
57
unittest::TestAllTypes::descriptor());
59
reflection_tester.ExpectClearViaReflection(*message.GetReflection());
61
const Message::Reflection& reflection = *message.GetReflection();
63
// Messages should return pointers to default instances until first use.
64
// (This is not checked by ExpectClear() since it is not actually true after
65
// the fields have been set and then cleared.)
66
EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
67
&reflection.GetMessage(F("optionalgroup")));
68
EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
69
&reflection.GetMessage(F("optional_nested_message")));
70
EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
71
&reflection.GetMessage(F("optional_foreign_message")));
72
EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
73
&reflection.GetMessage(F("optional_import_message")));
76
TEST(GeneratedMessageReflectionTest, Accessors) {
77
// Set every field to a unique value then go back and check all those
79
unittest::TestAllTypes message;
80
TestUtil::ReflectionTester reflection_tester(
81
unittest::TestAllTypes::descriptor());
83
reflection_tester.SetAllFieldsViaReflection(message.GetReflection());
84
TestUtil::ExpectAllFieldsSet(message);
85
reflection_tester.ExpectAllFieldsSetViaReflection(*message.GetReflection());
87
reflection_tester.ModifyRepeatedFieldsViaReflection(message.GetReflection());
88
TestUtil::ExpectRepeatedFieldsModified(message);
91
TEST(GeneratedMessageReflectionTest, GetStringReference) {
92
// Test that GetStringReference() returns the underlying string when it is
93
// a normal string field.
94
unittest::TestAllTypes message;
95
message.set_optional_string("foo");
96
message.add_repeated_string("foo");
98
const Message::Reflection& reflection = *message.GetReflection();
101
EXPECT_EQ(&message.optional_string(),
102
&reflection.GetStringReference(F("optional_string"), &scratch))
103
<< "For simple string fields, GetStringReference() should return a "
104
"reference to the underlying string.";
105
EXPECT_EQ(&message.repeated_string(0),
106
&reflection.GetRepeatedStringReference(F("repeated_string"), 0, &scratch))
107
<< "For simple string fields, GetRepeatedStringReference() should return "
108
"a reference to the underlying string.";
112
TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) {
113
// Check that after setting all fields and then clearing, getting an
114
// embedded message does NOT return the default instance.
115
unittest::TestAllTypes message;
116
TestUtil::ReflectionTester reflection_tester(
117
unittest::TestAllTypes::descriptor());
119
TestUtil::SetAllFields(&message);
122
const Message::Reflection& reflection = *message.GetReflection();
124
EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
125
&reflection.GetMessage(F("optionalgroup")));
126
EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
127
&reflection.GetMessage(F("optional_nested_message")));
128
EXPECT_NE(&unittest::ForeignMessage::default_instance(),
129
&reflection.GetMessage(F("optional_foreign_message")));
130
EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
131
&reflection.GetMessage(F("optional_import_message")));
134
TEST(GeneratedMessageReflectionTest, Extensions) {
135
// Set every extension to a unique value then go back and check all those
137
unittest::TestAllExtensions message;
138
TestUtil::ReflectionTester reflection_tester(
139
unittest::TestAllExtensions::descriptor());
141
reflection_tester.SetAllFieldsViaReflection(message.GetReflection());
142
TestUtil::ExpectAllExtensionsSet(message);
143
reflection_tester.ExpectAllFieldsSetViaReflection(*message.GetReflection());
145
reflection_tester.ModifyRepeatedFieldsViaReflection(message.GetReflection());
146
TestUtil::ExpectRepeatedExtensionsModified(message);
149
TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
150
const Message::Reflection& reflection =
151
*unittest::TestAllExtensions::default_instance().GetReflection();
153
const FieldDescriptor* extension1 =
154
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
155
"optional_int32_extension");
156
const FieldDescriptor* extension2 =
157
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
158
"repeated_string_extension");
160
EXPECT_EQ(extension1,
161
reflection.FindKnownExtensionByNumber(extension1->number()));
162
EXPECT_EQ(extension2,
163
reflection.FindKnownExtensionByNumber(extension2->number()));
165
// Non-existent extension.
166
EXPECT_TRUE(reflection.FindKnownExtensionByNumber(62341) == NULL);
168
// Extensions of TestAllExtensions should not show up as extensions of
170
EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
171
FindKnownExtensionByNumber(extension1->number()) == NULL);
174
TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
175
const Message::Reflection& reflection =
176
*unittest::TestAllExtensions::default_instance().GetReflection();
178
const FieldDescriptor* extension1 =
179
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
180
"optional_int32_extension");
181
const FieldDescriptor* extension2 =
182
unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
183
"repeated_string_extension");
185
EXPECT_EQ(extension1,
186
reflection.FindKnownExtensionByName(extension1->full_name()));
187
EXPECT_EQ(extension2,
188
reflection.FindKnownExtensionByName(extension2->full_name()));
190
// Non-existent extension.
191
EXPECT_TRUE(reflection.FindKnownExtensionByName("no_such_ext") == NULL);
193
// Extensions of TestAllExtensions should not show up as extensions of
195
EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
196
FindKnownExtensionByName(extension1->full_name()) == NULL);
199
#ifdef GTEST_HAS_DEATH_TEST
201
TEST(GeneratedMessageReflectionTest, UsageErrors) {
202
unittest::TestAllTypes message;
203
Message::Reflection* reflection = message.GetReflection();
204
const Descriptor* descriptor = message.GetDescriptor();
206
#define f(NAME) descriptor->FindFieldByName(NAME)
208
// Testing every single failure mode would be too much work. Let's just
211
reflection->GetInt32(descriptor->FindFieldByName("optional_int64")),
212
"Protocol Buffer reflection usage error:\n"
213
" Method : google::protobuf::Message::Reflection::GetInt32\n"
214
" Message type: protobuf_unittest\\.TestAllTypes\n"
215
" Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
216
" Problem : Field is not the right type for this message:\n"
217
" Expected : CPPTYPE_INT32\n"
218
" Field type: CPPTYPE_INT64");
220
reflection->GetInt32(descriptor->FindFieldByName("repeated_int32")),
221
"Protocol Buffer reflection usage error:\n"
222
" Method : google::protobuf::Message::Reflection::GetInt32\n"
223
" Message type: protobuf_unittest.TestAllTypes\n"
224
" Field : protobuf_unittest.TestAllTypes.repeated_int32\n"
225
" Problem : Field is repeated; the method requires a singular field.");
227
reflection->GetInt32(
228
unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
229
"Protocol Buffer reflection usage error:\n"
230
" Method : google::protobuf::Message::Reflection::GetInt32\n"
231
" Message type: protobuf_unittest.TestAllTypes\n"
232
" Field : protobuf_unittest.ForeignMessage.c\n"
233
" Problem : Field does not match message type.");
235
reflection->HasField(
236
unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
237
"Protocol Buffer reflection usage error:\n"
238
" Method : google::protobuf::Message::Reflection::HasField\n"
239
" Message type: protobuf_unittest.TestAllTypes\n"
240
" Field : protobuf_unittest.ForeignMessage.c\n"
241
" Problem : Field does not match message type.");
246
#endif // GTEST_HAS_DEATH_TEST
250
} // namespace protobuf
251
} // namespace google