Joshua Haberman | da64753 | 2021-08-18 15:30:03 -0700 | [diff] [blame] | 1 | # Copyright (c) 2009-2021, Google LLC |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are met: |
| 6 | # * Redistributions of source code must retain the above copyright |
| 7 | # notice, this list of conditions and the following disclaimer. |
| 8 | # * Redistributions in binary form must reproduce the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer in the |
| 10 | # documentation and/or other materials provided with the distribution. |
| 11 | # * Neither the name of Google LLC nor the |
| 12 | # names of its contributors may be used to endorse or promote products |
| 13 | # derived from this software without specific prior written permission. |
| 14 | # |
| 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY |
| 19 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | |
Joshua Haberman | cdc9fcd | 2021-08-23 16:57:40 -0700 | [diff] [blame] | 27 | """A bare-bones unit test that doesn't load any generated code.""" |
Joshua Haberman | a086aaf | 2021-08-18 15:10:06 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | import unittest |
| 31 | from google.protobuf.pyext import _message |
Joshua Haberman | e079f1b | 2021-12-18 22:10:46 -0800 | [diff] [blame] | 32 | from google.protobuf.internal import api_implementation |
Joshua Haberman | 5f0978a | 2021-12-28 16:52:43 -0800 | [diff] [blame^] | 33 | from google.protobuf import unittest_pb2 |
Joshua Haberman | a086aaf | 2021-08-18 15:10:06 -0700 | [diff] [blame] | 34 | |
| 35 | class TestMessageExtension(unittest.TestCase): |
| 36 | |
| 37 | def test_descriptor_pool(self): |
| 38 | serialized_desc = b'\n\ntest.proto\"\x0e\n\x02M1*\x08\x08\x01\x10\x80\x80\x80\x80\x02:\x15\n\x08test_ext\x12\x03.M1\x18\x01 \x01(\x05' |
| 39 | pool = _message.DescriptorPool() |
| 40 | file_desc = pool.AddSerializedFile(serialized_desc) |
| 41 | self.assertEqual("test.proto", file_desc.name) |
| 42 | ext_desc = pool.FindExtensionByName("test_ext") |
| 43 | self.assertEqual(1, ext_desc.number) |
| 44 | |
| 45 | # Test object cache: repeatedly retrieving the same descriptor |
| 46 | # should result in the same object |
| 47 | self.assertIs(ext_desc, pool.FindExtensionByName("test_ext")) |
| 48 | |
| 49 | |
| 50 | def test_lib_is_upb(self): |
| 51 | # Ensure we are not pulling in a different protobuf library on the |
| 52 | # system. |
Joshua Haberman | e079f1b | 2021-12-18 22:10:46 -0800 | [diff] [blame] | 53 | print(_message._IS_UPB) |
Joshua Haberman | a086aaf | 2021-08-18 15:10:06 -0700 | [diff] [blame] | 54 | self.assertTrue(_message._IS_UPB) |
Joshua Haberman | e079f1b | 2021-12-18 22:10:46 -0800 | [diff] [blame] | 55 | self.assertEqual(api_implementation.Type(), "cpp") |
Joshua Haberman | a086aaf | 2021-08-18 15:10:06 -0700 | [diff] [blame] | 56 | |
Joshua Haberman | 5f0978a | 2021-12-28 16:52:43 -0800 | [diff] [blame^] | 57 | def test_repeated_field_slice_delete(self): |
| 58 | def test_slice(start, end, step): |
| 59 | vals = list(range(20)) |
| 60 | message = unittest_pb2.TestAllTypes(repeated_int32=vals) |
| 61 | del vals[start:end:step] |
| 62 | del message.repeated_int32[start:end:step] |
| 63 | self.assertEqual(vals, list(message.repeated_int32)) |
| 64 | test_slice(3, 11, 1) |
| 65 | test_slice(3, 11, 2) |
| 66 | test_slice(3, 11, 3) |
| 67 | test_slice(11, 3, -1) |
| 68 | test_slice(11, 3, -2) |
| 69 | test_slice(11, 3, -3) |
| 70 | test_slice(10, 25, 4) |
| 71 | |
Joshua Haberman | e079f1b | 2021-12-18 22:10:46 -0800 | [diff] [blame] | 72 | #TestMessageExtension.test_descriptor_pool.__unittest_expecting_failure__ = True |
Joshua Haberman | b8736ae | 2021-12-13 12:51:10 -0800 | [diff] [blame] | 73 | |
Joshua Haberman | a086aaf | 2021-08-18 15:10:06 -0700 | [diff] [blame] | 74 | |
| 75 | if __name__ == '__main__': |
Joshua Haberman | b8736ae | 2021-12-13 12:51:10 -0800 | [diff] [blame] | 76 | unittest.main(verbosity=2) |