1#! /usr/bin/python 2# 3# Protocol Buffers - Google's data interchange format 4# Copyright 2008 Google Inc. All rights reserved. 5# https://developers.google.com/protocol-buffers/ 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions are 9# met: 10# 11# * Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# * Redistributions in binary form must reproduce the above 14# copyright notice, this list of conditions and the following disclaimer 15# in the documentation and/or other materials provided with the 16# distribution. 17# * Neither the name of Google Inc. nor the names of its 18# contributors may be used to endorse or promote products derived from 19# this software without specific prior written permission. 20# 21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33"""Unittest for google.protobuf.internal.descriptor.""" 34 35__author__ = 'robinson@google.com (Will Robinson)' 36 37from google.apputils import basetest 38from google.protobuf import unittest_custom_options_pb2 39from google.protobuf import unittest_import_pb2 40from google.protobuf import unittest_pb2 41from google.protobuf import descriptor_pb2 42from google.protobuf import descriptor 43from google.protobuf import text_format 44 45 46TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII = """ 47name: 'TestEmptyMessage' 48""" 49 50 51class DescriptorTest(basetest.TestCase): 52 53 def setUp(self): 54 self.my_file = descriptor.FileDescriptor( 55 name='some/filename/some.proto', 56 package='protobuf_unittest' 57 ) 58 self.my_enum = descriptor.EnumDescriptor( 59 name='ForeignEnum', 60 full_name='protobuf_unittest.ForeignEnum', 61 filename=None, 62 file=self.my_file, 63 values=[ 64 descriptor.EnumValueDescriptor(name='FOREIGN_FOO', index=0, number=4), 65 descriptor.EnumValueDescriptor(name='FOREIGN_BAR', index=1, number=5), 66 descriptor.EnumValueDescriptor(name='FOREIGN_BAZ', index=2, number=6), 67 ]) 68 self.my_message = descriptor.Descriptor( 69 name='NestedMessage', 70 full_name='protobuf_unittest.TestAllTypes.NestedMessage', 71 filename=None, 72 file=self.my_file, 73 containing_type=None, 74 fields=[ 75 descriptor.FieldDescriptor( 76 name='bb', 77 full_name='protobuf_unittest.TestAllTypes.NestedMessage.bb', 78 index=0, number=1, 79 type=5, cpp_type=1, label=1, 80 has_default_value=False, default_value=0, 81 message_type=None, enum_type=None, containing_type=None, 82 is_extension=False, extension_scope=None), 83 ], 84 nested_types=[], 85 enum_types=[ 86 self.my_enum, 87 ], 88 extensions=[]) 89 self.my_method = descriptor.MethodDescriptor( 90 name='Bar', 91 full_name='protobuf_unittest.TestService.Bar', 92 index=0, 93 containing_service=None, 94 input_type=None, 95 output_type=None) 96 self.my_service = descriptor.ServiceDescriptor( 97 name='TestServiceWithOptions', 98 full_name='protobuf_unittest.TestServiceWithOptions', 99 file=self.my_file, 100 index=0, 101 methods=[ 102 self.my_method 103 ]) 104 105 def testEnumValueName(self): 106 self.assertEqual(self.my_message.EnumValueName('ForeignEnum', 4), 107 'FOREIGN_FOO') 108 109 self.assertEqual( 110 self.my_message.enum_types_by_name[ 111 'ForeignEnum'].values_by_number[4].name, 112 self.my_message.EnumValueName('ForeignEnum', 4)) 113 114 def testEnumFixups(self): 115 self.assertEqual(self.my_enum, self.my_enum.values[0].type) 116 117 def testContainingTypeFixups(self): 118 self.assertEqual(self.my_message, self.my_message.fields[0].containing_type) 119 self.assertEqual(self.my_message, self.my_enum.containing_type) 120 121 def testContainingServiceFixups(self): 122 self.assertEqual(self.my_service, self.my_method.containing_service) 123 124 def testGetOptions(self): 125 self.assertEqual(self.my_enum.GetOptions(), 126 descriptor_pb2.EnumOptions()) 127 self.assertEqual(self.my_enum.values[0].GetOptions(), 128 descriptor_pb2.EnumValueOptions()) 129 self.assertEqual(self.my_message.GetOptions(), 130 descriptor_pb2.MessageOptions()) 131 self.assertEqual(self.my_message.fields[0].GetOptions(), 132 descriptor_pb2.FieldOptions()) 133 self.assertEqual(self.my_method.GetOptions(), 134 descriptor_pb2.MethodOptions()) 135 self.assertEqual(self.my_service.GetOptions(), 136 descriptor_pb2.ServiceOptions()) 137 138 def testSimpleCustomOptions(self): 139 file_descriptor = unittest_custom_options_pb2.DESCRIPTOR 140 message_descriptor =\ 141 unittest_custom_options_pb2.TestMessageWithCustomOptions.DESCRIPTOR 142 field_descriptor = message_descriptor.fields_by_name["field1"] 143 enum_descriptor = message_descriptor.enum_types_by_name["AnEnum"] 144 enum_value_descriptor =\ 145 message_descriptor.enum_values_by_name["ANENUM_VAL2"] 146 service_descriptor =\ 147 unittest_custom_options_pb2.TestServiceWithCustomOptions.DESCRIPTOR 148 method_descriptor = service_descriptor.FindMethodByName("Foo") 149 150 file_options = file_descriptor.GetOptions() 151 file_opt1 = unittest_custom_options_pb2.file_opt1 152 self.assertEqual(9876543210, file_options.Extensions[file_opt1]) 153 message_options = message_descriptor.GetOptions() 154 message_opt1 = unittest_custom_options_pb2.message_opt1 155 self.assertEqual(-56, message_options.Extensions[message_opt1]) 156 field_options = field_descriptor.GetOptions() 157 field_opt1 = unittest_custom_options_pb2.field_opt1 158 self.assertEqual(8765432109, field_options.Extensions[field_opt1]) 159 field_opt2 = unittest_custom_options_pb2.field_opt2 160 self.assertEqual(42, field_options.Extensions[field_opt2]) 161 enum_options = enum_descriptor.GetOptions() 162 enum_opt1 = unittest_custom_options_pb2.enum_opt1 163 self.assertEqual(-789, enum_options.Extensions[enum_opt1]) 164 enum_value_options = enum_value_descriptor.GetOptions() 165 enum_value_opt1 = unittest_custom_options_pb2.enum_value_opt1 166 self.assertEqual(123, enum_value_options.Extensions[enum_value_opt1]) 167 168 service_options = service_descriptor.GetOptions() 169 service_opt1 = unittest_custom_options_pb2.service_opt1 170 self.assertEqual(-9876543210, service_options.Extensions[service_opt1]) 171 method_options = method_descriptor.GetOptions() 172 method_opt1 = unittest_custom_options_pb2.method_opt1 173 self.assertEqual(unittest_custom_options_pb2.METHODOPT1_VAL2, 174 method_options.Extensions[method_opt1]) 175 176 def testDifferentCustomOptionTypes(self): 177 kint32min = -2**31 178 kint64min = -2**63 179 kint32max = 2**31 - 1 180 kint64max = 2**63 - 1 181 kuint32max = 2**32 - 1 182 kuint64max = 2**64 - 1 183 184 message_descriptor =\ 185 unittest_custom_options_pb2.CustomOptionMinIntegerValues.DESCRIPTOR 186 message_options = message_descriptor.GetOptions() 187 self.assertEqual(False, message_options.Extensions[ 188 unittest_custom_options_pb2.bool_opt]) 189 self.assertEqual(kint32min, message_options.Extensions[ 190 unittest_custom_options_pb2.int32_opt]) 191 self.assertEqual(kint64min, message_options.Extensions[ 192 unittest_custom_options_pb2.int64_opt]) 193 self.assertEqual(0, message_options.Extensions[ 194 unittest_custom_options_pb2.uint32_opt]) 195 self.assertEqual(0, message_options.Extensions[ 196 unittest_custom_options_pb2.uint64_opt]) 197 self.assertEqual(kint32min, message_options.Extensions[ 198 unittest_custom_options_pb2.sint32_opt]) 199 self.assertEqual(kint64min, message_options.Extensions[ 200 unittest_custom_options_pb2.sint64_opt]) 201 self.assertEqual(0, message_options.Extensions[ 202 unittest_custom_options_pb2.fixed32_opt]) 203 self.assertEqual(0, message_options.Extensions[ 204 unittest_custom_options_pb2.fixed64_opt]) 205 self.assertEqual(kint32min, message_options.Extensions[ 206 unittest_custom_options_pb2.sfixed32_opt]) 207 self.assertEqual(kint64min, message_options.Extensions[ 208 unittest_custom_options_pb2.sfixed64_opt]) 209 210 message_descriptor =\ 211 unittest_custom_options_pb2.CustomOptionMaxIntegerValues.DESCRIPTOR 212 message_options = message_descriptor.GetOptions() 213 self.assertEqual(True, message_options.Extensions[ 214 unittest_custom_options_pb2.bool_opt]) 215 self.assertEqual(kint32max, message_options.Extensions[ 216 unittest_custom_options_pb2.int32_opt]) 217 self.assertEqual(kint64max, message_options.Extensions[ 218 unittest_custom_options_pb2.int64_opt]) 219 self.assertEqual(kuint32max, message_options.Extensions[ 220 unittest_custom_options_pb2.uint32_opt]) 221 self.assertEqual(kuint64max, message_options.Extensions[ 222 unittest_custom_options_pb2.uint64_opt]) 223 self.assertEqual(kint32max, message_options.Extensions[ 224 unittest_custom_options_pb2.sint32_opt]) 225 self.assertEqual(kint64max, message_options.Extensions[ 226 unittest_custom_options_pb2.sint64_opt]) 227 self.assertEqual(kuint32max, message_options.Extensions[ 228 unittest_custom_options_pb2.fixed32_opt]) 229 self.assertEqual(kuint64max, message_options.Extensions[ 230 unittest_custom_options_pb2.fixed64_opt]) 231 self.assertEqual(kint32max, message_options.Extensions[ 232 unittest_custom_options_pb2.sfixed32_opt]) 233 self.assertEqual(kint64max, message_options.Extensions[ 234 unittest_custom_options_pb2.sfixed64_opt]) 235 236 message_descriptor =\ 237 unittest_custom_options_pb2.CustomOptionOtherValues.DESCRIPTOR 238 message_options = message_descriptor.GetOptions() 239 self.assertEqual(-100, message_options.Extensions[ 240 unittest_custom_options_pb2.int32_opt]) 241 self.assertAlmostEqual(12.3456789, message_options.Extensions[ 242 unittest_custom_options_pb2.float_opt], 6) 243 self.assertAlmostEqual(1.234567890123456789, message_options.Extensions[ 244 unittest_custom_options_pb2.double_opt]) 245 self.assertEqual("Hello, \"World\"", message_options.Extensions[ 246 unittest_custom_options_pb2.string_opt]) 247 self.assertEqual(b"Hello\0World", message_options.Extensions[ 248 unittest_custom_options_pb2.bytes_opt]) 249 dummy_enum = unittest_custom_options_pb2.DummyMessageContainingEnum 250 self.assertEqual( 251 dummy_enum.TEST_OPTION_ENUM_TYPE2, 252 message_options.Extensions[unittest_custom_options_pb2.enum_opt]) 253 254 message_descriptor =\ 255 unittest_custom_options_pb2.SettingRealsFromPositiveInts.DESCRIPTOR 256 message_options = message_descriptor.GetOptions() 257 self.assertAlmostEqual(12, message_options.Extensions[ 258 unittest_custom_options_pb2.float_opt], 6) 259 self.assertAlmostEqual(154, message_options.Extensions[ 260 unittest_custom_options_pb2.double_opt]) 261 262 message_descriptor =\ 263 unittest_custom_options_pb2.SettingRealsFromNegativeInts.DESCRIPTOR 264 message_options = message_descriptor.GetOptions() 265 self.assertAlmostEqual(-12, message_options.Extensions[ 266 unittest_custom_options_pb2.float_opt], 6) 267 self.assertAlmostEqual(-154, message_options.Extensions[ 268 unittest_custom_options_pb2.double_opt]) 269 270 def testComplexExtensionOptions(self): 271 descriptor =\ 272 unittest_custom_options_pb2.VariousComplexOptions.DESCRIPTOR 273 options = descriptor.GetOptions() 274 self.assertEqual(42, options.Extensions[ 275 unittest_custom_options_pb2.complex_opt1].foo) 276 self.assertEqual(324, options.Extensions[ 277 unittest_custom_options_pb2.complex_opt1].Extensions[ 278 unittest_custom_options_pb2.quux]) 279 self.assertEqual(876, options.Extensions[ 280 unittest_custom_options_pb2.complex_opt1].Extensions[ 281 unittest_custom_options_pb2.corge].qux) 282 self.assertEqual(987, options.Extensions[ 283 unittest_custom_options_pb2.complex_opt2].baz) 284 self.assertEqual(654, options.Extensions[ 285 unittest_custom_options_pb2.complex_opt2].Extensions[ 286 unittest_custom_options_pb2.grault]) 287 self.assertEqual(743, options.Extensions[ 288 unittest_custom_options_pb2.complex_opt2].bar.foo) 289 self.assertEqual(1999, options.Extensions[ 290 unittest_custom_options_pb2.complex_opt2].bar.Extensions[ 291 unittest_custom_options_pb2.quux]) 292 self.assertEqual(2008, options.Extensions[ 293 unittest_custom_options_pb2.complex_opt2].bar.Extensions[ 294 unittest_custom_options_pb2.corge].qux) 295 self.assertEqual(741, options.Extensions[ 296 unittest_custom_options_pb2.complex_opt2].Extensions[ 297 unittest_custom_options_pb2.garply].foo) 298 self.assertEqual(1998, options.Extensions[ 299 unittest_custom_options_pb2.complex_opt2].Extensions[ 300 unittest_custom_options_pb2.garply].Extensions[ 301 unittest_custom_options_pb2.quux]) 302 self.assertEqual(2121, options.Extensions[ 303 unittest_custom_options_pb2.complex_opt2].Extensions[ 304 unittest_custom_options_pb2.garply].Extensions[ 305 unittest_custom_options_pb2.corge].qux) 306 self.assertEqual(1971, options.Extensions[ 307 unittest_custom_options_pb2.ComplexOptionType2 308 .ComplexOptionType4.complex_opt4].waldo) 309 self.assertEqual(321, options.Extensions[ 310 unittest_custom_options_pb2.complex_opt2].fred.waldo) 311 self.assertEqual(9, options.Extensions[ 312 unittest_custom_options_pb2.complex_opt3].qux) 313 self.assertEqual(22, options.Extensions[ 314 unittest_custom_options_pb2.complex_opt3].complexoptiontype5.plugh) 315 self.assertEqual(24, options.Extensions[ 316 unittest_custom_options_pb2.complexopt6].xyzzy) 317 318 # Check that aggregate options were parsed and saved correctly in 319 # the appropriate descriptors. 320 def testAggregateOptions(self): 321 file_descriptor = unittest_custom_options_pb2.DESCRIPTOR 322 message_descriptor =\ 323 unittest_custom_options_pb2.AggregateMessage.DESCRIPTOR 324 field_descriptor = message_descriptor.fields_by_name["fieldname"] 325 enum_descriptor = unittest_custom_options_pb2.AggregateEnum.DESCRIPTOR 326 enum_value_descriptor = enum_descriptor.values_by_name["VALUE"] 327 service_descriptor =\ 328 unittest_custom_options_pb2.AggregateService.DESCRIPTOR 329 method_descriptor = service_descriptor.FindMethodByName("Method") 330 331 # Tests for the different types of data embedded in fileopt 332 file_options = file_descriptor.GetOptions().Extensions[ 333 unittest_custom_options_pb2.fileopt] 334 self.assertEqual(100, file_options.i) 335 self.assertEqual("FileAnnotation", file_options.s) 336 self.assertEqual("NestedFileAnnotation", file_options.sub.s) 337 self.assertEqual("FileExtensionAnnotation", file_options.file.Extensions[ 338 unittest_custom_options_pb2.fileopt].s) 339 self.assertEqual("EmbeddedMessageSetElement", file_options.mset.Extensions[ 340 unittest_custom_options_pb2.AggregateMessageSetElement 341 .message_set_extension].s) 342 343 # Simple tests for all the other types of annotations 344 self.assertEqual( 345 "MessageAnnotation", 346 message_descriptor.GetOptions().Extensions[ 347 unittest_custom_options_pb2.msgopt].s) 348 self.assertEqual( 349 "FieldAnnotation", 350 field_descriptor.GetOptions().Extensions[ 351 unittest_custom_options_pb2.fieldopt].s) 352 self.assertEqual( 353 "EnumAnnotation", 354 enum_descriptor.GetOptions().Extensions[ 355 unittest_custom_options_pb2.enumopt].s) 356 self.assertEqual( 357 "EnumValueAnnotation", 358 enum_value_descriptor.GetOptions().Extensions[ 359 unittest_custom_options_pb2.enumvalopt].s) 360 self.assertEqual( 361 "ServiceAnnotation", 362 service_descriptor.GetOptions().Extensions[ 363 unittest_custom_options_pb2.serviceopt].s) 364 self.assertEqual( 365 "MethodAnnotation", 366 method_descriptor.GetOptions().Extensions[ 367 unittest_custom_options_pb2.methodopt].s) 368 369 def testNestedOptions(self): 370 nested_message =\ 371 unittest_custom_options_pb2.NestedOptionType.NestedMessage.DESCRIPTOR 372 self.assertEqual(1001, nested_message.GetOptions().Extensions[ 373 unittest_custom_options_pb2.message_opt1]) 374 nested_field = nested_message.fields_by_name["nested_field"] 375 self.assertEqual(1002, nested_field.GetOptions().Extensions[ 376 unittest_custom_options_pb2.field_opt1]) 377 outer_message =\ 378 unittest_custom_options_pb2.NestedOptionType.DESCRIPTOR 379 nested_enum = outer_message.enum_types_by_name["NestedEnum"] 380 self.assertEqual(1003, nested_enum.GetOptions().Extensions[ 381 unittest_custom_options_pb2.enum_opt1]) 382 nested_enum_value = outer_message.enum_values_by_name["NESTED_ENUM_VALUE"] 383 self.assertEqual(1004, nested_enum_value.GetOptions().Extensions[ 384 unittest_custom_options_pb2.enum_value_opt1]) 385 nested_extension = outer_message.extensions_by_name["nested_extension"] 386 self.assertEqual(1005, nested_extension.GetOptions().Extensions[ 387 unittest_custom_options_pb2.field_opt2]) 388 389 def testFileDescriptorReferences(self): 390 self.assertEqual(self.my_enum.file, self.my_file) 391 self.assertEqual(self.my_message.file, self.my_file) 392 393 def testFileDescriptor(self): 394 self.assertEqual(self.my_file.name, 'some/filename/some.proto') 395 self.assertEqual(self.my_file.package, 'protobuf_unittest') 396 397 398class DescriptorCopyToProtoTest(basetest.TestCase): 399 """Tests for CopyTo functions of Descriptor.""" 400 401 def _AssertProtoEqual(self, actual_proto, expected_class, expected_ascii): 402 expected_proto = expected_class() 403 text_format.Merge(expected_ascii, expected_proto) 404 405 self.assertEqual( 406 actual_proto, expected_proto, 407 'Not equal,\nActual:\n%s\nExpected:\n%s\n' 408 % (str(actual_proto), str(expected_proto))) 409 410 def _InternalTestCopyToProto(self, desc, expected_proto_class, 411 expected_proto_ascii): 412 actual = expected_proto_class() 413 desc.CopyToProto(actual) 414 self._AssertProtoEqual( 415 actual, expected_proto_class, expected_proto_ascii) 416 417 def testCopyToProto_EmptyMessage(self): 418 self._InternalTestCopyToProto( 419 unittest_pb2.TestEmptyMessage.DESCRIPTOR, 420 descriptor_pb2.DescriptorProto, 421 TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII) 422 423 def testCopyToProto_NestedMessage(self): 424 TEST_NESTED_MESSAGE_ASCII = """ 425 name: 'NestedMessage' 426 field: < 427 name: 'bb' 428 number: 1 429 label: 1 # Optional 430 type: 5 # TYPE_INT32 431 > 432 """ 433 434 self._InternalTestCopyToProto( 435 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR, 436 descriptor_pb2.DescriptorProto, 437 TEST_NESTED_MESSAGE_ASCII) 438 439 def testCopyToProto_ForeignNestedMessage(self): 440 TEST_FOREIGN_NESTED_ASCII = """ 441 name: 'TestForeignNested' 442 field: < 443 name: 'foreign_nested' 444 number: 1 445 label: 1 # Optional 446 type: 11 # TYPE_MESSAGE 447 type_name: '.protobuf_unittest.TestAllTypes.NestedMessage' 448 > 449 """ 450 451 self._InternalTestCopyToProto( 452 unittest_pb2.TestForeignNested.DESCRIPTOR, 453 descriptor_pb2.DescriptorProto, 454 TEST_FOREIGN_NESTED_ASCII) 455 456 def testCopyToProto_ForeignEnum(self): 457 TEST_FOREIGN_ENUM_ASCII = """ 458 name: 'ForeignEnum' 459 value: < 460 name: 'FOREIGN_FOO' 461 number: 4 462 > 463 value: < 464 name: 'FOREIGN_BAR' 465 number: 5 466 > 467 value: < 468 name: 'FOREIGN_BAZ' 469 number: 6 470 > 471 """ 472 473 self._InternalTestCopyToProto( 474 unittest_pb2._FOREIGNENUM, 475 descriptor_pb2.EnumDescriptorProto, 476 TEST_FOREIGN_ENUM_ASCII) 477 478 def testCopyToProto_Options(self): 479 TEST_DEPRECATED_FIELDS_ASCII = """ 480 name: 'TestDeprecatedFields' 481 field: < 482 name: 'deprecated_int32' 483 number: 1 484 label: 1 # Optional 485 type: 5 # TYPE_INT32 486 options: < 487 deprecated: true 488 > 489 > 490 """ 491 492 self._InternalTestCopyToProto( 493 unittest_pb2.TestDeprecatedFields.DESCRIPTOR, 494 descriptor_pb2.DescriptorProto, 495 TEST_DEPRECATED_FIELDS_ASCII) 496 497 def testCopyToProto_AllExtensions(self): 498 TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII = """ 499 name: 'TestEmptyMessageWithExtensions' 500 extension_range: < 501 start: 1 502 end: 536870912 503 > 504 """ 505 506 self._InternalTestCopyToProto( 507 unittest_pb2.TestEmptyMessageWithExtensions.DESCRIPTOR, 508 descriptor_pb2.DescriptorProto, 509 TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII) 510 511 def testCopyToProto_SeveralExtensions(self): 512 TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII = """ 513 name: 'TestMultipleExtensionRanges' 514 extension_range: < 515 start: 42 516 end: 43 517 > 518 extension_range: < 519 start: 4143 520 end: 4244 521 > 522 extension_range: < 523 start: 65536 524 end: 536870912 525 > 526 """ 527 528 self._InternalTestCopyToProto( 529 unittest_pb2.TestMultipleExtensionRanges.DESCRIPTOR, 530 descriptor_pb2.DescriptorProto, 531 TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII) 532 533 # Disable this test so we can make changes to the proto file. 534 # TODO(xiaofeng): Enable this test after cl/55530659 is submitted. 535 # 536 # def testCopyToProto_FileDescriptor(self): 537 # UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII = (""" 538 # name: 'google/protobuf/unittest_import.proto' 539 # package: 'protobuf_unittest_import' 540 # dependency: 'google/protobuf/unittest_import_public.proto' 541 # message_type: < 542 # name: 'ImportMessage' 543 # field: < 544 # name: 'd' 545 # number: 1 546 # label: 1 # Optional 547 # type: 5 # TYPE_INT32 548 # > 549 # > 550 # """ + 551 # """enum_type: < 552 # name: 'ImportEnum' 553 # value: < 554 # name: 'IMPORT_FOO' 555 # number: 7 556 # > 557 # value: < 558 # name: 'IMPORT_BAR' 559 # number: 8 560 # > 561 # value: < 562 # name: 'IMPORT_BAZ' 563 # number: 9 564 # > 565 # > 566 # options: < 567 # java_package: 'com.google.protobuf.test' 568 # optimize_for: 1 # SPEED 569 # > 570 # public_dependency: 0 571 # """) 572 # self._InternalTestCopyToProto( 573 # unittest_import_pb2.DESCRIPTOR, 574 # descriptor_pb2.FileDescriptorProto, 575 # UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII) 576 577 def testCopyToProto_ServiceDescriptor(self): 578 TEST_SERVICE_ASCII = """ 579 name: 'TestService' 580 method: < 581 name: 'Foo' 582 input_type: '.protobuf_unittest.FooRequest' 583 output_type: '.protobuf_unittest.FooResponse' 584 > 585 method: < 586 name: 'Bar' 587 input_type: '.protobuf_unittest.BarRequest' 588 output_type: '.protobuf_unittest.BarResponse' 589 > 590 """ 591 self._InternalTestCopyToProto( 592 unittest_pb2.TestService.DESCRIPTOR, 593 descriptor_pb2.ServiceDescriptorProto, 594 TEST_SERVICE_ASCII) 595 596 597class MakeDescriptorTest(basetest.TestCase): 598 599 def testMakeDescriptorWithNestedFields(self): 600 file_descriptor_proto = descriptor_pb2.FileDescriptorProto() 601 file_descriptor_proto.name = 'Foo2' 602 message_type = file_descriptor_proto.message_type.add() 603 message_type.name = file_descriptor_proto.name 604 nested_type = message_type.nested_type.add() 605 nested_type.name = 'Sub' 606 enum_type = nested_type.enum_type.add() 607 enum_type.name = 'FOO' 608 enum_type_val = enum_type.value.add() 609 enum_type_val.name = 'BAR' 610 enum_type_val.number = 3 611 field = message_type.field.add() 612 field.number = 1 613 field.name = 'uint64_field' 614 field.label = descriptor.FieldDescriptor.LABEL_REQUIRED 615 field.type = descriptor.FieldDescriptor.TYPE_UINT64 616 field = message_type.field.add() 617 field.number = 2 618 field.name = 'nested_message_field' 619 field.label = descriptor.FieldDescriptor.LABEL_REQUIRED 620 field.type = descriptor.FieldDescriptor.TYPE_MESSAGE 621 field.type_name = 'Sub' 622 enum_field = nested_type.field.add() 623 enum_field.number = 2 624 enum_field.name = 'bar_field' 625 enum_field.label = descriptor.FieldDescriptor.LABEL_REQUIRED 626 enum_field.type = descriptor.FieldDescriptor.TYPE_ENUM 627 enum_field.type_name = 'Foo2.Sub.FOO' 628 629 result = descriptor.MakeDescriptor(message_type) 630 self.assertEqual(result.fields[0].cpp_type, 631 descriptor.FieldDescriptor.CPPTYPE_UINT64) 632 self.assertEqual(result.fields[1].cpp_type, 633 descriptor.FieldDescriptor.CPPTYPE_MESSAGE) 634 self.assertEqual(result.fields[1].message_type.containing_type, 635 result) 636 self.assertEqual(result.nested_types[0].fields[0].full_name, 637 'Foo2.Sub.bar_field') 638 self.assertEqual(result.nested_types[0].fields[0].enum_type, 639 result.nested_types[0].enum_types[0]) 640 641 def testMakeDescriptorWithUnsignedIntField(self): 642 file_descriptor_proto = descriptor_pb2.FileDescriptorProto() 643 file_descriptor_proto.name = 'Foo' 644 message_type = file_descriptor_proto.message_type.add() 645 message_type.name = file_descriptor_proto.name 646 enum_type = message_type.enum_type.add() 647 enum_type.name = 'FOO' 648 enum_type_val = enum_type.value.add() 649 enum_type_val.name = 'BAR' 650 enum_type_val.number = 3 651 field = message_type.field.add() 652 field.number = 1 653 field.name = 'uint64_field' 654 field.label = descriptor.FieldDescriptor.LABEL_REQUIRED 655 field.type = descriptor.FieldDescriptor.TYPE_UINT64 656 enum_field = message_type.field.add() 657 enum_field.number = 2 658 enum_field.name = 'bar_field' 659 enum_field.label = descriptor.FieldDescriptor.LABEL_REQUIRED 660 enum_field.type = descriptor.FieldDescriptor.TYPE_ENUM 661 enum_field.type_name = 'Foo.FOO' 662 663 result = descriptor.MakeDescriptor(message_type) 664 self.assertEqual(result.fields[0].cpp_type, 665 descriptor.FieldDescriptor.CPPTYPE_UINT64) 666 667 668if __name__ == '__main__': 669 basetest.main() 670