From 68a44df8e833aa5f48e76b6da9bbdda60e796cc1 Mon Sep 17 00:00:00 2001 From: Chih-Hsuan Yen Date: Mon, 29 Aug 2022 17:30:07 +0800 Subject: [PATCH] Add tests Also extracts parseCommand() to a separate file for simpler testing setup. --- CMakeLists.txt | 11 +++++++ src/main.cpp | 44 +--------------------------- src/qterminalutils.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ src/qterminalutils.h | 26 +++++++++++++++++ test/CMakeLists.txt | 7 +++++ test/qterminal_test.cpp | 53 ++++++++++++++++++++++++++++++++++ test/qterminal_test.h | 32 +++++++++++++++++++++ 7 files changed, 194 insertions(+), 43 deletions(-) create mode 100644 src/qterminalutils.cpp create mode 100644 src/qterminalutils.h create mode 100644 test/CMakeLists.txt create mode 100644 test/qterminal_test.cpp create mode 100644 test/qterminal_test.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f6fd520..0c85875a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ include(GNUInstallDirs) set(QTERMINAL_VERSION "1.1.0") option(UPDATE_TRANSLATIONS "Update source translation translations/*.ts files" OFF) +option(BUILD_TESTS "Builds tests" ON) if(APPLE) option(APPLEBUNDLE "Build as qterminal.app bundle" ON) @@ -36,6 +37,10 @@ endif() find_package(QTermWidget5 ${QTERMWIDGET_MINIMUM_VERSION} REQUIRED) find_package(lxqt-build-tools ${LXQTBT_MINIMUM_VERSION} REQUIRED) +if (BUILD_TESTS) + find_package(Qt5 ${QT_MINIMUM_VERSION} CONFIG REQUIRED Test) +endif() + include(LXQtPreventInSourceBuilds) include(LXQtTranslateTs) include(LXQtTranslateDesktop) @@ -75,6 +80,7 @@ set(QTERM_SRC src/fontdialog.cpp src/dbusaddressable.cpp src/tab-switcher.cpp + src/qterminalutils.cpp ) set(QTERM_MOC_SRC @@ -265,3 +271,8 @@ else() COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}.app/Contents/Resources/empty.lproj" COMMENT "Creating Resources/empty.lproj") endif() + +if(BUILD_TESTS) + enable_testing() + add_subdirectory(test) +endif() diff --git a/src/main.cpp b/src/main.cpp index 52a78504..23ce9173 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,7 @@ #include "mainwindow.h" #include "qterminalapp.h" +#include "qterminalutils.h" #include "terminalconfig.h" #define out @@ -72,49 +73,6 @@ QTerminalApp * QTerminalApp::m_instance = nullptr; exit(code); } -QStringList parse_command(const QString& str) -{ - const QRegularExpression separator(QString::fromLatin1(R"('|(? index) - { - list << str.mid(index, nextIndex - index).replace(escapedSpace, QStringLiteral(" ")); - } - if (match.capturedLength() == 0) - { // end of string ("\z") is matched - break; - } - index = nextIndex + match.capturedLength(); - auto c = str.at(index - 1); // last matched character - if (!c.isSpace()) - { // a single quote or an unescaped double quote is matched - nextIndex = str.indexOf(c == QLatin1Char('\'') ? singleQuote : doubleQuote, index, &match); - if (nextIndex == -1) - { // the quote is not closed - break; - } - else - { - if (nextIndex > index) - { - list << str.mid(index, nextIndex - index).replace(escapedSpace, QStringLiteral(" ")); - } - index = nextIndex + match.capturedLength(); - } - } - } - return list; -} - void parse_args(int argc, char* argv[], QString& workdir, QStringList & shell_command, out bool& dropMode) { int next_option; diff --git a/src/qterminalutils.cpp b/src/qterminalutils.cpp new file mode 100644 index 00000000..62ce95e2 --- /dev/null +++ b/src/qterminalutils.cpp @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (C) 2022 by LXQt team * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ***************************************************************************/ + +#include + +#include "qterminalutils.h" + +QStringList parse_command(const QString& str) +{ + const QRegularExpression separator(QString::fromLatin1(R"('|(? index) + { + list << str.mid(index, nextIndex - index).replace(escapedSpace, QStringLiteral(" ")); + } + if (match.capturedLength() == 0) + { // end of string ("\z") is matched + break; + } + index = nextIndex + match.capturedLength(); + auto c = str.at(index - 1); // last matched character + if (!c.isSpace()) + { // a single quote or an unescaped double quote is matched + nextIndex = str.indexOf(c == QLatin1Char('\'') ? singleQuote : doubleQuote, index, &match); + if (nextIndex == -1) + { // the quote is not closed + break; + } + else + { + if (nextIndex > index) + { + list << str.mid(index, nextIndex - index).replace(escapedSpace, QStringLiteral(" ")); + } + index = nextIndex + match.capturedLength(); + } + } + } + return list; +} + diff --git a/src/qterminalutils.h b/src/qterminalutils.h new file mode 100644 index 00000000..0b8d782c --- /dev/null +++ b/src/qterminalutils.h @@ -0,0 +1,26 @@ +/*************************************************************************** + * Copyright (C) 2022 by LXQt team * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ***************************************************************************/ + +#ifndef QTERMINALUTILS_H +#define QTERMINALUTILS_H + +#include +#include + +QStringList parse_command(const QString& str); + +#endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..5f332a04 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,7 @@ +qt5_wrap_cpp(QTERM_TEST_MOC qterminal_test.h) +add_executable(qterminal_test + qterminal_test.cpp + ${CMAKE_SOURCE_DIR}/src/qterminalutils.cpp + ${QTERM_TEST_MOC}) +target_link_libraries(qterminal_test Qt5::Test) +add_test(NAME qterminal_test COMMAND qterminal_test) diff --git a/test/qterminal_test.cpp b/test/qterminal_test.cpp new file mode 100644 index 00000000..c9c26513 --- /dev/null +++ b/test/qterminal_test.cpp @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (C) 2022 by LXQt team * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ***************************************************************************/ + +#include "qterminal_test.h" + +#include "qterminalutils.h" + +#include + +// handy shortcut copied from liblxqt +#ifndef QL1S +#define QL1S(x) QLatin1String(x) +#endif + +void QTerminalTest::testParseCommand() +{ + /* Common usage */ + // qterminal -e 'fpad -s "PATH/ha ha"' + // qterminal -e "fpad -s \"PATH/ha ha\"" # \" is decoded as " by the shell + QCOMPARE(parse_command(QL1S(R"(fpad -s "PATH/ha ha")")), + QStringList() << QL1S("fpad") << QL1S("-s") << QL1S("PATH/ha ha")); + // qterminal -e "fpad -s 'PATH/ha ha'" + QCOMPARE(parse_command(QL1S(R"(fpad -s 'PATH/ha ha')")), + QStringList() << QL1S("fpad") << QL1S("-s") << QL1S("PATH/ha ha")); + // qterminal -e 'fpad -s PATH/ha\ ha' + // qterminal -e "fpad -s PATH/ha\ ha" + QCOMPARE(parse_command(QL1S(R"(fpad -s PATH/ha\ ha)")), + QStringList() << QL1S("fpad") << QL1S("-s") << QL1S("PATH/ha ha")); + + /* Uncommon usage */ + // qterminal -e 'fpad -s \"PATH/ha ha\"' + QCOMPARE(parse_command(QL1S(R"(fpad -s \"PATH/ha ha\")")), + QStringList() << QL1S("fpad") << QL1S("-s") << QL1S("\\\"PATH/ha") << QL1S("ha\\\"")); + // qterminal -e 'fpad -s "PATH/ha\ ha"' + QCOMPARE(parse_command(QL1S(R"(fpad -s "PATH/ha\ ha")")), + QStringList() << QL1S("fpad") << QL1S("-s") << QL1S("PATH/ha ha")); +} + +QTEST_MAIN(QTerminalTest) diff --git a/test/qterminal_test.h b/test/qterminal_test.h new file mode 100644 index 00000000..9c64ec98 --- /dev/null +++ b/test/qterminal_test.h @@ -0,0 +1,32 @@ +/*************************************************************************** + * Copyright (C) 2022 by LXQt team * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + ***************************************************************************/ + +#ifndef QTERMINAL_TEST_H +#define QTERMINAL_TEST_H + +#include + +class QTerminalTest : public QObject +{ + Q_OBJECT + + // Each private slot is a test function +private Q_SLOTS: + void testParseCommand(); +}; + +#endif