3
# Protocol Buffers - Google's data interchange format
4
# Copyright 2008 Google Inc. All rights reserved.
5
# http://code.google.com/p/protobuf/
7
# Redistribution and use in source and binary forms, with or without
8
# modification, are permitted provided that the following conditions are
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
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.
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.
33
"""Tests for google.protobuf.symbol_database."""
35
from google.apputils import basetest
36
from google.protobuf import unittest_pb2
37
from google.protobuf import symbol_database
40
class SymbolDatabaseTest(basetest.TestCase):
43
db = symbol_database.SymbolDatabase()
44
# Register representative types from unittest_pb2.
45
db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
46
db.RegisterMessage(unittest_pb2.TestAllTypes)
47
db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
48
db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
49
db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
50
db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
51
db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
54
def testGetPrototype(self):
55
instance = self._Database().GetPrototype(
56
unittest_pb2.TestAllTypes.DESCRIPTOR)
57
self.assertTrue(instance is unittest_pb2.TestAllTypes)
59
def testGetMessages(self):
60
messages = self._Database().GetMessages(
61
['google/protobuf/unittest.proto'])
63
unittest_pb2.TestAllTypes is
64
messages['protobuf_unittest.TestAllTypes'])
66
def testGetSymbol(self):
68
unittest_pb2.TestAllTypes, self._Database().GetSymbol(
69
'protobuf_unittest.TestAllTypes'))
71
unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
72
'protobuf_unittest.TestAllTypes.NestedMessage'))
74
unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
75
'protobuf_unittest.TestAllTypes.OptionalGroup'))
77
unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
78
'protobuf_unittest.TestAllTypes.RepeatedGroup'))
81
# Check registration of types in the pool.
83
'protobuf_unittest.ForeignEnum',
84
self._Database().pool.FindEnumTypeByName(
85
'protobuf_unittest.ForeignEnum').full_name)
87
'protobuf_unittest.TestAllTypes.NestedEnum',
88
self._Database().pool.FindEnumTypeByName(
89
'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
91
def testFindMessageTypeByName(self):
93
'protobuf_unittest.TestAllTypes',
94
self._Database().pool.FindMessageTypeByName(
95
'protobuf_unittest.TestAllTypes').full_name)
97
'protobuf_unittest.TestAllTypes.NestedMessage',
98
self._Database().pool.FindMessageTypeByName(
99
'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
101
def testFindFindContainingSymbol(self):
102
# Lookup based on either enum or message.
104
'google/protobuf/unittest.proto',
105
self._Database().pool.FindFileContainingSymbol(
106
'protobuf_unittest.TestAllTypes.NestedEnum').name)
108
'google/protobuf/unittest.proto',
109
self._Database().pool.FindFileContainingSymbol(
110
'protobuf_unittest.TestAllTypes').name)
112
def testFindFileByName(self):
114
'google/protobuf/unittest.proto',
115
self._Database().pool.FindFileByName(
116
'google/protobuf/unittest.proto').name)
119
if __name__ == '__main__':