~ubuntu-branches/ubuntu/oneiric/protobuf/oneiric

« back to all changes in this revision

Viewing changes to src/google/protobuf/generated_message_reflection_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Iustin Pop
  • Date: 2008-08-03 11:01:44 UTC
  • Revision ID: james.westby@ubuntu.com-20080803110144-uyiw41bf1m2oe17t
Tags: upstream-2.0.0~b
ImportĀ upstreamĀ versionĀ 2.0.0~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Protocol Buffers - Google's data interchange format
 
2
// Copyright 2008 Google Inc.
 
3
// http://code.google.com/p/protobuf/
 
4
//
 
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
 
8
//
 
9
//      http://www.apache.org/licenses/LICENSE-2.0
 
10
//
 
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.
 
16
 
 
17
// Author: kenton@google.com (Kenton Varda)
 
18
//  Based on original Protocol Buffers design by
 
19
//  Sanjay Ghemawat, Jeff Dean, and others.
 
20
//
 
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
 
25
// to wrap.
 
26
//
 
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.
 
30
 
 
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>
 
35
 
 
36
#include <google/protobuf/stubs/common.h>
 
37
#include <google/protobuf/testing/googletest.h>
 
38
#include <gtest/gtest.h>
 
39
 
 
40
namespace google {
 
41
namespace protobuf {
 
42
 
 
43
namespace {
 
44
 
 
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);
 
50
  return result;
 
51
}
 
52
 
 
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());
 
58
 
 
59
  reflection_tester.ExpectClearViaReflection(*message.GetReflection());
 
60
 
 
61
  const Message::Reflection& reflection = *message.GetReflection();
 
62
 
 
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")));
 
74
}
 
75
 
 
76
TEST(GeneratedMessageReflectionTest, Accessors) {
 
77
  // Set every field to a unique value then go back and check all those
 
78
  // values.
 
79
  unittest::TestAllTypes message;
 
80
  TestUtil::ReflectionTester reflection_tester(
 
81
    unittest::TestAllTypes::descriptor());
 
82
 
 
83
  reflection_tester.SetAllFieldsViaReflection(message.GetReflection());
 
84
  TestUtil::ExpectAllFieldsSet(message);
 
85
  reflection_tester.ExpectAllFieldsSetViaReflection(*message.GetReflection());
 
86
 
 
87
  reflection_tester.ModifyRepeatedFieldsViaReflection(message.GetReflection());
 
88
  TestUtil::ExpectRepeatedFieldsModified(message);
 
89
}
 
90
 
 
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");
 
97
 
 
98
  const Message::Reflection& reflection = *message.GetReflection();
 
99
  string scratch;
 
100
 
 
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.";
 
109
}
 
110
 
 
111
 
 
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());
 
118
 
 
119
  TestUtil::SetAllFields(&message);
 
120
  message.Clear();
 
121
 
 
122
  const Message::Reflection& reflection = *message.GetReflection();
 
123
 
 
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")));
 
132
}
 
133
 
 
134
TEST(GeneratedMessageReflectionTest, Extensions) {
 
135
  // Set every extension to a unique value then go back and check all those
 
136
  // values.
 
137
  unittest::TestAllExtensions message;
 
138
  TestUtil::ReflectionTester reflection_tester(
 
139
    unittest::TestAllExtensions::descriptor());
 
140
 
 
141
  reflection_tester.SetAllFieldsViaReflection(message.GetReflection());
 
142
  TestUtil::ExpectAllExtensionsSet(message);
 
143
  reflection_tester.ExpectAllFieldsSetViaReflection(*message.GetReflection());
 
144
 
 
145
  reflection_tester.ModifyRepeatedFieldsViaReflection(message.GetReflection());
 
146
  TestUtil::ExpectRepeatedExtensionsModified(message);
 
147
}
 
148
 
 
149
TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
 
150
  const Message::Reflection& reflection =
 
151
    *unittest::TestAllExtensions::default_instance().GetReflection();
 
152
 
 
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");
 
159
 
 
160
  EXPECT_EQ(extension1,
 
161
            reflection.FindKnownExtensionByNumber(extension1->number()));
 
162
  EXPECT_EQ(extension2,
 
163
            reflection.FindKnownExtensionByNumber(extension2->number()));
 
164
 
 
165
  // Non-existent extension.
 
166
  EXPECT_TRUE(reflection.FindKnownExtensionByNumber(62341) == NULL);
 
167
 
 
168
  // Extensions of TestAllExtensions should not show up as extensions of
 
169
  // other types.
 
170
  EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
 
171
              FindKnownExtensionByNumber(extension1->number()) == NULL);
 
172
}
 
173
 
 
174
TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
 
175
  const Message::Reflection& reflection =
 
176
    *unittest::TestAllExtensions::default_instance().GetReflection();
 
177
 
 
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");
 
184
 
 
185
  EXPECT_EQ(extension1,
 
186
            reflection.FindKnownExtensionByName(extension1->full_name()));
 
187
  EXPECT_EQ(extension2,
 
188
            reflection.FindKnownExtensionByName(extension2->full_name()));
 
189
 
 
190
  // Non-existent extension.
 
191
  EXPECT_TRUE(reflection.FindKnownExtensionByName("no_such_ext") == NULL);
 
192
 
 
193
  // Extensions of TestAllExtensions should not show up as extensions of
 
194
  // other types.
 
195
  EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
 
196
              FindKnownExtensionByName(extension1->full_name()) == NULL);
 
197
}
 
198
 
 
199
#ifdef GTEST_HAS_DEATH_TEST
 
200
 
 
201
TEST(GeneratedMessageReflectionTest, UsageErrors) {
 
202
  unittest::TestAllTypes message;
 
203
  Message::Reflection* reflection = message.GetReflection();
 
204
  const Descriptor* descriptor = message.GetDescriptor();
 
205
 
 
206
#define f(NAME) descriptor->FindFieldByName(NAME)
 
207
 
 
208
  // Testing every single failure mode would be too much work.  Let's just
 
209
  // check a few.
 
210
  EXPECT_DEATH(
 
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");
 
219
  EXPECT_DEATH(
 
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.");
 
226
  EXPECT_DEATH(
 
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.");
 
234
  EXPECT_DEATH(
 
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.");
 
242
 
 
243
#undef f
 
244
}
 
245
 
 
246
#endif  // GTEST_HAS_DEATH_TEST
 
247
 
 
248
 
 
249
}  // namespace
 
250
}  // namespace protobuf
 
251
}  // namespace google