kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | # |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | # Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4 | # Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 5 | # http://code.google.com/p/protobuf/ |
| 6 | # |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 7 | # Redistribution and use in source and binary forms, with or without |
| 8 | # modification, are permitted provided that the following conditions are |
| 9 | # met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 10 | # |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 20 | # |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 32 | |
| 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 38 | compiler. See //google/protobuf/reflection_test.py for a test which |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | further ensures that we can use Python protocol message objects as we expect. |
| 40 | """ |
| 41 | |
| 42 | __author__ = 'robinson@google.com (Will Robinson)' |
| 43 | |
| 44 | import unittest |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 45 | from google.protobuf import unittest_custom_options_pb2 |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 46 | from google.protobuf import unittest_import_pb2 |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 47 | from google.protobuf import unittest_mset_pb2 |
| 48 | from google.protobuf import unittest_pb2 |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 49 | from google.protobuf import unittest_no_generic_services_pb2 |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 50 | from google.protobuf import service |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 51 | |
| 52 | MAX_EXTENSION = 536870912 |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 80 | def testExtremeDefaultValues(self): |
| 81 | message = unittest_pb2.TestExtremeDefaultValues() |
kenton@google.com | d0047c4 | 2009-12-23 02:01:01 +0000 | [diff] [blame] | 82 | |
| 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 103 | |
| 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 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 120 | 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.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 144 | 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 151 | 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 219 | 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.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 222 | |
| 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 240 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 241 | |
| 242 | if __name__ == '__main__': |
| 243 | unittest.main() |