-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also extracts parseCommand() to a separate file for simpler testing setup.
- Loading branch information
Chih-Hsuan Yen
committed
Aug 29, 2022
1 parent
c3fe003
commit 68a44df
Showing
7 changed files
with
194 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. * | ||
***************************************************************************/ | ||
|
||
#include <QRegularExpression> | ||
|
||
#include "qterminalutils.h" | ||
|
||
QStringList parse_command(const QString& str) | ||
{ | ||
const QRegularExpression separator(QString::fromLatin1(R"('|(?<!\\)(\\{2})*(\s|")|\z)")); | ||
const QRegularExpression doubleQuote(QString::fromLatin1(R"((?<!\\)(\\{2})*")")); | ||
const QRegularExpression escapedSpace(QString::fromLatin1(R"(\\(\\{2})*\s)")); | ||
const QRegularExpression singleQuote(QStringLiteral("'")); | ||
|
||
QStringList list; | ||
QRegularExpressionMatch match; | ||
int index = 0; | ||
int nextIndex; | ||
while((nextIndex = str.indexOf(separator, index, &match)) != -1) | ||
{ | ||
if (nextIndex > 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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. * | ||
***************************************************************************/ | ||
|
||
#ifndef QTERMINALUTILS_H | ||
#define QTERMINALUTILS_H | ||
|
||
#include <QString> | ||
#include <QStringList> | ||
|
||
QStringList parse_command(const QString& str); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. * | ||
***************************************************************************/ | ||
|
||
#include "qterminal_test.h" | ||
|
||
#include "qterminalutils.h" | ||
|
||
#include <QtTest> | ||
|
||
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. * | ||
***************************************************************************/ | ||
|
||
#ifndef QTERMINAL_TEST_H | ||
#define QTERMINAL_TEST_H | ||
|
||
#include <QObject> | ||
|
||
class QTerminalTest : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
// Each private slot is a test function | ||
private Q_SLOTS: | ||
void testParseCommand(); | ||
}; | ||
|
||
#endif |