descriptor_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # http://code.google.com/p/protobuf/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Unittest for google.protobuf.internal.descriptor."""
  31. __author__ = 'robinson@google.com (Will Robinson)'
  32. import unittest
  33. from google.protobuf import descriptor_pb2
  34. from google.protobuf import descriptor
  35. class DescriptorTest(unittest.TestCase):
  36. def setUp(self):
  37. self.my_enum = descriptor.EnumDescriptor(
  38. name='ForeignEnum',
  39. full_name='protobuf_unittest.ForeignEnum',
  40. filename='ForeignEnum',
  41. values=[
  42. descriptor.EnumValueDescriptor(name='FOREIGN_FOO', index=0, number=4),
  43. descriptor.EnumValueDescriptor(name='FOREIGN_BAR', index=1, number=5),
  44. descriptor.EnumValueDescriptor(name='FOREIGN_BAZ', index=2, number=6),
  45. ])
  46. self.my_message = descriptor.Descriptor(
  47. name='NestedMessage',
  48. full_name='protobuf_unittest.TestAllTypes.NestedMessage',
  49. filename='some/filename/some.proto',
  50. containing_type=None,
  51. fields=[
  52. descriptor.FieldDescriptor(
  53. name='bb',
  54. full_name='protobuf_unittest.TestAllTypes.NestedMessage.bb',
  55. index=0, number=1,
  56. type=5, cpp_type=1, label=1,
  57. default_value=0,
  58. message_type=None, enum_type=None, containing_type=None,
  59. is_extension=False, extension_scope=None),
  60. ],
  61. nested_types=[],
  62. enum_types=[
  63. self.my_enum,
  64. ],
  65. extensions=[])
  66. self.my_method = descriptor.MethodDescriptor(
  67. name='Bar',
  68. full_name='protobuf_unittest.TestService.Bar',
  69. index=0,
  70. containing_service=None,
  71. input_type=None,
  72. output_type=None)
  73. self.my_service = descriptor.ServiceDescriptor(
  74. name='TestServiceWithOptions',
  75. full_name='protobuf_unittest.TestServiceWithOptions',
  76. index=0,
  77. methods=[
  78. self.my_method
  79. ])
  80. def testEnumFixups(self):
  81. self.assertEqual(self.my_enum, self.my_enum.values[0].type)
  82. def testContainingTypeFixups(self):
  83. self.assertEqual(self.my_message, self.my_message.fields[0].containing_type)
  84. self.assertEqual(self.my_message, self.my_enum.containing_type)
  85. def testContainingServiceFixups(self):
  86. self.assertEqual(self.my_service, self.my_method.containing_service)
  87. def testGetOptions(self):
  88. self.assertEqual(self.my_enum.GetOptions(),
  89. descriptor_pb2.EnumOptions())
  90. self.assertEqual(self.my_enum.values[0].GetOptions(),
  91. descriptor_pb2.EnumValueOptions())
  92. self.assertEqual(self.my_message.GetOptions(),
  93. descriptor_pb2.MessageOptions())
  94. self.assertEqual(self.my_message.fields[0].GetOptions(),
  95. descriptor_pb2.FieldOptions())
  96. self.assertEqual(self.my_method.GetOptions(),
  97. descriptor_pb2.MethodOptions())
  98. self.assertEqual(self.my_service.GetOptions(),
  99. descriptor_pb2.ServiceOptions())
  100. if __name__ == '__main__':
  101. unittest.main()