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