diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-06-10 14:50:27 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-06-21 11:06:37 +0200 |
commit | b70d8b334c4f714a046d72ea0a5f1a8378fd55b9 (patch) | |
tree | c715f0ee50052f6f668ee9f2566456b1b0cbd809 /tools | |
parent | dedfebbc438ed2f5f6869092ee10e68f9ffd8a36 (diff) |
qtpy2cpp: Set up test infrastructure
Add test code and move the files to baseline.
Task-number: PYSIDE-1945
Pick-to: 6.3
Change-Id: I57975c774d53a4dc5f77ca671652ca32c04809de
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qtpy2cpp.pyproject | 3 | ||||
-rw-r--r-- | tools/qtpy2cpp_lib/test_baseline/basic_test.py | 17 | ||||
-rw-r--r-- | tools/qtpy2cpp_lib/test_baseline/uic.py | 182 | ||||
-rw-r--r-- | tools/qtpy2cpp_lib/tests/baseline/basic_test.cpp | 62 | ||||
-rw-r--r-- | tools/qtpy2cpp_lib/tests/baseline/basic_test.py | 44 | ||||
-rw-r--r-- | tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py | 54 |
6 files changed, 162 insertions, 200 deletions
diff --git a/tools/qtpy2cpp.pyproject b/tools/qtpy2cpp.pyproject index f519a4da4..a059aebca 100644 --- a/tools/qtpy2cpp.pyproject +++ b/tools/qtpy2cpp.pyproject @@ -2,5 +2,6 @@ "files": ["qtpy2cpp.py", "qtpy2cpp_lib/formatter.py", "qtpy2cpp_lib/visitor.py", "qtpy2cpp_lib/nodedump.py", "qtpy2cpp_lib/astdump.py", "qtpy2cpp_lib/tokenizer.py", "qtpy2cpp_lib/qt.py", - "qtpy2cpp_lib/test_baseline/basic_test.py", "qtpy2cpp_lib/test_baseline/uic.py"] + "qtpy2cpp_lib/tests/test_qtpy2cpp.py", + "qtpy2cpp_lib/tests/baseline/basic_test.py", "qtpy2cpp_lib/tests/baseline/uic.py"] } diff --git a/tools/qtpy2cpp_lib/test_baseline/basic_test.py b/tools/qtpy2cpp_lib/test_baseline/basic_test.py deleted file mode 100644 index 345eeb4bb..000000000 --- a/tools/qtpy2cpp_lib/test_baseline/basic_test.py +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -a = 7 - -if a > 5: - for f in [1, 2]: - print(f) -elif a == 2: - print(2) -elif not a: - print(0) -else: - for i in range(5): - print(i) - for i in range(2, 5): - print(i) diff --git a/tools/qtpy2cpp_lib/test_baseline/uic.py b/tools/qtpy2cpp_lib/test_baseline/uic.py deleted file mode 100644 index 7327104b1..000000000 --- a/tools/qtpy2cpp_lib/test_baseline/uic.py +++ /dev/null @@ -1,182 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -from PySide6.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint, - QRect, QSize, QUrl, Qt) -from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QFont, - QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap, - QRadialGradient) -from PySide6.QtWidgets import * - -class Ui_ImageDialog(object): - def setupUi(self, dialog): - if dialog.objectName(): - dialog.setObjectName(u"dialog") - dialog.setObjectName(u"ImageDialog") - dialog.resize(320, 180) - self.vboxLayout = QVBoxLayout(dialog) -#ifndef Q_OS_MAC - self.vboxLayout.setSpacing(6) -#endif -#ifndef Q_OS_MAC - self.vboxLayout.setContentsMargins(9, 9, 9, 9) -#endif - self.vboxLayout.setObjectName(u"vboxLayout") - self.vboxLayout.setObjectName(u"") - self.gridLayout = QGridLayout() -#ifndef Q_OS_MAC - self.gridLayout.setSpacing(6) -#endif - self.gridLayout.setContentsMargins(1, 1, 1, 1) - self.gridLayout.setObjectName(u"gridLayout") - self.gridLayout.setObjectName(u"") - self.widthLabel = QLabel(dialog) - self.widthLabel.setObjectName(u"widthLabel") - self.widthLabel.setObjectName(u"widthLabel") - self.widthLabel.setGeometry(QRect(1, 27, 67, 22)) - self.widthLabel.setFrameShape(QFrame.NoFrame) - self.widthLabel.setFrameShadow(QFrame.Plain) - self.widthLabel.setTextFormat(Qt.AutoText) - - self.gridLayout.addWidget(self.widthLabel, 1, 0, 1, 1) - - self.heightLabel = QLabel(dialog) - self.heightLabel.setObjectName(u"heightLabel") - self.heightLabel.setObjectName(u"heightLabel") - self.heightLabel.setGeometry(QRect(1, 55, 67, 22)) - self.heightLabel.setFrameShape(QFrame.NoFrame) - self.heightLabel.setFrameShadow(QFrame.Plain) - self.heightLabel.setTextFormat(Qt.AutoText) - - self.gridLayout.addWidget(self.heightLabel, 2, 0, 1, 1) - - self.colorDepthCombo = QComboBox(dialog) - self.colorDepthCombo.setObjectName(u"colorDepthCombo") - self.colorDepthCombo.setObjectName(u"colorDepthCombo") - self.colorDepthCombo.setGeometry(QRect(74, 83, 227, 22)) - sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed) - sizePolicy.setHorizontalStretch(0) - sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.colorDepthCombo.sizePolicy().hasHeightForWidth()) - self.colorDepthCombo.setSizePolicy(sizePolicy) - self.colorDepthCombo.setInsertPolicy(QComboBox.InsertAtBottom) - - self.gridLayout.addWidget(self.colorDepthCombo, 3, 1, 1, 1) - - self.nameLineEdit = QLineEdit(dialog) - self.nameLineEdit.setObjectName(u"nameLineEdit") - self.nameLineEdit.setObjectName(u"nameLineEdit") - self.nameLineEdit.setGeometry(QRect(74, 83, 227, 22)) - sizePolicy1 = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed) - sizePolicy1.setHorizontalStretch(1) - sizePolicy1.setVerticalStretch(0) - sizePolicy1.setHeightForWidth(self.nameLineEdit.sizePolicy().hasHeightForWidth()) - self.nameLineEdit.setSizePolicy(sizePolicy1) - self.nameLineEdit.setEchoMode(QLineEdit.Normal) - - self.gridLayout.addWidget(self.nameLineEdit, 0, 1, 1, 1) - - self.spinBox = QSpinBox(dialog) - self.spinBox.setObjectName(u"spinBox") - self.spinBox.setObjectName(u"spinBox") - self.spinBox.setGeometry(QRect(74, 1, 227, 20)) - sizePolicy.setHeightForWidth(self.spinBox.sizePolicy().hasHeightForWidth()) - self.spinBox.setSizePolicy(sizePolicy) - self.spinBox.setButtonSymbols(QAbstractSpinBox.UpDownArrows) - self.spinBox.setValue(32) - self.spinBox.setMaximum(1024) - self.spinBox.setMinimum(1) - - self.gridLayout.addWidget(self.spinBox, 1, 1, 1, 1) - - self.spinBox_2 = QSpinBox(dialog) - self.spinBox_2.setObjectName(u"spinBox_2") - self.spinBox_2.setObjectName(u"spinBox_2") - self.spinBox_2.setGeometry(QRect(74, 27, 227, 22)) - sizePolicy.setHeightForWidth(self.spinBox_2.sizePolicy().hasHeightForWidth()) - self.spinBox_2.setSizePolicy(sizePolicy) - self.spinBox_2.setButtonSymbols(QAbstractSpinBox.UpDownArrows) - self.spinBox_2.setValue(32) - self.spinBox_2.setMaximum(1024) - self.spinBox_2.setMinimum(1) - - self.gridLayout.addWidget(self.spinBox_2, 2, 1, 1, 1) - - self.nameLabel = QLabel(dialog) - self.nameLabel.setObjectName(u"nameLabel") - self.nameLabel.setObjectName(u"nameLabel") - self.nameLabel.setGeometry(QRect(1, 1, 67, 20)) - self.nameLabel.setFrameShape(QFrame.NoFrame) - self.nameLabel.setFrameShadow(QFrame.Plain) - self.nameLabel.setTextFormat(Qt.AutoText) - - self.gridLayout.addWidget(self.nameLabel, 0, 0, 1, 1) - - self.colorDepthLabel = QLabel(dialog) - self.colorDepthLabel.setObjectName(u"colorDepthLabel") - self.colorDepthLabel.setObjectName(u"colorDepthLabel") - self.colorDepthLabel.setGeometry(QRect(1, 83, 67, 22)) - self.colorDepthLabel.setFrameShape(QFrame.NoFrame) - self.colorDepthLabel.setFrameShadow(QFrame.Plain) - self.colorDepthLabel.setTextFormat(Qt.AutoText) - - self.gridLayout.addWidget(self.colorDepthLabel, 3, 0, 1, 1) - - - self.vboxLayout.addLayout(self.gridLayout) - - self.spacerItem = QSpacerItem(0, 0, QSizePolicy.Minimum, QSizePolicy.Expanding) - - self.vboxLayout.addItem(self.spacerItem) - - self.hboxLayout = QHBoxLayout() -#ifndef Q_OS_MAC - self.hboxLayout.setSpacing(6) -#endif - self.hboxLayout.setContentsMargins(1, 1, 1, 1) - self.hboxLayout.setObjectName(u"hboxLayout") - self.hboxLayout.setObjectName(u"") - self.spacerItem1 = QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum) - - self.hboxLayout.addItem(self.spacerItem1) - - self.okButton = QPushButton(dialog) - self.okButton.setObjectName(u"okButton") - self.okButton.setObjectName(u"okButton") - self.okButton.setGeometry(QRect(135, 1, 80, 24)) - - self.hboxLayout.addWidget(self.okButton) - - self.cancelButton = QPushButton(dialog) - self.cancelButton.setObjectName(u"cancelButton") - self.cancelButton.setObjectName(u"cancelButton") - self.cancelButton.setGeometry(QRect(221, 1, 80, 24)) - - self.hboxLayout.addWidget(self.cancelButton) - - - self.vboxLayout.addLayout(self.hboxLayout) - - QWidget.setTabOrder(self.nameLineEdit, self.spinBox) - QWidget.setTabOrder(self.spinBox, self.spinBox_2) - QWidget.setTabOrder(self.spinBox_2, self.colorDepthCombo) - QWidget.setTabOrder(self.colorDepthCombo, self.okButton) - QWidget.setTabOrder(self.okButton, self.cancelButton) - - self.retranslateUi(dialog) - self.nameLineEdit.returnPressed.connect(self.okButton.animateClick) - - QMetaObject.connectSlotsByName(dialog) - # setupUi - - def retranslateUi(self, dialog): - dialog.setWindowTitle(QCoreApplication.translate("ImageDialog", u"Create Image", None)) - self.widthLabel.setText(QCoreApplication.translate("ImageDialog", u"Width:", None)) - self.heightLabel.setText(QCoreApplication.translate("ImageDialog", u"Height:", None)) - self.nameLineEdit.setText(QCoreApplication.translate("ImageDialog", u"Untitled image", None)) - self.nameLabel.setText(QCoreApplication.translate("ImageDialog", u"Name:", None)) - self.colorDepthLabel.setText(QCoreApplication.translate("ImageDialog", u"Color depth:", None)) - self.okButton.setText(QCoreApplication.translate("ImageDialog", u"OK", None)) - self.cancelButton.setText(QCoreApplication.translate("ImageDialog", u"Cancel", None)) - # retranslateUi - diff --git a/tools/qtpy2cpp_lib/tests/baseline/basic_test.cpp b/tools/qtpy2cpp_lib/tests/baseline/basic_test.cpp new file mode 100644 index 000000000..8ee7be31e --- /dev/null +++ b/tools/qtpy2cpp_lib/tests/baseline/basic_test.cpp @@ -0,0 +1,62 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +// Converted from basic_test.py +#include <QtCore/Qt> +#include <QtGui/QColor> +#include <QtGui/QPainter> +#include <QtGui/QPaintEvent> +#include <QtGui/QShortcut> +#include <QtWidgets/QApplication> +#include <QtWidgets/QWidget> + +class Window : public QWidget +{ +public: + + Window(QWidget * parent = nullptr) + { + super()->__init__(parent); + } + + void paintEvent(QPaintEvent * e) + { + paint("bla"); + } + + void paint(const QString & what, color = Qt::blue) + { + { // Converted from context manager + p = QPainter(); + p->setPen(QColor(color)); + rect = rect(); + w = rect->width(); + h = rect->height(); + p->drawLine(0, 0, w, h); + p->drawLine(0, h, w, 0); + p->drawText(rect->center(), what); + } + } + + void sum() + { + values = {1, 2, 3}; + result = 0; + for (v: values) { + result += v + } + return result; + } +}; + +int main(int argc, char *argv[]) +{ + QApplication app(sys->argv); + window = Window(); + auto *sc = new QShortcut((Qt::CTRL | Qt::Key_Q), window); + sc->activated->connect(window->close); + window->setWindowTitle("Test"); + window->show(); + sys->exit(app.exec()); + return 0; +} diff --git a/tools/qtpy2cpp_lib/tests/baseline/basic_test.py b/tools/qtpy2cpp_lib/tests/baseline/basic_test.py new file mode 100644 index 000000000..10dc73767 --- /dev/null +++ b/tools/qtpy2cpp_lib/tests/baseline/basic_test.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +import sys + +from PySide6.QtCore import qVersion, Qt +from PySide6.QtGui import QColor, QPainter, QPaintEvent, QShortcut +from PySide6.QtWidgets import QApplication, QWidget + + +class Window(QWidget): + def __init__(self, parent: QWidget = None): + super().__init__(parent) + + def paintEvent(self, e: QPaintEvent): + self.paint("bla") + + def paint(self, what: str, color: Qt.GlobalColor = Qt.blue): + with QPainter(self) as p: + p.setPen(QColor(color)) + rect = self.rect() + w = rect.width() + h = rect.height() + p.drawLine(0, 0, w, h) + p.drawLine(0, h, w, 0) + p.drawText(rect.center(), what) + + def sum(self): + values = [1, 2, 3] + result = 0 + for v in values: + result += v + return result + + +if __name__ == '__main__': + app = QApplication(sys.argv) + window = Window() + sc = QShortcut(Qt.CTRL | Qt.Key_Q, window) + sc.activated.connect(window.close) + window.setWindowTitle("Test") + window.show() + sys.exit(app.exec()) diff --git a/tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py b/tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py new file mode 100644 index 000000000..f9f921705 --- /dev/null +++ b/tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py @@ -0,0 +1,54 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +import subprocess +import tempfile +import sys +from pathlib import Path + +# run pytest-3 + + +def diff_code(actual_code, expected_file): + """Helper to run diff if something fails (Linux only).""" + with tempfile.NamedTemporaryFile(suffix=".cpp") as tf: + tf.write(actual_code.encode('utf-8')) + tf.flush() + diff_cmd = ["diff", "-u", expected_file, tf.name] + subprocess.run(diff_cmd) + + +def run_converter(tool, file): + """Run the converter and return C++ code generated from file.""" + cmd = [sys.executable, tool, "--stdout", file] + output = "" + with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: + output_b, errors_b = proc.communicate() + output = output_b.decode('utf-8') + if errors_b: + print(errors_b.decode('utf-8'), file=sys.stderr) + return output + + +def test_examples(): + dir = Path(__file__).resolve().parent + tool = dir.parents[1] / "qtpy2cpp.py" + assert(tool.is_file) + for test_file in (dir / "baseline").glob("*.py"): + assert(test_file.is_file) + expected_file = test_file.parent / (test_file.stem + ".cpp") + if expected_file.is_file(): + actual_code = run_converter(tool, test_file) + assert(actual_code) + expected_code = expected_file.read_text() + # Strip the license + code_start = expected_code.find("// Converted from") + assert(code_start != -1) + expected_code = expected_code[code_start:] + + if actual_code != expected_code: + diff_code(actual_code, expected_file) + assert(actual_code == expected_code) + else: + print(f"Warning, {test_file} is missing a .cpp file.", + file=sys.stderr) |