blob: e4387c8553f267aff485d865c931c204a1862ba1 [file] [log] [blame]
kenton@google.com26bd9ee2008-11-21 00:06:27 +00001#! /usr/bin/python
2#
temporal40ee5512008-07-10 02:12:20 +00003# Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00004# Copyright 2008 Google Inc. All rights reserved.
temporal40ee5512008-07-10 02:12:20 +00005# http://code.google.com/p/protobuf/
6#
kenton@google.com24bf56f2008-09-24 20:31:01 +00007# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
temporal40ee5512008-07-10 02:12:20 +000010#
kenton@google.com24bf56f2008-09-24 20:31:01 +000011# * 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.
temporal40ee5512008-07-10 02:12:20 +000020#
kenton@google.com24bf56f2008-09-24 20:31:01 +000021# 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.
temporal40ee5512008-07-10 02:12:20 +000032
33# TODO(robinson): Flesh this out considerably. We focused on reflection_test.py
34# first, since it's testing the subtler code, and since it provides decent
35# indirect testing of the protocol compiler output.
36
37"""Unittest that directly tests the output of the pure-Python protocol
kenton@google.comfccb1462009-12-18 02:11:36 +000038compiler. See //google/protobuf/reflection_test.py for a test which
temporal40ee5512008-07-10 02:12:20 +000039further ensures that we can use Python protocol message objects as we expect.
40"""
41
42__author__ = 'robinson@google.com (Will Robinson)'
43
44import unittest
liujisi@google.com33165fe2010-11-02 13:14:58 +000045from google.protobuf import unittest_custom_options_pb2
kenton@google.comfccb1462009-12-18 02:11:36 +000046from google.protobuf import unittest_import_pb2
temporal40ee5512008-07-10 02:12:20 +000047from google.protobuf import unittest_mset_pb2
48from google.protobuf import unittest_pb2
kenton@google.comfccb1462009-12-18 02:11:36 +000049from google.protobuf import unittest_no_generic_services_pb2
liujisi@google.com33165fe2010-11-02 13:14:58 +000050from google.protobuf import service
kenton@google.comfccb1462009-12-18 02:11:36 +000051
52MAX_EXTENSION = 536870912
temporal40ee5512008-07-10 02:12:20 +000053
54
55class GeneratorTest(unittest.TestCase):
56
57 def testNestedMessageDescriptor(self):
58 field_name = 'optional_nested_message'
59 proto_type = unittest_pb2.TestAllTypes
60 self.assertEqual(
61 proto_type.NestedMessage.DESCRIPTOR,
62 proto_type.DESCRIPTOR.fields_by_name[field_name].message_type)
63
64 def testEnums(self):
65 # We test only module-level enums here.
66 # TODO(robinson): Examine descriptors directly to check
67 # enum descriptor output.
68 self.assertEqual(4, unittest_pb2.FOREIGN_FOO)
69 self.assertEqual(5, unittest_pb2.FOREIGN_BAR)
70 self.assertEqual(6, unittest_pb2.FOREIGN_BAZ)
71
72 proto = unittest_pb2.TestAllTypes()
73 self.assertEqual(1, proto.FOO)
74 self.assertEqual(1, unittest_pb2.TestAllTypes.FOO)
75 self.assertEqual(2, proto.BAR)
76 self.assertEqual(2, unittest_pb2.TestAllTypes.BAR)
77 self.assertEqual(3, proto.BAZ)
78 self.assertEqual(3, unittest_pb2.TestAllTypes.BAZ)
79
kenton@google.comfccb1462009-12-18 02:11:36 +000080 def testExtremeDefaultValues(self):
81 message = unittest_pb2.TestExtremeDefaultValues()
kenton@google.comd0047c42009-12-23 02:01:01 +000082
83 # Python pre-2.6 does not have isinf() or isnan() functions, so we have
84 # to provide our own.
85 def isnan(val):
86 # NaN is never equal to itself.
87 return val != val
88 def isinf(val):
89 # Infinity times zero equals NaN.
90 return not isnan(val) and isnan(val * 0)
91
92 self.assertTrue(isinf(message.inf_double))
93 self.assertTrue(message.inf_double > 0)
94 self.assertTrue(isinf(message.neg_inf_double))
95 self.assertTrue(message.neg_inf_double < 0)
96 self.assertTrue(isnan(message.nan_double))
97
98 self.assertTrue(isinf(message.inf_float))
99 self.assertTrue(message.inf_float > 0)
100 self.assertTrue(isinf(message.neg_inf_float))
101 self.assertTrue(message.neg_inf_float < 0)
102 self.assertTrue(isnan(message.nan_float))
kenton@google.comfccb1462009-12-18 02:11:36 +0000103
104 def testHasDefaultValues(self):
105 desc = unittest_pb2.TestAllTypes.DESCRIPTOR
106
107 expected_has_default_by_name = {
108 'optional_int32': False,
109 'repeated_int32': False,
110 'optional_nested_message': False,
111 'default_int32': True,
112 }
113
114 has_default_by_name = dict(
115 [(f.name, f.has_default_value)
116 for f in desc.fields
117 if f.name in expected_has_default_by_name])
118 self.assertEqual(expected_has_default_by_name, has_default_by_name)
119
temporal40ee5512008-07-10 02:12:20 +0000120 def testContainingTypeBehaviorForExtensions(self):
121 self.assertEqual(unittest_pb2.optional_int32_extension.containing_type,
122 unittest_pb2.TestAllExtensions.DESCRIPTOR)
123 self.assertEqual(unittest_pb2.TestRequired.single.containing_type,
124 unittest_pb2.TestAllExtensions.DESCRIPTOR)
125
126 def testExtensionScope(self):
127 self.assertEqual(unittest_pb2.optional_int32_extension.extension_scope,
128 None)
129 self.assertEqual(unittest_pb2.TestRequired.single.extension_scope,
130 unittest_pb2.TestRequired.DESCRIPTOR)
131
132 def testIsExtension(self):
133 self.assertTrue(unittest_pb2.optional_int32_extension.is_extension)
134 self.assertTrue(unittest_pb2.TestRequired.single.is_extension)
135
136 message_descriptor = unittest_pb2.TestRequired.DESCRIPTOR
137 non_extension_descriptor = message_descriptor.fields_by_name['a']
138 self.assertTrue(not non_extension_descriptor.is_extension)
139
140 def testOptions(self):
141 proto = unittest_mset_pb2.TestMessageSet()
142 self.assertTrue(proto.DESCRIPTOR.GetOptions().message_set_wire_format)
143
liujisi@google.com33165fe2010-11-02 13:14:58 +0000144 def testMessageWithCustomOptions(self):
145 proto = unittest_custom_options_pb2.TestMessageWithCustomOptions()
146 enum_options = proto.DESCRIPTOR.enum_types_by_name['AnEnum'].GetOptions()
147 self.assertTrue(enum_options is not None)
148 # TODO(gps): We really should test for the presense of the enum_opt1
149 # extension and for its value to be set to -789.
150
kenton@google.comfccb1462009-12-18 02:11:36 +0000151 def testNestedTypes(self):
152 self.assertEquals(
153 set(unittest_pb2.TestAllTypes.DESCRIPTOR.nested_types),
154 set([
155 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR,
156 unittest_pb2.TestAllTypes.OptionalGroup.DESCRIPTOR,
157 unittest_pb2.TestAllTypes.RepeatedGroup.DESCRIPTOR,
158 ]))
159 self.assertEqual(unittest_pb2.TestEmptyMessage.DESCRIPTOR.nested_types, [])
160 self.assertEqual(
161 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR.nested_types, [])
162
163 def testContainingType(self):
164 self.assertTrue(
165 unittest_pb2.TestEmptyMessage.DESCRIPTOR.containing_type is None)
166 self.assertTrue(
167 unittest_pb2.TestAllTypes.DESCRIPTOR.containing_type is None)
168 self.assertEqual(
169 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR.containing_type,
170 unittest_pb2.TestAllTypes.DESCRIPTOR)
171 self.assertEqual(
172 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR.containing_type,
173 unittest_pb2.TestAllTypes.DESCRIPTOR)
174 self.assertEqual(
175 unittest_pb2.TestAllTypes.RepeatedGroup.DESCRIPTOR.containing_type,
176 unittest_pb2.TestAllTypes.DESCRIPTOR)
177
178 def testContainingTypeInEnumDescriptor(self):
179 self.assertTrue(unittest_pb2._FOREIGNENUM.containing_type is None)
180 self.assertEqual(unittest_pb2._TESTALLTYPES_NESTEDENUM.containing_type,
181 unittest_pb2.TestAllTypes.DESCRIPTOR)
182
183 def testPackage(self):
184 self.assertEqual(
185 unittest_pb2.TestAllTypes.DESCRIPTOR.file.package,
186 'protobuf_unittest')
187 desc = unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR
188 self.assertEqual(desc.file.package, 'protobuf_unittest')
189 self.assertEqual(
190 unittest_import_pb2.ImportMessage.DESCRIPTOR.file.package,
191 'protobuf_unittest_import')
192
193 self.assertEqual(
194 unittest_pb2._FOREIGNENUM.file.package, 'protobuf_unittest')
195 self.assertEqual(
196 unittest_pb2._TESTALLTYPES_NESTEDENUM.file.package,
197 'protobuf_unittest')
198 self.assertEqual(
199 unittest_import_pb2._IMPORTENUM.file.package,
200 'protobuf_unittest_import')
201
202 def testExtensionRange(self):
203 self.assertEqual(
204 unittest_pb2.TestAllTypes.DESCRIPTOR.extension_ranges, [])
205 self.assertEqual(
206 unittest_pb2.TestAllExtensions.DESCRIPTOR.extension_ranges,
207 [(1, MAX_EXTENSION)])
208 self.assertEqual(
209 unittest_pb2.TestMultipleExtensionRanges.DESCRIPTOR.extension_ranges,
210 [(42, 43), (4143, 4244), (65536, MAX_EXTENSION)])
211
212 def testFileDescriptor(self):
213 self.assertEqual(unittest_pb2.DESCRIPTOR.name,
214 'google/protobuf/unittest.proto')
215 self.assertEqual(unittest_pb2.DESCRIPTOR.package, 'protobuf_unittest')
216 self.assertFalse(unittest_pb2.DESCRIPTOR.serialized_pb is None)
217
218 def testNoGenericServices(self):
kenton@google.comfccb1462009-12-18 02:11:36 +0000219 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "TestMessage"))
220 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "FOO"))
221 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "test_extension"))
liujisi@google.com33165fe2010-11-02 13:14:58 +0000222
223 # Make sure unittest_no_generic_services_pb2 has no services subclassing
224 # Proto2 Service class.
225 if hasattr(unittest_no_generic_services_pb2, "TestService"):
226 self.assertFalse(issubclass(unittest_no_generic_services_pb2.TestService,
227 service.Service))
228
229 def testMessageTypesByName(self):
230 file_type = unittest_pb2.DESCRIPTOR
231 self.assertEqual(
232 unittest_pb2._TESTALLTYPES,
233 file_type.message_types_by_name[unittest_pb2._TESTALLTYPES.name])
234
235 # Nested messages shouldn't be included in the message_types_by_name
236 # dictionary (like in the C++ API).
237 self.assertFalse(
238 unittest_pb2._TESTALLTYPES_NESTEDMESSAGE.name in
239 file_type.message_types_by_name)
kenton@google.comfccb1462009-12-18 02:11:36 +0000240
temporal40ee5512008-07-10 02:12:20 +0000241
242if __name__ == '__main__':
243 unittest.main()