From 3b310a3ec13a743ae2258bc43d92a5df42c6ad37 Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Fri, 14 Jun 2019 16:15:16 +0300 Subject: Add regular expression support to replace operation Introduce search with regular expressions support to replace operation. Add unit tests and make associated documentation changes. Task-number: QTIFW-889 Change-Id: I82e30056030ebc900be49046fda1903b27a2824d Reviewed-by: Katja Marttila --- tests/auto/installer/installer.pro | 3 +- .../replaceoperation/replaceoperation.pro | 6 + .../replaceoperation/tst_replaceoperation.cpp | 184 +++++++++++++++++++++ 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 tests/auto/installer/replaceoperation/replaceoperation.pro create mode 100644 tests/auto/installer/replaceoperation/tst_replaceoperation.cpp (limited to 'tests/auto') diff --git a/tests/auto/installer/installer.pro b/tests/auto/installer/installer.pro index db94a23cb..7081a6316 100644 --- a/tests/auto/installer/installer.pro +++ b/tests/auto/installer/installer.pro @@ -21,7 +21,8 @@ SUBDIRS += \ settingsoperation \ task \ clientserver \ - factory + factory \ + replaceoperation win32 { SUBDIRS += registerfiletypeoperation diff --git a/tests/auto/installer/replaceoperation/replaceoperation.pro b/tests/auto/installer/replaceoperation/replaceoperation.pro new file mode 100644 index 000000000..d2756d153 --- /dev/null +++ b/tests/auto/installer/replaceoperation/replaceoperation.pro @@ -0,0 +1,6 @@ +include(../../qttest.pri) + +QT -= gui +QT += testlib + +SOURCES += tst_replaceoperation.cpp diff --git a/tests/auto/installer/replaceoperation/tst_replaceoperation.cpp b/tests/auto/installer/replaceoperation/tst_replaceoperation.cpp new file mode 100644 index 000000000..9afd91875 --- /dev/null +++ b/tests/auto/installer/replaceoperation/tst_replaceoperation.cpp @@ -0,0 +1,184 @@ +/************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include + +using namespace KDUpdater; +using namespace QInstaller; + +class tst_replaceoperation : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase() + { + m_testDirectory = QInstaller::generateTemporaryFileName(); + m_testFilePath = m_testDirectory + "/test." + QString::number(qrand() % 1000); + } + + void testWrongArguments() + { + ReplaceOperation missingArgumentsOperation(nullptr); + missingArgumentsOperation.setArguments(QStringList() << "testFile" << "testSearch"); + + // should do nothing if there are missing arguments + QVERIFY(missingArgumentsOperation.testOperation()); + QCOMPARE(missingArgumentsOperation.performOperation(), false); + + ReplaceOperation invalidModeArgumentOperation(nullptr); + invalidModeArgumentOperation.setArguments(QStringList() << "testFile" + << "testSearch" << "testReplace" << "invalid"); + + // should do nothing if there is an invalid mode argument + QVERIFY(invalidModeArgumentOperation.testOperation()); + QCOMPARE(invalidModeArgumentOperation.performOperation(), false); + + QCOMPARE(UpdateOperation::Error(invalidModeArgumentOperation.error()), + UpdateOperation::InvalidArguments); + + QString compareString = "Current mode argument calling \"Replace\" with " + "arguments \"testFile; testSearch; testReplace; invalid\" is not supported. " + "Please use string or regex."; + + QCOMPARE(invalidModeArgumentOperation.errorString(), compareString); + + ReplaceOperation emptySearchArgumentOperation(nullptr); + emptySearchArgumentOperation.setArguments(QStringList() << "testFile" + << "" << "testReplace" << "regex"); + + // should do nothing if there is an empty search argument + QVERIFY(emptySearchArgumentOperation.testOperation()); + QCOMPARE(emptySearchArgumentOperation.performOperation(), false); + + QCOMPARE(UpdateOperation::Error(emptySearchArgumentOperation.error()), + UpdateOperation::InvalidArguments); + + compareString = "Current search argument calling \"Replace\" with " + "empty search argument is not supported."; + + QCOMPARE(emptySearchArgumentOperation.errorString(), compareString); + } + + void testStringSearchReplace() + { + QVERIFY(QDir().mkpath(m_testDirectory)); + QVERIFY(QDir(m_testDirectory).exists()); + + QFile file(m_testFilePath); + QVERIFY(file.open(QIODevice::WriteOnly)); + + QTextStream stream(&file); + stream << "Lorem ipsum dolore sit amet, consectetur adipiscing elit, sed do eiusmod " + "tempor incididunt ut labore et dolore magna aliqua." << endl; + file.close(); + + ReplaceOperation searchReplaceOperation(nullptr); + searchReplaceOperation.setArguments(QStringList() << m_testFilePath + << "dolore" << "test"); + + // should succeed + QVERIFY(searchReplaceOperation.testOperation()); + QCOMPARE(searchReplaceOperation.performOperation(), true); + + QVERIFY(file.open(QIODevice::ReadOnly)); + + QString fileContent = stream.readAll(); + QCOMPARE(fileContent, QLatin1String( + "Lorem ipsum test sit amet, consectetur adipiscing elit, sed do eiusmod " + "tempor incididunt ut labore et test magna aliqua.\n")); + + file.close(); + QVERIFY(file.remove()); + QVERIFY(QDir().rmdir(m_testDirectory)); + } + + void testRegexSearchReplace() + { + // Test with three different regexes, one containing + // a capturing group + + QVERIFY(QDir().mkpath(m_testDirectory)); + QVERIFY(QDir(m_testDirectory).exists()); + + QFile file(m_testFilePath); + QVERIFY(file.open(QIODevice::WriteOnly)); + + QTextStream stream(&file); + stream << "one | 10/10/2010 | three | 1.2345 | 0.00001 " + "| 7 | A bon mot." << endl; + file.close(); + + ReplaceOperation searchReplaceOperation(nullptr); + searchReplaceOperation.setArguments(QStringList() << m_testFilePath + << "\\d{1,2}/\\d{1,2}/\\d{4}" << "date-match" << "regex"); + + // should succeed + QVERIFY(searchReplaceOperation.testOperation()); + QCOMPARE(searchReplaceOperation.performOperation(), true); + + searchReplaceOperation.setArguments(QStringList() << m_testFilePath + << "[-+]?[0-9]*\\.?[0-9]+" << "number-match" << "regex"); + + // should succeed + QVERIFY(searchReplaceOperation.testOperation()); + QCOMPARE(searchReplaceOperation.performOperation(), true); + + searchReplaceOperation.setArguments(QStringList() << m_testFilePath + << "([^<]*)" << "\\emph{\\1}" << "regex"); + + // should succeed + QVERIFY(searchReplaceOperation.testOperation()); + QCOMPARE(searchReplaceOperation.performOperation(), true); + + QVERIFY(file.open(QIODevice::ReadOnly)); + + QString fileContent = stream.readAll(); + QCOMPARE(fileContent, QLatin1String("one | date-match | three " + "| number-match | number-match | number-match | A \\emph{bon mot}.\n")); + + file.close(); + QVERIFY(file.remove()); + QVERIFY(QDir().rmdir(m_testDirectory)); + } + +private: + QString m_testDirectory; + QString m_testFilePath; +}; + +QTEST_MAIN(tst_replaceoperation) + +#include "tst_replaceoperation.moc" -- cgit v1.2.3