~ubuntu-branches/debian/squeeze/protobuf/squeeze

« back to all changes in this revision

Viewing changes to python/google/protobuf/internal/descriptor_test.py

  • Committer: Bazaar Package Importer
  • Author(s): Robert S. Edmonds
  • Date: 2010-01-25 18:14:49 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100125181449-98wvlw14imerlej8
Tags: 2.3.0-1
* New upstream version.
* Split out libprotobuf-lite from the libprotobuf package.
* Add CFLAGS specific to sh4; closes: #560322.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
__author__ = 'robinson@google.com (Will Robinson)'
36
36
 
37
37
import unittest
 
38
from google.protobuf import unittest_import_pb2
 
39
from google.protobuf import unittest_pb2
38
40
from google.protobuf import descriptor_pb2
39
41
from google.protobuf import descriptor
 
42
from google.protobuf import text_format
 
43
 
 
44
 
 
45
TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII = """
 
46
name: 'TestEmptyMessage'
 
47
"""
 
48
 
40
49
 
41
50
class DescriptorTest(unittest.TestCase):
42
51
 
43
52
  def setUp(self):
 
53
    self.my_file = descriptor.FileDescriptor(
 
54
        name='some/filename/some.proto',
 
55
        package='protobuf_unittest'
 
56
        )
44
57
    self.my_enum = descriptor.EnumDescriptor(
45
58
        name='ForeignEnum',
46
59
        full_name='protobuf_unittest.ForeignEnum',
47
 
        filename='ForeignEnum',
 
60
        filename=None,
 
61
        file=self.my_file,
48
62
        values=[
49
63
          descriptor.EnumValueDescriptor(name='FOREIGN_FOO', index=0, number=4),
50
64
          descriptor.EnumValueDescriptor(name='FOREIGN_BAR', index=1, number=5),
53
67
    self.my_message = descriptor.Descriptor(
54
68
        name='NestedMessage',
55
69
        full_name='protobuf_unittest.TestAllTypes.NestedMessage',
56
 
        filename='some/filename/some.proto',
 
70
        filename=None,
 
71
        file=self.my_file,
57
72
        containing_type=None,
58
73
        fields=[
59
74
          descriptor.FieldDescriptor(
61
76
            full_name='protobuf_unittest.TestAllTypes.NestedMessage.bb',
62
77
            index=0, number=1,
63
78
            type=5, cpp_type=1, label=1,
64
 
            default_value=0,
 
79
            has_default_value=False, default_value=0,
65
80
            message_type=None, enum_type=None, containing_type=None,
66
81
            is_extension=False, extension_scope=None),
67
82
        ],
80
95
    self.my_service = descriptor.ServiceDescriptor(
81
96
        name='TestServiceWithOptions',
82
97
        full_name='protobuf_unittest.TestServiceWithOptions',
 
98
        file=self.my_file,
83
99
        index=0,
84
100
        methods=[
85
101
            self.my_method
109
125
    self.assertEqual(self.my_service.GetOptions(),
110
126
                     descriptor_pb2.ServiceOptions())
111
127
 
 
128
  def testFileDescriptorReferences(self):
 
129
    self.assertEqual(self.my_enum.file, self.my_file)
 
130
    self.assertEqual(self.my_message.file, self.my_file)
 
131
 
 
132
  def testFileDescriptor(self):
 
133
    self.assertEqual(self.my_file.name, 'some/filename/some.proto')
 
134
    self.assertEqual(self.my_file.package, 'protobuf_unittest')
 
135
 
 
136
 
 
137
class DescriptorCopyToProtoTest(unittest.TestCase):
 
138
  """Tests for CopyTo functions of Descriptor."""
 
139
 
 
140
  def _AssertProtoEqual(self, actual_proto, expected_class, expected_ascii):
 
141
    expected_proto = expected_class()
 
142
    text_format.Merge(expected_ascii, expected_proto)
 
143
 
 
144
    self.assertEqual(
 
145
        actual_proto, expected_proto,
 
146
        'Not equal,\nActual:\n%s\nExpected:\n%s\n'
 
147
        % (str(actual_proto), str(expected_proto)))
 
148
 
 
149
  def _InternalTestCopyToProto(self, desc, expected_proto_class,
 
150
                               expected_proto_ascii):
 
151
    actual = expected_proto_class()
 
152
    desc.CopyToProto(actual)
 
153
    self._AssertProtoEqual(
 
154
        actual, expected_proto_class, expected_proto_ascii)
 
155
 
 
156
  def testCopyToProto_EmptyMessage(self):
 
157
    self._InternalTestCopyToProto(
 
158
        unittest_pb2.TestEmptyMessage.DESCRIPTOR,
 
159
        descriptor_pb2.DescriptorProto,
 
160
        TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII)
 
161
 
 
162
  def testCopyToProto_NestedMessage(self):
 
163
    TEST_NESTED_MESSAGE_ASCII = """
 
164
      name: 'NestedMessage'
 
165
      field: <
 
166
        name: 'bb'
 
167
        number: 1
 
168
        label: 1  # Optional
 
169
        type: 5  # TYPE_INT32
 
170
      >
 
171
      """
 
172
 
 
173
    self._InternalTestCopyToProto(
 
174
        unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR,
 
175
        descriptor_pb2.DescriptorProto,
 
176
        TEST_NESTED_MESSAGE_ASCII)
 
177
 
 
178
  def testCopyToProto_ForeignNestedMessage(self):
 
179
    TEST_FOREIGN_NESTED_ASCII = """
 
180
      name: 'TestForeignNested'
 
181
      field: <
 
182
        name: 'foreign_nested'
 
183
        number: 1
 
184
        label: 1  # Optional
 
185
        type: 11  # TYPE_MESSAGE
 
186
        type_name: '.protobuf_unittest.TestAllTypes.NestedMessage'
 
187
      >
 
188
      """
 
189
 
 
190
    self._InternalTestCopyToProto(
 
191
        unittest_pb2.TestForeignNested.DESCRIPTOR,
 
192
        descriptor_pb2.DescriptorProto,
 
193
        TEST_FOREIGN_NESTED_ASCII)
 
194
 
 
195
  def testCopyToProto_ForeignEnum(self):
 
196
    TEST_FOREIGN_ENUM_ASCII = """
 
197
      name: 'ForeignEnum'
 
198
      value: <
 
199
        name: 'FOREIGN_FOO'
 
200
        number: 4
 
201
      >
 
202
      value: <
 
203
        name: 'FOREIGN_BAR'
 
204
        number: 5
 
205
      >
 
206
      value: <
 
207
        name: 'FOREIGN_BAZ'
 
208
        number: 6
 
209
      >
 
210
      """
 
211
 
 
212
    self._InternalTestCopyToProto(
 
213
        unittest_pb2._FOREIGNENUM,
 
214
        descriptor_pb2.EnumDescriptorProto,
 
215
        TEST_FOREIGN_ENUM_ASCII)
 
216
 
 
217
  def testCopyToProto_Options(self):
 
218
    TEST_DEPRECATED_FIELDS_ASCII = """
 
219
      name: 'TestDeprecatedFields'
 
220
      field: <
 
221
        name: 'deprecated_int32'
 
222
        number: 1
 
223
        label: 1  # Optional
 
224
        type: 5  # TYPE_INT32
 
225
        options: <
 
226
          deprecated: true
 
227
        >
 
228
      >
 
229
      """
 
230
 
 
231
    self._InternalTestCopyToProto(
 
232
        unittest_pb2.TestDeprecatedFields.DESCRIPTOR,
 
233
        descriptor_pb2.DescriptorProto,
 
234
        TEST_DEPRECATED_FIELDS_ASCII)
 
235
 
 
236
  def testCopyToProto_AllExtensions(self):
 
237
    TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII = """
 
238
      name: 'TestEmptyMessageWithExtensions'
 
239
      extension_range: <
 
240
        start: 1
 
241
        end: 536870912
 
242
      >
 
243
      """
 
244
 
 
245
    self._InternalTestCopyToProto(
 
246
        unittest_pb2.TestEmptyMessageWithExtensions.DESCRIPTOR,
 
247
        descriptor_pb2.DescriptorProto,
 
248
        TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII)
 
249
 
 
250
  def testCopyToProto_SeveralExtensions(self):
 
251
    TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII = """
 
252
      name: 'TestMultipleExtensionRanges'
 
253
      extension_range: <
 
254
        start: 42
 
255
        end: 43
 
256
      >
 
257
      extension_range: <
 
258
        start: 4143
 
259
        end: 4244
 
260
      >
 
261
      extension_range: <
 
262
        start: 65536
 
263
        end: 536870912
 
264
      >
 
265
      """
 
266
 
 
267
    self._InternalTestCopyToProto(
 
268
        unittest_pb2.TestMultipleExtensionRanges.DESCRIPTOR,
 
269
        descriptor_pb2.DescriptorProto,
 
270
        TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII)
 
271
 
 
272
  def testCopyToProto_FileDescriptor(self):
 
273
    UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII = ("""
 
274
      name: 'google/protobuf/unittest_import.proto'
 
275
      package: 'protobuf_unittest_import'
 
276
      message_type: <
 
277
        name: 'ImportMessage'
 
278
        field: <
 
279
          name: 'd'
 
280
          number: 1
 
281
          label: 1  # Optional
 
282
          type: 5  # TYPE_INT32
 
283
        >
 
284
      >
 
285
      """ +
 
286
      """enum_type: <
 
287
        name: 'ImportEnum'
 
288
        value: <
 
289
          name: 'IMPORT_FOO'
 
290
          number: 7
 
291
        >
 
292
        value: <
 
293
          name: 'IMPORT_BAR'
 
294
          number: 8
 
295
        >
 
296
        value: <
 
297
          name: 'IMPORT_BAZ'
 
298
          number: 9
 
299
        >
 
300
      >
 
301
      options: <
 
302
        java_package: 'com.google.protobuf.test'
 
303
        optimize_for: 1  # SPEED
 
304
      >
 
305
      """)
 
306
 
 
307
    self._InternalTestCopyToProto(
 
308
        unittest_import_pb2.DESCRIPTOR,
 
309
        descriptor_pb2.FileDescriptorProto,
 
310
        UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII)
 
311
 
 
312
  def testCopyToProto_ServiceDescriptor(self):
 
313
    TEST_SERVICE_ASCII = """
 
314
      name: 'TestService'
 
315
      method: <
 
316
        name: 'Foo'
 
317
        input_type: '.protobuf_unittest.FooRequest'
 
318
        output_type: '.protobuf_unittest.FooResponse'
 
319
      >
 
320
      method: <
 
321
        name: 'Bar'
 
322
        input_type: '.protobuf_unittest.BarRequest'
 
323
        output_type: '.protobuf_unittest.BarResponse'
 
324
      >
 
325
      """
 
326
 
 
327
    self._InternalTestCopyToProto(
 
328
        unittest_pb2.TestService.DESCRIPTOR,
 
329
        descriptor_pb2.ServiceDescriptorProto,
 
330
        TEST_SERVICE_ASCII)
 
331
 
 
332
 
112
333
if __name__ == '__main__':
113
334
  unittest.main()