diff options
Diffstat (limited to 'sources/pyside-tools/qtpy2cpp_lib/tests')
3 files changed, 162 insertions, 0 deletions
diff --git a/sources/pyside-tools/qtpy2cpp_lib/tests/baseline/basic_test.cpp b/sources/pyside-tools/qtpy2cpp_lib/tests/baseline/basic_test.cpp new file mode 100644 index 000000000..8ee7be31e --- /dev/null +++ b/sources/pyside-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/sources/pyside-tools/qtpy2cpp_lib/tests/baseline/basic_test.py b/sources/pyside-tools/qtpy2cpp_lib/tests/baseline/basic_test.py new file mode 100644 index 000000000..62805cd15 --- /dev/null +++ b/sources/pyside-tools/qtpy2cpp_lib/tests/baseline/basic_test.py @@ -0,0 +1,45 @@ +#!/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 +from __future__ import annotations + +import sys + +from PySide6.QtCore import 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/sources/pyside-tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py b/sources/pyside-tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py new file mode 100644 index 000000000..73dbd2e58 --- /dev/null +++ b/sources/pyside-tools/qtpy2cpp_lib/tests/test_qtpy2cpp.py @@ -0,0 +1,55 @@ +# 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 +from __future__ import annotations + +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) |