Replace "requires" class decorator with a simple if (for Python 2.5 compatibility)

Reviewed-by: Lauro Moura <lauro.neto@openbossa.org>
Reviewed-by: Bruno Araujo <bruno.araujo@openbossa.org>
This commit is contained in:
Anderson Lizardo 2010-02-25 12:25:41 -04:00
commit a13b5b0145
6 changed files with 159 additions and 206 deletions

View file

@ -7,32 +7,31 @@ from PySide.QtCore import QObject, SIGNAL
try: try:
from PySide.QtGui import QSpinBox, QPushButton from PySide.QtGui import QSpinBox, QPushButton
hasQtGui = True
except ImportError: except ImportError:
pass hasQtGui = False
from helper import UsesQApplication from helper import UsesQApplication
from helper import decorators
if hasQtGui:
class QtGuiSigLambda(UsesQApplication):
@decorators.requires('PySide.QtGui') def testButton(self):
class QtGuiSigLambda(UsesQApplication): #Connecting a lambda to a QPushButton.clicked()
obj = QPushButton('label')
QObject.connect(obj, SIGNAL('clicked()'),
lambda: setattr(obj, 'called', True))
obj.click()
self.assert_(obj.called)
def testButton(self): def testSpinButton(self):
#Connecting a lambda to a QPushButton.clicked() #Connecting a lambda to a QPushButton.clicked()
obj = QPushButton('label') obj = QSpinBox()
QObject.connect(obj, SIGNAL('clicked()'), arg = 444
lambda: setattr(obj, 'called', True)) QObject.connect(obj, SIGNAL('valueChanged(int)'),
obj.click() lambda x: setattr(obj, 'arg', 444))
self.assert_(obj.called) obj.setValue(444)
self.assertEqual(obj.arg, arg)
def testSpinButton(self):
#Connecting a lambda to a QPushButton.clicked()
obj = QSpinBox()
arg = 444
QObject.connect(obj, SIGNAL('valueChanged(int)'),
lambda x: setattr(obj, 'arg', 444))
obj.setValue(444)
self.assertEqual(obj.arg, arg)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -6,12 +6,11 @@ from PySide.QtCore import QObject, SIGNAL
try: try:
from PySide.QtGui import QPushButton, QSpinBox from PySide.QtGui import QPushButton, QSpinBox
hasQtGui = True
except ImportError: except ImportError:
pass hasQtGui = False
from helper import BasicPySlotCase, UsesQApplication from helper import BasicPySlotCase, UsesQApplication
from helper.decorators import requires
def random_gen(count=100, largest=99, lowest=0): def random_gen(count=100, largest=99, lowest=0):
for i in range(count): for i in range(count):
@ -44,25 +43,24 @@ class MultipleSignalConnections(unittest.TestCase):
self.assert_(rec.called) self.assert_(rec.called)
@requires('PySide.QtGui') if hasQtGui:
class QtGuiMultipleSlots(UsesQApplication, MultipleSignalConnections): class QtGuiMultipleSlots(UsesQApplication, MultipleSignalConnections):
'''Multiple connections to QtGui signals''' '''Multiple connections to QtGui signals'''
def testButtonClick(self): def testButtonClick(self):
"""Multiple connections to QPushButton.clicked()""" """Multiple connections to QPushButton.clicked()"""
sender = QPushButton('button') sender = QPushButton('button')
receivers = [BasicPySlotCase() for x in range(30)] receivers = [BasicPySlotCase() for x in range(30)]
self.run_many(sender, 'clicked()', sender.click, receivers) self.run_many(sender, 'clicked()', sender.click, receivers)
def testSpinBoxValueChanged(self):
"""Multiple connections to QSpinBox.valueChanged(int)"""
for test in random_gen(10):
sender = QSpinBox()
#FIXME if number of receivers if higher than 50, segfaults
receivers = [BasicPySlotCase() for x in range(10)]
self.run_many(sender, 'valueChanged(int)', sender.setValue,
receivers, (test,))
def testSpinBoxValueChanged(self):
"""Multiple connections to QSpinBox.valueChanged(int)"""
for test in random_gen(10):
sender = QSpinBox()
#FIXME if number of receivers if higher than 50, segfaults
receivers = [BasicPySlotCase() for x in range(10)]
self.run_many(sender, 'valueChanged(int)', sender.setValue,
receivers, (test,))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -7,7 +7,6 @@ from functools import partial
from PySide.QtCore import QObject, SIGNAL, QProcess from PySide.QtCore import QObject, SIGNAL, QProcess
from helper import BasicPySlotCase, UsesQCoreApplication from helper import BasicPySlotCase, UsesQCoreApplication
from helper.decorators import requires
def random_gen(count=50, largest=49, lowest=0): def random_gen(count=50, largest=49, lowest=0):

View file

@ -4,11 +4,11 @@ from PySide.QtCore import QObject, SIGNAL, SLOT
try: try:
from PySide.QtGui import QSpinBox, QApplication, QWidget from PySide.QtGui import QSpinBox, QApplication, QWidget
hasQtGui = True
except ImportError: except ImportError:
pass hasQtGui = False
from helper import UsesQApplication from helper import UsesQApplication
from helper.decorators import requires
class Dummy(QObject): class Dummy(QObject):
"""Dummy class used in this test.""" """Dummy class used in this test."""
@ -62,64 +62,64 @@ class PythonSigSlot(unittest.TestCase):
self.assert_(not self.called) self.assert_(not self.called)
@requires('PySide.QtGui') if hasQtGui:
class SpinBoxPySignal(UsesQApplication): class SpinBoxPySignal(UsesQApplication):
"""Tests the connection of python signals to QSpinBox qt slots.""" """Tests the connection of python signals to QSpinBox qt slots."""
def setUp(self): def setUp(self):
super(SpinBoxPySignal, self).setUp() super(SpinBoxPySignal, self).setUp()
self.obj = Dummy() self.obj = Dummy()
self.spin = QSpinBox() self.spin = QSpinBox()
self.spin.setValue(0) self.spin.setValue(0)
def tearDown(self): def tearDown(self):
super(SpinBoxPySignal, self).tearDown() super(SpinBoxPySignal, self).tearDown()
del self.obj del self.obj
del self.spin del self.spin
def testValueChanged(self): def testValueChanged(self):
"""Emission of a python signal to QSpinBox setValue(int)""" """Emission of a python signal to QSpinBox setValue(int)"""
QObject.connect(self.obj, SIGNAL('dummy(int)'), self.spin, SLOT('setValue(int)')) QObject.connect(self.obj, SIGNAL('dummy(int)'), self.spin, SLOT('setValue(int)'))
self.assertEqual(self.spin.value(), 0) self.assertEqual(self.spin.value(), 0)
self.obj.emit(SIGNAL('dummy(int)'), 4) self.obj.emit(SIGNAL('dummy(int)'), 4)
self.assertEqual(self.spin.value(), 4) self.assertEqual(self.spin.value(), 4)
def testValueChangedMultiple(self): def testValueChangedMultiple(self):
"""Multiple emissions of a python signal to QSpinBox setValue(int)""" """Multiple emissions of a python signal to QSpinBox setValue(int)"""
QObject.connect(self.obj, SIGNAL('dummy(int)'), self.spin, SLOT('setValue(int)')) QObject.connect(self.obj, SIGNAL('dummy(int)'), self.spin, SLOT('setValue(int)'))
self.assertEqual(self.spin.value(), 0) self.assertEqual(self.spin.value(), 0)
self.obj.emit(SIGNAL('dummy(int)'), 4) self.obj.emit(SIGNAL('dummy(int)'), 4)
self.assertEqual(self.spin.value(), 4) self.assertEqual(self.spin.value(), 4)
self.obj.emit(SIGNAL('dummy(int)'), 77) self.obj.emit(SIGNAL('dummy(int)'), 77)
self.assertEqual(self.spin.value(), 77) self.assertEqual(self.spin.value(), 77)
@requires('PySide.QtGui') if hasQtGui:
class WidgetPySignal(UsesQApplication): class WidgetPySignal(UsesQApplication):
"""Tests the connection of python signals to QWidget qt slots.""" """Tests the connection of python signals to QWidget qt slots."""
def setUp(self): def setUp(self):
super(WidgetPySignal, self).setUp() super(WidgetPySignal, self).setUp()
self.obj = Dummy() self.obj = Dummy()
self.widget = QWidget() self.widget = QWidget()
def tearDown(self): def tearDown(self):
super(WidgetPySignal, self).tearDown() super(WidgetPySignal, self).tearDown()
del self.obj del self.obj
del self.widget del self.widget
def testShow(self): def testShow(self):
"""Emission of a python signal to QWidget slot show()""" """Emission of a python signal to QWidget slot show()"""
self.widget.hide() self.widget.hide()
QObject.connect(self.obj, SIGNAL('dummy()'), self.widget, SLOT('show()')) QObject.connect(self.obj, SIGNAL('dummy()'), self.widget, SLOT('show()'))
self.assert_(not self.widget.isVisible()) self.assert_(not self.widget.isVisible())
self.obj.emit(SIGNAL('dummy()')) self.obj.emit(SIGNAL('dummy()'))
self.assert_(self.widget.isVisible()) self.assert_(self.widget.isVisible())
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -8,105 +8,105 @@ from PySide.QtCore import QObject, SIGNAL, SLOT
try: try:
from PySide.QtGui import QSpinBox, QPushButton from PySide.QtGui import QSpinBox, QPushButton
hasQtGui = True
except ImportError: except ImportError:
pass hasQtGui = False
from helper import BasicPySlotCase, UsesQApplication from helper import BasicPySlotCase, UsesQApplication
from helper.decorators import requires
@requires('PySide.QtGui') if hasQtGui:
class ButtonPySlot(UsesQApplication, BasicPySlotCase): class ButtonPySlot(UsesQApplication, BasicPySlotCase):
"""Tests the connection of python slots to QPushButton signals""" """Tests the connection of python slots to QPushButton signals"""
def testButtonClicked(self): def testButtonClicked(self):
"""Connection of a python slot to QPushButton.clicked()""" """Connection of a python slot to QPushButton.clicked()"""
button = QPushButton('Mylabel') button = QPushButton('Mylabel')
QObject.connect(button, SIGNAL('clicked()'), self.cb) QObject.connect(button, SIGNAL('clicked()'), self.cb)
self.args = tuple() self.args = tuple()
button.emit(SIGNAL('clicked(bool)'), False) button.emit(SIGNAL('clicked(bool)'), False)
self.assert_(self.called) self.assert_(self.called)
def testButtonClick(self): def testButtonClick(self):
"""Indirect qt signal emission using the QPushButton.click() method """ """Indirect qt signal emission using the QPushButton.click() method """
button = QPushButton('label') button = QPushButton('label')
QObject.connect(button, SIGNAL('clicked()'), self.cb) QObject.connect(button, SIGNAL('clicked()'), self.cb)
self.args = tuple() self.args = tuple()
button.click() button.click()
self.assert_(self.called) self.assert_(self.called)
@requires('PySide.QtGui') if hasQtGui:
class SpinBoxPySlot(UsesQApplication, BasicPySlotCase): class SpinBoxPySlot(UsesQApplication, BasicPySlotCase):
"""Tests the connection of python slots to QSpinBox signals""" """Tests the connection of python slots to QSpinBox signals"""
def setUp(self): def setUp(self):
super(SpinBoxPySlot, self).setUp() super(SpinBoxPySlot, self).setUp()
self.spin = QSpinBox() self.spin = QSpinBox()
def tearDown(self): def tearDown(self):
del self.spin del self.spin
super(SpinBoxPySlot, self).tearDown() super(SpinBoxPySlot, self).tearDown()
def testSpinBoxValueChanged(self): def testSpinBoxValueChanged(self):
"""Connection of a python slot to QSpinBox.valueChanged(int)""" """Connection of a python slot to QSpinBox.valueChanged(int)"""
QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb) QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb)
self.args = [3] self.args = [3]
self.spin.emit(SIGNAL('valueChanged(int)'), *self.args) self.spin.emit(SIGNAL('valueChanged(int)'), *self.args)
self.assert_(self.called) self.assert_(self.called)
def testSpinBoxValueChangedImplicit(self): def testSpinBoxValueChangedImplicit(self):
"""Indirect qt signal emission using QSpinBox.setValue(int)""" """Indirect qt signal emission using QSpinBox.setValue(int)"""
QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb) QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb)
self.args = [42] self.args = [42]
self.spin.setValue(self.args[0]) self.spin.setValue(self.args[0])
self.assert_(self.called) self.assert_(self.called)
def atestSpinBoxValueChangedFewArgs(self): def atestSpinBoxValueChangedFewArgs(self):
"""Emission of signals with fewer arguments than needed""" """Emission of signals with fewer arguments than needed"""
# XXX: PyQt4 crashes on the assertRaises # XXX: PyQt4 crashes on the assertRaises
QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb) QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb)
self.args = (554,) self.args = (554,)
self.assertRaises(TypeError, self.spin.emit, SIGNAL('valueChanged(int)')) self.assertRaises(TypeError, self.spin.emit, SIGNAL('valueChanged(int)'))
@requires('PySide.QtGui') if hasQtGui:
class QSpinBoxQtSlots(UsesQApplication): class QSpinBoxQtSlots(UsesQApplication):
"""Tests the connection to QSpinBox qt slots""" """Tests the connection to QSpinBox qt slots"""
qapplication = True qapplication = True
def testSetValueIndirect(self): def testSetValueIndirect(self):
"""Indirect signal emission: QSpinBox using valueChanged(int)/setValue(int)""" """Indirect signal emission: QSpinBox using valueChanged(int)/setValue(int)"""
spinSend = QSpinBox() spinSend = QSpinBox()
spinRec = QSpinBox() spinRec = QSpinBox()
spinRec.setValue(5) spinRec.setValue(5)
QObject.connect(spinSend, SIGNAL('valueChanged(int)'), spinRec, SLOT('setValue(int)')) QObject.connect(spinSend, SIGNAL('valueChanged(int)'), spinRec, SLOT('setValue(int)'))
self.assertEqual(spinRec.value(), 5) self.assertEqual(spinRec.value(), 5)
spinSend.setValue(3) spinSend.setValue(3)
self.assertEqual(spinRec.value(), 3) self.assertEqual(spinRec.value(), 3)
self.assertEqual(spinSend.value(), 3) self.assertEqual(spinSend.value(), 3)
def testSetValue(self): def testSetValue(self):
"""Direct signal emission: QSpinBox using valueChanged(int)/setValue(int)""" """Direct signal emission: QSpinBox using valueChanged(int)/setValue(int)"""
spinSend = QSpinBox() spinSend = QSpinBox()
spinRec = QSpinBox() spinRec = QSpinBox()
spinRec.setValue(5) spinRec.setValue(5)
spinSend.setValue(42) spinSend.setValue(42)
QObject.connect(spinSend, SIGNAL('valueChanged(int)'), spinRec, SLOT('setValue(int)')) QObject.connect(spinSend, SIGNAL('valueChanged(int)'), spinRec, SLOT('setValue(int)'))
self.assertEqual(spinRec.value(), 5) self.assertEqual(spinRec.value(), 5)
self.assertEqual(spinSend.value(), 42) self.assertEqual(spinSend.value(), 42)
spinSend.emit(SIGNAL('valueChanged(int)'), 3) spinSend.emit(SIGNAL('valueChanged(int)'), 3)
self.assertEqual(spinRec.value(), 3) self.assertEqual(spinRec.value(), 3)
#Direct emission shouldn't change the value of the emitter #Direct emission shouldn't change the value of the emitter
self.assertEqual(spinSend.value(), 42) self.assertEqual(spinSend.value(), 42)
spinSend.emit(SIGNAL('valueChanged(int)'), 66) spinSend.emit(SIGNAL('valueChanged(int)'), 66)
self.assertEqual(spinRec.value(), 66) self.assertEqual(spinRec.value(), 66)
self.assertEqual(spinSend.value(), 42) self.assertEqual(spinSend.value(), 42)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -1,43 +0,0 @@
'''Decorators for skipping test methods and test cases'''
import logging
import unittest
class requires(object):
'''Skip if the given module is not found
Usage:
@requires('RequiredModule')
class MyTestCase(unittest.TestCase):
...
'''
def __init__(self, *args):
'''Setup this decorator. Args should be a list of
module names'''
self.skip = False
for module in args:
try:
__import__(module)
except ImportError:
self.skip = True
self.skipped = module
break
def __call__(self, klass):
'''Replace a skipped class with a dummy testCase'''
if not self.skip:
return klass
logging.warning('Module %s not found. Skipping %s' % (self.skipped,
klass.__name__))
class SkipWrapper(unittest.TestCase):
'''Dummy wrapper'''
pass
return SkipWrapper