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# http://code.google.com/p/protobuf/
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'
40
41import unittest
42from google.protobuf.internal import api_implementation
43from google.protobuf.internal import more_extensions_dynamic_pb2
44from google.protobuf.internal import more_extensions_pb2
45from google.protobuf.internal.reflection_test import *
46
47
48class ReflectionCppTest(unittest.TestCase):
49  def testImplementationSetting(self):
50    self.assertEqual('cpp', api_implementation.Type())
51
52  def testExtensionOfGeneratedTypeInDynamicFile(self):
53    """Tests that a file built dynamically can extend a generated C++ type.
54
55    The C++ implementation uses a DescriptorPool that has the generated
56    DescriptorPool as an underlay. Typically, a type can only find
57    extensions in its own pool. With the python C-extension, the generated C++
58    extendee may be available, but not the extension. This tests that the
59    C-extension implements the correct special handling to make such extensions
60    available.
61    """
62    pb1 = more_extensions_pb2.ExtendedMessage()
63    # Test that basic accessors work.
64    self.assertFalse(
65        pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension))
66    self.assertFalse(
67        pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension))
68    pb1.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension] = 17
69    pb1.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a = 24
70    self.assertTrue(
71        pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension))
72    self.assertTrue(
73        pb1.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension))
74
75    # Now serialize the data and parse to a new message.
76    pb2 = more_extensions_pb2.ExtendedMessage()
77    pb2.MergeFromString(pb1.SerializeToString())
78
79    self.assertTrue(
80        pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_int32_extension))
81    self.assertTrue(
82        pb2.HasExtension(more_extensions_dynamic_pb2.dynamic_message_extension))
83    self.assertEqual(
84        17, pb2.Extensions[more_extensions_dynamic_pb2.dynamic_int32_extension])
85    self.assertEqual(
86        24,
87        pb2.Extensions[more_extensions_dynamic_pb2.dynamic_message_extension].a)
88
89
90if __name__ == '__main__':
91  unittest.main()
92