blob: 0cb935a88064fb8a41d59d968009f1abb4347130 [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
jieluo@google.combde4a322014-08-12 21:10:30 +00002#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07005# https://developers.google.com/protocol-buffers/
jieluo@google.combde4a322014-08-12 21:10:30 +00006#
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"""Tests for google.protobuf.symbol_database."""
34
Jisi Liudbea00a2015-10-05 16:08:22 -070035try:
36 import unittest2 as unittest
37except ImportError:
38 import unittest
jieluo@google.combde4a322014-08-12 21:10:30 +000039from google.protobuf import unittest_pb2
Jisi Liu46e8ff62015-10-05 11:59:43 -070040from google.protobuf import descriptor
jieluo@google.combde4a322014-08-12 21:10:30 +000041from google.protobuf import symbol_database
42
Tres Seaver7ee25832015-01-13 14:47:32 -050043class SymbolDatabaseTest(unittest.TestCase):
jieluo@google.combde4a322014-08-12 21:10:30 +000044
45 def _Database(self):
Jisi Liu46e8ff62015-10-05 11:59:43 -070046 # TODO(b/17734095): Remove this difference when the C++ implementation
47 # supports multiple databases.
48 if descriptor._USE_C_DESCRIPTORS:
49 return symbol_database.Default()
50 else:
51 db = symbol_database.SymbolDatabase()
52 # Register representative types from unittest_pb2.
53 db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
54 db.RegisterMessage(unittest_pb2.TestAllTypes)
55 db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
56 db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
57 db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
58 db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
59 db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
60 return db
jieluo@google.combde4a322014-08-12 21:10:30 +000061
62 def testGetPrototype(self):
63 instance = self._Database().GetPrototype(
64 unittest_pb2.TestAllTypes.DESCRIPTOR)
65 self.assertTrue(instance is unittest_pb2.TestAllTypes)
66
67 def testGetMessages(self):
68 messages = self._Database().GetMessages(
69 ['google/protobuf/unittest.proto'])
70 self.assertTrue(
71 unittest_pb2.TestAllTypes is
72 messages['protobuf_unittest.TestAllTypes'])
73
74 def testGetSymbol(self):
Tres Seavera2abc942015-01-13 15:47:55 -050075 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +000076 unittest_pb2.TestAllTypes, self._Database().GetSymbol(
77 'protobuf_unittest.TestAllTypes'))
Tres Seavera2abc942015-01-13 15:47:55 -050078 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +000079 unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
80 'protobuf_unittest.TestAllTypes.NestedMessage'))
Tres Seavera2abc942015-01-13 15:47:55 -050081 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +000082 unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
83 'protobuf_unittest.TestAllTypes.OptionalGroup'))
Tres Seavera2abc942015-01-13 15:47:55 -050084 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +000085 unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
86 'protobuf_unittest.TestAllTypes.RepeatedGroup'))
87
88 def testEnums(self):
89 # Check registration of types in the pool.
Tres Seavera2abc942015-01-13 15:47:55 -050090 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +000091 'protobuf_unittest.ForeignEnum',
92 self._Database().pool.FindEnumTypeByName(
93 'protobuf_unittest.ForeignEnum').full_name)
Tres Seavera2abc942015-01-13 15:47:55 -050094 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +000095 'protobuf_unittest.TestAllTypes.NestedEnum',
96 self._Database().pool.FindEnumTypeByName(
97 'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
98
99 def testFindMessageTypeByName(self):
Tres Seavera2abc942015-01-13 15:47:55 -0500100 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +0000101 'protobuf_unittest.TestAllTypes',
102 self._Database().pool.FindMessageTypeByName(
103 'protobuf_unittest.TestAllTypes').full_name)
Tres Seavera2abc942015-01-13 15:47:55 -0500104 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +0000105 'protobuf_unittest.TestAllTypes.NestedMessage',
106 self._Database().pool.FindMessageTypeByName(
107 'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
108
109 def testFindFindContainingSymbol(self):
110 # Lookup based on either enum or message.
Tres Seavera2abc942015-01-13 15:47:55 -0500111 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +0000112 'google/protobuf/unittest.proto',
113 self._Database().pool.FindFileContainingSymbol(
114 'protobuf_unittest.TestAllTypes.NestedEnum').name)
Tres Seavera2abc942015-01-13 15:47:55 -0500115 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +0000116 'google/protobuf/unittest.proto',
117 self._Database().pool.FindFileContainingSymbol(
118 'protobuf_unittest.TestAllTypes').name)
119
120 def testFindFileByName(self):
Tres Seavera2abc942015-01-13 15:47:55 -0500121 self.assertEqual(
jieluo@google.combde4a322014-08-12 21:10:30 +0000122 'google/protobuf/unittest.proto',
123 self._Database().pool.FindFileByName(
124 'google/protobuf/unittest.proto').name)
125
126
127if __name__ == '__main__':
Tres Seaver7ee25832015-01-13 14:47:32 -0500128 unittest.main()