| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the tools applications of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtCore/QtCore> | 
|---|
| 43 |  | 
|---|
| 44 | #include <stdio.h> | 
|---|
| 45 |  | 
|---|
| 46 | QT_USE_NAMESPACE | 
|---|
| 47 |  | 
|---|
| 48 | static void printHelp(char *argv[]) | 
|---|
| 49 | { | 
|---|
| 50 | qDebug("Usage: %s [-diff] FILES", argv[0] ? argv[0] : "qtest2to4"); | 
|---|
| 51 | qDebug("updates files from QtTestLib 2.x to QTestLib 4.1"); | 
|---|
| 52 | qDebug("\noptions:\n    -diff   Don't write any changes, output differences instead."); | 
|---|
| 53 | exit(2); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | int main(int argc, char *argv[]) | 
|---|
| 57 | { | 
|---|
| 58 | bool printDiff = false; | 
|---|
| 59 | int i = 1; | 
|---|
| 60 |  | 
|---|
| 61 | if (argc == 1) | 
|---|
| 62 | printHelp(argv); | 
|---|
| 63 | if (argv[1][0] == '-') { | 
|---|
| 64 | if (qstrcmp(argv[1], "-diff") == 0) { | 
|---|
| 65 | printDiff = true; | 
|---|
| 66 | ++i; | 
|---|
| 67 | } else { | 
|---|
| 68 | qDebug("Unknown option: %s\n", argv[1]); | 
|---|
| 69 | printHelp(argv); | 
|---|
| 70 | } | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | QRegExp dataHeaderRx(QLatin1String("_data(\\s*)\\((\\s*)QtTestTable\\s*\\&\\s*\\w*\\s*\\)")); | 
|---|
| 74 | QRegExp defElemRx(QLatin1String("\\w+\\.defineElement\\s*\\(\\s*\"(.+)\"\\s*,\\s*\"(.+)\"\\s*\\)")); | 
|---|
| 75 | defElemRx.setMinimal(true); | 
|---|
| 76 | QRegExp addDataRx(QLatin1String("\\*\\w+\\.newData(\\s*)(\\(\\s*\".*\"\\s*\\))")); | 
|---|
| 77 | addDataRx.setMinimal(true); | 
|---|
| 78 | QRegExp nsRx(QLatin1String("namespace(\\s+)QtTest")); | 
|---|
| 79 | QRegExp callRx(QLatin1String("QtTest(\\s*)::")); | 
|---|
| 80 |  | 
|---|
| 81 | enum { MacroCount = 11 }; | 
|---|
| 82 | static const char *macroNames[MacroCount] = { | 
|---|
| 83 | "VERIFY", "FAIL", "VERIFY2", "COMPARE", "SKIP", "VERIFY_EVENT", | 
|---|
| 84 | "EXPECT_FAIL", "FETCH", "FETCH_GLOBAL", "TEST", "WARN" | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | for (; i < argc; ++i) { | 
|---|
| 88 | QFile f(QString::fromLocal8Bit(argv[i])); | 
|---|
| 89 | if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) | 
|---|
| 90 | qFatal("Unable to open file '%s' for reading: %s", argv[i], qPrintable(f.errorString())); | 
|---|
| 91 |  | 
|---|
| 92 | if (printDiff) | 
|---|
| 93 | printf("diff %s\n", argv[i]); | 
|---|
| 94 |  | 
|---|
| 95 | QStringList contents; | 
|---|
| 96 | int lineNumber = 0; | 
|---|
| 97 | int changedLines = 0; | 
|---|
| 98 | while (!f.atEnd()) { | 
|---|
| 99 | QString origLine = QString::fromLatin1(f.readLine()); | 
|---|
| 100 | QString line = origLine; | 
|---|
| 101 | ++lineNumber; | 
|---|
| 102 |  | 
|---|
| 103 | if (dataHeaderRx.indexIn(line) != -1) { | 
|---|
| 104 | QString ws = dataHeaderRx.cap(1); | 
|---|
| 105 | line.replace(dataHeaderRx, QString::fromLatin1("_data%1()").arg(ws)); | 
|---|
| 106 | } | 
|---|
| 107 | if (defElemRx.indexIn(line) != -1) { | 
|---|
| 108 | QString type = defElemRx.cap(1); | 
|---|
| 109 | QString name = defElemRx.cap(2); | 
|---|
| 110 | if (type.endsWith(QLatin1Char('>'))) | 
|---|
| 111 | type.append(QLatin1Char(' ')); | 
|---|
| 112 | line.replace(defElemRx, QString::fromLatin1("QTest::addColumn<%1>(\"%2\")").arg( | 
|---|
| 113 | type).arg(name)); | 
|---|
| 114 | } | 
|---|
| 115 | if (addDataRx.indexIn(line) != -1) { | 
|---|
| 116 | QString repl = QLatin1String("QTest::newRow"); | 
|---|
| 117 | repl += addDataRx.cap(1); | 
|---|
| 118 | repl += addDataRx.cap(2); | 
|---|
| 119 | line.replace(addDataRx, repl); | 
|---|
| 120 | } | 
|---|
| 121 | if (nsRx.indexIn(line) != -1) | 
|---|
| 122 | line.replace(nsRx, QString::fromLatin1("namespace%1QTest").arg(nsRx.cap(1))); | 
|---|
| 123 | int pos = 0; | 
|---|
| 124 | while ((pos = callRx.indexIn(line, pos)) != -1) { | 
|---|
| 125 | line.replace(callRx, QString::fromLatin1("QTest%1::").arg(callRx.cap(1))); | 
|---|
| 126 | pos += callRx.matchedLength(); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | line.replace(QLatin1String("QTTEST_MAIN"), QLatin1String("QTEST_MAIN")); | 
|---|
| 130 | line.replace(QLatin1String("QTTEST_APPLESS_MAIN"), QLatin1String("QTEST_APPLESS_MAIN")); | 
|---|
| 131 | line.replace(QLatin1String("QTTEST_NOOP_MAIN"), QLatin1String("QTEST_NOOP_MAIN")); | 
|---|
| 132 | line.replace(QLatin1String("QtTestEventLoop"), QLatin1String("QTestEventLoop")); | 
|---|
| 133 | line.replace(QLatin1String("QtTestEventList"), QLatin1String("QTestEventList")); | 
|---|
| 134 | line.replace(QLatin1String("QtTestAccessibility"), QLatin1String("QTestAccessibility")); | 
|---|
| 135 | line.replace(QLatin1String("QTest::sleep"), QLatin1String("QTest::qSleep")); | 
|---|
| 136 | line.replace(QLatin1String("QTest::wait"), QLatin1String("QTest::qWait")); | 
|---|
| 137 |  | 
|---|
| 138 | for (int m = 0; m < MacroCount; ++m) { | 
|---|
| 139 | QRegExp macroRe(QString::fromLatin1("\\b%1(\\s*\\()").arg( | 
|---|
| 140 | QLatin1String(macroNames[m]))); | 
|---|
| 141 | QString newMacroName = QLatin1Char('Q') + QString::fromLatin1(macroNames[m]); | 
|---|
| 142 | int pos = 0; | 
|---|
| 143 | while ((pos = macroRe.indexIn(line)) != -1) { | 
|---|
| 144 | line.replace(macroRe, newMacroName + macroRe.cap(1)); | 
|---|
| 145 | pos += macroRe.matchedLength(); | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | if (line != origLine) { | 
|---|
| 150 | if (printDiff) { | 
|---|
| 151 | printf("%dc%d\n", lineNumber, lineNumber); | 
|---|
| 152 | printf("<%s", qPrintable(origLine)); | 
|---|
| 153 | printf("---\n"); | 
|---|
| 154 | printf(">%s", qPrintable(line)); | 
|---|
| 155 | } | 
|---|
| 156 | ++changedLines; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | contents.append(line); | 
|---|
| 160 | } | 
|---|
| 161 | f.close(); | 
|---|
| 162 |  | 
|---|
| 163 | if (printDiff) | 
|---|
| 164 | continue; | 
|---|
| 165 | qDebug("%s: %d change%s made.", argv[i], changedLines, changedLines == 1 ? "" : "s"); | 
|---|
| 166 | if (!changedLines) | 
|---|
| 167 | continue; | 
|---|
| 168 |  | 
|---|
| 169 | if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) | 
|---|
| 170 | qFatal("Unable to open file '%s' for writing: %s", argv[i], qPrintable(f.errorString())); | 
|---|
| 171 | foreach (QString s, contents) | 
|---|
| 172 | f.write(s.toLatin1()); | 
|---|
| 173 | f.close(); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | return 0; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|