diff options
Diffstat (limited to 'sources/pyside2/tests')
-rw-r--r-- | sources/pyside2/tests/QtCore/attr_cache_py3k.py | 6 | ||||
-rw-r--r-- | sources/pyside2/tests/QtCore/qsettings_test.py | 46 | ||||
-rw-r--r-- | sources/pyside2/tests/QtCore/qslot_object_test.py | 14 | ||||
-rw-r--r-- | sources/pyside2/tests/QtWidgets/bug_862.py | 33 | ||||
-rw-r--r-- | sources/pyside2/tests/pysidetest/CMakeLists.txt | 2 | ||||
-rw-r--r-- | sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py | 18 | ||||
-rw-r--r-- | sources/pyside2/tests/pysidetest/repr_test.py | 83 | ||||
-rw-r--r-- | sources/pyside2/tests/pysidetest/testobject.cpp | 22 | ||||
-rw-r--r-- | sources/pyside2/tests/pysidetest/testobject.h | 12 | ||||
-rw-r--r-- | sources/pyside2/tests/pysidetest/typesystem_pysidetest.xml | 7 |
10 files changed, 223 insertions, 20 deletions
diff --git a/sources/pyside2/tests/QtCore/attr_cache_py3k.py b/sources/pyside2/tests/QtCore/attr_cache_py3k.py index ec0575b02..f9761a9d3 100644 --- a/sources/pyside2/tests/QtCore/attr_cache_py3k.py +++ b/sources/pyside2/tests/QtCore/attr_cache_py3k.py @@ -56,9 +56,9 @@ class A(QObject): def test(cls): cls.instance cls.instance = cls() - assert "<__main__.A object " in repr(cls.__dict__['instance']) - assert "<__main__.A object " in repr(cls.instance) - assert "<__main__.A object " in repr(type.__getattribute__(cls, 'instance')) + assert "<__main__.A(0x" in repr(cls.__dict__['instance']) + assert "<__main__.A(0x" in repr(cls.instance) + assert "<__main__.A(0x" in repr(type.__getattribute__(cls, 'instance')) if __name__ == "__main__": diff --git a/sources/pyside2/tests/QtCore/qsettings_test.py b/sources/pyside2/tests/QtCore/qsettings_test.py index 6d64b0db3..36a4c3c62 100644 --- a/sources/pyside2/tests/QtCore/qsettings_test.py +++ b/sources/pyside2/tests/QtCore/qsettings_test.py @@ -55,15 +55,55 @@ class TestQSettings(unittest.TestCase): def testDefaultValueConversion(self): settings = QSettings('foo.ini', QSettings.IniFormat) - r = settings.value('lala', 22) + settings.setValue('zero_value', 0) + settings.setValue('empty_list', []) + settings.setValue('bool1', False) + settings.setValue('bool2', True) + del settings + + # Loading values already set + settings = QSettings('foo.ini', QSettings.IniFormat) + + # Getting value that doesn't exist + r = settings.value("variable") + self.assertEqual(type(r), type(None)) + + # Handling zero value + r = settings.value('zero_value') if py3k.IS_PY3K: self.assertEqual(type(r), int) else: self.assertEqual(type(r), long) - r = settings.value('lala', 22, type=str) - self.assertEqual(type(r), str) + r = settings.value('zero_value', type=int) + self.assertEqual(type(r), int) + + # Empty list + r = settings.value('empty_list') + self.assertTrue(len(r) == 0) + self.assertEqual(type(r), list) + + r = settings.value('empty_list', type=list) + self.assertTrue(len(r) == 0) + self.assertEqual(type(r), list) + + # Booleans + r = settings.value('bool1') + self.assertEqual(type(r), bool) + + r = settings.value('bool2') + self.assertEqual(type(r), bool) + + r = settings.value('bool1', type=bool) + self.assertEqual(type(r), bool) + + r = settings.value('bool2', type=int) + self.assertEqual(type(r), int) + + r = settings.value('bool2', type=bool) + self.assertEqual(type(r), bool) + # Not set variable, but with default value r = settings.value('lala', 22, type=bytes) self.assertEqual(type(r), bytes) diff --git a/sources/pyside2/tests/QtCore/qslot_object_test.py b/sources/pyside2/tests/QtCore/qslot_object_test.py index b8d5513ff..7a2691a06 100644 --- a/sources/pyside2/tests/QtCore/qslot_object_test.py +++ b/sources/pyside2/tests/QtCore/qslot_object_test.py @@ -31,7 +31,10 @@ import unittest from PySide2 import QtCore -global qApp +""" +This is a simple slot test that was updated to use the qApp "macro". +It is implicitly in builtins and does not need an import. +""" class objTest(QtCore.QObject): @@ -41,21 +44,15 @@ class objTest(QtCore.QObject): self.ok = False def slot(self): - global qApp - self.ok = True qApp.quit() - class slotTest(unittest.TestCase): def quit_app(self): - global qApp - qApp.quit() def testBasic(self): - global qApp timer = QtCore.QTimer() timer.setInterval(100) @@ -71,6 +68,5 @@ class slotTest(unittest.TestCase): if __name__ == '__main__': - global qApp - qApp = QtCore.QCoreApplication([]) + QtCore.QCoreApplication() unittest.main() diff --git a/sources/pyside2/tests/QtWidgets/bug_862.py b/sources/pyside2/tests/QtWidgets/bug_862.py index ac0325536..4621fc3b4 100644 --- a/sources/pyside2/tests/QtWidgets/bug_862.py +++ b/sources/pyside2/tests/QtWidgets/bug_862.py @@ -26,6 +26,29 @@ ## ############################################################################# + +# +# Test for bug 862, original description was: +# +# print seems to be broken at least for QGraphicsItems-derived objects. The +# attached code shows: +# +# <__main__.MyQObject object at 0xf99f38> +# <__main__.MyQWidget object at 0xf99f38> +# <PySide.QtGui.MyQGraphicsObject (this = 0x11c0d60 , parent = 0x0 , pos = +# QPointF(0, 0) , z = 0 , flags = ( ) ) at 0xf99f38> +# <PySide.QtGui.QGraphicsItem (this = 0x11c2e60 , parent = 0x0 , pos = QPointF(0, +# 0) , z = 0 , flags = ( ) ) at 0xf99f38> +# +# Where it should be showing something like: +# +# <__main__.MyQObject object at 0x7f55cf226c20> +# <__main__.MyQWidget object at 0x7f55cf226c20> +# <__main__.MyQGraphicsObject object at 0x7f55cf226c20> +# <__main__.MyQGraphicsItem object at 0x7f55cf226c20> +# + + from PySide2.QtCore import QObject from PySide2.QtWidgets import * import PySide2.QtCore @@ -53,14 +76,14 @@ class TestRepr (unittest.TestCase): app = QApplication([]) - self.assertEqual("<__main__.MyQObject object at ", repr(MyQObject())[:30]) - self.assertEqual("<__main__.MyQWidget object at ", repr(MyQWidget())[:30]) + self.assertEqual("<__main__.MyQObject(0x", repr(MyQObject())[:22]) + self.assertEqual("<__main__.MyQWidget(0x", repr(MyQWidget())[:22]) self.assertEqual("<__main__.MyQGraphicsObject(0x", repr(MyQGraphicsObject())[:30]) self.assertEqual("<__main__.MyQGraphicsItem(0x", repr(MyQGraphicsItem())[:28]) - self.assertEqual("<PySide2.QtCore.QObject object at ", repr(QObject())[:34]) - self.assertEqual("<PySide2.QtCore.QObject object at ", repr(PySide2.QtCore.QObject())[:34]) - self.assertEqual("<PySide2.QtWidgets.QWidget object at ", repr(QWidget())[:37]) + self.assertEqual("<PySide2.QtCore.QObject(0x", repr(QObject())[:26]) + self.assertEqual("<PySide2.QtCore.QObject(0x", repr(PySide2.QtCore.QObject())[:26]) + self.assertEqual("<PySide2.QtWidgets.QWidget(0x", repr(QWidget())[:29]) self.assertEqual("<PySide2.QtWidgets.QGraphicsWidget(0x", repr(QGraphicsWidget())[:37]) if __name__ == "__main__": diff --git a/sources/pyside2/tests/pysidetest/CMakeLists.txt b/sources/pyside2/tests/pysidetest/CMakeLists.txt index 3c993cf4e..b8ec54270 100644 --- a/sources/pyside2/tests/pysidetest/CMakeLists.txt +++ b/sources/pyside2/tests/pysidetest/CMakeLists.txt @@ -30,6 +30,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/testbinding/testobject_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/intvalue_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/pysidecpp_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/pysidecpp_testobjectwithnamespace_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/testbinding/pysidecpp_testobject2withnamespace_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/pysidecpp2_testobjectwithoutnamespace_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/testview_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/testbinding/testbinding_module_wrapper.cpp @@ -142,3 +143,4 @@ PYSIDE_TEST(signal_slot_warning.py) PYSIDE_TEST(all_modules_load_test.py) PYSIDE_TEST(qapp_like_a_macro_test.py) PYSIDE_TEST(embedding_test.py) +PYSIDE_TEST(repr_test.py) diff --git a/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py b/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py index c58aba82e..1c0d5d55d 100644 --- a/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py +++ b/sources/pyside2/tests/pysidetest/qapp_like_a_macro_test.py @@ -33,7 +33,10 @@ import PySide2 # It also uses the qApp variable to finish the instance and start over. class qAppMacroTest(unittest.TestCase): + _test_1093_is_first = True + def test_qApp_is_like_a_macro_and_can_restart(self): + self._test_1093_is_first = False from PySide2 import QtCore try: from PySide2 import QtGui, QtWidgets @@ -72,5 +75,20 @@ class qAppMacroTest(unittest.TestCase): # and they are again all the same self.assertTrue(qApp is QtCore.qApp is QtGui.qApp is QtWidgets.qApp) + def test_1093(self): + # Test that without creating a QApplication staticMetaObject still exists. + # Please see https://bugreports.qt.io/browse/PYSIDE-1093 for explanation. + # Note: This test must run first, otherwise we would be mislead! + assert self._test_1093_is_first + from PySide2 import QtCore + self.assertTrue(QtCore.QObject.staticMetaObject is not None) + app = QtCore.QCoreApplication.instance() + self.assertTrue(QtCore.QObject.staticMetaObject is not None) + if app is None: + app = QtCore.QCoreApplication([]) + self.assertTrue(QtCore.QObject.staticMetaObject is not None) + del __builtins__.qApp + + if __name__ == '__main__': unittest.main() diff --git a/sources/pyside2/tests/pysidetest/repr_test.py b/sources/pyside2/tests/pysidetest/repr_test.py new file mode 100644 index 000000000..295084f17 --- /dev/null +++ b/sources/pyside2/tests/pysidetest/repr_test.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- + +############################################################################# +## +## Copyright (C) 2019 The Qt Company Ltd. +## Copyright (C) 2019 Andreas Beckermann +## Contact: https://www.qt.io/licensing/ +## +## This file is part of Qt for Python. +## +## $QT_BEGIN_LICENSE:LGPL$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU Lesser General Public License Usage +## Alternatively, this file may be used under the terms of the GNU Lesser +## General Public License version 3 as published by the Free Software +## Foundation and appearing in the file LICENSE.LGPL3 included in the +## packaging of this file. Please review the following information to +## ensure the GNU Lesser General Public License version 3 requirements +## will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 2.0 or (at your option) the GNU General +## Public license version 3 or any later version approved by the KDE Free +## Qt Foundation. The licenses are as published by the Free Software +## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-2.0.html and +## https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from testbinding import PySideCPP, TestObject + +class QObjectDerivedReprTest(unittest.TestCase): + """Test the __repr__ implementation of QObject derived classes""" + + def testReprWithoutNamespace(self): + """Test that classes outside a namespace that have a operator<<(QDebug,...) defined use that + for __repr__""" + t = TestObject(123) + + # We don't define __str__, so str(q) should call __repr__ + self.assertEqual(t.__repr__(), str(t)) + + # __repr__ should use the operator<<(QDebug,...) implementation + self.assertIn('TestObject(id=123)', str(t)) + + def testReprWithNamespace(self): + """Test that classes inside a namespace that have a operator<<(QDebug,...) defined use that + for __repr__""" + t = PySideCPP.TestObjectWithNamespace(None) + + # We don't define __str__, so str(q) should call __repr__ + self.assertEqual(t.__repr__(), str(t)) + + # __repr__ should use the operator<<(QDebug,...) implementation + self.assertIn('TestObjectWithNamespace("TestObjectWithNamespace")', str(t)) + + def testReprInject(self): + """Test that injecting __repr__ via typesystem overrides the operator<<(QDebug, ...)""" + t = PySideCPP.TestObject2WithNamespace(None) + + # We don't define __str__, so str(q) should call __repr__ + self.assertEqual(t.__repr__(), str(t)) + + # __repr__ should use the operator<<(QDebug,...) implementation + self.assertEqual(str(t), "TestObject2WithNamespace(injected_repr)") + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/pyside2/tests/pysidetest/testobject.cpp b/sources/pyside2/tests/pysidetest/testobject.cpp index 03a7a965c..441ae872f 100644 --- a/sources/pyside2/tests/pysidetest/testobject.cpp +++ b/sources/pyside2/tests/pysidetest/testobject.cpp @@ -52,3 +52,25 @@ void TestObject::emitSignalWithTypedefValue(int value) { emit signalWithTypedefValue(TypedefValue(value)); } + +QDebug operator<<(QDebug dbg, TestObject& testObject) +{ + QDebugStateSaver saver(dbg); + dbg.nospace() << "TestObject(id=" << testObject.idValue() << ") "; + return dbg; +} + +namespace PySideCPP { + QDebug operator<<(QDebug dbg, TestObjectWithNamespace& testObject) + { + QDebugStateSaver saver(dbg); + dbg.nospace() << "TestObjectWithNamespace(" << testObject.name() << ") "; + return dbg; + } + QDebug operator<<(QDebug dbg, TestObject2WithNamespace& testObject) + { + QDebugStateSaver saver(dbg); + dbg.nospace() << "TestObject2WithNamespace(" << testObject.name() << ") "; + return dbg; + } +} diff --git a/sources/pyside2/tests/pysidetest/testobject.h b/sources/pyside2/tests/pysidetest/testobject.h index 6cfb01101..f8a174d46 100644 --- a/sources/pyside2/tests/pysidetest/testobject.h +++ b/sources/pyside2/tests/pysidetest/testobject.h @@ -33,6 +33,7 @@ #include <QApplication> #include <QMetaType> #include <QVariant> +#include <QDebug> #ifdef pysidetest_EXPORTS #define PYSIDE_EXPORTS 1 #endif @@ -81,6 +82,7 @@ private: int m_idValue; QList<QObject*> m_children; }; +PYSIDE_API QDebug operator<<(QDebug dbg, TestObject &testObject); typedef int PySideInt; @@ -104,6 +106,16 @@ signals: void emitSignalWithNamespace(PySideCPP::TestObjectWithNamespace* obj); void emitSignalWithTypedef(PySideInt val); }; +PYSIDE_API QDebug operator<<(QDebug dbg, TestObjectWithNamespace &testObject); + +class PYSIDE_API TestObject2WithNamespace : public QObject +{ + Q_OBJECT +public: + TestObject2WithNamespace(QObject* parent) : QObject(parent) {} + QString name() { return "TestObject2WithNamespace"; } +}; +PYSIDE_API QDebug operator<<(QDebug dbg, TestObject2WithNamespace& testObject); } // Namespace PySideCPP diff --git a/sources/pyside2/tests/pysidetest/typesystem_pysidetest.xml b/sources/pyside2/tests/pysidetest/typesystem_pysidetest.xml index 1904f236f..1e777edd2 100644 --- a/sources/pyside2/tests/pysidetest/typesystem_pysidetest.xml +++ b/sources/pyside2/tests/pysidetest/typesystem_pysidetest.xml @@ -37,6 +37,13 @@ <namespace-type name="PySideCPP"> <object-type name="TestObjectWithNamespace"/> + <object-type name="TestObject2WithNamespace"> + <add-function signature="__repr__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + %PYARG_0 = Shiboken::String::fromCString("TestObject2WithNamespace(injected_repr)"); + </inject-code> + </add-function> + </object-type> </namespace-type> <namespace-type name="PySideCPP2" generate="no"> |