symbol_database_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. """Tests for google.protobuf.symbol_database."""
  33. import unittest
  34. from google.protobuf import unittest_pb2
  35. from google.protobuf import symbol_database
  36. class SymbolDatabaseTest(unittest.TestCase):
  37. def _Database(self):
  38. db = symbol_database.SymbolDatabase()
  39. # Register representative types from unittest_pb2.
  40. db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
  41. db.RegisterMessage(unittest_pb2.TestAllTypes)
  42. db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
  43. db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
  44. db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
  45. db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
  46. db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
  47. return db
  48. def testGetPrototype(self):
  49. instance = self._Database().GetPrototype(
  50. unittest_pb2.TestAllTypes.DESCRIPTOR)
  51. self.assertTrue(instance is unittest_pb2.TestAllTypes)
  52. def testGetMessages(self):
  53. messages = self._Database().GetMessages(
  54. ['google/protobuf/unittest.proto'])
  55. self.assertTrue(
  56. unittest_pb2.TestAllTypes is
  57. messages['protobuf_unittest.TestAllTypes'])
  58. def testGetSymbol(self):
  59. self.assertEquals(
  60. unittest_pb2.TestAllTypes, self._Database().GetSymbol(
  61. 'protobuf_unittest.TestAllTypes'))
  62. self.assertEquals(
  63. unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
  64. 'protobuf_unittest.TestAllTypes.NestedMessage'))
  65. self.assertEquals(
  66. unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
  67. 'protobuf_unittest.TestAllTypes.OptionalGroup'))
  68. self.assertEquals(
  69. unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
  70. 'protobuf_unittest.TestAllTypes.RepeatedGroup'))
  71. def testEnums(self):
  72. # Check registration of types in the pool.
  73. self.assertEquals(
  74. 'protobuf_unittest.ForeignEnum',
  75. self._Database().pool.FindEnumTypeByName(
  76. 'protobuf_unittest.ForeignEnum').full_name)
  77. self.assertEquals(
  78. 'protobuf_unittest.TestAllTypes.NestedEnum',
  79. self._Database().pool.FindEnumTypeByName(
  80. 'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
  81. def testFindMessageTypeByName(self):
  82. self.assertEquals(
  83. 'protobuf_unittest.TestAllTypes',
  84. self._Database().pool.FindMessageTypeByName(
  85. 'protobuf_unittest.TestAllTypes').full_name)
  86. self.assertEquals(
  87. 'protobuf_unittest.TestAllTypes.NestedMessage',
  88. self._Database().pool.FindMessageTypeByName(
  89. 'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
  90. def testFindFindContainingSymbol(self):
  91. # Lookup based on either enum or message.
  92. self.assertEquals(
  93. 'google/protobuf/unittest.proto',
  94. self._Database().pool.FindFileContainingSymbol(
  95. 'protobuf_unittest.TestAllTypes.NestedEnum').name)
  96. self.assertEquals(
  97. 'google/protobuf/unittest.proto',
  98. self._Database().pool.FindFileContainingSymbol(
  99. 'protobuf_unittest.TestAllTypes').name)
  100. def testFindFileByName(self):
  101. self.assertEquals(
  102. 'google/protobuf/unittest.proto',
  103. self._Database().pool.FindFileByName(
  104. 'google/protobuf/unittest.proto').name)
  105. if __name__ == '__main__':
  106. unittest.main()