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 18 matching lines...) Expand all Loading... |
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | 33 |
34 """Test for preservation of unknown fields in the pure Python implementation.""" | 34 """Test for preservation of unknown fields in the pure Python implementation.""" |
35 | 35 |
36 __author__ = 'bohdank@google.com (Bohdan Koval)' | 36 __author__ = 'bohdank@google.com (Bohdan Koval)' |
37 | 37 |
38 try: | 38 try: |
39 import unittest2 as unittest | 39 import unittest2 as unittest #PY26 |
40 except ImportError: | 40 except ImportError: |
41 import unittest | 41 import unittest |
42 from google.protobuf import unittest_mset_pb2 | 42 from google.protobuf import unittest_mset_pb2 |
43 from google.protobuf import unittest_pb2 | 43 from google.protobuf import unittest_pb2 |
44 from google.protobuf import unittest_proto3_arena_pb2 | 44 from google.protobuf import unittest_proto3_arena_pb2 |
45 from google.protobuf.internal import api_implementation | 45 from google.protobuf.internal import api_implementation |
46 from google.protobuf.internal import encoder | 46 from google.protobuf.internal import encoder |
47 from google.protobuf.internal import message_set_extensions_pb2 | 47 from google.protobuf.internal import message_set_extensions_pb2 |
48 from google.protobuf.internal import missing_enum_values_pb2 | 48 from google.protobuf.internal import missing_enum_values_pb2 |
49 from google.protobuf.internal import test_util | 49 from google.protobuf.internal import test_util |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 112 |
113 def testEquals(self): | 113 def testEquals(self): |
114 message = unittest_pb2.TestEmptyMessage() | 114 message = unittest_pb2.TestEmptyMessage() |
115 message.ParseFromString(self.all_fields_data) | 115 message.ParseFromString(self.all_fields_data) |
116 self.assertEqual(self.empty_message, message) | 116 self.assertEqual(self.empty_message, message) |
117 | 117 |
118 self.all_fields.ClearField('optional_string') | 118 self.all_fields.ClearField('optional_string') |
119 message.ParseFromString(self.all_fields.SerializeToString()) | 119 message.ParseFromString(self.all_fields.SerializeToString()) |
120 self.assertNotEqual(self.empty_message, message) | 120 self.assertNotEqual(self.empty_message, message) |
121 | 121 |
| 122 def testDiscardUnknownFields(self): |
| 123 self.empty_message.DiscardUnknownFields() |
| 124 self.assertEqual(b'', self.empty_message.SerializeToString()) |
| 125 # Test message field and repeated message field. |
| 126 message = unittest_pb2.TestAllTypes() |
| 127 other_message = unittest_pb2.TestAllTypes() |
| 128 other_message.optional_string = 'discard' |
| 129 message.optional_nested_message.ParseFromString( |
| 130 other_message.SerializeToString()) |
| 131 message.repeated_nested_message.add().ParseFromString( |
| 132 other_message.SerializeToString()) |
| 133 self.assertNotEqual( |
| 134 b'', message.optional_nested_message.SerializeToString()) |
| 135 self.assertNotEqual( |
| 136 b'', message.repeated_nested_message[0].SerializeToString()) |
| 137 message.DiscardUnknownFields() |
| 138 self.assertEqual(b'', message.optional_nested_message.SerializeToString()) |
| 139 self.assertEqual( |
| 140 b'', message.repeated_nested_message[0].SerializeToString()) |
| 141 |
122 | 142 |
123 class UnknownFieldsAccessorsTest(unittest.TestCase): | 143 class UnknownFieldsAccessorsTest(unittest.TestCase): |
124 | 144 |
125 def setUp(self): | 145 def setUp(self): |
126 self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR | 146 self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR |
127 self.all_fields = unittest_pb2.TestAllTypes() | 147 self.all_fields = unittest_pb2.TestAllTypes() |
128 test_util.SetAllFields(self.all_fields) | 148 test_util.SetAllFields(self.all_fields) |
129 self.all_fields_data = self.all_fields.SerializeToString() | 149 self.all_fields_data = self.all_fields.SerializeToString() |
130 self.empty_message = unittest_pb2.TestEmptyMessage() | 150 self.empty_message = unittest_pb2.TestEmptyMessage() |
131 self.empty_message.ParseFromString(self.all_fields_data) | 151 self.empty_message.ParseFromString(self.all_fields_data) |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 self.assertEqual(self.message.packed_nested_enum, value) | 311 self.assertEqual(self.message.packed_nested_enum, value) |
292 | 312 |
293 def testRoundTrip(self): | 313 def testRoundTrip(self): |
294 new_message = missing_enum_values_pb2.TestEnumValues() | 314 new_message = missing_enum_values_pb2.TestEnumValues() |
295 new_message.ParseFromString(self.missing_message.SerializeToString()) | 315 new_message.ParseFromString(self.missing_message.SerializeToString()) |
296 self.assertEqual(self.message, new_message) | 316 self.assertEqual(self.message, new_message) |
297 | 317 |
298 | 318 |
299 if __name__ == '__main__': | 319 if __name__ == '__main__': |
300 unittest.main() | 320 unittest.main() |
OLD | NEW |