diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-05-23 20:12:12 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-05-23 18:18:44 +0000 |
commit | fe5a157cbe37d2516ca69c92c4ec989ce77a25a2 (patch) | |
tree | ab3033995e6e7d24dfd846913edce4827deef381 /tests | |
parent | f377c9ce92be7f5ef82abbf585d88dd9f46a8811 (diff) |
tests: remove uses of Q_FOREACH
All pretty straight-forward, except two:
- in tst_QHelpPrjectData::filterSections(), use QCOMPARE() on two
lists instead of running it by hand. To allow for variation in
order, sort the list under test, and make sure the reference list is
sorted, too.
- in tst_QHelpEngineCore::files(), the trailing printing of 'lst' was
dead code: either the QCOMPARE succeeded, then 'lst' was empty, or
it failed, in which case QCOMPARE exits the function. The loop was
obviously meant to show the remaining items in case the test fails,
so stuff the loop (w/o Q_FOREACH) into a scope guard.
QtC tricked me into believing that these were the only occurrences in
this module. Alas, it just failed to parse large parts of the module's
pro-files. The module continues to be one of the worst offenders
w.r.t. Q_FOREACH with qdoc and linguist each containing ~150
instances, as well as macdeploy with a another dozen or two. The rest
is clean now.
Change-Id: I89f37ef07ab284da7881d624d9111e89cd2b2cbc
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/linguist/lconvert/tst_lconvert.cpp | 2 | ||||
-rw-r--r-- | tests/auto/linguist/lupdate/tst_lupdate.cpp | 9 | ||||
-rw-r--r-- | tests/auto/qhelpenginecore/tst_qhelpenginecore.cpp | 21 | ||||
-rw-r--r-- | tests/auto/qhelpindexmodel/tst_qhelpindexmodel.cpp | 5 | ||||
-rw-r--r-- | tests/auto/qhelpprojectdata/tst_qhelpprojectdata.cpp | 30 |
5 files changed, 38 insertions, 29 deletions
diff --git a/tests/auto/linguist/lconvert/tst_lconvert.cpp b/tests/auto/linguist/lconvert/tst_lconvert.cpp index eb5367b55..2b3729d6c 100644 --- a/tests/auto/linguist/lconvert/tst_lconvert.cpp +++ b/tests/auto/linguist/lconvert/tst_lconvert.cpp @@ -173,7 +173,7 @@ void tst_lconvert::convertChain(const QString &_inFileName, const QString &_outF for (QProcess *cvt : qAsConst(cvts)) QVERIFY2(cvt->waitForStarted(), qPrintable(cvt->errorString())); int st = 0; - foreach (QProcess *cvt, cvts) + for (QProcess *cvt : qAsConst(cvts)) doWait(cvt, ++st); if (!QTest::currentTestFailed()) diff --git a/tests/auto/linguist/lupdate/tst_lupdate.cpp b/tests/auto/linguist/lupdate/tst_lupdate.cpp index ea049a1f9..c55167a55 100644 --- a/tests/auto/linguist/lupdate/tst_lupdate.cpp +++ b/tests/auto/linguist/lupdate/tst_lupdate.cpp @@ -223,7 +223,7 @@ void tst_lupdate::good_data() dirs.removeAll(QLatin1String("backslashes")); #endif - foreach (const QString &dir, dirs) + for (const QString &dir : qAsConst(dirs)) QTest::newRow(dir.toLocal8Bit()) << dir; } @@ -253,7 +253,8 @@ void tst_lupdate::good() } else if (cmdstring.startsWith("TRANSLATION:")) { cmdstring.remove(0, 12); generatedtsfiles.clear(); - foreach (const QByteArray &s, cmdstring.split(' ')) + const auto parts = cmdstring.split(' '); + for (const QByteArray &s : parts) if (!s.isEmpty()) generatedtsfiles << s; } else if (cmdstring.startsWith("cd ")) { @@ -264,7 +265,7 @@ void tst_lupdate::good() file.close(); } - foreach (const QString &ts, generatedtsfiles) { + for (const QString &ts : qAsConst(generatedtsfiles)) { QString genTs = workDir + QLatin1Char('/') + ts; QFile::remove(genTs); QString beforetsfile = dir + QLatin1Char('/') + ts + QLatin1String(".before"); @@ -304,7 +305,7 @@ void tst_lupdate::good() return; } - foreach (const QString &ts, generatedtsfiles) + for (const QString &ts : qAsConst(generatedtsfiles)) doCompare(workDir + QLatin1Char('/') + ts, dir + QLatin1Char('/') + ts + QLatin1String(".result"), false); } diff --git a/tests/auto/qhelpenginecore/tst_qhelpenginecore.cpp b/tests/auto/qhelpenginecore/tst_qhelpenginecore.cpp index ba2b1532d..ddadda10b 100644 --- a/tests/auto/qhelpenginecore/tst_qhelpenginecore.cpp +++ b/tests/auto/qhelpenginecore/tst_qhelpenginecore.cpp @@ -29,6 +29,7 @@ #include <QtCore/QUrl> #include <QtCore/QFileInfo> +#include <QtCore/QScopeGuard> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlQuery> @@ -189,12 +190,12 @@ void tst_QHelpEngineCore::registeredDocumentations() { QHelpEngineCore help(m_colFile, 0); QCOMPARE(help.setupData(), true); - QStringList docs = help.registeredDocumentations(); + const QStringList docs = help.registeredDocumentations(); QCOMPARE(docs.count(), 3); QStringList lst; lst << "trolltech.com.3-3-8.qmake" << "trolltech.com.4-3-0.qmake" << "trolltech.com.1.0.0.test"; - foreach (QString s, docs) + for (const QString &s : docs) lst.removeAll(s); QCOMPARE(lst.isEmpty(), true); } @@ -259,12 +260,12 @@ void tst_QHelpEngineCore::customFilters() { QHelpEngineCore help(m_colFile, 0); QCOMPARE(help.setupData(), true); - QStringList custom = help.customFilters(); + const QStringList custom = help.customFilters(); QCOMPARE(custom.count(), 4); QStringList lst; lst << "qmake Manual" << "Custom Filter 1" << "Custom Filter 2" << "unfiltered"; - foreach (QString s, custom) + for (const QString &s : custom) lst.removeAll(s); QCOMPARE(lst.count(), 0); } @@ -293,11 +294,11 @@ void tst_QHelpEngineCore::filterAttributes() { QHelpEngineCore help(m_colFile, 0); QCOMPARE(help.setupData(), true); - QStringList atts = help.filterAttributes("qmake Manual"); + const QStringList atts = help.filterAttributes("qmake Manual"); QCOMPARE(atts.count(), 3); QStringList lst; lst << "qmake" << "tools" << "qt"; - foreach (QString s, atts) + for (const QString &s : atts) lst.removeAll(s); QCOMPARE(lst.count(), 0); } @@ -352,11 +353,13 @@ void tst_QHelpEngineCore::files() QCOMPARE(lst.count(), 0); lst = help.files("trolltech.com.4-3-0.qmake", QStringList() << "qt" << "qmake", "foo"); - QCOMPARE(lst.count(), 0); - foreach (QUrl url, lst) - qDebug() << url; + // print 'lst' if test fails: + auto printRemainder = qScopeGuard([&]{ for (const QUrl &url : lst) qDebug() << url; }); + + QCOMPARE(lst.count(), 0); + printRemainder.dismiss(); } void tst_QHelpEngineCore::fileData() diff --git a/tests/auto/qhelpindexmodel/tst_qhelpindexmodel.cpp b/tests/auto/qhelpindexmodel/tst_qhelpindexmodel.cpp index 713c229ce..d304c3f3b 100644 --- a/tests/auto/qhelpindexmodel/tst_qhelpindexmodel.cpp +++ b/tests/auto/qhelpindexmodel/tst_qhelpindexmodel.cpp @@ -119,8 +119,9 @@ void tst_QHelpIndexModel::setupIndex() QStringList lst; lst << "foo" << "bar" << "bla" << "einstein" << "newton"; - QCOMPARE(m->stringList().count(), 5); - foreach (QString s, m->stringList()) + const auto stringList = m->stringList(); + QCOMPARE(stringList.count(), 5); + for (const QString &s : stringList) lst.removeAll(s); QCOMPARE(lst.isEmpty(), true); } diff --git a/tests/auto/qhelpprojectdata/tst_qhelpprojectdata.cpp b/tests/auto/qhelpprojectdata/tst_qhelpprojectdata.cpp index a09135d7c..9b47be519 100644 --- a/tests/auto/qhelpprojectdata/tst_qhelpprojectdata.cpp +++ b/tests/auto/qhelpprojectdata/tst_qhelpprojectdata.cpp @@ -85,18 +85,18 @@ void tst_QHelpProjectData::customFilters() if (!data.readData(m_inputFile)) QFAIL("Cannot read qhp file!"); - QList<QHelpDataCustomFilter> filters = data.customFilters(); + const QList<QHelpDataCustomFilter> filters = data.customFilters(); QCOMPARE(filters.count(), 2); - foreach (QHelpDataCustomFilter f, filters) { + for (const QHelpDataCustomFilter &f : filters) { if (f.name == QLatin1String("Custom Filter 1")) { - foreach (QString id, f.filterAttributes) { + for (const QString &id : f.filterAttributes) { if (id != QLatin1String("test") && id != QLatin1String("filter1")) QFAIL("Wrong filter attribute!"); } } else if (f.name == QLatin1String("Custom Filter 2")) { - foreach (QString id, f.filterAttributes) { + for (const QString &id : f.filterAttributes) { if (id != QLatin1String("test") && id != QLatin1String("filter2")) QFAIL("Wrong filter attribute!"); @@ -113,13 +113,14 @@ void tst_QHelpProjectData::filterSections() if (!data.readData(m_inputFile)) QFAIL("Cannot read qhp file!"); - QList<QHelpDataFilterSection> sections = data.filterSections(); + const QList<QHelpDataFilterSection> sections = data.filterSections(); QCOMPARE(sections.count(), 2); - foreach (QHelpDataFilterSection s, sections) { + for (const QHelpDataFilterSection &s : sections) { if (s.filterAttributes().contains("filter1")) { - QCOMPARE(s.indices().count(), 5); - foreach (QHelpDataIndexItem idx, s.indices()) { + const auto indices = s.indices(); + QCOMPARE(indices.size(), 5); + for (const QHelpDataIndexItem &idx : indices) { if (idx.name == QLatin1String("foo")) { QCOMPARE(idx.identifier, QString("Test::foo")); } else if (idx.name == QLatin1String("bar")) { @@ -138,11 +139,14 @@ void tst_QHelpProjectData::filterSections() QCOMPARE(s.contents().first()->children().count(), 5); } else if (s.filterAttributes().contains("filter2")) { QCOMPARE(s.contents().count(), 1); - QStringList lst; - lst << "classic.css" << "fancy.html" << "cars.html"; - foreach (QString f, s.files()) - lst.removeAll(f); - QCOMPARE(lst.count(), 0); + const QStringList lst = { + "cars.html", + "classic.css", + "fancy.html", + }; + auto files = s.files(); + std::sort(files.begin(), files.end()); + QCOMPARE(files, lst); } else { QFAIL("Unexpected filter attribute!"); } |