Isolate QVariant tests that use pure python objs
This commit is contained in:
parent
1356f832ae
commit
737952531b
2 changed files with 121 additions and 49 deletions
71
tests/qtcore/qvariant_pyobject_test.py
Normal file
71
tests/qtcore/qvariant_pyobject_test.py
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
|
||||||
|
'''QVariant handling of PyObjects, including pure-python or derived from Qt'''
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from PySide.QtCore import QSize, QVariant, QString
|
||||||
|
|
||||||
|
|
||||||
|
class Dummy(object):
|
||||||
|
'''Pure python sample class'''
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MySize(QSize):
|
||||||
|
'''Extended class'''
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class QVariantPurePython(unittest.TestCase):
|
||||||
|
'''QVariant + pure python classes'''
|
||||||
|
|
||||||
|
def testTypeNamePythonClasses(self):
|
||||||
|
'''QVariant of pure python classes'''
|
||||||
|
d = Dummy()
|
||||||
|
obj = QVariant(d)
|
||||||
|
# inherited type name from other binding
|
||||||
|
self.assertEqual('PyQt_PyObject', obj.typeName())
|
||||||
|
|
||||||
|
|
||||||
|
class QVariantInheritedPython(unittest.TestCase):
|
||||||
|
'''QVariant + classes inherited from C++'''
|
||||||
|
|
||||||
|
# This works only on PyQt4 4.5.x, not on PyQt4 4.4.x or PySide
|
||||||
|
def testSubClassConvertion(self):
|
||||||
|
'''QVariant(QSize subclass) type is UserType and returns same object'''
|
||||||
|
mysize = MySize(0, 0)
|
||||||
|
variant = QVariant(mysize)
|
||||||
|
|
||||||
|
self.assertEqual(variant.type(), QVariant.UserType)
|
||||||
|
self.assertTrue(variant.toPyObject() is mysize)
|
||||||
|
|
||||||
|
|
||||||
|
class QVariantToPyObject(unittest.TestCase):
|
||||||
|
'''QVariant.toPyObject tests'''
|
||||||
|
|
||||||
|
def testQVariantPyList(self):
|
||||||
|
'''QVariant(QVariantList).toPyObject() equals original list'''
|
||||||
|
obj = QVariant([1, 'two', 3])
|
||||||
|
self.assertEqual(obj.toPyObject(), [1, 'two', 3])
|
||||||
|
|
||||||
|
def testPyObject(self):
|
||||||
|
'''QVariant(pure PyObject).toPyObject should return the same object'''
|
||||||
|
d = Dummy()
|
||||||
|
obj = QVariant(d)
|
||||||
|
self.assert_(d is obj.toPyObject())
|
||||||
|
|
||||||
|
def testNoneToPyObject(self):
|
||||||
|
'''QVariant().toPyObject() should return None'''
|
||||||
|
obj = QVariant()
|
||||||
|
self.assertEqual(None, obj.toPyObject())
|
||||||
|
|
||||||
|
def testQStringToPyObject(self):
|
||||||
|
'''QVariant(python string).toPyObject() return an equal QString'''
|
||||||
|
d = 'abc'
|
||||||
|
obj = QVariant('abc')
|
||||||
|
self.assert_(isinstance(obj.toPyObject(), QString))
|
||||||
|
self.assertEqual(d, obj.toPyObject())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
@ -5,131 +5,132 @@
|
||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from PySide.QtCore import QSize, QVariant, QByteArray, QStringList
|
from PySide.QtCore import QSize, QVariant, QByteArray, QStringList, QString
|
||||||
|
|
||||||
class Dummy(object):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class MySize(QSize):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class QVariantToNumber(unittest.TestCase):
|
class QVariantToNumber(unittest.TestCase):
|
||||||
|
'''QVariant of number types'''
|
||||||
|
|
||||||
def testToNumberInt(self):
|
def testToNumberInt(self):
|
||||||
|
'''QVariant(int).toInt()'''
|
||||||
obj = QVariant('37')
|
obj = QVariant('37')
|
||||||
self.assertEqual((37, True), obj.toInt())
|
self.assertEqual((37, True), obj.toInt())
|
||||||
|
|
||||||
def testToNumberLongLong(self):
|
def testToNumberLongLong(self):
|
||||||
|
'''QVariant(int).toLongLong()'''
|
||||||
obj = QVariant('37')
|
obj = QVariant('37')
|
||||||
self.assertEqual((37, True), obj.toLongLong())
|
self.assertEqual((37, True), obj.toLongLong())
|
||||||
|
|
||||||
def testToNumberUInt(self):
|
def testToNumberUInt(self):
|
||||||
|
'''QVariant(int).toUInt()'''
|
||||||
obj = QVariant('37')
|
obj = QVariant('37')
|
||||||
self.assertEqual((37, True), obj.toUInt())
|
self.assertEqual((37, True), obj.toUInt())
|
||||||
|
|
||||||
def testToNumberUIntNegative(self):
|
def testToNumberUIntNegative(self):
|
||||||
|
'''QVariant(negative int).toUInt()'''
|
||||||
obj = QVariant('-37')
|
obj = QVariant('-37')
|
||||||
self.assert_(not obj.toUInt()[1])
|
self.assert_(not obj.toUInt()[1])
|
||||||
|
|
||||||
def testToNumberULongLong(self):
|
def testToNumberULongLong(self):
|
||||||
|
'''QVariant(int).toULongLong()'''
|
||||||
obj = QVariant('37')
|
obj = QVariant('37')
|
||||||
self.assertEqual((37, True), obj.toULongLong())
|
self.assertEqual((37, True), obj.toULongLong())
|
||||||
|
|
||||||
def testToNumberULongLongNegative(self):
|
def testToNumberULongLongNegative(self):
|
||||||
|
'''QVariant(negative int).toULongLong()'''
|
||||||
obj = QVariant('-37')
|
obj = QVariant('-37')
|
||||||
self.assert_(not obj.toULongLong()[1])
|
self.assert_(not obj.toULongLong()[1])
|
||||||
|
|
||||||
def testToNumberFloat(self):
|
def testToNumberFloat(self):
|
||||||
|
'''QVariant(double).toFloat()'''
|
||||||
obj = QVariant('37.109')
|
obj = QVariant('37.109')
|
||||||
self.assertEqual((37.109, True), obj.toDouble())
|
self.assertEqual((37.109, True), obj.toDouble())
|
||||||
|
|
||||||
|
|
||||||
class QVariantTypeName(unittest.TestCase):
|
class QVariantTypeName(unittest.TestCase):
|
||||||
|
'''QVariant.typeName()'''
|
||||||
|
|
||||||
def testTypeNameQString(self):
|
def testTypeNameQString(self):
|
||||||
#QVariant.typeName()
|
'''QVariant(QString).typeName()'''
|
||||||
|
obj = QVariant(QString('aaaa'))
|
||||||
|
self.assertEqual('QString', obj.typeName())
|
||||||
|
|
||||||
|
def testTypeNameString(self):
|
||||||
|
'''QVariant(PyString).typeName()'''
|
||||||
obj = QVariant('aaaa')
|
obj = QVariant('aaaa')
|
||||||
self.assertEqual('QString', obj.typeName())
|
self.assertEqual('QString', obj.typeName())
|
||||||
|
|
||||||
def testTypeNameInt(self):
|
def testTypeNameInt(self):
|
||||||
|
'''QVariant(int).typeName()'''
|
||||||
obj = QVariant(34)
|
obj = QVariant(34)
|
||||||
self.assertEqual('int', obj.typeName())
|
self.assertEqual('int', obj.typeName())
|
||||||
|
|
||||||
def testTypeNameDouble(self):
|
def testTypeNameDouble(self):
|
||||||
|
'''QVariant(double).typeName()'''
|
||||||
obj = QVariant(3.14)
|
obj = QVariant(3.14)
|
||||||
self.assertEqual('double', obj.typeName())
|
self.assertEqual('double', obj.typeName())
|
||||||
|
|
||||||
def testTypeNameBool(self):
|
def testTypeNameBool(self):
|
||||||
|
'''QVariant(bool).typeName()'''
|
||||||
obj = QVariant(True)
|
obj = QVariant(True)
|
||||||
self.assertEqual('bool', obj.typeName())
|
self.assertEqual('bool', obj.typeName())
|
||||||
|
|
||||||
def testTypeNameQByteArray(self):
|
def testTypeNameQByteArray(self):
|
||||||
|
'''QVariant(QByteArray).typeName()'''
|
||||||
obj = QVariant(QByteArray('aaaa'))
|
obj = QVariant(QByteArray('aaaa'))
|
||||||
self.assertEqual('QByteArray', obj.typeName())
|
self.assertEqual('QByteArray', obj.typeName())
|
||||||
|
|
||||||
def testTypeNameNone(self):
|
def testTypeNameNone(self):
|
||||||
|
'''QVariant().typeName()'''
|
||||||
obj = QVariant()
|
obj = QVariant()
|
||||||
self.assertEqual(None, obj.typeName())
|
self.assertEqual(None, obj.typeName())
|
||||||
|
|
||||||
def testTypeNameQVariantList(self):
|
def testTypeNameQVariantList(self):
|
||||||
obj = QVariant([1,Dummy(),3,4])
|
'''QVariant(QVariantList).typeName()'''
|
||||||
|
obj = QVariant([1, 2, 3, 4])
|
||||||
|
self.assertEqual('QVariantList', obj.typeName())
|
||||||
|
|
||||||
|
obj = QVariant([1.0, 2.2, 3.3, 4.2])
|
||||||
|
self.assertEqual('QVariantList', obj.typeName())
|
||||||
|
|
||||||
|
obj = QVariant(['aaa', 'bbb', 'ccc', 'dddd'])
|
||||||
|
self.assertEqual('QVariantList', obj.typeName())
|
||||||
|
|
||||||
|
obj = QVariant([QString('aaa'), QString('bbb'),
|
||||||
|
QString('ccc'), QString('dddd')])
|
||||||
self.assertEqual('QVariantList', obj.typeName())
|
self.assertEqual('QVariantList', obj.typeName())
|
||||||
|
|
||||||
def testTypeNameQStringList(self):
|
def testTypeNameQStringList(self):
|
||||||
|
'''QVariant(QStringList).typeName()'''
|
||||||
obj = QVariant(QStringList())
|
obj = QVariant(QStringList())
|
||||||
self.assertEqual('QStringList', obj.typeName())
|
self.assertEqual('QStringList', obj.typeName())
|
||||||
|
obj = QVariant(QStringList(['aaa', 'bbb', 'ccc']))
|
||||||
|
self.assertEqual('QStringList', obj.typeName())
|
||||||
|
|
||||||
def testTypeNamePythonClasses(self):
|
|
||||||
## QVariant of python classes
|
|
||||||
d = Dummy()
|
|
||||||
obj = QVariant(d)
|
|
||||||
self.assertEqual('PyQt_PyObject', obj.typeName())
|
|
||||||
|
|
||||||
# This works only on PyQt4 4.5.x, not on PyQt4 4.4.x or PySide
|
|
||||||
def testSubClassConvertion(self):
|
|
||||||
mysize = MySize(0, 0)
|
|
||||||
variant = QVariant(mysize)
|
|
||||||
|
|
||||||
assert(variant.type() != QVariant.Size)
|
|
||||||
assert(variant.toPyObject() is mysize)
|
|
||||||
|
|
||||||
class QVariantConstructor(unittest.TestCase):
|
class QVariantConstructor(unittest.TestCase):
|
||||||
|
'''More qvariant constructions'''
|
||||||
|
|
||||||
def testCopyConstructor(self):
|
def testCopyConstructor(self):
|
||||||
|
'''QVariant copy constructor'''
|
||||||
obj = QVariant(1)
|
obj = QVariant(1)
|
||||||
cpy = QVariant(obj)
|
cpy = QVariant(obj)
|
||||||
|
|
||||||
self.assertEqual(obj.type(), cpy.type())
|
self.assertEqual(obj.type(), cpy.type())
|
||||||
|
|
||||||
def testQStringConstructor(self):
|
def testQStringConstructor(self):
|
||||||
|
'''QVariant(PyString).type == QVariant.string'''
|
||||||
obj = QVariant("PySide")
|
obj = QVariant("PySide")
|
||||||
self.assertEqual(obj.type(), QVariant.String)
|
self.assertEqual(obj.type(), QVariant.String)
|
||||||
|
|
||||||
class QVariantToPyObject(unittest.TestCase):
|
def testQSizeConstructor(self):
|
||||||
def testQVariantPyList(self):
|
'''QVariant(QSize).type == QVariant.Size'''
|
||||||
obj = QVariant([1, 'two', 3])
|
mysize = QSize(0, 0)
|
||||||
self.assertEqual(obj.toPyObject(), [1, 'two', 3])
|
|
||||||
|
|
||||||
def testPyObject(self):
|
|
||||||
d = Dummy()
|
|
||||||
obj = QVariant(d)
|
|
||||||
self.assertEqual(d, obj.toPyObject())
|
|
||||||
|
|
||||||
def atestNoneToPyObject(self):
|
|
||||||
# XXX Do not run by default because segfaults with PyQt4
|
|
||||||
obj = QVariant()
|
|
||||||
self.assertEqual(None, obj.toPyObject())
|
|
||||||
|
|
||||||
def testQStringToPyObject(self):
|
|
||||||
d = 'abc'
|
|
||||||
obj = QVariant('abc')
|
|
||||||
self.assertEqual(d, obj.toPyObject())
|
|
||||||
|
|
||||||
class QVartiantTypeTest(unittest.TestCase):
|
|
||||||
def testQSize(self):
|
|
||||||
class MySize(QSize):
|
|
||||||
pass
|
|
||||||
|
|
||||||
mysize = MySize(5, 5)
|
|
||||||
variant = QVariant(mysize)
|
variant = QVariant(mysize)
|
||||||
self.assert_(variant.type() != QVariant.Size)
|
|
||||||
self.assert_(variant.toPyObject() is mysize)
|
self.assertEqual(variant.type(), QVariant.Size)
|
||||||
|
self.assertEqual(variant.toSize(), mysize)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue