diff options
author | Lauro Neto <lauro.neto@openbossa.org> | 2010-02-01 18:00:43 -0300 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2010-02-02 18:19:28 -0200 |
commit | 74351882b5e613f109cfe1f3c0b60885dcad06bc (patch) | |
tree | 151063fd3ea5004e49a544836226a81ecf10679c /tests/qtcore | |
parent | 76529055ff3894793cf83be02248a9f189a1983f (diff) |
Fixing several tests
Using isinstance instead of comparing classes names
Removing unnecessary tuple check, treated by the return value unpacking
Adding missing inject code tests
Adding qfile.getchar test
Fixing QLocale.to* methods
fixing __setitem__ in qbytearray
Diffstat (limited to 'tests/qtcore')
-rwxr-xr-x | tests/qtcore/qbitarray_test.py | 19 | ||||
-rwxr-xr-x | tests/qtcore/qbytearray_buffer_protocol_test.py | 7 | ||||
-rwxr-xr-x | tests/qtcore/qbytearray_concatenation_operator_test.py | 20 | ||||
-rw-r--r-- | tests/qtcore/qbytearray_operator_test.py | 21 | ||||
-rw-r--r-- | tests/qtcore/qbytearray_test.py | 65 | ||||
-rw-r--r-- | tests/qtcore/qchar_test.py | 33 | ||||
-rw-r--r-- | tests/qtcore/qfile_test.py | 31 | ||||
-rw-r--r-- | tests/qtcore/qlatin1string_test.py | 21 | ||||
-rw-r--r-- | tests/qtcore/qlocale_test.py | 24 | ||||
-rw-r--r-- | tests/qtcore/qprocess_test.py | 8 | ||||
-rw-r--r-- | tests/qtcore/qvariant_test.py | 24 | ||||
-rw-r--r-- | tests/qtcore/translation_test.py | 10 |
12 files changed, 246 insertions, 37 deletions
diff --git a/tests/qtcore/qbitarray_test.py b/tests/qtcore/qbitarray_test.py index d68b4756d..0ae6e1d2f 100755 --- a/tests/qtcore/qbitarray_test.py +++ b/tests/qtcore/qbitarray_test.py @@ -95,6 +95,25 @@ class QBitArrayIsIterableTest(unittest.TestCase): self.assertTrue(has_xor_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(xored_qbitarray), xored_bool_list) + +class QBitArrayGetItemTest(unittest.TestCase): + '''Test case for []/__getitem__ operator''' + + def create_bitarray(self, values): + '''helper function to create a bit array''' + obj = QBitArray(len(values)) + for i, value in enumerate(values): + obj.setBit(i, value) + return obj + + def testSequenceProtocol(self): + '''QBitArray sequence protocol''' + data = [True, False, True] + obj = self.create_bitarray(data) + for reference, value in zip(data, obj): + self.assertEqual(reference, value) + + if __name__ == '__main__': unittest.main() diff --git a/tests/qtcore/qbytearray_buffer_protocol_test.py b/tests/qtcore/qbytearray_buffer_protocol_test.py index 6e81f1e03..117e50b93 100755 --- a/tests/qtcore/qbytearray_buffer_protocol_test.py +++ b/tests/qtcore/qbytearray_buffer_protocol_test.py @@ -13,12 +13,7 @@ class QByteArrayBufferProtocolTest(unittest.TestCase): def testQByteArrayBufferProtocol(self): #Tests QByteArray implementation of Python buffer protocol using the os.path.isdir #function which an unicode object or other object implementing the Python buffer protocol - os_path_isdir_function_correctly_called_with_a_qbytearray = True - try: - isdir(QByteArray('/tmp')) - except: - os_path_isdir_function_correctly_called_with_a_qbytearray = False - self.assertTrue(os_path_isdir_function_correctly_called_with_a_qbytearray) + isdir(QByteArray('/tmp')) if __name__ == '__main__': unittest.main() diff --git a/tests/qtcore/qbytearray_concatenation_operator_test.py b/tests/qtcore/qbytearray_concatenation_operator_test.py index 932f84d93..b9a01ff63 100755 --- a/tests/qtcore/qbytearray_concatenation_operator_test.py +++ b/tests/qtcore/qbytearray_concatenation_operator_test.py @@ -11,14 +11,10 @@ class QByteArrayConcatenationOperatorTest(unittest.TestCase): def testConcatQByteArrayAndPythonString(self): #Test concatenation of a QByteArray with a Python string, in this order - concat_qbytearray_add_python_string_worked = True qba = QByteArray('foo') - result = None - try: - result = qba + 'bar' - except: - concat_qbytearray_add_python_string_worked = False - self.assertTrue(concat_qbytearray_add_python_string_worked) + result = qba + 'bar' + self.assert_(isinstance(result, QByteArray)) + self.assertEqual(result, 'foobar') # NOTICE: the standard behavior of PyQt is to return a QString object # for this case. As this is a minor issue the assertion will be left commented. #self.assertEqual(result.__class__.__name__, 'QString') @@ -27,13 +23,9 @@ class QByteArrayConcatenationOperatorTest(unittest.TestCase): #Test concatenation of a Python string with a QByteArray, in this order concat_python_string_add_qbytearray_worked = True qba = QByteArray('foo') - result = None - try: - result = 'bar' + qba - except: - concat_python_string_add_qbytearray_worked = False - self.assertTrue(concat_python_string_add_qbytearray_worked) - self.assertEqual(result.__class__.__name__, 'QByteArray') + result = 'bar' + qba + self.assert_(isinstance(result, QByteArray)) + self.assertEqual(result, 'barfoo') # NOTICE: Does not makes sense concat a unicode string with a QByteArray, because the # user does not know nothing about the internal representation of the unicode string. diff --git a/tests/qtcore/qbytearray_operator_test.py b/tests/qtcore/qbytearray_operator_test.py index 3485026c1..4a20c04c5 100644 --- a/tests/qtcore/qbytearray_operator_test.py +++ b/tests/qtcore/qbytearray_operator_test.py @@ -57,5 +57,26 @@ class QByteArrayOperatorAt(unittest.TestCase): self.assertRaises(IndexError, lambda :obj[len(string)]) +class QByteArrayOperatorLen(unittest.TestCase): + '''Test case for __len__ operator of QByteArray''' + + def testBasic(self): + '''QByteArray __len__''' + self.assertEqual(len(QByteArray()), 0) + self.assertEqual(len(QByteArray('')), 0) + self.assertEqual(len(QByteArray(' ')), 1) + self.assertEqual(len(QByteArray('yabadaba')), 8) + + +class QByteArrayOperatorStr(unittest.TestCase): + '''Test case for __str__ operator of QByteArray''' + + def testBasic(self): + '''QByteArray __str__''' + self.assertEqual(QByteArray().__str__(), '') + self.assertEqual(QByteArray('').__str__(), '') + self.assertEqual(QByteArray('aaa').__str__(), 'aaa') + + if __name__ == '__main__': unittest.main() diff --git a/tests/qtcore/qbytearray_test.py b/tests/qtcore/qbytearray_test.py index 67f99f3d4..20bba1400 100644 --- a/tests/qtcore/qbytearray_test.py +++ b/tests/qtcore/qbytearray_test.py @@ -10,17 +10,21 @@ from PySide.QtCore import QByteArray class QByteArrayTestToNumber(unittest.TestCase): def testToNumberInt(self): obj = QByteArray('37') - self.assertEqual(37, obj.toInt()[0]) + self.assertEqual((37, True), obj.toInt()) + + def testToNumberUShort(self): + obj = QByteArray('37') + self.assertEqual((37, True), obj.toUShort()) def testToNumberFloat(self): obj = QByteArray('37.109') - self.assertEqual(ctypes.c_float(37.109).value, - obj.toFloat()[0]) + self.assertEqual((ctypes.c_float(37.109).value, True), + obj.toFloat()) def testToNumberDouble(self): obj = QByteArray('37.109') - self.assertEqual(ctypes.c_double(37.109).value, - obj.toDouble()[0]) + self.assertEqual((ctypes.c_double(37.109).value, True), + obj.toDouble()) class QByteArraySplit(unittest.TestCase): '''Test case for QByteArray.split''' @@ -38,6 +42,57 @@ class QByteArrayData(unittest.TestCase): url = QByteArray("http://web.openbossa.org/") self.assertEqual(url.data(), "http://web.openbossa.org/") +class QByteArrayOperatorAtSetter(unittest.TestCase): + '''Test case for operator QByteArray[] - __setitem__''' + + def testSetterString(self): + '''QByteArray[x] = pythonstring''' + obj = QByteArray('123456') + obj[1] = '0' + self.assertEqual(obj, QByteArray('103456')) + + def testSetterStringLarge(self): + '''QByteArray[x] = pythonstring (larget than 1 char)''' + obj = QByteArray('123456') + obj[3] = 'abba' + self.assertEqual(obj, QByteArray('123abba56')) + + def testSetterQByteArray(self): + '''QByteArray[x] = qbytearray''' + obj = QByteArray('123456') + obj[3] = QByteArray('array') + self.assertEqual(obj, QByteArray('123array56')) + + +class QByteArrayOperatorAtSetterNegativeIndex(unittest.TestCase): + '''Test case for QByteArray[] - __setitem__ - for negative index''' + + def testSetterNegativeIndex(self): + '''QByteArray[x] = string - negative index''' + obj = QByteArray('123456') + obj[-3] = 'array' + self.assertEqual(obj, QByteArray('123array56')) + + +class QByteArrayOperatorAtSetterLargeIndex(unittest.TestCase): + '''Test case for QByteArray[] - __setitem__ - for 'overflown' index''' + + def testSetterLargeIndexEmpty(self): + '''QByteArray[x] = somestring - Overflow index on empty string''' + # should pad with spaces if the index is larger + obj = QByteArray('') + obj[2] = 'a' + self.assertEqual(obj, QByteArray(' a')) + + def testSetterLargeIndexNormal(self): + '''QByteArray[x] = somestring - Overflow index on normal string''' + # should pad with spaces if the index is larger + obj = QByteArray('mystring') + obj[10] = 'normal' + self.assertEqual(obj, QByteArray('mystring normal')) + + + if __name__ == '__main__': unittest.main() diff --git a/tests/qtcore/qchar_test.py b/tests/qtcore/qchar_test.py new file mode 100644 index 000000000..2aa234e46 --- /dev/null +++ b/tests/qtcore/qchar_test.py @@ -0,0 +1,33 @@ + +'''Test cases for QChar''' + +import unittest + +from PySide.QtCore import QString, QChar, QTextStream, QLatin1Char + + +class EqualTest(unittest.TestCase): + '''Tests for '__equal__''' + + def testEqualQChar(self): + '''QChar == QChar''' + self.assertEqual(QChar('a'), QChar('a')) + + def testEqualPyString(self): + '''QChar == Python string''' + self.assertEqual(QChar('a'), 'a') + + +class ImplicitConvQLatin1Char(unittest.TestCase): + '''Tests for implicit conversion from QLatin1Char to QChar''' + + def testQLatin1CharToChar(self): + '''QLatin1Char implicitly convertible to QChar''' + stream = QTextStream() + stream.setPadChar(QLatin1Char('-')) + self.assertEqual(QChar('-'), stream.padChar()) + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/qtcore/qfile_test.py b/tests/qtcore/qfile_test.py new file mode 100644 index 000000000..adbdd68b7 --- /dev/null +++ b/tests/qtcore/qfile_test.py @@ -0,0 +1,31 @@ + +import unittest + +import os +import tempfile + +from PySide.QtCore import QFile, QIODevice + +class GetCharTest(unittest.TestCase): + '''Test case for QIODevice.getChar in QFile''' + + def setUp(self): + '''Acquire resources''' + handle, self.filename = tempfile.mkstemp() + os.write(handle, 'a') + os.close(handle) + + def tearDown(self): + '''release resources''' + os.remove(self.filename) + + def testBasic(self): + '''QFile.getChar''' + obj = QFile(self.filename) + obj.open(QIODevice.ReadOnly) + self.assertEqual(obj.getChar(), (True, 'a')) + self.assert_(not obj.getChar()[0]) + obj.close() + +if __name__ == '__main__': + unittest.main() diff --git a/tests/qtcore/qlatin1string_test.py b/tests/qtcore/qlatin1string_test.py new file mode 100644 index 000000000..fdcf6ec98 --- /dev/null +++ b/tests/qtcore/qlatin1string_test.py @@ -0,0 +1,21 @@ + +'''Test cases for QLatin1String''' + +import unittest + +from PySide.QtCore import QString, QLatin1String, QObject + + +class ImplicitConvQLatin1String(unittest.TestCase): + '''Tests for implicit conversion from QLatin1String to QString''' + + def testQLatin1String(self): + '''QString implicit convertion from QLatin1String''' + obj = QObject() + obj.setObjectName(QLatin1String('dummy')) + + self.assertEqual(QString('dummy'), obj.objectName()) + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/qtcore/qlocale_test.py b/tests/qtcore/qlocale_test.py index e2fd49d19..f1bff8e82 100644 --- a/tests/qtcore/qlocale_test.py +++ b/tests/qtcore/qlocale_test.py @@ -10,17 +10,31 @@ from PySide.QtCore import QLocale class QLocaleTestToNumber(unittest.TestCase): def testToNumberInt(self): obj = QLocale(QLocale.C) - self.assertEqual(37, obj.toInt('37')[0]) + self.assertEqual((37, True), obj.toInt('37')) def testToNumberFloat(self): obj = QLocale(QLocale.C) - self.assertEqual(ctypes.c_float(37.109).value, - obj.toFloat('37.109')[0]) + self.assertEqual((ctypes.c_float(37.109).value, True), + obj.toFloat('37.109')) def testToNumberDouble(self): obj = QLocale(QLocale.C) - self.assertEqual(ctypes.c_double(37.109).value, - obj.toDouble('37.109')[0]) + self.assertEqual((ctypes.c_double(37.109).value, True), + obj.toDouble('37.109')) + + def testToNumberShort(self): + obj = QLocale(QLocale.C) + self.assertEqual((ctypes.c_short(37).value, True), + obj.toShort('37')) + + def testToNumberULongLong(self): + obj = QLocale(QLocale.C) + self.assertEqual((ctypes.c_ulonglong(37).value, True), + obj.toULongLong('37')) + + def testToNumberULongLongNegative(self): + obj = QLocale(QLocale.C) + self.assert_(not obj.toULongLong('-37')[1]) if __name__ == '__main__': unittest.main() diff --git a/tests/qtcore/qprocess_test.py b/tests/qtcore/qprocess_test.py index 7770379a7..b01c68ba3 100644 --- a/tests/qtcore/qprocess_test.py +++ b/tests/qtcore/qprocess_test.py @@ -8,11 +8,9 @@ from PySide.QtCore import * class TestQProcess (unittest.TestCase): def testStartDetached(self): - tuple_ = QProcess.startDetached("dir", [], os.getcwd()) - self.assertEquals(tuple, tuple_.__class__) - (value, pid) = tuple_ - self.assertEquals(bool, value.__class__) - self.assertEqual(long, pid.__class__) + value, pid = QProcess.startDetached("dir", [], os.getcwd()) + self.assert_(isinstance(value, bool)) + self.assert_(isinstance(pid, long)) if __name__ == '__main__': unittest.main() diff --git a/tests/qtcore/qvariant_test.py b/tests/qtcore/qvariant_test.py index b3f80c895..8937e319c 100644 --- a/tests/qtcore/qvariant_test.py +++ b/tests/qtcore/qvariant_test.py @@ -16,11 +16,31 @@ class MySize(QSize): class QVariantToNumber(unittest.TestCase): def testToNumberInt(self): obj = QVariant('37') - self.assertEqual(37, obj.toInt()[0]) + self.assertEqual((37, True), obj.toInt()) + + def testToNumberLongLong(self): + obj = QVariant('37') + self.assertEqual((37, True), obj.toLongLong()) + + def testToNumberUInt(self): + obj = QVariant('37') + self.assertEqual((37, True), obj.toUInt()) + + def testToNumberUIntNegative(self): + obj = QVariant('-37') + self.assert_(not obj.toUInt()[1]) + + def testToNumberULongLong(self): + obj = QVariant('37') + self.assertEqual((37, True), obj.toULongLong()) + + def testToNumberULongLongNegative(self): + obj = QVariant('-37') + self.assert_(not obj.toULongLong()[1]) def testToNumberFloat(self): obj = QVariant('37.109') - self.assertEqual(37.109, obj.toDouble()[0]) + self.assertEqual((37.109, True), obj.toDouble()) class QVariantTypeName(unittest.TestCase): def testTypeNameQString(self): diff --git a/tests/qtcore/translation_test.py b/tests/qtcore/translation_test.py index 31bb20e74..e1007cc5f 100644 --- a/tests/qtcore/translation_test.py +++ b/tests/qtcore/translation_test.py @@ -42,6 +42,16 @@ class TranslationTest(UsesQCoreApplication): obj.setObjectName(obj.tr('Hello World!')) self.assertEqual(obj.objectName(), u'привет мир!') + def testUtf8(self): + translator = QTranslator() + translator.load(os.path.join(self.trdir, 'trans_russian.qm')) + self.app.installTranslator(translator) + + obj = QObject() + obj.setObjectName(obj.trUtf8('Hello World!')) + self.assertEqual(obj.objectName(), u'привет мир!') + + if __name__ == '__main__': unittest.main() |