aboutsummaryrefslogtreecommitdiffstats
path: root/tools/snippets_translate/tests/test_converter.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/snippets_translate/tests/test_converter.py')
-rw-r--r--tools/snippets_translate/tests/test_converter.py144
1 files changed, 93 insertions, 51 deletions
diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py
index 5656ff5e8..084cc8a6d 100644
--- a/tools/snippets_translate/tests/test_converter.py
+++ b/tools/snippets_translate/tests/test_converter.py
@@ -1,45 +1,14 @@
-#############################################################################
-##
-## Copyright (C) 2021 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$
-##
-#############################################################################
+# 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 converter import snippet_translate as st
+def multi_st(lines):
+ result = [st(l) for l in lines.split("\n")]
+ return "\n".join(result)
+
+
def test_comments():
assert st("// This is a comment") == "# This is a comment"
assert st("// double slash // inside") == "# double slash // inside"
@@ -78,6 +47,7 @@ def test_and_or():
def test_while_if_elseif():
assert st("while(a)") == "while a:"
assert st("if (condition){") == "if condition:"
+ assert st(" if (condition){") == " if condition:"
assert st("} else if (a) {") == " elif a:"
assert (
st("if (!m_vbo.isCreated()) // init() failed,")
@@ -99,7 +69,11 @@ def test_else():
def test_new():
assert st("a = new Something(...);") == "a = Something(...)"
- assert st("a = new Something") == "a = Something"
+ assert st("a = new Something") == "a = Something()"
+ assert st("foo(new X, new Y(b), new Z)") == "foo(X(), Y(b), Z())"
+ # Class member initialization list
+ assert st("m_mem(new Something(p)),") == "m_mem(Something(p)),"
+ assert st("m_mem(new Something),") == "m_mem(Something()),"
def test_semicolon():
@@ -126,13 +100,36 @@ def test_cast():
st("elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;")
== "elapsed = (elapsed + QTimer(sender()).interval()) % 1000"
)
+ assert (
+ st("a = qobject_cast<type*>(data) * 9 + static_cast<int>(42)")
+ == "a = type(data) * 9 + int(42)"
+ )
def test_double_colon():
assert st("Qt::Align") == "Qt.Align"
assert st('QSound::play("mysounds/bells.wav");') == 'QSound.play("mysounds/bells.wav")'
- # FIXME
- assert st("Widget::method") == "Widget::method"
+ assert st("Widget::method") == "Widget.method"
+
+ # multiline statement connect statement
+ # eg: connect(reply, &QNetworkReply::errorOccurred,
+ # this, &MyClass::slotError);
+ assert st("this, &MyClass::slotError);") == "self.slotError)"
+
+
+def test_connects():
+ assert (
+ st("connect(button, &QPushButton::clicked, this, &MyClass::slotClicked);")
+ == "button.clicked.connect(self.slotClicked)"
+ )
+ assert (
+ st("connect(m_ui->button, &QPushButton::clicked, this, &MyClass::slotClicked);")
+ == "m_ui.button.clicked.connect(self.slotClicked)"
+ )
+ assert (
+ st("connect(button.get(), &QPushButton::clicked, this, &MyClass::slotClicked);")
+ == "button.clicked.connect(self.slotClicked)"
+ )
def test_cout_endl():
@@ -169,19 +166,25 @@ def test_cout_endl():
def test_variable_declaration():
assert st("QLabel label;") == "label = QLabel()"
- assert st('QLabel label("Hello")') == 'label = QLabel("Hello")'
+ assert st('QLabel label("Hello");') == 'label = QLabel("Hello")'
assert st("Widget w;") == "w = Widget()"
assert st('QLabel *label = new QLabel("Hello");') == 'label = QLabel("Hello")'
assert st('QLabel label = a_function("Hello");') == 'label = a_function("Hello")'
assert st('QString a = "something";') == 'a = "something"'
assert st("int var;") == "var = int()"
assert st("float v = 0.1;") == "v = 0.1"
- assert st("QSome<thing> var") == "var = QSome()"
+ assert st("QSome<thing> var;") == "var = QSome()"
assert st("QQueue<int> queue;") == "queue = QQueue()"
assert st("QVBoxLayout *layout = new QVBoxLayout;") == "layout = QVBoxLayout()"
assert st("QPointer<QLabel> label = new QLabel;") == "label = QLabel()"
assert st("QMatrix4x4 matrix;") == "matrix = QMatrix4x4()"
assert st("QList<QImage> collage =") == "collage ="
+ assert st("bool b = true;") == "b = True"
+ assert st("Q3DBars *m_graph = nullptr;") == "m_graph = None"
+ # Do not fall for member function definitions
+ assert st("Q3DBars *Graph::bars() const") == "Q3DBars Graph.bars()"
+ # Do not fall for member function declarations
+ assert st("virtual Q3DBars *bars();") == "virtual Q3DBars bars()"
def test_for():
@@ -223,7 +226,7 @@ def test_for():
assert st("for (QChar ch : s)") == "for ch in s:"
assert (
st("for (const QByteArray &ext : " "qAsConst(extensionList))")
- == "for ext in qAsConst(extensionList):"
+ == "for ext in extensionList:"
)
assert st("for (QTreeWidgetItem *item : found) {") == "for item in found:"
@@ -317,24 +320,24 @@ def test_constuctors():
def test_inheritance_init():
assert (
st(": QClass(fun(re, 1, 2), parent), a(1)")
- == " QClass.__init__(self, fun(re, 1, 2), parent)\n self.a = 1"
+ == " super().__init__(fun(re, 1, 2), parent)\n self.a = 1"
)
assert (
st(": QQmlNdefRecord(copyFooRecord(record), parent)")
- == " QQmlNdefRecord.__init__(self, copyFooRecord(record), parent)"
+ == " super().__init__(copyFooRecord(record), parent)"
)
assert (
st(" : QWidget(parent), helper(helper)")
- == " QWidget.__init__(self, parent)\n self.helper = helper"
+ == " super().__init__(parent)\n self.helper = helper"
)
- assert st(" : QWidget(parent)") == " QWidget.__init__(self, parent)"
+ assert st(" : QWidget(parent)") == " super().__init__(parent)"
assert (
st(": a(0), bB(99), cC2(1), p_S(10),")
== " self.a = 0\n self.bB = 99\n self.cC2 = 1\n self.p_S = 10"
)
assert (
st(": QAbstractFileEngineIterator(nameFilters, filters), index(0) ")
- == " QAbstractFileEngineIterator.__init__(self, nameFilters, filters)\n self.index = 0"
+ == " super().__init__(nameFilters, filters)\n self.index = 0"
)
assert (
st(": m_document(doc), m_text(text)") == " self.m_document = doc\n self.m_text = text"
@@ -344,7 +347,7 @@ def test_inheritance_init():
st(": option->palette.color(QPalette::Mid);")
== " self.option.palette.color = QPalette.Mid"
)
- assert st(": QSqlResult(driver) {}") == " QSqlResult.__init__(self, driver)"
+ assert st(": QSqlResult(driver) {}") == " super().__init__(driver)"
def test_arrays():
@@ -362,6 +365,7 @@ def test_functions():
st("QString myDecoderFunc(const QByteArray &localFileName);")
== "def myDecoderFunc(localFileName):"
)
+ assert st("return QModelIndex();") == "return QModelIndex()"
def test_foreach():
@@ -387,9 +391,16 @@ def test_ternary_operator():
== "if not game.saveGame(json if Game.Json else Game.Binary):"
)
+
def test_useless_qt_classes():
assert st('result += QLatin1String("; ");') == 'result += "; "'
+ assert st('result += QString::fromLatin1("; ");') == 'result += "; "'
+ assert (
+ st('result = QStringLiteral("A") + QStringLiteral("B");')
+ == 'result = "A" + "B"')
assert st("<< QLatin1Char('\0') << endl;") == "print('\0')"
+ assert st('result = u"A"_s;') == 'result = "A"'
+
def test_special_cases():
assert (
@@ -403,7 +414,7 @@ def test_special_cases():
)
assert (
st("QObject::connect(&window1, &Window::messageSent,")
- == "QObject.connect(window1, Window.messageSent,"
+ == "window1.messageSent.connect("
)
assert st("double num;") == "num = float()"
@@ -411,6 +422,8 @@ def test_special_cases():
assert st("public:") == "# public"
assert st("private:") == "# private"
+ #iterator declaration
+ assert st("std::vector<int>::iterator i;") == ""
# TODO: Handle the existing ones with Python equivalents
# assert st("std::...")
@@ -434,6 +447,35 @@ def test_lambdas():
pass
+def test_switch_case():
+ source = """switch (v) {
+case 1:
+ f1();
+ break;
+case ClassName::EnumValue:
+ f2();
+ break;
+default:
+ f3();
+ break;
+}
+"""
+ expected = """
+if v == 1:
+ f1()
+ break
+elif v == ClassName.EnumValue:
+ f2()
+ break
+else:
+ f3()
+ break
+
+"""
+
+ assert multi_st(source) == expected
+
+
def test_std_function():
# std::function<QImage(const QImage &)> scale = [](const QImage &img) {
pass