descriptor_test.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. """Unittest for google.protobuf.internal.descriptor."""
  17. __author__ = 'robinson@google.com (Will Robinson)'
  18. import unittest
  19. from google.protobuf import descriptor_pb2
  20. from google.protobuf import descriptor
  21. class DescriptorTest(unittest.TestCase):
  22. def setUp(self):
  23. self.my_enum = descriptor.EnumDescriptor(
  24. name='ForeignEnum',
  25. full_name='protobuf_unittest.ForeignEnum',
  26. filename='ForeignEnum',
  27. values=[
  28. descriptor.EnumValueDescriptor(name='FOREIGN_FOO', index=0, number=4),
  29. descriptor.EnumValueDescriptor(name='FOREIGN_BAR', index=1, number=5),
  30. descriptor.EnumValueDescriptor(name='FOREIGN_BAZ', index=2, number=6),
  31. ])
  32. self.my_message = descriptor.Descriptor(
  33. name='NestedMessage',
  34. full_name='protobuf_unittest.TestAllTypes.NestedMessage',
  35. filename='some/filename/some.proto',
  36. containing_type=None,
  37. fields=[
  38. descriptor.FieldDescriptor(
  39. name='bb',
  40. full_name='protobuf_unittest.TestAllTypes.NestedMessage.bb',
  41. index=0, number=1,
  42. type=5, cpp_type=1, label=1,
  43. default_value=0,
  44. message_type=None, enum_type=None, containing_type=None,
  45. is_extension=False, extension_scope=None),
  46. ],
  47. nested_types=[],
  48. enum_types=[
  49. self.my_enum,
  50. ],
  51. extensions=[])
  52. self.my_method = descriptor.MethodDescriptor(
  53. name='Bar',
  54. full_name='protobuf_unittest.TestService.Bar',
  55. index=0,
  56. containing_service=None,
  57. input_type=None,
  58. output_type=None)
  59. self.my_service = descriptor.ServiceDescriptor(
  60. name='TestServiceWithOptions',
  61. full_name='protobuf_unittest.TestServiceWithOptions',
  62. index=0,
  63. methods=[
  64. self.my_method
  65. ])
  66. def testEnumFixups(self):
  67. self.assertEqual(self.my_enum, self.my_enum.values[0].type)
  68. def testContainingTypeFixups(self):
  69. self.assertEqual(self.my_message, self.my_message.fields[0].containing_type)
  70. self.assertEqual(self.my_message, self.my_enum.containing_type)
  71. def testContainingServiceFixups(self):
  72. self.assertEqual(self.my_service, self.my_method.containing_service)
  73. def testGetOptions(self):
  74. self.assertEqual(self.my_enum.GetOptions(),
  75. descriptor_pb2.EnumOptions())
  76. self.assertEqual(self.my_enum.values[0].GetOptions(),
  77. descriptor_pb2.EnumValueOptions())
  78. self.assertEqual(self.my_message.GetOptions(),
  79. descriptor_pb2.MessageOptions())
  80. self.assertEqual(self.my_message.fields[0].GetOptions(),
  81. descriptor_pb2.FieldOptions())
  82. self.assertEqual(self.my_method.GetOptions(),
  83. descriptor_pb2.MethodOptions())
  84. self.assertEqual(self.my_service.GetOptions(),
  85. descriptor_pb2.ServiceOptions())
  86. if __name__ == '__main__':
  87. unittest.main()