diff options
-rw-r--r-- | tests/libsample/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/libsample/protected.cpp | 38 | ||||
-rw-r--r-- | tests/libsample/protected.h | 94 | ||||
-rw-r--r-- | tests/libsample/virtualmethods.cpp | 1 | ||||
-rw-r--r-- | tests/libsample/virtualmethods.h | 4 | ||||
-rw-r--r-- | tests/samplebinding/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/samplebinding/global.h | 1 | ||||
-rwxr-xr-x | tests/samplebinding/protected_test.py | 124 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 48 | ||||
-rwxr-xr-x | tests/samplebinding/virtualdtor_test.py | 24 |
10 files changed, 335 insertions, 3 deletions
diff --git a/tests/libsample/CMakeLists.txt b/tests/libsample/CMakeLists.txt index 534931aaf..a84985f28 100644 --- a/tests/libsample/CMakeLists.txt +++ b/tests/libsample/CMakeLists.txt @@ -18,6 +18,7 @@ objecttype.cpp overload.cpp pairuser.cpp point.cpp +protected.cpp reference.cpp samplenamespace.cpp simplefile.cpp diff --git a/tests/libsample/protected.cpp b/tests/libsample/protected.cpp new file mode 100644 index 000000000..4a2b251db --- /dev/null +++ b/tests/libsample/protected.cpp @@ -0,0 +1,38 @@ +/* + * This file is part of the Shiboken Python Binding Generator project. + * + * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: PySide team <contact@pyside.org> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. Please + * review the following information to ensure the GNU Lesser General + * Public License version 2.1 requirements will be met: + * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "protected.h" + +int ProtectedVirtualDestructor::dtor_called = 0; + diff --git a/tests/libsample/protected.h b/tests/libsample/protected.h new file mode 100644 index 000000000..fd14f7628 --- /dev/null +++ b/tests/libsample/protected.h @@ -0,0 +1,94 @@ +/* + * This file is part of the Shiboken Python Binding Generator project. + * + * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: PySide team <contact@pyside.org> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. Please + * review the following information to ensure the GNU Lesser General + * Public License version 2.1 requirements will be met: + * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef PROTECTED_H +#define PROTECTED_H + +class ProtectedNonPolymorphic +{ +public: + explicit ProtectedNonPolymorphic(const char* name) : m_name(name) {} + ~ProtectedNonPolymorphic() {} + + const char* publicName() { return m_name; } + + static ProtectedNonPolymorphic* create() { return new ProtectedNonPolymorphic("created"); } + +protected: + const char* protectedName() { return m_name; } + int protectedSum(int a0, int a1) { return a0 + a1; } + int modifiedProtectedSum(int a0, int a1) { return a0 + a1; } + static const char* protectedStatic() { return "protectedStatic"; } + + inline const char* dataTypeName(void *data = 0) const { return "pointer"; } + inline const char* dataTypeName(int data) const { return "integer"; } + +private: + const char* m_name; +}; + +class ProtectedPolymorphic +{ +public: + explicit ProtectedPolymorphic(const char* name) : m_name(name) {} + ~ProtectedPolymorphic() {} + + const char* publicName() { return m_name; } + + static ProtectedPolymorphic* create() { return new ProtectedPolymorphic("created"); } + + const char* callProtectedName() { return protectedName(); } + +protected: + virtual const char* protectedName() { return m_name; } + +private: + const char* m_name; +}; + +class ProtectedVirtualDestructor +{ +public: + ProtectedVirtualDestructor() {} + static ProtectedVirtualDestructor* create() { return new ProtectedVirtualDestructor(); } + static int dtorCalled() { return dtor_called; } + static void resetDtorCounter() { dtor_called = 0; } +protected: + virtual ~ProtectedVirtualDestructor() { dtor_called++; } +private: + static int dtor_called; +}; + +#endif // PROTECTED_H + diff --git a/tests/libsample/virtualmethods.cpp b/tests/libsample/virtualmethods.cpp index e2e6a2374..997f8f249 100644 --- a/tests/libsample/virtualmethods.cpp +++ b/tests/libsample/virtualmethods.cpp @@ -32,7 +32,6 @@ * 02110-1301 USA */ -#include <string.h> #include "virtualmethods.h" int VirtualDtor::dtor_called = 0; diff --git a/tests/libsample/virtualmethods.h b/tests/libsample/virtualmethods.h index cd504d90f..be7f54ec0 100644 --- a/tests/libsample/virtualmethods.h +++ b/tests/libsample/virtualmethods.h @@ -90,9 +90,11 @@ class VirtualDtor { public: VirtualDtor() {} - virtual ~VirtualDtor() { VirtualDtor::dtor_called++; } + virtual ~VirtualDtor() { dtor_called++; } + static VirtualDtor* create() { return new VirtualDtor(); } static int dtorCalled() { return dtor_called; } + static void resetDtorCounter() { dtor_called = 0; } private: static int dtor_called; diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt index f5789259c..4f8f4b0e0 100644 --- a/tests/samplebinding/CMakeLists.txt +++ b/tests/samplebinding/CMakeLists.txt @@ -28,6 +28,9 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/overload_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/point_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/privatedtor_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectednonpolymorphic_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphic_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedvirtualdestructor_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/reference_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/sample_module_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_wrapper.cpp diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h index b93cacfeb..7e53b8325 100644 --- a/tests/samplebinding/global.h +++ b/tests/samplebinding/global.h @@ -18,6 +18,7 @@ #include "pairuser.h" #include "point.h" #include "privatedtor.h" +#include "protected.h" #include "reference.h" #include "samplenamespace.h" #include "simplefile.h" diff --git a/tests/samplebinding/protected_test.py b/tests/samplebinding/protected_test.py new file mode 100755 index 000000000..b0b3b6f70 --- /dev/null +++ b/tests/samplebinding/protected_test.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of the Shiboken Python Bindings Generator project. +# +# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +# +# Contact: PySide team <contact@pyside.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License +# version 2.1 as published by the Free Software Foundation. Please +# review the following information to ensure the GNU Lesser General +# Public License version 2.1 requirements will be met: +# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +# # +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA + +'''Test cases for protected methods.''' + +import os +import unittest + +from sample import ProtectedNonPolymorphic, ProtectedPolymorphic, ProtectedVirtualDestructor +from sample import Point + +class ExtendedProtectedPolymorphic(ProtectedPolymorphic): + def __init__(self, name): + ProtectedPolymorphic.__init__(self, name) + def protectedName(self): + return 'Extended' + ProtectedPolymorphic.protectedName(self) + +class ExtendedProtectedVirtualDestructor(ProtectedVirtualDestructor): + def __init__(self): + ProtectedVirtualDestructor.__init__(self) + +class ProtectedNonPolymorphicTest(unittest.TestCase): + '''Test cases for protected method in a class without virtual methods.''' + + def testProtectedCall(self): + '''Calls a non-virtual protected method.''' + p = ProtectedNonPolymorphic('NonPoly') + self.assertEqual(p.publicName(), p.protectedName()) + a0, a1 = 1, 2 + self.assertEqual(p.protectedSum(a0, a1), a0 + a1) + + def testProtectedCallWithInstanceCreatedOnCpp(self): + '''Calls a non-virtual protected method on an instance created in C++.''' + p = ProtectedNonPolymorphic.create() + self.assertEqual(p.publicName(), p.protectedName()) + a0, a1 = 1, 2 + self.assertEqual(p.protectedSum(a0, a1), a0 + a1) + + def testModifiedProtectedCall(self): + '''Calls a non-virtual protected method modified with code injection.''' + p = ProtectedNonPolymorphic('NonPoly') + self.assertEqual(p.dataTypeName(), 'integer') + self.assertEqual(p.dataTypeName(1), 'integer') + self.assertEqual(p.dataTypeName(Point(1, 2)), 'pointer') + +class ProtectedPolymorphicTest(unittest.TestCase): + '''Test cases for protected method in a class with virtual methods.''' + + def testProtectedCall(self): + '''Calls a virtual protected method.''' + p = ProtectedNonPolymorphic('Poly') + self.assertEqual(p.publicName(), p.protectedName()) + a0, a1 = 1, 2 + self.assertEqual(p.protectedSum(a0, a1), a0 + a1) + + def testProtectedCallWithInstanceCreatedOnCpp(self): + '''Calls a virtual protected method on an instance created in C++.''' + p = ProtectedPolymorphic.create() + self.assertEqual(p.publicName(), p.protectedName()) + self.assertEqual(p.callProtectedName(), p.protectedName()) + + def testReimplementedProtectedCall(self): + '''Calls a reimplemented virtual protected method.''' + p = ExtendedProtectedPolymorphic('Poly') + self.assertEqual(p.callProtectedName(), p.protectedName()) + + +class ProtectedVirtualDtorTest(unittest.TestCase): + '''Test cases for protected virtual destructor.''' + + def setUp(self): + ProtectedVirtualDestructor.resetDtorCounter() + + def testVirtualProtectedDtor(self): + '''Original protected virtual destructor is being called.''' + dtor_called = ProtectedVirtualDestructor.dtorCalled() + for i in range(1, 10): + pvd = ProtectedVirtualDestructor() + del pvd + self.assertEqual(ProtectedVirtualDestructor.dtorCalled(), dtor_called + i) + + def testVirtualProtectedDtorOnCppCreatedObject(self): + '''Original protected virtual destructor is being called for a C++ created object.''' + dtor_called = ProtectedVirtualDestructor.dtorCalled() + for i in range(1, 10): + pvd = ProtectedVirtualDestructor.create() + del pvd + self.assertEqual(ProtectedVirtualDestructor.dtorCalled(), dtor_called + i) + + def testProtectedDtorOnDerivedClass(self): + '''Original protected virtual destructor is being called for a derived class.''' + dtor_called = ExtendedProtectedVirtualDestructor.dtorCalled() + for i in range(1, 10): + pvd = ExtendedProtectedVirtualDestructor() + del pvd + self.assertEqual(ExtendedProtectedVirtualDestructor.dtorCalled(), dtor_called + i) + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 796f5f3cc..eb75ba80a 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -80,6 +80,46 @@ </modify-function> </value-type> + <value-type name="ProtectedNonPolymorphic"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + <modify-function signature="modifiedProtectedSum(int, int)"> + <inject-code class="target" position="beginning"> + %0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%TYPE::%FUNCTION_NAME(%1, %2) * 10); + </inject-code> + </modify-function> + <modify-function signature="dataTypeName(void*) const" remove="all"/> + <modify-function signature="dataTypeName(int) const"> + <modify-argument index="1"> + <replace-default-expression with="0"/> + </modify-argument> + </modify-function> + <add-function signature="dataTypeName(PyObject*)const" return-type="const char*"> + <inject-code class="target" position="beginning"> + %0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%FUNCTION_NAME(%1)); + </inject-code> + </add-function> + </value-type> + + <value-type name="ProtectedPolymorphic"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + + <value-type name="ProtectedVirtualDestructor"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + <template name="boolptr_at_end_fix_beginning"> bool __ok__; %0 = %CONVERTTOPYTHON[%RETURN_TYPE]( @@ -417,7 +457,13 @@ </modify-function> </value-type> - <value-type name="VirtualDtor"/> + <value-type name="VirtualDtor"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> <value-type name="InjectCode"> <!-- diff --git a/tests/samplebinding/virtualdtor_test.py b/tests/samplebinding/virtualdtor_test.py index 19d7176f2..877634fe9 100755 --- a/tests/samplebinding/virtualdtor_test.py +++ b/tests/samplebinding/virtualdtor_test.py @@ -31,9 +31,16 @@ import unittest from sample import VirtualDtor +class ExtendedVirtualDtor(VirtualDtor): + def __init__(self): + VirtualDtor.__init__(self) + class VirtualDtorTest(unittest.TestCase): '''Test case for virtual destructor.''' + def setUp(self): + VirtualDtor.resetDtorCounter() + def testVirtualDtor(self): '''Original virtual destructor is being called.''' dtor_called = VirtualDtor.dtorCalled() @@ -42,6 +49,23 @@ class VirtualDtorTest(unittest.TestCase): del vd self.assertEqual(VirtualDtor.dtorCalled(), dtor_called + i) + def testVirtualDtorOnCppCreatedObject(self): + '''Original virtual destructor is being called for a C++ created object.''' + dtor_called = VirtualDtor.dtorCalled() + for i in range(1, 10): + vd = VirtualDtor.create() + del vd + self.assertEqual(VirtualDtor.dtorCalled(), dtor_called + i) + + def testDtorOnDerivedClass(self): + '''Original virtual destructor is being called for a derived class.''' + dtor_called = ExtendedVirtualDtor.dtorCalled() + for i in range(1, 10): + evd = ExtendedVirtualDtor() + del evd + self.assertEqual(ExtendedVirtualDtor.dtorCalled(), dtor_called + i) + + if __name__ == '__main__': unittest.main() |