1#! /usr/bin/python 2# -*- coding: utf-8 -*- 3# 4# Protocol Buffers - Google's data interchange format 5# Copyright 2008 Google Inc. All rights reserved. 6# https://developers.google.com/protocol-buffers/ 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions are 10# met: 11# 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 17# distribution. 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. 21# 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. 33 34"""Unittest for reflection.py, which tests the generated C++ implementation.""" 35 36__author__ = 'jasonh@google.com (Jason Hsueh)' 37 38import os 39os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 40os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2' 41 42from google.apputils import basetest 43from google.protobuf.internal import api_implementation 44from google.protobuf.internal import more_extensions_dynamic_pb2 45from google.protobuf.internal import more_extensions_pb2 46from google.protobuf.internal.reflection_test import * 47 48 49class ReflectionCppTest(basetest.TestCase): 50 def testImplementationSetting(self): 51 self.assertEqual('cpp', api_implementation.Type()) 52 self.assertEqual(2, api_implementation.Version()) 53 54 def testExtensionOfGeneratedTypeInDynamicFile(self): 55 """Tests that a file built dynamically can extend a generated C++ type. 56 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 62 available. 63 """ 64 pb1 = more_extensions_pb2.ExtendedMessage() 65 # Test that basic accessors work. 66 self.assertFalse( 67 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension)) 68 self.assertFalse( 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 72 self.assertTrue( 73 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension)) 74 self.assertTrue( 75 pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension)) 76 77 # Now serialize the data and parse to a new message. 78 pb2 = more_extensions_pb2.ExtendedMessage() 79 pb2.MergeFromString(pb1.SerializeToString()) 80 81 self.assertTrue( 82 pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension)) 83 self.assertTrue( 84 pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension)) 85 self.assertEqual( 86 17, pb2.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension]) 87 self.assertEqual( 88 24, 89 pb2.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a) 90 91 92 93if __name__ == '__main__': 94 basetest.main() 95