Commit b0bf07c6 authored by Christian Stenger's avatar Christian Stenger
Browse files

AutoTest: Modernize code a bit



Change-Id: Ie56f6e5cb8a4f1962bd8df5e57214911ae609921
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
parent 8b579fca
......@@ -38,7 +38,7 @@ class TestUtils
public:
static QString getCMakeDisplayNameIfNecessary(const QString &filePath, const QString &proFile)
{
static const QString CMAKE_LISTS = QLatin1String("CMakeLists.txt");
static const QString CMAKE_LISTS("CMakeLists.txt");
if (!proFile.endsWith(CMAKE_LISTS))
return QString();
......
......@@ -48,9 +48,9 @@ public:
QSharedPointer<TestSettings> settings() const;
bool initialize(const QStringList &arguments, QString *errorString);
void extensionsInitialized();
ShutdownFlag aboutToShutdown();
bool initialize(const QStringList &arguments, QString *errorString) override;
void extensionsInitialized() override;
ShutdownFlag aboutToShutdown() override;
private:
bool checkLicense();
......@@ -58,7 +58,7 @@ private:
void onRunAllTriggered();
void onRunSelectedTriggered();
void updateMenuItemsEnabledState();
QList<QObject *> createTestObjects() const;
QList<QObject *> createTestObjects() const override;
const QSharedPointer<TestSettings> m_settings;
TestFrameworkManager *m_frameworkManager = 0;
};
......
......@@ -65,7 +65,7 @@ void AutoTestUnitTests::initTestCase()
if (allKits.count() != 1)
QSKIP("This test requires exactly one kit to be present");
if (auto qtVersion = QtSupport::QtKitInformation::qtVersion(allKits.first()))
m_isQt4 = qtVersion->qtVersionString().startsWith(QLatin1Char('4'));
m_isQt4 = qtVersion->qtVersionString().startsWith('4');
else
QSKIP("Could not figure out which Qt version is used for default kit.");
const ToolChain * const toolchain = ToolChainKitInformation::toolChain(allKits.first(),
......
......@@ -41,14 +41,14 @@ QStringList GTestConfiguration::argumentsForTestRunner(const TestSettings &setti
QStringList arguments;
const QStringList &testSets = testCases();
if (testSets.size())
arguments << QLatin1String("--gtest_filter=") + testSets.join(QLatin1Char(':'));
arguments << "--gtest_filter=" + testSets.join(':');
if (settings.gTestSettings.runDisabled)
arguments << QLatin1String("--gtest_also_run_disabled_tests");
arguments << "--gtest_also_run_disabled_tests";
if (settings.gTestSettings.repeat)
arguments << QString::fromLatin1("--gtest_repeat=%1").arg(settings.gTestSettings.iterations);
arguments << QString("--gtest_repeat=%1").arg(settings.gTestSettings.iterations);
if (settings.gTestSettings.shuffle) {
arguments << QLatin1String("--gtest_shuffle")
<< QString::fromLatin1("--gtest_random_seed=%1").arg(settings.gTestSettings.seed);
arguments << "--gtest_shuffle"
<< QString("--gtest_random_seed=%1").arg(settings.gTestSettings.seed);
}
if (settings.gTestSettings.throwOnFailure)
arguments << "--gtest_throw_on_failure";
......
......@@ -72,8 +72,8 @@ void GTestOutputReader::processOutput(const QByteArray &outputLine)
if (line.trimmed().isEmpty())
return;
if (!line.startsWith(QLatin1Char('['))) {
m_description.append(line).append(QLatin1Char('\n'));
if (!line.startsWith('[')) {
m_description.append(line).append('\n');
if (iterations.exactMatch(line)) {
m_iteration = iterations.cap(1).toInt();
m_description.clear();
......@@ -142,7 +142,7 @@ void GTestOutputReader::processOutput(const QByteArray &outputLine)
m_description.chop(1);
testResult->setDescription(m_description);
foreach (const QString &output, m_description.split(QLatin1Char('\n'))) {
foreach (const QString &output, m_description.split('\n')) {
QRegExp *match = 0;
if (failureLocation.exactMatch(output))
match = &failureLocation;
......
......@@ -42,7 +42,7 @@ TestTreeItem *GTestParseResult::createTestTreeItem() const
static bool includesGTest(const CPlusPlus::Document::Ptr &doc,
const CPlusPlus::Snapshot &snapshot)
{
const QString gtestH = QLatin1String("gtest/gtest.h");
static const QString gtestH("gtest/gtest.h");
foreach (const CPlusPlus::Document::Include &inc, doc->resolvedIncludes()) {
if (inc.resolvedFileName().endsWith(gtestH))
return true;
......
......@@ -42,12 +42,12 @@ const QString GTestResult::outputString(bool selected) const
case Result::Fail:
output = m_testSetName;
if (selected && !desc.isEmpty())
output.append(QLatin1Char('\n')).append(desc);
output.append('\n').append(desc);
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
output = output.split('\n').first();
}
return output;
}
......
......@@ -43,24 +43,24 @@ QString GTestSettings::name() const
void GTestSettings::fromSettings(const QSettings *s)
{
runDisabled = s->value(QLatin1String(runDisabledKey), false).toBool();
repeat = s->value(QLatin1String(repeatKey), false).toBool();
shuffle = s->value(QLatin1String(shuffleKey), false).toBool();
iterations = s->value(QLatin1String(iterationsKey), 1).toInt();
seed = s->value(QLatin1String(seedKey), 0).toInt();
breakOnFailure = s->value(QLatin1String(breakOnFailureKey), true).toBool();
throwOnFailure = s->value(QLatin1String(throwOnFailureKey), false).toBool();
runDisabled = s->value(runDisabledKey, false).toBool();
repeat = s->value(repeatKey, false).toBool();
shuffle = s->value(shuffleKey, false).toBool();
iterations = s->value(iterationsKey, 1).toInt();
seed = s->value(seedKey, 0).toInt();
breakOnFailure = s->value(breakOnFailureKey, true).toBool();
throwOnFailure = s->value(throwOnFailureKey, false).toBool();
}
void GTestSettings::toSettings(QSettings *s) const
{
s->setValue(QLatin1String(runDisabledKey), runDisabled);
s->setValue(QLatin1String(repeatKey), repeat);
s->setValue(QLatin1String(shuffleKey), shuffle);
s->setValue(QLatin1String(iterationsKey), iterations);
s->setValue(QLatin1String(seedKey), seed);
s->setValue(QLatin1String(breakOnFailureKey), breakOnFailure);
s->setValue(QLatin1String(throwOnFailureKey), throwOnFailure);
s->setValue(runDisabledKey, runDisabled);
s->setValue(repeatKey, repeat);
s->setValue(shuffleKey, shuffle);
s->setValue(iterationsKey, iterations);
s->setValue(seedKey, seed);
s->setValue(breakOnFailureKey, breakOnFailure);
s->setValue(throwOnFailureKey, throwOnFailure);
}
} // namespace Internal
......
......@@ -101,7 +101,7 @@ TestConfiguration *GTestTreeItem::testConfiguration() const
GTestConfiguration *config = 0;
switch (type()) {
case TestCase: {
const QString &testSpecifier = gtestFilter(state()).arg(name()).arg(QLatin1Char('*'));
const QString &testSpecifier = gtestFilter(state()).arg(name()).arg('*');
if (int count = childCount()) {
config = new GTestConfiguration;
config->setTestCases(QStringList(testSpecifier));
......@@ -339,7 +339,7 @@ QString GTestTreeItem::nameSuffix() const
if (m_state & Typed)
suffix += (suffix.isEmpty() ? QLatin1String(" [") : QLatin1String(", ")) + markups[1];
if (!suffix.isEmpty())
suffix += QLatin1Char(']');
suffix += ']';
return suffix;
}
......
......@@ -46,8 +46,8 @@ public:
Q_FLAGS(TestState)
Q_DECLARE_FLAGS(TestStates, TestState)
GTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type), m_state(Enabled) {}
explicit GTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type), m_state(Enabled) {}
static GTestTreeItem *createTestItem(const TestParseResult *result);
......
......@@ -57,7 +57,7 @@ inline bool operator<(const GTestCaseSpec &spec1, const GTestCaseSpec &spec2)
class GTestVisitor : public CPlusPlus::ASTVisitor
{
public:
GTestVisitor(CPlusPlus::Document::Ptr doc);
explicit GTestVisitor(CPlusPlus::Document::Ptr doc);
bool visit(CPlusPlus::FunctionDefinitionAST *ast);
QMap<GTestCaseSpec, GTestCodeLocationList> gtestFunctions() const { return m_gtestFunctions; }
......
......@@ -34,7 +34,7 @@ namespace Internal {
class ITestFramework
{
public:
ITestFramework(bool activeByDefault) : m_active(activeByDefault) {}
explicit ITestFramework(bool activeByDefault) : m_active(activeByDefault) {}
virtual ~ITestFramework()
{
delete m_rootNode;
......
......@@ -38,13 +38,13 @@ namespace Internal {
static QString decode(const QString& original)
{
QString result(original);
static QRegExp regex(QLatin1String("&#((x[0-9A-F]+)|([0-9]+));"), Qt::CaseInsensitive);
static QRegExp regex("&#((x[0-9A-F]+)|([0-9]+));", Qt::CaseInsensitive);
regex.setMinimal(true);
int pos = 0;
while ((pos = regex.indexIn(original, pos)) != -1) {
const QString value = regex.cap(1);
if (value.startsWith(QLatin1Char('x')))
if (value.startsWith('x'))
result.replace(regex.cap(0), QChar(value.midRef(1).toInt(0, 16)));
else
result.replace(regex.cap(0), QChar(value.toInt(0, 10)));
......@@ -80,13 +80,13 @@ static QString formatResult(double value)
beforeDecimalPoint.chop(beforeRemove);
for (int i = 0; i < beforeRemove; ++i)
beforeDecimalPoint.append(QLatin1Char('0'));
beforeDecimalPoint.append('0');
int afterUse = significantDigits - beforeUse;
if (beforeDecimalPoint == QLatin1String("0") && !afterDecimalPoint.isEmpty()) {
++afterUse;
int i = 0;
while (i < afterDecimalPoint.count() && afterDecimalPoint.at(i) == QLatin1Char('0'))
while (i < afterDecimalPoint.count() && afterDecimalPoint.at(i) == '0')
++i;
afterUse += i;
}
......@@ -96,7 +96,7 @@ static QString formatResult(double value)
QString result = beforeDecimalPoint;
if (afterUse > 0)
result.append(QLatin1Char('.'));
result.append('.');
result += afterDecimalPoint;
return result;
......@@ -105,16 +105,16 @@ static QString formatResult(double value)
static QString constructBenchmarkInformation(const QString &metric, double value, int iterations)
{
QString metricsText;
if (metric == QLatin1String("WalltimeMilliseconds")) // default
metricsText = QLatin1String("msecs");
else if (metric == QLatin1String("CPUTicks")) // -tickcounter
metricsText = QLatin1String("CPU ticks");
else if (metric == QLatin1String("Events")) // -eventcounter
metricsText = QLatin1String("events");
else if (metric == QLatin1String("InstructionReads")) // -callgrind
metricsText = QLatin1String("instruction reads");
else if (metric == QLatin1String("CPUCycles")) // -perf
metricsText = QLatin1String("CPU cycles");
if (metric == "WalltimeMilliseconds") // default
metricsText = "msecs";
else if (metric == "CPUTicks") // -tickcounter
metricsText = "CPU ticks";
else if (metric == "Events") // -eventcounter
metricsText = "events";
else if (metric == "InstructionReads") // -callgrind
metricsText = "instruction reads";
else if (metric == "CPUCycles") // -perf
metricsText = "CPU cycles";
return QtTestOutputReader::tr("%1 %2 per iteration (total: %3, iterations: %4)")
.arg(formatResult(value))
.arg(metricsText)
......@@ -227,7 +227,7 @@ void QtTestOutputReader::processOutput(const QByteArray &outputLine)
break;
case Description:
if (!m_description.isEmpty())
m_description.append(QLatin1Char('\n'));
m_description.append('\n');
m_description.append(text);
break;
case QtVersion:
......
......@@ -43,8 +43,7 @@ static bool includesQtTest(const CPlusPlus::Document::Ptr &doc, const CPlusPlus:
{
static QStringList expectedHeaderPrefixes
= Utils::HostOsInfo::isMacHost()
? QStringList({ QLatin1String("QtTest.framework/Headers"), QLatin1String("QtTest") })
: QStringList({ QLatin1String("QtTest") });
? QStringList({ "QtTest.framework/Headers", "QtTest" }) : QStringList({ "QtTest" });
const QList<CPlusPlus::Document::Include> includes = doc->resolvedIncludes();
......@@ -53,7 +52,7 @@ static bool includesQtTest(const CPlusPlus::Document::Ptr &doc, const CPlusPlus:
// bad, as there could be much more different approaches
if (inc.unresolvedFileName() == QLatin1String("QtTest")) {
foreach (const QString &prefix, expectedHeaderPrefixes) {
if (inc.resolvedFileName().endsWith(QString::fromLatin1("%1/QtTest").arg(prefix)))
if (inc.resolvedFileName().endsWith(QString("%1/QtTest").arg(prefix)))
return true;
}
}
......@@ -62,7 +61,7 @@ static bool includesQtTest(const CPlusPlus::Document::Ptr &doc, const CPlusPlus:
const QSet<QString> allIncludes = snapshot.allIncludesForDocument(doc->fileName());
foreach (const QString &include, allIncludes) {
foreach (const QString &prefix, expectedHeaderPrefixes) {
if (include.endsWith(QString::fromLatin1("%1/qtest.h").arg(prefix)))
if (include.endsWith(QString("%1/qtest.h").arg(prefix)))
return true;
}
}
......@@ -141,7 +140,7 @@ static QSet<QString> filesWithDataFunctionDefinitions(
for ( ; it != end; ++it) {
const QString &key = it.key();
if (key.endsWith(QLatin1String("_data")) && testFunctions.contains(key.left(key.size() - 5)))
if (key.endsWith("_data") && testFunctions.contains(key.left(key.size() - 5)))
result.insert(it.value().m_name);
}
return result;
......@@ -209,7 +208,7 @@ static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
const TestCodeLocationAndType &location = it.value();
QtTestParseResult *func = new QtTestParseResult(id);
func->itemType = location.m_type;
func->name = testCaseName + QLatin1String("::") + it.key();
func->name = testCaseName + "::" + it.key();
func->displayName = it.key();
func->fileName = location.m_name;
func->line = location.m_line;
......@@ -220,7 +219,7 @@ static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
dataTag->itemType = tag.m_type;
dataTag->name = tag.m_name;
dataTag->displayName = tag.m_name;
dataTag->fileName = testFunctions.value(it.key() + QLatin1String("_data")).m_name;
dataTag->fileName = testFunctions.value(it.key() + "_data").m_name;
dataTag->line = tag.m_line;
dataTag->column = tag.m_column;
......
......@@ -45,28 +45,28 @@ const QString QtTestResult::outputString(bool selected) const
case Result::UnexpectedPass:
case Result::BlacklistedFail:
case Result::BlacklistedPass:
output = className + QLatin1String("::") + m_function;
output = className + "::" + m_function;
if (!m_dataTag.isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(m_dataTag));
output.append(QString(" (%1)").arg(m_dataTag));
if (selected && !desc.isEmpty()) {
output.append(QLatin1Char('\n')).append(desc);
output.append('\n').append(desc);
}
break;
case Result::Benchmark:
output = className + QLatin1String("::") + m_function;
output = className + "::" + m_function;
if (!m_dataTag.isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(m_dataTag));
output.append(QString(" (%1)").arg(m_dataTag));
if (!desc.isEmpty()) {
int breakPos = desc.indexOf(QLatin1Char('('));
output.append(QLatin1String(": ")).append(desc.left(breakPos));
int breakPos = desc.indexOf('(');
output.append(": ").append(desc.left(breakPos));
if (selected)
output.append(QLatin1Char('\n')).append(desc.mid(breakPos));
output.append('\n').append(desc.mid(breakPos));
}
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
output = output.split('\n').first();
}
return output;
}
......
......@@ -56,14 +56,14 @@ QString QtTestSettings::name() const
void QtTestSettings::fromSettings(const QSettings *s)
{
metrics = intToMetrics(s->value(QLatin1String(metricsKey), Walltime).toInt());
noCrashHandler = s->value(QLatin1String(noCrashhandlerKey), true).toBool();
metrics = intToMetrics(s->value(metricsKey, Walltime).toInt());
noCrashHandler = s->value(noCrashhandlerKey, true).toBool();
}
void QtTestSettings::toSettings(QSettings *s) const
{
s->setValue(QLatin1String(metricsKey), metrics);
s->setValue(QLatin1String(noCrashhandlerKey), noCrashHandler);
s->setValue(metricsKey, metrics);
s->setValue(noCrashhandlerKey, noCrashHandler);
}
QString QtTestSettings::metricsTypeToOption(const MetricsType type)
......
......@@ -118,7 +118,7 @@ TestConfiguration *QtTestTreeItem::testConfiguration() const
const TestTreeItem *parent = function ? function->parentItem() : 0;
if (!parent)
return 0;
const QString functionWithTag = function->name() + QLatin1Char(':') + name();
const QString functionWithTag = function->name() + ':' + name();
config = new QtTestConfiguration();
config->setTestCases(QStringList(functionWithTag));
config->setProFile(parent->proFile());
......
......@@ -33,8 +33,8 @@ namespace Internal {
class QtTestTreeItem : public TestTreeItem
{
public:
QtTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type) {}
explicit QtTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type) {}
static QtTestTreeItem *createTestItem(const TestParseResult *result);
......
......@@ -35,10 +35,7 @@
namespace Autotest {
namespace Internal {
static QStringList specialFunctions({ QLatin1String("initTestCase"),
QLatin1String("cleanupTestCase"),
QLatin1String("init"),
QLatin1String("cleanup") });
static QStringList specialFunctions({ "initTestCase", "cleanupTestCase", "init", "cleanup" });
/************************** Cpp Test Symbol Visitor ***************************/
......@@ -82,7 +79,7 @@ bool TestVisitor::visit(CPlusPlus::Class *symbol)
}
if (specialFunctions.contains(name))
locationAndType.m_type = TestTreeItem::TestSpecialFunction;
else if (name.endsWith(QLatin1String("_data")))
else if (name.endsWith("_data"))
locationAndType.m_type = TestTreeItem::TestDataFunction;
else
locationAndType.m_type = TestTreeItem::TestFunctionOrSet;
......@@ -111,7 +108,7 @@ bool TestAstVisitor::visit(CPlusPlus::CallAST *ast)
if (const auto qualifiedNameAST = idExpressionAST->name->asQualifiedName()) {
const CPlusPlus::Overview o;
const QString prettyName = o.prettyName(qualifiedNameAST->name);
if (prettyName == QLatin1String("QTest::qExec")) {
if (prettyName == "QTest::qExec") {
if (const auto expressionListAST = ast->expression_list) {
// first argument is the one we need
if (const auto argumentExpressionAST = expressionListAST->value) {
......@@ -148,17 +145,14 @@ bool TestAstVisitor::visit(CPlusPlus::CompoundStatementAST *ast)
TestDataFunctionVisitor::TestDataFunctionVisitor(CPlusPlus::Document::Ptr doc)
: CPlusPlus::ASTVisitor(doc->translationUnit()),
m_currentDoc(doc),
m_currentAstDepth(0),
m_insideUsingQTestDepth(0),
m_insideUsingQTest(false)
m_currentDoc(doc)
{
}
bool TestDataFunctionVisitor::visit(CPlusPlus::UsingDirectiveAST *ast)
{
if (auto nameAST = ast->name) {
if (m_overview.prettyName(nameAST->name) == QLatin1String("QTest")) {
if (m_overview.prettyName(nameAST->name) == "QTest") {
m_insideUsingQTest = true;
// we need the surrounding AST depth as using directive is an AST itself
m_insideUsingQTestDepth = m_currentAstDepth - 1;
......@@ -177,7 +171,7 @@ bool TestDataFunctionVisitor::visit(CPlusPlus::FunctionDefinitionAST *ast)
CPlusPlus::LookupContext lc;
const QString prettyName = m_overview.prettyName(lc.fullyQualifiedName(ast->symbol));
// do not handle functions that aren't real test data functions
if (!prettyName.endsWith(QLatin1String("_data")))
if (!prettyName.endsWith("_data"))
return false;
m_currentFunction = prettyName.left(prettyName.size() - 5);
......@@ -275,10 +269,10 @@ bool TestDataFunctionVisitor::newRowCallFound(CPlusPlus::CallAST *ast, unsigned
return false;
if (const auto qualifiedNameAST = exp->name->asQualifiedName()) {
found = m_overview.prettyName(qualifiedNameAST->name) == QLatin1String("QTest::newRow");
found = m_overview.prettyName(qualifiedNameAST->name) == "QTest::newRow";
*firstToken = qualifiedNameAST->firstToken();
} else if (m_insideUsingQTest) {
found = m_overview.prettyName(exp->name->name) == QLatin1String("newRow");
found = m_overview.prettyName(exp->name->name) == "newRow";
*firstToken = exp->name->firstToken();
}
}
......
......@@ -42,7 +42,7 @@ namespace Internal {
class TestVisitor : public CPlusPlus::SymbolVisitor
{
public:
TestVisitor(const QString &fullQualifiedClassName);
explicit TestVisitor(const QString &fullQualifiedClassName);
QMap<QString, TestCodeLocationAndType> privateSlots() const { return m_privSlots; }
bool resultValid() const { return m_valid; }
......@@ -59,7 +59,7 @@ private:
class TestAstVisitor : public CPlusPlus::ASTVisitor
{
public:
TestAstVisitor(CPlusPlus::Document::Ptr doc);
explicit TestAstVisitor(CPlusPlus::Document::Ptr doc);
bool visit(CPlusPlus::CallAST *ast);
bool visit(CPlusPlus::CompoundStatementAST *ast);
......@@ -75,7 +75,7 @@ private:
class TestDataFunctionVisitor : public CPlusPlus::ASTVisitor
{
public:
TestDataFunctionVisitor(CPlusPlus::Document::Ptr doc);
explicit TestDataFunctionVisitor(CPlusPlus::Document::Ptr doc);
bool visit(CPlusPlus::UsingDirectiveAST *ast);
bool visit(CPlusPlus::FunctionDefinitionAST *ast);
......@@ -93,9 +93,9 @@ private:
QString m_currentFunction;
QMap<QString, TestCodeLocationList> m_dataTags;
TestCodeLocationList m_currentTags;
unsigned m_currentAstDepth;
unsigned m_insideUsingQTestDepth;
bool m_insideUsingQTest;
unsigned m_currentAstDepth = 0;
unsigned m_insideUsingQTestDepth = 0;
bool m_insideUsingQTest = false;
};
} // namespace Internal
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment