|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from absl.testing import absltest |
| 16 | + |
| 17 | +from clif.testing.python import python_multiple_inheritance as tm |
| 18 | + |
| 19 | + |
| 20 | +class PC(tm.CppBase): |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +class PPCC(PC, tm.CppDrvd): |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class PPCCInit(PC, tm.CppDrvd): |
| 29 | + |
| 30 | + def __init__(self, value): |
| 31 | + PC.__init__(self, value) |
| 32 | + tm.CppDrvd.__init__(self, value + 1) |
| 33 | + |
| 34 | + |
| 35 | +class PythonMultipleInheritanceTest(absltest.TestCase): |
| 36 | + |
| 37 | + def testPC(self): |
| 38 | + d = PC(11) |
| 39 | + self.assertEqual(d.get_base_value(), 11) |
| 40 | + d.reset_base_value(13) |
| 41 | + self.assertEqual(d.get_base_value(), 13) |
| 42 | + |
| 43 | + @absltest.skipIf( |
| 44 | + "pybind11" in tm.__doc__, |
| 45 | + "Preempt `__init__() must be called when overriding __init__` exception.", |
| 46 | + ) |
| 47 | + def testPPCC(self): |
| 48 | + d = PPCC(11) |
| 49 | + self.assertEqual(d.get_drvd_value(), 33) |
| 50 | + d.reset_drvd_value(55) |
| 51 | + self.assertEqual(d.get_drvd_value(), 55) |
| 52 | + |
| 53 | + # PyCLIF-C-API: CppBase is not initialized and never used. |
| 54 | + self.assertEqual(d.get_base_value(), 11) |
| 55 | + self.assertEqual(d.get_base_value_from_drvd(), 11) |
| 56 | + d.reset_base_value(20) |
| 57 | + self.assertEqual(d.get_base_value(), 20) |
| 58 | + self.assertEqual(d.get_base_value_from_drvd(), 20) |
| 59 | + d.reset_base_value_from_drvd(30) |
| 60 | + self.assertEqual(d.get_base_value(), 30) |
| 61 | + self.assertEqual(d.get_base_value_from_drvd(), 30) |
| 62 | + |
| 63 | + def testPPCCInit(self): |
| 64 | + d = PPCCInit(11) |
| 65 | + self.assertEqual(d.get_drvd_value(), 36) |
| 66 | + d.reset_drvd_value(55) |
| 67 | + self.assertEqual(d.get_drvd_value(), 55) |
| 68 | + |
| 69 | + # PyCLIF-C-API: CppBase is initialized but never used. |
| 70 | + # PyCLIF-pybind11: CppBase is initialized and used when CppBase methods |
| 71 | + # are called, but CppDrvd is used when CppDrvd methods |
| 72 | + # are called. |
| 73 | + i = 1 if "pybind11" in tm.__doc__ else 0 |
| 74 | + self.assertEqual(d.get_base_value(), (12, 11)[i]) |
| 75 | + self.assertEqual(d.get_base_value_from_drvd(), 12) |
| 76 | + d.reset_base_value(20) |
| 77 | + self.assertEqual(d.get_base_value(), 20) |
| 78 | + self.assertEqual(d.get_base_value_from_drvd(), (20, 12)[i]) |
| 79 | + d.reset_base_value_from_drvd(30) |
| 80 | + self.assertEqual(d.get_base_value(), (30, 20)[i]) |
| 81 | + self.assertEqual(d.get_base_value_from_drvd(), 30) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + absltest.main() |
0 commit comments