PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/python/google/protobuf/internal/descriptor_test.py

http://github.com/tomahawk-player/tomahawk
Python | 334 lines | 297 code | 5 blank | 32 comment | 0 complexity | 5b291f7e0e231ec22a9677942ba6bba2 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. #! /usr/bin/python
  2. #
  3. # Protocol Buffers - Google's data interchange format
  4. # Copyright 2008 Google Inc. All rights reserved.
  5. # http://code.google.com/p/protobuf/
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are
  9. # met:
  10. #
  11. # * 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.
  20. #
  21. # 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.
  32. """Unittest for google.protobuf.internal.descriptor."""
  33. __author__ = 'robinson@google.com (Will Robinson)'
  34. import unittest
  35. from google.protobuf import unittest_import_pb2
  36. from google.protobuf import unittest_pb2
  37. from google.protobuf import descriptor_pb2
  38. from google.protobuf import descriptor
  39. from google.protobuf import text_format
  40. TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII = """
  41. name: 'TestEmptyMessage'
  42. """
  43. class DescriptorTest(unittest.TestCase):
  44. def setUp(self):
  45. self.my_file = descriptor.FileDescriptor(
  46. name='some/filename/some.proto',
  47. package='protobuf_unittest'
  48. )
  49. self.my_enum = descriptor.EnumDescriptor(
  50. name='ForeignEnum',
  51. full_name='protobuf_unittest.ForeignEnum',
  52. filename=None,
  53. file=self.my_file,
  54. values=[
  55. descriptor.EnumValueDescriptor(name='FOREIGN_FOO', index=0, number=4),
  56. descriptor.EnumValueDescriptor(name='FOREIGN_BAR', index=1, number=5),
  57. descriptor.EnumValueDescriptor(name='FOREIGN_BAZ', index=2, number=6),
  58. ])
  59. self.my_message = descriptor.Descriptor(
  60. name='NestedMessage',
  61. full_name='protobuf_unittest.TestAllTypes.NestedMessage',
  62. filename=None,
  63. file=self.my_file,
  64. containing_type=None,
  65. fields=[
  66. descriptor.FieldDescriptor(
  67. name='bb',
  68. full_name='protobuf_unittest.TestAllTypes.NestedMessage.bb',
  69. index=0, number=1,
  70. type=5, cpp_type=1, label=1,
  71. has_default_value=False, default_value=0,
  72. message_type=None, enum_type=None, containing_type=None,
  73. is_extension=False, extension_scope=None),
  74. ],
  75. nested_types=[],
  76. enum_types=[
  77. self.my_enum,
  78. ],
  79. extensions=[])
  80. self.my_method = descriptor.MethodDescriptor(
  81. name='Bar',
  82. full_name='protobuf_unittest.TestService.Bar',
  83. index=0,
  84. containing_service=None,
  85. input_type=None,
  86. output_type=None)
  87. self.my_service = descriptor.ServiceDescriptor(
  88. name='TestServiceWithOptions',
  89. full_name='protobuf_unittest.TestServiceWithOptions',
  90. file=self.my_file,
  91. index=0,
  92. methods=[
  93. self.my_method
  94. ])
  95. def testEnumFixups(self):
  96. self.assertEqual(self.my_enum, self.my_enum.values[0].type)
  97. def testContainingTypeFixups(self):
  98. self.assertEqual(self.my_message, self.my_message.fields[0].containing_type)
  99. self.assertEqual(self.my_message, self.my_enum.containing_type)
  100. def testContainingServiceFixups(self):
  101. self.assertEqual(self.my_service, self.my_method.containing_service)
  102. def testGetOptions(self):
  103. self.assertEqual(self.my_enum.GetOptions(),
  104. descriptor_pb2.EnumOptions())
  105. self.assertEqual(self.my_enum.values[0].GetOptions(),
  106. descriptor_pb2.EnumValueOptions())
  107. self.assertEqual(self.my_message.GetOptions(),
  108. descriptor_pb2.MessageOptions())
  109. self.assertEqual(self.my_message.fields[0].GetOptions(),
  110. descriptor_pb2.FieldOptions())
  111. self.assertEqual(self.my_method.GetOptions(),
  112. descriptor_pb2.MethodOptions())
  113. self.assertEqual(self.my_service.GetOptions(),
  114. descriptor_pb2.ServiceOptions())
  115. def testFileDescriptorReferences(self):
  116. self.assertEqual(self.my_enum.file, self.my_file)
  117. self.assertEqual(self.my_message.file, self.my_file)
  118. def testFileDescriptor(self):
  119. self.assertEqual(self.my_file.name, 'some/filename/some.proto')
  120. self.assertEqual(self.my_file.package, 'protobuf_unittest')
  121. class DescriptorCopyToProtoTest(unittest.TestCase):
  122. """Tests for CopyTo functions of Descriptor."""
  123. def _AssertProtoEqual(self, actual_proto, expected_class, expected_ascii):
  124. expected_proto = expected_class()
  125. text_format.Merge(expected_ascii, expected_proto)
  126. self.assertEqual(
  127. actual_proto, expected_proto,
  128. 'Not equal,\nActual:\n%s\nExpected:\n%s\n'
  129. % (str(actual_proto), str(expected_proto)))
  130. def _InternalTestCopyToProto(self, desc, expected_proto_class,
  131. expected_proto_ascii):
  132. actual = expected_proto_class()
  133. desc.CopyToProto(actual)
  134. self._AssertProtoEqual(
  135. actual, expected_proto_class, expected_proto_ascii)
  136. def testCopyToProto_EmptyMessage(self):
  137. self._InternalTestCopyToProto(
  138. unittest_pb2.TestEmptyMessage.DESCRIPTOR,
  139. descriptor_pb2.DescriptorProto,
  140. TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII)
  141. def testCopyToProto_NestedMessage(self):
  142. TEST_NESTED_MESSAGE_ASCII = """
  143. name: 'NestedMessage'
  144. field: <
  145. name: 'bb'
  146. number: 1
  147. label: 1 # Optional
  148. type: 5 # TYPE_INT32
  149. >
  150. """
  151. self._InternalTestCopyToProto(
  152. unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR,
  153. descriptor_pb2.DescriptorProto,
  154. TEST_NESTED_MESSAGE_ASCII)
  155. def testCopyToProto_ForeignNestedMessage(self):
  156. TEST_FOREIGN_NESTED_ASCII = """
  157. name: 'TestForeignNested'
  158. field: <
  159. name: 'foreign_nested'
  160. number: 1
  161. label: 1 # Optional
  162. type: 11 # TYPE_MESSAGE
  163. type_name: '.protobuf_unittest.TestAllTypes.NestedMessage'
  164. >
  165. """
  166. self._InternalTestCopyToProto(
  167. unittest_pb2.TestForeignNested.DESCRIPTOR,
  168. descriptor_pb2.DescriptorProto,
  169. TEST_FOREIGN_NESTED_ASCII)
  170. def testCopyToProto_ForeignEnum(self):
  171. TEST_FOREIGN_ENUM_ASCII = """
  172. name: 'ForeignEnum'
  173. value: <
  174. name: 'FOREIGN_FOO'
  175. number: 4
  176. >
  177. value: <
  178. name: 'FOREIGN_BAR'
  179. number: 5
  180. >
  181. value: <
  182. name: 'FOREIGN_BAZ'
  183. number: 6
  184. >
  185. """
  186. self._InternalTestCopyToProto(
  187. unittest_pb2._FOREIGNENUM,
  188. descriptor_pb2.EnumDescriptorProto,
  189. TEST_FOREIGN_ENUM_ASCII)
  190. def testCopyToProto_Options(self):
  191. TEST_DEPRECATED_FIELDS_ASCII = """
  192. name: 'TestDeprecatedFields'
  193. field: <
  194. name: 'deprecated_int32'
  195. number: 1
  196. label: 1 # Optional
  197. type: 5 # TYPE_INT32
  198. options: <
  199. deprecated: true
  200. >
  201. >
  202. """
  203. self._InternalTestCopyToProto(
  204. unittest_pb2.TestDeprecatedFields.DESCRIPTOR,
  205. descriptor_pb2.DescriptorProto,
  206. TEST_DEPRECATED_FIELDS_ASCII)
  207. def testCopyToProto_AllExtensions(self):
  208. TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII = """
  209. name: 'TestEmptyMessageWithExtensions'
  210. extension_range: <
  211. start: 1
  212. end: 536870912
  213. >
  214. """
  215. self._InternalTestCopyToProto(
  216. unittest_pb2.TestEmptyMessageWithExtensions.DESCRIPTOR,
  217. descriptor_pb2.DescriptorProto,
  218. TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII)
  219. def testCopyToProto_SeveralExtensions(self):
  220. TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII = """
  221. name: 'TestMultipleExtensionRanges'
  222. extension_range: <
  223. start: 42
  224. end: 43
  225. >
  226. extension_range: <
  227. start: 4143
  228. end: 4244
  229. >
  230. extension_range: <
  231. start: 65536
  232. end: 536870912
  233. >
  234. """
  235. self._InternalTestCopyToProto(
  236. unittest_pb2.TestMultipleExtensionRanges.DESCRIPTOR,
  237. descriptor_pb2.DescriptorProto,
  238. TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII)
  239. def testCopyToProto_FileDescriptor(self):
  240. UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII = ("""
  241. name: 'google/protobuf/unittest_import.proto'
  242. package: 'protobuf_unittest_import'
  243. message_type: <
  244. name: 'ImportMessage'
  245. field: <
  246. name: 'd'
  247. number: 1
  248. label: 1 # Optional
  249. type: 5 # TYPE_INT32
  250. >
  251. >
  252. """ +
  253. """enum_type: <
  254. name: 'ImportEnum'
  255. value: <
  256. name: 'IMPORT_FOO'
  257. number: 7
  258. >
  259. value: <
  260. name: 'IMPORT_BAR'
  261. number: 8
  262. >
  263. value: <
  264. name: 'IMPORT_BAZ'
  265. number: 9
  266. >
  267. >
  268. options: <
  269. java_package: 'com.google.protobuf.test'
  270. optimize_for: 1 # SPEED
  271. >
  272. """)
  273. self._InternalTestCopyToProto(
  274. unittest_import_pb2.DESCRIPTOR,
  275. descriptor_pb2.FileDescriptorProto,
  276. UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII)
  277. def testCopyToProto_ServiceDescriptor(self):
  278. TEST_SERVICE_ASCII = """
  279. name: 'TestService'
  280. method: <
  281. name: 'Foo'
  282. input_type: '.protobuf_unittest.FooRequest'
  283. output_type: '.protobuf_unittest.FooResponse'
  284. >
  285. method: <
  286. name: 'Bar'
  287. input_type: '.protobuf_unittest.BarRequest'
  288. output_type: '.protobuf_unittest.BarResponse'
  289. >
  290. """
  291. self._InternalTestCopyToProto(
  292. unittest_pb2.TestService.DESCRIPTOR,
  293. descriptor_pb2.ServiceDescriptorProto,
  294. TEST_SERVICE_ASCII)
  295. if __name__ == '__main__':
  296. unittest.main()