OLD | NEW |
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 # | 3 # |
4 # Protocol Buffers - Google's data interchange format | 4 # Protocol Buffers - Google's data interchange format |
5 # Copyright 2008 Google Inc. All rights reserved. | 5 # Copyright 2008 Google Inc. All rights reserved. |
6 # https://developers.google.com/protocol-buffers/ | 6 # https://developers.google.com/protocol-buffers/ |
7 # | 7 # |
8 # Redistribution and use in source and binary forms, with or without | 8 # Redistribution and use in source and binary forms, with or without |
9 # modification, are permitted provided that the following conditions are | 9 # modification, are permitted provided that the following conditions are |
10 # met: | 10 # met: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 from google.protobuf import descriptor | 53 from google.protobuf import descriptor |
54 from google.protobuf import message | 54 from google.protobuf import message |
55 from google.protobuf import reflection | 55 from google.protobuf import reflection |
56 from google.protobuf import text_format | 56 from google.protobuf import text_format |
57 from google.protobuf.internal import api_implementation | 57 from google.protobuf.internal import api_implementation |
58 from google.protobuf.internal import more_extensions_pb2 | 58 from google.protobuf.internal import more_extensions_pb2 |
59 from google.protobuf.internal import more_messages_pb2 | 59 from google.protobuf.internal import more_messages_pb2 |
60 from google.protobuf.internal import message_set_extensions_pb2 | 60 from google.protobuf.internal import message_set_extensions_pb2 |
61 from google.protobuf.internal import wire_format | 61 from google.protobuf.internal import wire_format |
62 from google.protobuf.internal import test_util | 62 from google.protobuf.internal import test_util |
63 from google.protobuf.internal import testing_refleaks | |
64 from google.protobuf.internal import decoder | 63 from google.protobuf.internal import decoder |
65 | 64 |
66 | 65 |
67 BaseTestCase = testing_refleaks.BaseTestCase | |
68 | |
69 | |
70 class _MiniDecoder(object): | 66 class _MiniDecoder(object): |
71 """Decodes a stream of values from a string. | 67 """Decodes a stream of values from a string. |
72 | 68 |
73 Once upon a time we actually had a class called decoder.Decoder. Then we | 69 Once upon a time we actually had a class called decoder.Decoder. Then we |
74 got rid of it during a redesign that made decoding much, much faster overall. | 70 got rid of it during a redesign that made decoding much, much faster overall. |
75 But a couple tests in this file used it to check that the serialized form of | 71 But a couple tests in this file used it to check that the serialized form of |
76 a message was correct. So, this class implements just the methods that were | 72 a message was correct. So, this class implements just the methods that were |
77 used by said tests, so that we don't have to rewrite the tests. | 73 used by said tests, so that we don't have to rewrite the tests. |
78 """ | 74 """ |
79 | 75 |
(...skipping 12 matching lines...) Expand all Loading... |
92 | 88 |
93 def ReadSInt64(self): | 89 def ReadSInt64(self): |
94 return wire_format.ZigZagDecode(self.ReadVarint()) | 90 return wire_format.ZigZagDecode(self.ReadVarint()) |
95 | 91 |
96 ReadSInt32 = ReadSInt64 | 92 ReadSInt32 = ReadSInt64 |
97 | 93 |
98 def ReadFieldNumberAndWireType(self): | 94 def ReadFieldNumberAndWireType(self): |
99 return wire_format.UnpackTag(self.ReadVarint()) | 95 return wire_format.UnpackTag(self.ReadVarint()) |
100 | 96 |
101 def ReadFloat(self): | 97 def ReadFloat(self): |
102 result = struct.unpack('<f', self._bytes[self._pos:self._pos+4])[0] | 98 result = struct.unpack("<f", self._bytes[self._pos:self._pos+4])[0] |
103 self._pos += 4 | 99 self._pos += 4 |
104 return result | 100 return result |
105 | 101 |
106 def ReadDouble(self): | 102 def ReadDouble(self): |
107 result = struct.unpack('<d', self._bytes[self._pos:self._pos+8])[0] | 103 result = struct.unpack("<d", self._bytes[self._pos:self._pos+8])[0] |
108 self._pos += 8 | 104 self._pos += 8 |
109 return result | 105 return result |
110 | 106 |
111 def EndOfStream(self): | 107 def EndOfStream(self): |
112 return self._pos == len(self._bytes) | 108 return self._pos == len(self._bytes) |
113 | 109 |
114 | 110 |
115 class ReflectionTest(BaseTestCase): | 111 class ReflectionTest(unittest.TestCase): |
116 | 112 |
117 def assertListsEqual(self, values, others): | 113 def assertListsEqual(self, values, others): |
118 self.assertEqual(len(values), len(others)) | 114 self.assertEqual(len(values), len(others)) |
119 for i in range(len(values)): | 115 for i in range(len(values)): |
120 self.assertEqual(values[i], others[i]) | 116 self.assertEqual(values[i], others[i]) |
121 | 117 |
122 def testScalarConstructor(self): | 118 def testScalarConstructor(self): |
123 # Constructor with only scalar types should succeed. | 119 # Constructor with only scalar types should succeed. |
124 proto = unittest_pb2.TestAllTypes( | 120 proto = unittest_pb2.TestAllTypes( |
125 optional_int32=24, | 121 optional_int32=24, |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 # proto.nonexistent_field = 23 should fail as well. | 610 # proto.nonexistent_field = 23 should fail as well. |
615 self.assertRaises(AttributeError, setattr, proto, 'nonexistent_field', 23) | 611 self.assertRaises(AttributeError, setattr, proto, 'nonexistent_field', 23) |
616 | 612 |
617 def testSingleScalarTypeSafety(self): | 613 def testSingleScalarTypeSafety(self): |
618 proto = unittest_pb2.TestAllTypes() | 614 proto = unittest_pb2.TestAllTypes() |
619 self.assertRaises(TypeError, setattr, proto, 'optional_int32', 1.1) | 615 self.assertRaises(TypeError, setattr, proto, 'optional_int32', 1.1) |
620 self.assertRaises(TypeError, setattr, proto, 'optional_int32', 'foo') | 616 self.assertRaises(TypeError, setattr, proto, 'optional_int32', 'foo') |
621 self.assertRaises(TypeError, setattr, proto, 'optional_string', 10) | 617 self.assertRaises(TypeError, setattr, proto, 'optional_string', 10) |
622 self.assertRaises(TypeError, setattr, proto, 'optional_bytes', 10) | 618 self.assertRaises(TypeError, setattr, proto, 'optional_bytes', 10) |
623 | 619 |
624 def assertIntegerTypes(self, integer_fn): | 620 def testIntegerTypes(self): |
625 """Verifies setting of scalar integers. | |
626 | |
627 Args: | |
628 integer_fn: A function to wrap the integers that will be assigned. | |
629 """ | |
630 def TestGetAndDeserialize(field_name, value, expected_type): | 621 def TestGetAndDeserialize(field_name, value, expected_type): |
631 proto = unittest_pb2.TestAllTypes() | 622 proto = unittest_pb2.TestAllTypes() |
632 value = integer_fn(value) | |
633 setattr(proto, field_name, value) | 623 setattr(proto, field_name, value) |
634 self.assertIsInstance(getattr(proto, field_name), expected_type) | 624 self.assertIsInstance(getattr(proto, field_name), expected_type) |
635 proto2 = unittest_pb2.TestAllTypes() | 625 proto2 = unittest_pb2.TestAllTypes() |
636 proto2.ParseFromString(proto.SerializeToString()) | 626 proto2.ParseFromString(proto.SerializeToString()) |
637 self.assertIsInstance(getattr(proto2, field_name), expected_type) | 627 self.assertIsInstance(getattr(proto2, field_name), expected_type) |
638 | 628 |
639 TestGetAndDeserialize('optional_int32', 1, int) | 629 TestGetAndDeserialize('optional_int32', 1, int) |
640 TestGetAndDeserialize('optional_int32', 1 << 30, int) | 630 TestGetAndDeserialize('optional_int32', 1 << 30, int) |
641 TestGetAndDeserialize('optional_uint32', 1 << 30, int) | 631 TestGetAndDeserialize('optional_uint32', 1 << 30, int) |
642 try: | 632 try: |
643 integer_64 = long | 633 integer_64 = long |
644 except NameError: # Python3 | 634 except NameError: # Python3 |
645 integer_64 = int | 635 integer_64 = int |
646 if struct.calcsize('L') == 4: | 636 if struct.calcsize('L') == 4: |
647 # Python only has signed ints, so 32-bit python can't fit an uint32 | 637 # Python only has signed ints, so 32-bit python can't fit an uint32 |
648 # in an int. | 638 # in an int. |
649 TestGetAndDeserialize('optional_uint32', 1 << 31, integer_64) | 639 TestGetAndDeserialize('optional_uint32', 1 << 31, long) |
650 else: | 640 else: |
651 # 64-bit python can fit uint32 inside an int | 641 # 64-bit python can fit uint32 inside an int |
652 TestGetAndDeserialize('optional_uint32', 1 << 31, int) | 642 TestGetAndDeserialize('optional_uint32', 1 << 31, int) |
653 TestGetAndDeserialize('optional_int64', 1 << 30, integer_64) | 643 TestGetAndDeserialize('optional_int64', 1 << 30, integer_64) |
654 TestGetAndDeserialize('optional_int64', 1 << 60, integer_64) | 644 TestGetAndDeserialize('optional_int64', 1 << 60, integer_64) |
655 TestGetAndDeserialize('optional_uint64', 1 << 30, integer_64) | 645 TestGetAndDeserialize('optional_uint64', 1 << 30, integer_64) |
656 TestGetAndDeserialize('optional_uint64', 1 << 60, integer_64) | 646 TestGetAndDeserialize('optional_uint64', 1 << 60, integer_64) |
657 | 647 |
658 def testIntegerTypes(self): | 648 def testSingleScalarBoundsChecking(self): |
659 self.assertIntegerTypes(lambda x: x) | |
660 | |
661 def testNonStandardIntegerTypes(self): | |
662 self.assertIntegerTypes(test_util.NonStandardInteger) | |
663 | |
664 def testIllegalValuesForIntegers(self): | |
665 pb = unittest_pb2.TestAllTypes() | |
666 | |
667 # Strings are illegal, even when the represent an integer. | |
668 with self.assertRaises(TypeError): | |
669 pb.optional_uint64 = '2' | |
670 | |
671 # The exact error should propagate with a poorly written custom integer. | |
672 with self.assertRaisesRegexp(RuntimeError, 'my_error'): | |
673 pb.optional_uint64 = test_util.NonStandardInteger(5, 'my_error') | |
674 | |
675 def assetIntegerBoundsChecking(self, integer_fn): | |
676 """Verifies bounds checking for scalar integer fields. | |
677 | |
678 Args: | |
679 integer_fn: A function to wrap the integers that will be assigned. | |
680 """ | |
681 def TestMinAndMaxIntegers(field_name, expected_min, expected_max): | 649 def TestMinAndMaxIntegers(field_name, expected_min, expected_max): |
682 pb = unittest_pb2.TestAllTypes() | 650 pb = unittest_pb2.TestAllTypes() |
683 expected_min = integer_fn(expected_min) | |
684 expected_max = integer_fn(expected_max) | |
685 setattr(pb, field_name, expected_min) | 651 setattr(pb, field_name, expected_min) |
686 self.assertEqual(expected_min, getattr(pb, field_name)) | 652 self.assertEqual(expected_min, getattr(pb, field_name)) |
687 setattr(pb, field_name, expected_max) | 653 setattr(pb, field_name, expected_max) |
688 self.assertEqual(expected_max, getattr(pb, field_name)) | 654 self.assertEqual(expected_max, getattr(pb, field_name)) |
689 self.assertRaises(ValueError, setattr, pb, field_name, expected_min - 1) | 655 self.assertRaises(ValueError, setattr, pb, field_name, expected_min - 1) |
690 self.assertRaises(ValueError, setattr, pb, field_name, expected_max + 1) | 656 self.assertRaises(ValueError, setattr, pb, field_name, expected_max + 1) |
691 | 657 |
692 TestMinAndMaxIntegers('optional_int32', -(1 << 31), (1 << 31) - 1) | 658 TestMinAndMaxIntegers('optional_int32', -(1 << 31), (1 << 31) - 1) |
693 TestMinAndMaxIntegers('optional_uint32', 0, 0xffffffff) | 659 TestMinAndMaxIntegers('optional_uint32', 0, 0xffffffff) |
694 TestMinAndMaxIntegers('optional_int64', -(1 << 63), (1 << 63) - 1) | 660 TestMinAndMaxIntegers('optional_int64', -(1 << 63), (1 << 63) - 1) |
695 TestMinAndMaxIntegers('optional_uint64', 0, 0xffffffffffffffff) | 661 TestMinAndMaxIntegers('optional_uint64', 0, 0xffffffffffffffff) |
696 # A bit of white-box testing since -1 is an int and not a long in C++ and | |
697 # so goes down a different path. | |
698 pb = unittest_pb2.TestAllTypes() | |
699 with self.assertRaises(ValueError): | |
700 pb.optional_uint64 = integer_fn(-(1 << 63)) | |
701 | 662 |
702 pb = unittest_pb2.TestAllTypes() | 663 pb = unittest_pb2.TestAllTypes() |
703 pb.optional_nested_enum = integer_fn(1) | 664 pb.optional_nested_enum = 1 |
704 self.assertEqual(1, pb.optional_nested_enum) | 665 self.assertEqual(1, pb.optional_nested_enum) |
705 | 666 |
706 def testSingleScalarBoundsChecking(self): | |
707 self.assetIntegerBoundsChecking(lambda x: x) | |
708 | |
709 def testNonStandardSingleScalarBoundsChecking(self): | |
710 self.assetIntegerBoundsChecking(test_util.NonStandardInteger) | |
711 | |
712 def testRepeatedScalarTypeSafety(self): | 667 def testRepeatedScalarTypeSafety(self): |
713 proto = unittest_pb2.TestAllTypes() | 668 proto = unittest_pb2.TestAllTypes() |
714 self.assertRaises(TypeError, proto.repeated_int32.append, 1.1) | 669 self.assertRaises(TypeError, proto.repeated_int32.append, 1.1) |
715 self.assertRaises(TypeError, proto.repeated_int32.append, 'foo') | 670 self.assertRaises(TypeError, proto.repeated_int32.append, 'foo') |
716 self.assertRaises(TypeError, proto.repeated_string, 10) | 671 self.assertRaises(TypeError, proto.repeated_string, 10) |
717 self.assertRaises(TypeError, proto.repeated_bytes, 10) | 672 self.assertRaises(TypeError, proto.repeated_bytes, 10) |
718 | 673 |
719 proto.repeated_int32.append(10) | 674 proto.repeated_int32.append(10) |
720 proto.repeated_int32[0] = 23 | 675 proto.repeated_int32[0] = 23 |
721 self.assertRaises(IndexError, proto.repeated_int32.__setitem__, 500, 23) | 676 self.assertRaises(IndexError, proto.repeated_int32.__setitem__, 500, 23) |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 | 965 |
1011 # Test clearing. | 966 # Test clearing. |
1012 proto.ClearField('repeated_nested_message') | 967 proto.ClearField('repeated_nested_message') |
1013 self.assertTrue(not proto.repeated_nested_message) | 968 self.assertTrue(not proto.repeated_nested_message) |
1014 self.assertEqual(0, len(proto.repeated_nested_message)) | 969 self.assertEqual(0, len(proto.repeated_nested_message)) |
1015 | 970 |
1016 # Test constructing an element while adding it. | 971 # Test constructing an element while adding it. |
1017 proto.repeated_nested_message.add(bb=23) | 972 proto.repeated_nested_message.add(bb=23) |
1018 self.assertEqual(1, len(proto.repeated_nested_message)) | 973 self.assertEqual(1, len(proto.repeated_nested_message)) |
1019 self.assertEqual(23, proto.repeated_nested_message[0].bb) | 974 self.assertEqual(23, proto.repeated_nested_message[0].bb) |
1020 self.assertRaises(TypeError, proto.repeated_nested_message.add, 23) | |
1021 | 975 |
1022 def testRepeatedCompositeRemove(self): | 976 def testRepeatedCompositeRemove(self): |
1023 proto = unittest_pb2.TestAllTypes() | 977 proto = unittest_pb2.TestAllTypes() |
1024 | 978 |
1025 self.assertEqual(0, len(proto.repeated_nested_message)) | 979 self.assertEqual(0, len(proto.repeated_nested_message)) |
1026 m0 = proto.repeated_nested_message.add() | 980 m0 = proto.repeated_nested_message.add() |
1027 # Need to set some differentiating variable so m0 != m1 != m2: | 981 # Need to set some differentiating variable so m0 != m1 != m2: |
1028 m0.bb = len(proto.repeated_nested_message) | 982 m0.bb = len(proto.repeated_nested_message) |
1029 m1 = proto.repeated_nested_message.add() | 983 m1 = proto.repeated_nested_message.add() |
1030 m1.bb = len(proto.repeated_nested_message) | 984 m1.bb = len(proto.repeated_nested_message) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1221 self.assertEqual(0, required.a) | 1175 self.assertEqual(0, required.a) |
1222 self.assertTrue(not extendee_proto.HasExtension(extension)) | 1176 self.assertTrue(not extendee_proto.HasExtension(extension)) |
1223 required.a = 23 | 1177 required.a = 23 |
1224 self.assertEqual(23, extendee_proto.Extensions[extension].a) | 1178 self.assertEqual(23, extendee_proto.Extensions[extension].a) |
1225 self.assertTrue(extendee_proto.HasExtension(extension)) | 1179 self.assertTrue(extendee_proto.HasExtension(extension)) |
1226 extendee_proto.ClearExtension(extension) | 1180 extendee_proto.ClearExtension(extension) |
1227 self.assertTrue(required is not extendee_proto.Extensions[extension]) | 1181 self.assertTrue(required is not extendee_proto.Extensions[extension]) |
1228 self.assertTrue(not extendee_proto.HasExtension(extension)) | 1182 self.assertTrue(not extendee_proto.HasExtension(extension)) |
1229 | 1183 |
1230 def testRegisteredExtensions(self): | 1184 def testRegisteredExtensions(self): |
1231 pool = unittest_pb2.DESCRIPTOR.pool | 1185 self.assertTrue('protobuf_unittest.optional_int32_extension' in |
1232 self.assertTrue( | 1186 unittest_pb2.TestAllExtensions._extensions_by_name) |
1233 pool.FindExtensionByNumber( | 1187 self.assertTrue(1 in unittest_pb2.TestAllExtensions._extensions_by_number) |
1234 unittest_pb2.TestAllExtensions.DESCRIPTOR, 1)) | |
1235 self.assertIs( | |
1236 pool.FindExtensionByName( | |
1237 'protobuf_unittest.optional_int32_extension').containing_type, | |
1238 unittest_pb2.TestAllExtensions.DESCRIPTOR) | |
1239 # Make sure extensions haven't been registered into types that shouldn't | 1188 # Make sure extensions haven't been registered into types that shouldn't |
1240 # have any. | 1189 # have any. |
1241 self.assertEqual(0, len( | 1190 self.assertEqual(0, len(unittest_pb2.TestAllTypes._extensions_by_name)) |
1242 pool.FindAllExtensions(unittest_pb2.TestAllTypes.DESCRIPTOR))) | |
1243 | 1191 |
1244 # If message A directly contains message B, and | 1192 # If message A directly contains message B, and |
1245 # a.HasField('b') is currently False, then mutating any | 1193 # a.HasField('b') is currently False, then mutating any |
1246 # extension in B should change a.HasField('b') to True | 1194 # extension in B should change a.HasField('b') to True |
1247 # (and so on up the object tree). | 1195 # (and so on up the object tree). |
1248 def testHasBitsForAncestorsOfExtendedMessage(self): | 1196 def testHasBitsForAncestorsOfExtendedMessage(self): |
1249 # Optional scalar extension. | 1197 # Optional scalar extension. |
1250 toplevel = more_extensions_pb2.TopLevelMessage() | 1198 toplevel = more_extensions_pb2.TopLevelMessage() |
1251 self.assertTrue(not toplevel.HasField('submessage')) | 1199 self.assertTrue(not toplevel.HasField('submessage')) |
1252 self.assertEqual(0, toplevel.submessage.Extensions[ | 1200 self.assertEqual(0, toplevel.submessage.Extensions[ |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1596 self.assertTrue(foreign is not proto.optional_foreign_message) | 1544 self.assertTrue(foreign is not proto.optional_foreign_message) |
1597 self.assertEqual(5, nested.bb) | 1545 self.assertEqual(5, nested.bb) |
1598 self.assertEqual(6, foreign.c) | 1546 self.assertEqual(6, foreign.c) |
1599 nested.bb = 15 | 1547 nested.bb = 15 |
1600 foreign.c = 16 | 1548 foreign.c = 16 |
1601 self.assertFalse(proto.HasField('optional_nested_message')) | 1549 self.assertFalse(proto.HasField('optional_nested_message')) |
1602 self.assertEqual(0, proto.optional_nested_message.bb) | 1550 self.assertEqual(0, proto.optional_nested_message.bb) |
1603 self.assertFalse(proto.HasField('optional_foreign_message')) | 1551 self.assertFalse(proto.HasField('optional_foreign_message')) |
1604 self.assertEqual(0, proto.optional_foreign_message.c) | 1552 self.assertEqual(0, proto.optional_foreign_message.c) |
1605 | 1553 |
1606 def testDisconnectingInOneof(self): | |
1607 m = unittest_pb2.TestOneof2() # This message has two messages in a oneof. | |
1608 m.foo_message.qux_int = 5 | |
1609 sub_message = m.foo_message | |
1610 # Accessing another message's field does not clear the first one | |
1611 self.assertEqual(m.foo_lazy_message.qux_int, 0) | |
1612 self.assertEqual(m.foo_message.qux_int, 5) | |
1613 # But mutating another message in the oneof detaches the first one. | |
1614 m.foo_lazy_message.qux_int = 6 | |
1615 self.assertEqual(m.foo_message.qux_int, 0) | |
1616 # The reference we got above was detached and is still valid. | |
1617 self.assertEqual(sub_message.qux_int, 5) | |
1618 sub_message.qux_int = 7 | |
1619 | |
1620 def testOneOf(self): | 1554 def testOneOf(self): |
1621 proto = unittest_pb2.TestAllTypes() | 1555 proto = unittest_pb2.TestAllTypes() |
1622 proto.oneof_uint32 = 10 | 1556 proto.oneof_uint32 = 10 |
1623 proto.oneof_nested_message.bb = 11 | 1557 proto.oneof_nested_message.bb = 11 |
1624 self.assertEqual(11, proto.oneof_nested_message.bb) | 1558 self.assertEqual(11, proto.oneof_nested_message.bb) |
1625 self.assertFalse(proto.HasField('oneof_uint32')) | 1559 self.assertFalse(proto.HasField('oneof_uint32')) |
1626 nested = proto.oneof_nested_message | 1560 nested = proto.oneof_nested_message |
1627 proto.oneof_string = 'abc' | 1561 proto.oneof_string = 'abc' |
1628 self.assertEqual('abc', proto.oneof_string) | 1562 self.assertEqual('abc', proto.oneof_string) |
1629 self.assertEqual(11, nested.bb) | 1563 self.assertEqual(11, nested.bb) |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1868 from google.protobuf.internal import import_test_package | 1802 from google.protobuf.internal import import_test_package |
1869 # pylint: enable=g-import-not-at-top | 1803 # pylint: enable=g-import-not-at-top |
1870 msg = import_test_package.myproto.Outer() | 1804 msg = import_test_package.myproto.Outer() |
1871 # Just check the default value. | 1805 # Just check the default value. |
1872 self.assertEqual(57, msg.inner.value) | 1806 self.assertEqual(57, msg.inner.value) |
1873 | 1807 |
1874 # Since we had so many tests for protocol buffer equality, we broke these out | 1808 # Since we had so many tests for protocol buffer equality, we broke these out |
1875 # into separate TestCase classes. | 1809 # into separate TestCase classes. |
1876 | 1810 |
1877 | 1811 |
1878 class TestAllTypesEqualityTest(BaseTestCase): | 1812 class TestAllTypesEqualityTest(unittest.TestCase): |
1879 | 1813 |
1880 def setUp(self): | 1814 def setUp(self): |
1881 self.first_proto = unittest_pb2.TestAllTypes() | 1815 self.first_proto = unittest_pb2.TestAllTypes() |
1882 self.second_proto = unittest_pb2.TestAllTypes() | 1816 self.second_proto = unittest_pb2.TestAllTypes() |
1883 | 1817 |
1884 def testNotHashable(self): | 1818 def testNotHashable(self): |
1885 self.assertRaises(TypeError, hash, self.first_proto) | 1819 self.assertRaises(TypeError, hash, self.first_proto) |
1886 | 1820 |
1887 def testSelfEquality(self): | 1821 def testSelfEquality(self): |
1888 self.assertEqual(self.first_proto, self.first_proto) | 1822 self.assertEqual(self.first_proto, self.first_proto) |
1889 | 1823 |
1890 def testEmptyProtosEqual(self): | 1824 def testEmptyProtosEqual(self): |
1891 self.assertEqual(self.first_proto, self.second_proto) | 1825 self.assertEqual(self.first_proto, self.second_proto) |
1892 | 1826 |
1893 | 1827 |
1894 class FullProtosEqualityTest(BaseTestCase): | 1828 class FullProtosEqualityTest(unittest.TestCase): |
1895 | 1829 |
1896 """Equality tests using completely-full protos as a starting point.""" | 1830 """Equality tests using completely-full protos as a starting point.""" |
1897 | 1831 |
1898 def setUp(self): | 1832 def setUp(self): |
1899 self.first_proto = unittest_pb2.TestAllTypes() | 1833 self.first_proto = unittest_pb2.TestAllTypes() |
1900 self.second_proto = unittest_pb2.TestAllTypes() | 1834 self.second_proto = unittest_pb2.TestAllTypes() |
1901 test_util.SetAllFields(self.first_proto) | 1835 test_util.SetAllFields(self.first_proto) |
1902 test_util.SetAllFields(self.second_proto) | 1836 test_util.SetAllFields(self.second_proto) |
1903 | 1837 |
1904 def testNotHashable(self): | 1838 def testNotHashable(self): |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1970 # Ensure that we test "has" bits as well as value for | 1904 # Ensure that we test "has" bits as well as value for |
1971 # nonrepeated composite field. | 1905 # nonrepeated composite field. |
1972 self.first_proto.ClearField('optional_nested_message') | 1906 self.first_proto.ClearField('optional_nested_message') |
1973 self.second_proto.optional_nested_message.ClearField('bb') | 1907 self.second_proto.optional_nested_message.ClearField('bb') |
1974 self.assertNotEqual(self.first_proto, self.second_proto) | 1908 self.assertNotEqual(self.first_proto, self.second_proto) |
1975 self.first_proto.optional_nested_message.bb = 0 | 1909 self.first_proto.optional_nested_message.bb = 0 |
1976 self.first_proto.optional_nested_message.ClearField('bb') | 1910 self.first_proto.optional_nested_message.ClearField('bb') |
1977 self.assertEqual(self.first_proto, self.second_proto) | 1911 self.assertEqual(self.first_proto, self.second_proto) |
1978 | 1912 |
1979 | 1913 |
1980 class ExtensionEqualityTest(BaseTestCase): | 1914 class ExtensionEqualityTest(unittest.TestCase): |
1981 | 1915 |
1982 def testExtensionEquality(self): | 1916 def testExtensionEquality(self): |
1983 first_proto = unittest_pb2.TestAllExtensions() | 1917 first_proto = unittest_pb2.TestAllExtensions() |
1984 second_proto = unittest_pb2.TestAllExtensions() | 1918 second_proto = unittest_pb2.TestAllExtensions() |
1985 self.assertEqual(first_proto, second_proto) | 1919 self.assertEqual(first_proto, second_proto) |
1986 test_util.SetAllExtensions(first_proto) | 1920 test_util.SetAllExtensions(first_proto) |
1987 self.assertNotEqual(first_proto, second_proto) | 1921 self.assertNotEqual(first_proto, second_proto) |
1988 test_util.SetAllExtensions(second_proto) | 1922 test_util.SetAllExtensions(second_proto) |
1989 self.assertEqual(first_proto, second_proto) | 1923 self.assertEqual(first_proto, second_proto) |
1990 | 1924 |
(...skipping 12 matching lines...) Expand all Loading... |
2003 | 1937 |
2004 # Ensure that differences in cached values | 1938 # Ensure that differences in cached values |
2005 # don't matter if "has" bits are both false. | 1939 # don't matter if "has" bits are both false. |
2006 first_proto = unittest_pb2.TestAllExtensions() | 1940 first_proto = unittest_pb2.TestAllExtensions() |
2007 second_proto = unittest_pb2.TestAllExtensions() | 1941 second_proto = unittest_pb2.TestAllExtensions() |
2008 self.assertEqual( | 1942 self.assertEqual( |
2009 0, first_proto.Extensions[unittest_pb2.optional_int32_extension]) | 1943 0, first_proto.Extensions[unittest_pb2.optional_int32_extension]) |
2010 self.assertEqual(first_proto, second_proto) | 1944 self.assertEqual(first_proto, second_proto) |
2011 | 1945 |
2012 | 1946 |
2013 class MutualRecursionEqualityTest(BaseTestCase): | 1947 class MutualRecursionEqualityTest(unittest.TestCase): |
2014 | 1948 |
2015 def testEqualityWithMutualRecursion(self): | 1949 def testEqualityWithMutualRecursion(self): |
2016 first_proto = unittest_pb2.TestMutualRecursionA() | 1950 first_proto = unittest_pb2.TestMutualRecursionA() |
2017 second_proto = unittest_pb2.TestMutualRecursionA() | 1951 second_proto = unittest_pb2.TestMutualRecursionA() |
2018 self.assertEqual(first_proto, second_proto) | 1952 self.assertEqual(first_proto, second_proto) |
2019 first_proto.bb.a.bb.optional_int32 = 23 | 1953 first_proto.bb.a.bb.optional_int32 = 23 |
2020 self.assertNotEqual(first_proto, second_proto) | 1954 self.assertNotEqual(first_proto, second_proto) |
2021 second_proto.bb.a.bb.optional_int32 = 23 | 1955 second_proto.bb.a.bb.optional_int32 = 23 |
2022 self.assertEqual(first_proto, second_proto) | 1956 self.assertEqual(first_proto, second_proto) |
2023 | 1957 |
2024 | 1958 |
2025 class ByteSizeTest(BaseTestCase): | 1959 class ByteSizeTest(unittest.TestCase): |
2026 | 1960 |
2027 def setUp(self): | 1961 def setUp(self): |
2028 self.proto = unittest_pb2.TestAllTypes() | 1962 self.proto = unittest_pb2.TestAllTypes() |
2029 self.extended_proto = more_extensions_pb2.ExtendedMessage() | 1963 self.extended_proto = more_extensions_pb2.ExtendedMessage() |
2030 self.packed_proto = unittest_pb2.TestPackedTypes() | 1964 self.packed_proto = unittest_pb2.TestPackedTypes() |
2031 self.packed_extended_proto = unittest_pb2.TestPackedExtensions() | 1965 self.packed_extended_proto = unittest_pb2.TestPackedExtensions() |
2032 | 1966 |
2033 def Size(self): | 1967 def Size(self): |
2034 return self.proto.ByteSize() | 1968 return self.proto.ByteSize() |
2035 | 1969 |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2311 # * Handling of unrecognized tags ("uninterpreted_bytes"). | 2245 # * Handling of unrecognized tags ("uninterpreted_bytes"). |
2312 # * Handling of MessageSets. | 2246 # * Handling of MessageSets. |
2313 # * Consistent ordering of tags in the wire format, | 2247 # * Consistent ordering of tags in the wire format, |
2314 # including ordering between extensions and non-extension | 2248 # including ordering between extensions and non-extension |
2315 # fields. | 2249 # fields. |
2316 # * Consistent serialization of negative numbers, especially | 2250 # * Consistent serialization of negative numbers, especially |
2317 # negative int32s. | 2251 # negative int32s. |
2318 # * Handling of empty submessages (with and without "has" | 2252 # * Handling of empty submessages (with and without "has" |
2319 # bits set). | 2253 # bits set). |
2320 | 2254 |
2321 class SerializationTest(BaseTestCase): | 2255 class SerializationTest(unittest.TestCase): |
2322 | 2256 |
2323 def testSerializeEmtpyMessage(self): | 2257 def testSerializeEmtpyMessage(self): |
2324 first_proto = unittest_pb2.TestAllTypes() | 2258 first_proto = unittest_pb2.TestAllTypes() |
2325 second_proto = unittest_pb2.TestAllTypes() | 2259 second_proto = unittest_pb2.TestAllTypes() |
2326 serialized = first_proto.SerializeToString() | 2260 serialized = first_proto.SerializeToString() |
2327 self.assertEqual(first_proto.ByteSize(), len(serialized)) | 2261 self.assertEqual(first_proto.ByteSize(), len(serialized)) |
2328 self.assertEqual( | 2262 self.assertEqual( |
2329 len(serialized), | 2263 len(serialized), |
2330 second_proto.MergeFromString(serialized)) | 2264 second_proto.MergeFromString(serialized)) |
2331 self.assertEqual(first_proto, second_proto) | 2265 self.assertEqual(first_proto, second_proto) |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2872 self.assertEqual(1, proto.optional_message.c) | 2806 self.assertEqual(1, proto.optional_message.c) |
2873 | 2807 |
2874 def testInitRepeatedKwargs(self): | 2808 def testInitRepeatedKwargs(self): |
2875 proto = unittest_pb2.TestAllTypes(repeated_int32=[1, 2, 3]) | 2809 proto = unittest_pb2.TestAllTypes(repeated_int32=[1, 2, 3]) |
2876 self.assertTrue(proto.IsInitialized()) | 2810 self.assertTrue(proto.IsInitialized()) |
2877 self.assertEqual(1, proto.repeated_int32[0]) | 2811 self.assertEqual(1, proto.repeated_int32[0]) |
2878 self.assertEqual(2, proto.repeated_int32[1]) | 2812 self.assertEqual(2, proto.repeated_int32[1]) |
2879 self.assertEqual(3, proto.repeated_int32[2]) | 2813 self.assertEqual(3, proto.repeated_int32[2]) |
2880 | 2814 |
2881 | 2815 |
2882 class OptionsTest(BaseTestCase): | 2816 class OptionsTest(unittest.TestCase): |
2883 | 2817 |
2884 def testMessageOptions(self): | 2818 def testMessageOptions(self): |
2885 proto = message_set_extensions_pb2.TestMessageSet() | 2819 proto = message_set_extensions_pb2.TestMessageSet() |
2886 self.assertEqual(True, | 2820 self.assertEqual(True, |
2887 proto.DESCRIPTOR.GetOptions().message_set_wire_format) | 2821 proto.DESCRIPTOR.GetOptions().message_set_wire_format) |
2888 proto = unittest_pb2.TestAllTypes() | 2822 proto = unittest_pb2.TestAllTypes() |
2889 self.assertEqual(False, | 2823 self.assertEqual(False, |
2890 proto.DESCRIPTOR.GetOptions().message_set_wire_format) | 2824 proto.DESCRIPTOR.GetOptions().message_set_wire_format) |
2891 | 2825 |
2892 def testPackedOptions(self): | 2826 def testPackedOptions(self): |
2893 proto = unittest_pb2.TestAllTypes() | 2827 proto = unittest_pb2.TestAllTypes() |
2894 proto.optional_int32 = 1 | 2828 proto.optional_int32 = 1 |
2895 proto.optional_double = 3.0 | 2829 proto.optional_double = 3.0 |
2896 for field_descriptor, _ in proto.ListFields(): | 2830 for field_descriptor, _ in proto.ListFields(): |
2897 self.assertEqual(False, field_descriptor.GetOptions().packed) | 2831 self.assertEqual(False, field_descriptor.GetOptions().packed) |
2898 | 2832 |
2899 proto = unittest_pb2.TestPackedTypes() | 2833 proto = unittest_pb2.TestPackedTypes() |
2900 proto.packed_int32.append(1) | 2834 proto.packed_int32.append(1) |
2901 proto.packed_double.append(3.0) | 2835 proto.packed_double.append(3.0) |
2902 for field_descriptor, _ in proto.ListFields(): | 2836 for field_descriptor, _ in proto.ListFields(): |
2903 self.assertEqual(True, field_descriptor.GetOptions().packed) | 2837 self.assertEqual(True, field_descriptor.GetOptions().packed) |
2904 self.assertEqual(descriptor.FieldDescriptor.LABEL_REPEATED, | 2838 self.assertEqual(descriptor.FieldDescriptor.LABEL_REPEATED, |
2905 field_descriptor.label) | 2839 field_descriptor.label) |
2906 | 2840 |
2907 | 2841 |
2908 | 2842 |
2909 class ClassAPITest(BaseTestCase): | 2843 class ClassAPITest(unittest.TestCase): |
2910 | 2844 |
2911 @unittest.skipIf( | 2845 @unittest.skipIf( |
2912 api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, | 2846 api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, |
2913 'C++ implementation requires a call to MakeDescriptor()') | 2847 'C++ implementation requires a call to MakeDescriptor()') |
2914 def testMakeClassWithNestedDescriptor(self): | 2848 def testMakeClassWithNestedDescriptor(self): |
2915 leaf_desc = descriptor.Descriptor('leaf', 'package.parent.child.leaf', '', | 2849 leaf_desc = descriptor.Descriptor('leaf', 'package.parent.child.leaf', '', |
2916 containing_type=None, fields=[], | 2850 containing_type=None, fields=[], |
2917 nested_types=[], enum_types=[], | 2851 nested_types=[], enum_types=[], |
2918 extensions=[]) | 2852 extensions=[]) |
2919 child_desc = descriptor.Descriptor('child', 'package.parent.child', '', | 2853 child_desc = descriptor.Descriptor('child', 'package.parent.child', '', |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2982 ' label: LABEL_OPTIONAL' | 2916 ' label: LABEL_OPTIONAL' |
2983 ' type: TYPE_UINT32' | 2917 ' type: TYPE_UINT32' |
2984 ' }' | 2918 ' }' |
2985 ' }' | 2919 ' }' |
2986 ' }' | 2920 ' }' |
2987 '}') | 2921 '}') |
2988 file_descriptor = descriptor_pb2.FileDescriptorProto() | 2922 file_descriptor = descriptor_pb2.FileDescriptorProto() |
2989 text_format.Merge(file_descriptor_str, file_descriptor) | 2923 text_format.Merge(file_descriptor_str, file_descriptor) |
2990 return file_descriptor.SerializeToString() | 2924 return file_descriptor.SerializeToString() |
2991 | 2925 |
2992 @testing_refleaks.SkipReferenceLeakChecker('MakeDescriptor is not repeatable') | |
2993 # This test can only run once; the second time, it raises errors about | |
2994 # conflicting message descriptors. | |
2995 def testParsingFlatClassWithExplicitClassDeclaration(self): | 2926 def testParsingFlatClassWithExplicitClassDeclaration(self): |
2996 """Test that the generated class can parse a flat message.""" | 2927 """Test that the generated class can parse a flat message.""" |
2997 # TODO(xiaofeng): This test fails with cpp implemetnation in the call | 2928 # TODO(xiaofeng): This test fails with cpp implemetnation in the call |
2998 # of six.with_metaclass(). The other two callsites of with_metaclass | 2929 # of six.with_metaclass(). The other two callsites of with_metaclass |
2999 # in this file are both excluded from cpp test, so it might be expected | 2930 # in this file are both excluded from cpp test, so it might be expected |
3000 # to fail. Need someone more familiar with the python code to take a | 2931 # to fail. Need someone more familiar with the python code to take a |
3001 # look at this. | 2932 # look at this. |
3002 if api_implementation.Type() != 'python': | 2933 if api_implementation.Type() != 'python': |
3003 return | 2934 return |
3004 file_descriptor = descriptor_pb2.FileDescriptorProto() | 2935 file_descriptor = descriptor_pb2.FileDescriptorProto() |
3005 file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('A')) | 2936 file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('A')) |
3006 msg_descriptor = descriptor.MakeDescriptor( | 2937 msg_descriptor = descriptor.MakeDescriptor( |
3007 file_descriptor.message_type[0]) | 2938 file_descriptor.message_type[0]) |
3008 | 2939 |
3009 class MessageClass(six.with_metaclass(reflection.GeneratedProtocolMessageTyp
e, message.Message)): | 2940 class MessageClass(six.with_metaclass(reflection.GeneratedProtocolMessageTyp
e, message.Message)): |
3010 DESCRIPTOR = msg_descriptor | 2941 DESCRIPTOR = msg_descriptor |
3011 msg = MessageClass() | 2942 msg = MessageClass() |
3012 msg_str = ( | 2943 msg_str = ( |
3013 'flat: 0 ' | 2944 'flat: 0 ' |
3014 'flat: 1 ' | 2945 'flat: 1 ' |
3015 'flat: 2 ') | 2946 'flat: 2 ') |
3016 text_format.Merge(msg_str, msg) | 2947 text_format.Merge(msg_str, msg) |
3017 self.assertEqual(msg.flat, [0, 1, 2]) | 2948 self.assertEqual(msg.flat, [0, 1, 2]) |
3018 | 2949 |
3019 @testing_refleaks.SkipReferenceLeakChecker('MakeDescriptor is not repeatable') | |
3020 def testParsingFlatClass(self): | 2950 def testParsingFlatClass(self): |
3021 """Test that the generated class can parse a flat message.""" | 2951 """Test that the generated class can parse a flat message.""" |
3022 file_descriptor = descriptor_pb2.FileDescriptorProto() | 2952 file_descriptor = descriptor_pb2.FileDescriptorProto() |
3023 file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('B')) | 2953 file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('B')) |
3024 msg_descriptor = descriptor.MakeDescriptor( | 2954 msg_descriptor = descriptor.MakeDescriptor( |
3025 file_descriptor.message_type[0]) | 2955 file_descriptor.message_type[0]) |
3026 msg_class = reflection.MakeClass(msg_descriptor) | 2956 msg_class = reflection.MakeClass(msg_descriptor) |
3027 msg = msg_class() | 2957 msg = msg_class() |
3028 msg_str = ( | 2958 msg_str = ( |
3029 'flat: 0 ' | 2959 'flat: 0 ' |
3030 'flat: 1 ' | 2960 'flat: 1 ' |
3031 'flat: 2 ') | 2961 'flat: 2 ') |
3032 text_format.Merge(msg_str, msg) | 2962 text_format.Merge(msg_str, msg) |
3033 self.assertEqual(msg.flat, [0, 1, 2]) | 2963 self.assertEqual(msg.flat, [0, 1, 2]) |
3034 | 2964 |
3035 @testing_refleaks.SkipReferenceLeakChecker('MakeDescriptor is not repeatable') | |
3036 def testParsingNestedClass(self): | 2965 def testParsingNestedClass(self): |
3037 """Test that the generated class can parse a nested message.""" | 2966 """Test that the generated class can parse a nested message.""" |
3038 file_descriptor = descriptor_pb2.FileDescriptorProto() | 2967 file_descriptor = descriptor_pb2.FileDescriptorProto() |
3039 file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('C')) | 2968 file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('C')) |
3040 msg_descriptor = descriptor.MakeDescriptor( | 2969 msg_descriptor = descriptor.MakeDescriptor( |
3041 file_descriptor.message_type[0]) | 2970 file_descriptor.message_type[0]) |
3042 msg_class = reflection.MakeClass(msg_descriptor) | 2971 msg_class = reflection.MakeClass(msg_descriptor) |
3043 msg = msg_class() | 2972 msg = msg_class() |
3044 msg_str = ( | 2973 msg_str = ( |
3045 'bar {' | 2974 'bar {' |
3046 ' baz {' | 2975 ' baz {' |
3047 ' deep: 4' | 2976 ' deep: 4' |
3048 ' }' | 2977 ' }' |
3049 '}') | 2978 '}') |
3050 text_format.Merge(msg_str, msg) | 2979 text_format.Merge(msg_str, msg) |
3051 self.assertEqual(msg.bar.baz.deep, 4) | 2980 self.assertEqual(msg.bar.baz.deep, 4) |
3052 | 2981 |
3053 if __name__ == '__main__': | 2982 if __name__ == '__main__': |
3054 unittest.main() | 2983 unittest.main() |
OLD | NEW |