blob: 0c73a4b1a86631f5245913e3dddd50c880526979 [file] [log] [blame]
Joshua Habermanda647532021-08-18 15:30:03 -07001# 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 Habermancdc9fcd2021-08-23 16:57:40 -070027"""A bare-bones unit test that doesn't load any generated code."""
Joshua Habermana086aaf2021-08-18 15:10:06 -070028
29
30import unittest
31from google.protobuf.pyext import _message
Joshua Habermane079f1b2021-12-18 22:10:46 -080032from google.protobuf.internal import api_implementation
Joshua Haberman5f0978a2021-12-28 16:52:43 -080033from google.protobuf import unittest_pb2
Joshua Habermana086aaf2021-08-18 15:10:06 -070034
35class 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 Habermane079f1b2021-12-18 22:10:46 -080053 print(_message._IS_UPB)
Joshua Habermana086aaf2021-08-18 15:10:06 -070054 self.assertTrue(_message._IS_UPB)
Joshua Habermane079f1b2021-12-18 22:10:46 -080055 self.assertEqual(api_implementation.Type(), "cpp")
Joshua Habermana086aaf2021-08-18 15:10:06 -070056
Joshua Haberman5f0978a2021-12-28 16:52:43 -080057 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 Habermane079f1b2021-12-18 22:10:46 -080072#TestMessageExtension.test_descriptor_pool.__unittest_expecting_failure__ = True
Joshua Habermanb8736ae2021-12-13 12:51:10 -080073
Joshua Habermana086aaf2021-08-18 15:10:06 -070074
75if __name__ == '__main__':
Joshua Habermanb8736ae2021-12-13 12:51:10 -080076 unittest.main(verbosity=2)