OLD | NEW |
1 #! /usr/bin/python | 1 #! /usr/bin/env python |
2 # | 2 # |
3 # Protocol Buffers - Google's data interchange format | 3 # Protocol Buffers - Google's data interchange format |
4 # Copyright 2008 Google Inc. All rights reserved. | 4 # Copyright 2008 Google Inc. All rights reserved. |
5 # http://code.google.com/p/protobuf/ | 5 # https://developers.google.com/protocol-buffers/ |
6 # | 6 # |
7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
9 # met: | 9 # met: |
10 # | 10 # |
11 # * Redistributions of source code must retain the above copyright | 11 # * Redistributions of source code must retain the above copyright |
12 # notice, this list of conditions and the following disclaimer. | 12 # notice, this list of conditions and the following disclaimer. |
13 # * Redistributions in binary form must reproduce the above | 13 # * Redistributions in binary form must reproduce the above |
14 # copyright notice, this list of conditions and the following disclaimer | 14 # copyright notice, this list of conditions and the following disclaimer |
15 # in the documentation and/or other materials provided with the | 15 # in the documentation and/or other materials provided with the |
(...skipping 12 matching lines...) Expand all Loading... |
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | 32 |
33 # TODO(robinson): Flesh this out considerably. We focused on reflection_test.py | 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 | 34 # first, since it's testing the subtler code, and since it provides decent |
35 # indirect testing of the protocol compiler output. | 35 # indirect testing of the protocol compiler output. |
36 | 36 |
37 """Unittest that directly tests the output of the pure-Python protocol | 37 """Unittest that directly tests the output of the pure-Python protocol |
38 compiler. See //google/protobuf/reflection_test.py for a test which | 38 compiler. See //google/protobuf/internal/reflection_test.py for a test which |
39 further ensures that we can use Python protocol message objects as we expect. | 39 further ensures that we can use Python protocol message objects as we expect. |
40 """ | 40 """ |
41 | 41 |
42 __author__ = 'robinson@google.com (Will Robinson)' | 42 __author__ = 'robinson@google.com (Will Robinson)' |
43 | 43 |
44 import unittest | 44 try: |
| 45 import unittest2 as unittest |
| 46 except ImportError: |
| 47 import unittest |
45 from google.protobuf.internal import test_bad_identifiers_pb2 | 48 from google.protobuf.internal import test_bad_identifiers_pb2 |
46 from google.protobuf import unittest_custom_options_pb2 | 49 from google.protobuf import unittest_custom_options_pb2 |
47 from google.protobuf import unittest_import_pb2 | 50 from google.protobuf import unittest_import_pb2 |
48 from google.protobuf import unittest_import_public_pb2 | 51 from google.protobuf import unittest_import_public_pb2 |
49 from google.protobuf import unittest_mset_pb2 | 52 from google.protobuf import unittest_mset_pb2 |
| 53 from google.protobuf import unittest_mset_wire_format_pb2 |
| 54 from google.protobuf import unittest_no_generic_services_pb2 |
50 from google.protobuf import unittest_pb2 | 55 from google.protobuf import unittest_pb2 |
51 from google.protobuf import unittest_no_generic_services_pb2 | |
52 from google.protobuf import service | 56 from google.protobuf import service |
| 57 from google.protobuf import symbol_database |
53 | 58 |
54 MAX_EXTENSION = 536870912 | 59 MAX_EXTENSION = 536870912 |
55 | 60 |
56 | 61 |
57 class GeneratorTest(unittest.TestCase): | 62 class GeneratorTest(unittest.TestCase): |
58 | 63 |
59 def testNestedMessageDescriptor(self): | 64 def testNestedMessageDescriptor(self): |
60 field_name = 'optional_nested_message' | 65 field_name = 'optional_nested_message' |
61 proto_type = unittest_pb2.TestAllTypes | 66 proto_type = unittest_pb2.TestAllTypes |
62 self.assertEqual( | 67 self.assertEqual( |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 139 |
135 def testIsExtension(self): | 140 def testIsExtension(self): |
136 self.assertTrue(unittest_pb2.optional_int32_extension.is_extension) | 141 self.assertTrue(unittest_pb2.optional_int32_extension.is_extension) |
137 self.assertTrue(unittest_pb2.TestRequired.single.is_extension) | 142 self.assertTrue(unittest_pb2.TestRequired.single.is_extension) |
138 | 143 |
139 message_descriptor = unittest_pb2.TestRequired.DESCRIPTOR | 144 message_descriptor = unittest_pb2.TestRequired.DESCRIPTOR |
140 non_extension_descriptor = message_descriptor.fields_by_name['a'] | 145 non_extension_descriptor = message_descriptor.fields_by_name['a'] |
141 self.assertTrue(not non_extension_descriptor.is_extension) | 146 self.assertTrue(not non_extension_descriptor.is_extension) |
142 | 147 |
143 def testOptions(self): | 148 def testOptions(self): |
144 proto = unittest_mset_pb2.TestMessageSet() | 149 proto = unittest_mset_wire_format_pb2.TestMessageSet() |
145 self.assertTrue(proto.DESCRIPTOR.GetOptions().message_set_wire_format) | 150 self.assertTrue(proto.DESCRIPTOR.GetOptions().message_set_wire_format) |
146 | 151 |
147 def testMessageWithCustomOptions(self): | 152 def testMessageWithCustomOptions(self): |
148 proto = unittest_custom_options_pb2.TestMessageWithCustomOptions() | 153 proto = unittest_custom_options_pb2.TestMessageWithCustomOptions() |
149 enum_options = proto.DESCRIPTOR.enum_types_by_name['AnEnum'].GetOptions() | 154 enum_options = proto.DESCRIPTOR.enum_types_by_name['AnEnum'].GetOptions() |
150 self.assertTrue(enum_options is not None) | 155 self.assertTrue(enum_options is not None) |
151 # TODO(gps): We really should test for the presense of the enum_opt1 | 156 # TODO(gps): We really should test for the presence of the enum_opt1 |
152 # extension and for its value to be set to -789. | 157 # extension and for its value to be set to -789. |
153 | 158 |
154 def testNestedTypes(self): | 159 def testNestedTypes(self): |
155 self.assertEquals( | 160 self.assertEqual( |
156 set(unittest_pb2.TestAllTypes.DESCRIPTOR.nested_types), | 161 set(unittest_pb2.TestAllTypes.DESCRIPTOR.nested_types), |
157 set([ | 162 set([ |
158 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR, | 163 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR, |
159 unittest_pb2.TestAllTypes.OptionalGroup.DESCRIPTOR, | 164 unittest_pb2.TestAllTypes.OptionalGroup.DESCRIPTOR, |
160 unittest_pb2.TestAllTypes.RepeatedGroup.DESCRIPTOR, | 165 unittest_pb2.TestAllTypes.RepeatedGroup.DESCRIPTOR, |
161 ])) | 166 ])) |
162 self.assertEqual(unittest_pb2.TestEmptyMessage.DESCRIPTOR.nested_types, []) | 167 self.assertEqual(unittest_pb2.TestEmptyMessage.DESCRIPTOR.nested_types, []) |
163 self.assertEqual( | 168 self.assertEqual( |
164 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR.nested_types, []) | 169 unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR.nested_types, []) |
165 | 170 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 [(1, MAX_EXTENSION)]) | 215 [(1, MAX_EXTENSION)]) |
211 self.assertEqual( | 216 self.assertEqual( |
212 unittest_pb2.TestMultipleExtensionRanges.DESCRIPTOR.extension_ranges, | 217 unittest_pb2.TestMultipleExtensionRanges.DESCRIPTOR.extension_ranges, |
213 [(42, 43), (4143, 4244), (65536, MAX_EXTENSION)]) | 218 [(42, 43), (4143, 4244), (65536, MAX_EXTENSION)]) |
214 | 219 |
215 def testFileDescriptor(self): | 220 def testFileDescriptor(self): |
216 self.assertEqual(unittest_pb2.DESCRIPTOR.name, | 221 self.assertEqual(unittest_pb2.DESCRIPTOR.name, |
217 'google/protobuf/unittest.proto') | 222 'google/protobuf/unittest.proto') |
218 self.assertEqual(unittest_pb2.DESCRIPTOR.package, 'protobuf_unittest') | 223 self.assertEqual(unittest_pb2.DESCRIPTOR.package, 'protobuf_unittest') |
219 self.assertFalse(unittest_pb2.DESCRIPTOR.serialized_pb is None) | 224 self.assertFalse(unittest_pb2.DESCRIPTOR.serialized_pb is None) |
| 225 self.assertEqual(unittest_pb2.DESCRIPTOR.dependencies, |
| 226 [unittest_import_pb2.DESCRIPTOR]) |
| 227 self.assertEqual(unittest_import_pb2.DESCRIPTOR.dependencies, |
| 228 [unittest_import_public_pb2.DESCRIPTOR]) |
220 | 229 |
221 def testNoGenericServices(self): | 230 def testNoGenericServices(self): |
222 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "TestMessage")) | 231 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "TestMessage")) |
223 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "FOO")) | 232 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "FOO")) |
224 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "test_extension")) | 233 self.assertTrue(hasattr(unittest_no_generic_services_pb2, "test_extension")) |
225 | 234 |
226 # Make sure unittest_no_generic_services_pb2 has no services subclassing | 235 # Make sure unittest_no_generic_services_pb2 has no services subclassing |
227 # Proto2 Service class. | 236 # Proto2 Service class. |
228 if hasattr(unittest_no_generic_services_pb2, "TestService"): | 237 if hasattr(unittest_no_generic_services_pb2, "TestService"): |
229 self.assertFalse(issubclass(unittest_no_generic_services_pb2.TestService, | 238 self.assertFalse(issubclass(unittest_no_generic_services_pb2.TestService, |
230 service.Service)) | 239 service.Service)) |
231 | 240 |
232 def testMessageTypesByName(self): | 241 def testMessageTypesByName(self): |
233 file_type = unittest_pb2.DESCRIPTOR | 242 file_type = unittest_pb2.DESCRIPTOR |
234 self.assertEqual( | 243 self.assertEqual( |
235 unittest_pb2._TESTALLTYPES, | 244 unittest_pb2._TESTALLTYPES, |
236 file_type.message_types_by_name[unittest_pb2._TESTALLTYPES.name]) | 245 file_type.message_types_by_name[unittest_pb2._TESTALLTYPES.name]) |
237 | 246 |
238 # Nested messages shouldn't be included in the message_types_by_name | 247 # Nested messages shouldn't be included in the message_types_by_name |
239 # dictionary (like in the C++ API). | 248 # dictionary (like in the C++ API). |
240 self.assertFalse( | 249 self.assertFalse( |
241 unittest_pb2._TESTALLTYPES_NESTEDMESSAGE.name in | 250 unittest_pb2._TESTALLTYPES_NESTEDMESSAGE.name in |
242 file_type.message_types_by_name) | 251 file_type.message_types_by_name) |
243 | 252 |
| 253 def testEnumTypesByName(self): |
| 254 file_type = unittest_pb2.DESCRIPTOR |
| 255 self.assertEqual( |
| 256 unittest_pb2._FOREIGNENUM, |
| 257 file_type.enum_types_by_name[unittest_pb2._FOREIGNENUM.name]) |
| 258 |
| 259 def testExtensionsByName(self): |
| 260 file_type = unittest_pb2.DESCRIPTOR |
| 261 self.assertEqual( |
| 262 unittest_pb2.my_extension_string, |
| 263 file_type.extensions_by_name[unittest_pb2.my_extension_string.name]) |
| 264 |
244 def testPublicImports(self): | 265 def testPublicImports(self): |
245 # Test public imports as embedded message. | 266 # Test public imports as embedded message. |
246 all_type_proto = unittest_pb2.TestAllTypes() | 267 all_type_proto = unittest_pb2.TestAllTypes() |
247 self.assertEqual(0, all_type_proto.optional_public_import_message.e) | 268 self.assertEqual(0, all_type_proto.optional_public_import_message.e) |
248 | 269 |
249 # PublicImportMessage is actually defined in unittest_import_public_pb2 | 270 # PublicImportMessage is actually defined in unittest_import_public_pb2 |
250 # module, and is public imported by unittest_import_pb2 module. | 271 # module, and is public imported by unittest_import_pb2 module. |
251 public_import_proto = unittest_import_pb2.PublicImportMessage() | 272 public_import_proto = unittest_import_pb2.PublicImportMessage() |
252 self.assertEqual(0, public_import_proto.e) | 273 self.assertEqual(0, public_import_proto.e) |
253 self.assertTrue(unittest_import_public_pb2.PublicImportMessage is | 274 self.assertTrue(unittest_import_public_pb2.PublicImportMessage is |
254 unittest_import_pb2.PublicImportMessage) | 275 unittest_import_pb2.PublicImportMessage) |
255 | 276 |
256 def testBadIdentifiers(self): | 277 def testBadIdentifiers(self): |
257 # We're just testing that the code was imported without problems. | 278 # We're just testing that the code was imported without problems. |
258 message = test_bad_identifiers_pb2.TestBadIdentifiers() | 279 message = test_bad_identifiers_pb2.TestBadIdentifiers() |
259 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.message], | 280 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.message], |
260 "foo") | 281 "foo") |
261 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.descriptor], | 282 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.descriptor], |
262 "bar") | 283 "bar") |
263 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.reflection], | 284 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.reflection], |
264 "baz") | 285 "baz") |
265 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.service], | 286 self.assertEqual(message.Extensions[test_bad_identifiers_pb2.service], |
266 "qux") | 287 "qux") |
267 | 288 |
| 289 def testOneof(self): |
| 290 desc = unittest_pb2.TestAllTypes.DESCRIPTOR |
| 291 self.assertEqual(1, len(desc.oneofs)) |
| 292 self.assertEqual('oneof_field', desc.oneofs[0].name) |
| 293 self.assertEqual(0, desc.oneofs[0].index) |
| 294 self.assertIs(desc, desc.oneofs[0].containing_type) |
| 295 self.assertIs(desc.oneofs[0], desc.oneofs_by_name['oneof_field']) |
| 296 nested_names = set(['oneof_uint32', 'oneof_nested_message', |
| 297 'oneof_string', 'oneof_bytes']) |
| 298 self.assertEqual( |
| 299 nested_names, |
| 300 set([field.name for field in desc.oneofs[0].fields])) |
| 301 for field_name, field_desc in desc.fields_by_name.items(): |
| 302 if field_name in nested_names: |
| 303 self.assertIs(desc.oneofs[0], field_desc.containing_oneof) |
| 304 else: |
| 305 self.assertIsNone(field_desc.containing_oneof) |
| 306 |
| 307 |
| 308 class SymbolDatabaseRegistrationTest(unittest.TestCase): |
| 309 """Checks that messages, enums and files are correctly registered.""" |
| 310 |
| 311 def testGetSymbol(self): |
| 312 self.assertEqual( |
| 313 unittest_pb2.TestAllTypes, symbol_database.Default().GetSymbol( |
| 314 'protobuf_unittest.TestAllTypes')) |
| 315 self.assertEqual( |
| 316 unittest_pb2.TestAllTypes.NestedMessage, |
| 317 symbol_database.Default().GetSymbol( |
| 318 'protobuf_unittest.TestAllTypes.NestedMessage')) |
| 319 with self.assertRaises(KeyError): |
| 320 symbol_database.Default().GetSymbol('protobuf_unittest.NestedMessage') |
| 321 self.assertEqual( |
| 322 unittest_pb2.TestAllTypes.OptionalGroup, |
| 323 symbol_database.Default().GetSymbol( |
| 324 'protobuf_unittest.TestAllTypes.OptionalGroup')) |
| 325 self.assertEqual( |
| 326 unittest_pb2.TestAllTypes.RepeatedGroup, |
| 327 symbol_database.Default().GetSymbol( |
| 328 'protobuf_unittest.TestAllTypes.RepeatedGroup')) |
| 329 |
| 330 def testEnums(self): |
| 331 self.assertEqual( |
| 332 'protobuf_unittest.ForeignEnum', |
| 333 symbol_database.Default().pool.FindEnumTypeByName( |
| 334 'protobuf_unittest.ForeignEnum').full_name) |
| 335 self.assertEqual( |
| 336 'protobuf_unittest.TestAllTypes.NestedEnum', |
| 337 symbol_database.Default().pool.FindEnumTypeByName( |
| 338 'protobuf_unittest.TestAllTypes.NestedEnum').full_name) |
| 339 |
| 340 def testFindFileByName(self): |
| 341 self.assertEqual( |
| 342 'google/protobuf/unittest.proto', |
| 343 symbol_database.Default().pool.FindFileByName( |
| 344 'google/protobuf/unittest.proto').name) |
| 345 |
268 if __name__ == '__main__': | 346 if __name__ == '__main__': |
269 unittest.main() | 347 unittest.main() |
OLD | NEW |