2 # -*- coding: utf-8 -*-
4 # Protocol Buffers - Google's data interchange format
5 # Copyright 2008 Google Inc. All rights reserved.
6 # https://developers.google.com/protocol-buffers/
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following disclaimer
16 # in the documentation and/or other materials provided with the
18 # * Neither the name of Google Inc. nor the names of its
19 # contributors may be used to endorse or promote products derived from
20 # this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 """Unittest for reflection.py, which tests the generated C++ implementation."""
36 __author__ = 'jasonh@google.com (Jason Hsueh)'
39 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
40 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2'
42 from google.apputils import basetest
43 from google.protobuf.internal import api_implementation
44 from google.protobuf.internal import more_extensions_dynamic_pb2
45 from google.protobuf.internal import more_extensions_pb2
46 from google.protobuf.internal.reflection_test import *
49 class ReflectionCppTest(basetest.TestCase):
50 def testImplementationSetting(self):
51 self.assertEqual('cpp', api_implementation.Type())
52 self.assertEqual(2, api_implementation.Version())
54 def testExtensionOfGeneratedTypeInDynamicFile(self):
55 """Tests that a file built dynamically can extend a generated C++ type.
57 The C++ implementation uses a DescriptorPool that has the generated
58 DescriptorPool as an underlay. Typically, a type can only find
59 extensions in its own pool. With the python C-extension, the generated C++
60 extendee may be available, but not the extension. This tests that the
61 C-extension implements the correct special handling to make such extensions
64 pb1 = more_extensions_pb2.ExtendedMessage()
65 # Test that basic accessors work.
67 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension))
69 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension))
70 pb1.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension] = 17
71 pb1.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a = 24
73 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension))
75 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension))
77 # Now serialize the data and parse to a new message.
78 pb2 = more_extensions_pb2.ExtendedMessage()
79 pb2.MergeFromString(pb1.SerializeToString())
82 pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension))
84 pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension))
86 17, pb2.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension])
89 pb2.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a)
93 if __name__ == '__main__':