symbol_database_test.py 5.0 KB

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