1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of the Shiboken Python Bindings Generator project.
#
# Copyright (C) 2013 Digia Plc 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 unittest
from sample import cacheSize
from sample import ProtectedNonPolymorphic, ProtectedVirtualDestructor
from sample import ProtectedPolymorphic, ProtectedPolymorphicDaughter, ProtectedPolymorphicGrandDaughter
from sample import ProtectedProperty, ProtectedEnumClass
from sample import PrivateDtor
from sample import Event, ObjectType, Point
class ExtendedProtectedPolymorphic(ProtectedPolymorphic):
def __init__(self, name):
ProtectedPolymorphic.__init__(self, name)
self.protectedName_called = False
def protectedName(self):
self.protectedName_called = True
self._name = 'Extended' + ProtectedPolymorphic.protectedName(self)
return self._name
class ExtendedProtectedPolymorphicDaughter(ProtectedPolymorphicDaughter):
def __init__(self, name):
self.protectedName_called = False
ProtectedPolymorphicDaughter.__init__(self, name)
def protectedName(self):
self.protectedName_called = True
self._name = 'ExtendedDaughter' + ProtectedPolymorphicDaughter.protectedName(self)
return self._name
class ExtendedProtectedPolymorphicGrandDaughter(ProtectedPolymorphicGrandDaughter):
def __init__(self, name):
self.protectedName_called = False
ProtectedPolymorphicGrandDaughter.__init__(self, name)
def protectedName(self):
self.protectedName_called = True
self._name = 'ExtendedGrandDaughter' + ProtectedPolymorphicGrandDaughter.protectedName(self)
return self._name
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 tearDown(self):
self.assertEqual(cacheSize(), 0)
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 tearDown(self):
self.assertEqual(cacheSize(), 0)
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.'''
original_name = 'Poly'
p = ExtendedProtectedPolymorphic(original_name)
name = p.callProtectedName()
self.assert_(p.protectedName_called)
self.assertEqual(p.protectedName(), name)
self.assertEqual(ProtectedPolymorphic.protectedName(p), original_name)
class ProtectedPolymorphicDaugherTest(unittest.TestCase):
'''Test cases for protected method in a class inheriting for a class with virtual methods.'''
def testProtectedCallWithInstanceCreatedOnCpp(self):
'''Calls a virtual protected method from parent class on an instance created in C++.'''
p = ProtectedPolymorphicDaughter.create()
self.assertEqual(p.publicName(), p.protectedName())
self.assertEqual(p.callProtectedName(), p.protectedName())
def testReimplementedProtectedCall(self):
'''Calls a reimplemented virtual protected method from parent class.'''
original_name = 'Poly'
p = ExtendedProtectedPolymorphicDaughter(original_name)
name = p.callProtectedName()
self.assert_(p.protectedName_called)
self.assertEqual(p.protectedName(), name)
self.assertEqual(ProtectedPolymorphicDaughter.protectedName(p), original_name)
class ProtectedPolymorphicGrandDaugherTest(unittest.TestCase):
'''Test cases for protected method in a class inheriting for a class that inherits from
another with protected virtual methods.'''
def tearDown(self):
self.assertEqual(cacheSize(), 0)
def testProtectedCallWithInstanceCreatedOnCpp(self):
'''Calls a virtual protected method from parent class on an instance created in C++.'''
p = ProtectedPolymorphicGrandDaughter.create()
self.assertEqual(p.publicName(), p.protectedName())
self.assertEqual(p.callProtectedName(), p.protectedName())
def testReimplementedProtectedCall(self):
'''Calls a reimplemented virtual protected method from parent class.'''
original_name = 'Poly'
p = ExtendedProtectedPolymorphicGrandDaughter(original_name)
name = p.callProtectedName()
self.assert_(p.protectedName_called)
self.assertEqual(p.protectedName(), name)
self.assertEqual(ProtectedPolymorphicGrandDaughter.protectedName(p), original_name)
class ProtectedVirtualDtorTest(unittest.TestCase):
'''Test cases for protected virtual destructor.'''
def setUp(self):
ProtectedVirtualDestructor.resetDtorCounter()
def tearDown(self):
self.assertEqual(cacheSize(), 0)
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)
class ExtendedProtectedEnumClass(ProtectedEnumClass):
def __init__(self):
ProtectedEnumClass.__init__(self)
def protectedEnumMethod(self, value):
if value == ProtectedEnumClass.ProtectedItem0:
return ProtectedEnumClass.ProtectedItem1
return ProtectedEnumClass.ProtectedItem0
def publicEnumMethod(self, value):
if value == ProtectedEnumClass.PublicItem0:
return ProtectedEnumClass.PublicItem1
return ProtectedEnumClass.PublicItem0
class ProtectedEnumTest(unittest.TestCase):
'''Test cases for protected enum.'''
def tearDown(self):
self.assertEqual(cacheSize(), 0)
def testProtectedMethodWithProtectedEnumArgument(self):
'''Calls protected method with protected enum argument.'''
obj = ProtectedEnumClass()
self.assertEqual(type(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedEnum)
self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem0)
self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem1)
self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem0)
self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem1)
def testProtectedMethodWithPublicEnumArgument(self):
'''Calls protected method with public enum argument.'''
obj = ProtectedEnumClass()
self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem0)
self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem1)
self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem0)
self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem1)
def testOverriddenProtectedMethodWithProtectedEnumArgument(self):
'''Calls overridden protected method with protected enum argument.'''
obj = ExtendedProtectedEnumClass()
self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem1)
self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem0)
self.assertEqual(ProtectedEnumClass.protectedEnumMethod(obj, ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem0)
self.assertEqual(ProtectedEnumClass.protectedEnumMethod(obj, ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem1)
self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem1)
self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem0)
def testOverriddenProtectedMethodWithPublicEnumArgument(self):
'''Calls overridden protected method with public enum argument.'''
obj = ExtendedProtectedEnumClass()
self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem1)
self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem0)
self.assertEqual(ProtectedEnumClass.publicEnumMethod(obj, ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem0)
self.assertEqual(ProtectedEnumClass.publicEnumMethod(obj, ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem1)
self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem1)
self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem0)
class ProtectedPropertyTest(unittest.TestCase):
'''Test cases for a class with a protected property (or field in C++).'''
def setUp(self):
self.obj = ProtectedProperty()
def tearDown(self):
del self.obj
self.assertEqual(cacheSize(), 0)
def testProtectedProperty(self):
'''Writes and reads a protected integer property.'''
self.obj.protectedProperty = 3
self.assertEqual(self.obj.protectedProperty, 3)
def testProtectedContainerProperty(self):
'''Writes and reads a protected list of integers property.'''
lst = [1, 2, 3, 4]
self.obj.protectedContainerProperty = lst
self.assertEqual(self.obj.protectedContainerProperty, lst)
def testProtectedEnumProperty(self):
'''Writes and reads a protected enum property.'''
self.obj.protectedEnumProperty = Event.SOME_EVENT
self.assertEqual(self.obj.protectedEnumProperty, Event.SOME_EVENT)
def testProtectedValueTypeProperty(self):
'''Writes and reads a protected value type property.'''
point = Point(12, 34)
self.obj.protectedValueTypeProperty = point
self.assertEqual(self.obj.protectedValueTypeProperty, point)
self.assertFalse(self.obj.protectedValueTypeProperty is point)
pointProperty = self.obj.protectedValueTypeProperty
self.assertFalse(self.obj.protectedValueTypeProperty is pointProperty)
def testProtectedValueTypePointerProperty(self):
'''Writes and reads a protected value type pointer property.'''
pt1 = Point(12, 34)
pt2 = Point(12, 34)
self.obj.protectedValueTypePointerProperty = pt1
self.assertEqual(self.obj.protectedValueTypePointerProperty, pt1)
self.assertEqual(self.obj.protectedValueTypePointerProperty, pt2)
self.assert_(self.obj.protectedValueTypePointerProperty is pt1)
self.assertFalse(self.obj.protectedValueTypePointerProperty is pt2)
def testProtectedObjectTypeProperty(self):
'''Writes and reads a protected object type property.'''
obj = ObjectType()
self.obj.protectedObjectTypeProperty = obj
self.assertEqual(self.obj.protectedObjectTypeProperty, obj)
class PrivateDtorProtectedMethodTest(unittest.TestCase):
'''Test cases for classes with private destructors and protected methods.'''
def tearDown(self):
self.assertEqual(cacheSize(), 0)
def testProtectedMethod(self):
'''Calls protected method of a class with a private destructor.'''
obj = PrivateDtor.instance()
self.assertEqual(type(obj), PrivateDtor)
self.assertEqual(obj.instanceCalls(), 1)
self.assertEqual(obj.instanceCalls(), obj.protectedInstanceCalls())
obj = PrivateDtor.instance()
self.assertEqual(obj.instanceCalls(), 2)
self.assertEqual(obj.instanceCalls(), obj.protectedInstanceCalls())
if __name__ == '__main__':
unittest.main()
|