diff options
author | Christian Tismer <tismer@stackless.com> | 2020-07-15 15:39:48 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2020-07-24 01:19:21 +0200 |
commit | 2d44c85faa01c7e805ff27bac4e3e1574ab0f5d3 (patch) | |
tree | c0a5359f6a6d001aac596af5b3e520802beef114 /sources/pyside2/tests | |
parent | b429d2a06bf5acb4b44cd0c7bd599c6d4cc7ebae (diff) |
feature-select: allow snake_case instead of camelCase for methods
This is the implementation of the first of a series of dynamically
selectable features.
The decision depends of the following setting at the beginning of
a module after PySide2 import:
from __feature__ import snake_case
For more info, see the Jira issue, section
The Principle Of Selectable Features In PySide
The crucial problems that are now solved were:
- it is not sufficient to patch a type dict, instead the whole
`tp_mro` must be walked to rename everything.
- tp_getattro must be changed for every existing type. This
is done either in shiboken by a changed PyObject_GenericGetAttr
or PyObject_SenericGetAttr, or in the generated tp_(get|set)attro
functions.
An example is included in sources/pyside2/doc/tutorial/expenses.
Task-number: PYSIDE-1019
Change-Id: I5f103190be2c884b0b4ad806187f3fef8e6598c9
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside2/tests')
-rw-r--r-- | sources/pyside2/tests/QtCore/CMakeLists.txt | 3 | ||||
-rw-r--r-- | sources/pyside2/tests/QtCore/multiple_feature_test.py (renamed from sources/pyside2/tests/QtCore/feature_test.py) | 36 | ||||
-rw-r--r-- | sources/pyside2/tests/QtCore/snake_case_feature_test.py | 86 |
3 files changed, 117 insertions, 8 deletions
diff --git a/sources/pyside2/tests/QtCore/CMakeLists.txt b/sources/pyside2/tests/QtCore/CMakeLists.txt index 771e1aeef..0c89f0d03 100644 --- a/sources/pyside2/tests/QtCore/CMakeLists.txt +++ b/sources/pyside2/tests/QtCore/CMakeLists.txt @@ -37,12 +37,12 @@ PYSIDE_TEST(deletelater_test.py) PYSIDE_TEST(destroysignal_test.py) PYSIDE_TEST(duck_punching_test.py) PYSIDE_TEST(emoji_string_test.py) -PYSIDE_TEST(feature_test.py) PYSIDE_TEST(hash_test.py) PYSIDE_TEST(inherits_test.py) PYSIDE_TEST(max_signals.py) PYSIDE_TEST(missing_symbols_test.py) PYSIDE_TEST(mockclass_test.py) +PYSIDE_TEST(multiple_feature_test.py) PYSIDE_TEST(python_conversion.py) PYSIDE_TEST(qabs_test.py) PYSIDE_TEST(qabstractitemmodel_test.py) @@ -128,6 +128,7 @@ PYSIDE_TEST(quuid_test.py) PYSIDE_TEST(qversionnumber_test.py) PYSIDE_TEST(repr_test.py) PYSIDE_TEST(setprop_on_ctor_test.py) +PYSIDE_TEST(snake_case_feature_test.py) PYSIDE_TEST(staticMetaObject_test.py) PYSIDE_TEST(static_method_test.py) PYSIDE_TEST(thread_signals_test.py) diff --git a/sources/pyside2/tests/QtCore/feature_test.py b/sources/pyside2/tests/QtCore/multiple_feature_test.py index cf1e8c3f2..26488326c 100644 --- a/sources/pyside2/tests/QtCore/feature_test.py +++ b/sources/pyside2/tests/QtCore/multiple_feature_test.py @@ -37,6 +37,8 @@ ## ############################################################################# +from __future__ import print_function, absolute_import + import os import sys import unittest @@ -50,12 +52,13 @@ from PySide2.support.__feature__ import _really_all_feature_names from textwrap import dedent """ -feature_test.py --------------- +multiple_feature_test.py +------------------------ This tests the selectable features in PySide. -There are no real features implemented. They will be added, later. +The first feature is `snake_case` instead of `camelCase`. +There is much more to come. """ class FeaturesTest(unittest.TestCase): @@ -66,9 +69,27 @@ class FeaturesTest(unittest.TestCase): """ global __name__ - for bit in range(8): + def tst_bit0(flag, self): + if flag == 0: + QtCore.QCborArray.isEmpty + QtCore.QCborArray.__dict__["isEmpty"] + with self.assertRaises(AttributeError): + QtCore.QCborArray.is_empty + with self.assertRaises(KeyError): + QtCore.QCborArray.__dict__["is_empty"] + else: + QtCore.QCborArray.is_empty + QtCore.QCborArray.__dict__["is_empty"] + with self.assertRaises(AttributeError): + QtCore.QCborArray.isEmpty + with self.assertRaises(KeyError): + QtCore.QCborArray.__dict__["isEmpty"] + + edict = {} + for bit in range(1, 8): # We are cheating here, since the functions are in the globals. - exec(dedent(""" + + eval(compile(dedent(""" def tst_bit{0}(flag, self): if flag == 0: @@ -80,7 +101,8 @@ class FeaturesTest(unittest.TestCase): QtCore.QCborArray.fake_feature_{1:02x} QtCore.QCborArray.__dict__["fake_feature_{1:02x}"] - """.format(bit, 1 << bit)), globals(), globals()) + """).format(bit, 1 << bit), "<string>", "exec"), globals(), edict) + globals().update(edict) feature_list = _really_all_feature_names func_list = [tst_bit0, tst_bit1, tst_bit2, tst_bit3, tst_bit4, tst_bit5, tst_bit6, tst_bit7] @@ -95,7 +117,7 @@ class FeaturesTest(unittest.TestCase): feature = feature_list[bit] text = "from __feature__ import {}".format(feature) print(text) - exec(text) + eval(compile(text, "<string>", "exec"), globals(), edict) for bit in range(8): value = idx & 1 << bit func_list[bit](value, self=self) diff --git a/sources/pyside2/tests/QtCore/snake_case_feature_test.py b/sources/pyside2/tests/QtCore/snake_case_feature_test.py new file mode 100644 index 000000000..b7f23396e --- /dev/null +++ b/sources/pyside2/tests/QtCore/snake_case_feature_test.py @@ -0,0 +1,86 @@ +############################################################################# +## +## Copyright (C) 2020 The Qt Company Ltd. +## 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 os +import sys +import unittest + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from init_paths import init_test_paths +init_test_paths(False) + +from PySide2 import QtWidgets + +""" +snake_case_feature_test.py +-------------------------- + +Test the snake_case feature. + +This works now. More tests needed! +""" + +class RenamingTest(unittest.TestCase): + def setUp(self): + qApp or QtWidgets.QApplication() + + def tearDown(self): + qApp.shutdown() + + def testRenamedFunctions(self): + + class Window(QtWidgets.QWidget): + def __init__(self): + super(Window, self).__init__() + + window = Window() + window.setWindowTitle('camelCase') + + # and now the same with snake_case enabled + from __feature__ import snake_case + + class Window(QtWidgets.QWidget): + def __init__(self): + super(Window, self).__init__() + + window = Window() + window.set_window_title('snake_case') + +if __name__ == '__main__': + unittest.main() |