ClangTools: Generalize unit tests

Add an option to run different tools.

Change-Id: Iaf5c928663a888526eba9ee9d1fcea59e6083488
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-04-24 15:30:19 +02:00
parent b57ce2d7a6
commit 43072e82d2
6 changed files with 45 additions and 32 deletions

View File

@@ -59,13 +59,13 @@ FORMS += \
equals(TEST, 1) { equals(TEST, 1) {
HEADERS += \ HEADERS += \
clangstaticanalyzerpreconfiguredsessiontests.h \ clangstaticanalyzerpreconfiguredsessiontests.h \
clangstaticanalyzerunittests.h clangtoolsunittests.h
SOURCES += \ SOURCES += \
clangstaticanalyzerpreconfiguredsessiontests.cpp \ clangstaticanalyzerpreconfiguredsessiontests.cpp \
clangstaticanalyzerunittests.cpp clangtoolsunittests.cpp
RESOURCES += clangstaticanalyzerunittests.qrc RESOURCES += clangtoolsunittests.qrc
} }
DISTFILES += \ DISTFILES += \

View File

@@ -79,9 +79,9 @@ QtcPlugin {
files: [ files: [
"clangstaticanalyzerpreconfiguredsessiontests.cpp", "clangstaticanalyzerpreconfiguredsessiontests.cpp",
"clangstaticanalyzerpreconfiguredsessiontests.h", "clangstaticanalyzerpreconfiguredsessiontests.h",
"clangstaticanalyzerunittests.cpp", "clangtoolsunittests.cpp",
"clangstaticanalyzerunittests.h", "clangtoolsunittests.h",
"clangstaticanalyzerunittests.qrc", "clangtoolsunittests.qrc",
] ]
} }

View File

@@ -34,7 +34,7 @@
#ifdef WITH_TESTS #ifdef WITH_TESTS
#include "clangstaticanalyzerpreconfiguredsessiontests.h" #include "clangstaticanalyzerpreconfiguredsessiontests.h"
#include "clangstaticanalyzerunittests.h" #include "clangtoolsunittests.h"
#endif #endif
#include <debugger/analyzer/analyzericons.h> #include <debugger/analyzer/analyzericons.h>
@@ -135,7 +135,7 @@ QList<QObject *> ClangToolsPlugin::createTestObjects() const
QList<QObject *> tests; QList<QObject *> tests;
#ifdef WITH_TESTS #ifdef WITH_TESTS
tests << new ClangStaticAnalyzerPreconfiguredSessionTests; tests << new ClangStaticAnalyzerPreconfiguredSessionTests;
tests << new ClangStaticAnalyzerUnitTests; tests << new ClangToolsUnitTests;
#endif #endif
return tests; return tests;
} }

View File

@@ -23,10 +23,11 @@
** **
****************************************************************************/ ****************************************************************************/
#include "clangstaticanalyzerunittests.h" #include "clangtoolsunittests.h"
#include "clangtoolsdiagnostic.h" #include "clangtoolsdiagnostic.h"
#include "clangstaticanalyzertool.h" #include "clangstaticanalyzertool.h"
#include "clangtidyclazytool.h"
#include "clangtoolsutils.h" #include "clangtoolsutils.h"
#include <cpptools/cppmodelmanager.h> #include <cpptools/cppmodelmanager.h>
@@ -48,7 +49,7 @@ using namespace Utils;
namespace ClangTools { namespace ClangTools {
namespace Internal { namespace Internal {
void ClangStaticAnalyzerUnitTests::initTestCase() void ClangToolsUnitTests::initTestCase()
{ {
const QList<Kit *> allKits = KitManager::kits(); const QList<Kit *> allKits = KitManager::kits();
if (allKits.count() != 1) if (allKits.count() != 1)
@@ -66,13 +67,14 @@ void ClangStaticAnalyzerUnitTests::initTestCase()
QVERIFY(m_tmpDir->isValid()); QVERIFY(m_tmpDir->isValid());
} }
void ClangStaticAnalyzerUnitTests::cleanupTestCase() void ClangToolsUnitTests::cleanupTestCase()
{ {
delete m_tmpDir; delete m_tmpDir;
} }
void ClangStaticAnalyzerUnitTests::testProject() void ClangToolsUnitTests::testProject()
{ {
QFETCH(Tool, clangtool);
QFETCH(QString, projectFilePath); QFETCH(QString, projectFilePath);
QFETCH(int, expectedDiagCount); QFETCH(int, expectedDiagCount);
if (projectFilePath.contains("mingw")) { if (projectFilePath.contains("mingw")) {
@@ -86,7 +88,9 @@ void ClangStaticAnalyzerUnitTests::testProject()
CppTools::Tests::ProjectOpenerAndCloser projectManager; CppTools::Tests::ProjectOpenerAndCloser projectManager;
const CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true); const CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
QVERIFY(projectInfo.isValid()); QVERIFY(projectInfo.isValid());
auto tool = ClangStaticAnalyzerTool::instance(); ClangTool *tool = (clangtool == Tool::ClangStaticAnalyzer)
? ClangStaticAnalyzerTool::instance()
: static_cast<ClangTool *>(ClangTidyClazyTool::instance());
tool->startTool(); tool->startTool();
QSignalSpy waiter(tool, SIGNAL(finished(bool))); QSignalSpy waiter(tool, SIGNAL(finished(bool)));
QVERIFY(waiter.wait(30000)); QVERIFY(waiter.wait(30000));
@@ -95,37 +99,38 @@ void ClangStaticAnalyzerUnitTests::testProject()
QCOMPARE(tool->diagnostics().count(), expectedDiagCount); QCOMPARE(tool->diagnostics().count(), expectedDiagCount);
} }
void ClangStaticAnalyzerUnitTests::testProject_data() void ClangToolsUnitTests::testProject_data()
{ {
QTest::addColumn<Tool>("clangtool");
QTest::addColumn<QString>("projectFilePath"); QTest::addColumn<QString>("projectFilePath");
QTest::addColumn<int>("expectedDiagCount"); QTest::addColumn<int>("expectedDiagCount");
addTestRow("simple/simple.qbs", 1); addTestRow(Tool::ClangStaticAnalyzer, "simple/simple.qbs", 1);
addTestRow("simple/simple.pro", 1); addTestRow(Tool::ClangStaticAnalyzer, "simple/simple.pro", 1);
addTestRow("simple-library/simple-library.qbs", 0); addTestRow(Tool::ClangStaticAnalyzer, "simple-library/simple-library.qbs", 0);
addTestRow("simple-library/simple-library.pro", 0); addTestRow(Tool::ClangStaticAnalyzer, "simple-library/simple-library.pro", 0);
addTestRow("stdc++11-includes/stdc++11-includes.qbs", 0); addTestRow(Tool::ClangStaticAnalyzer, "stdc++11-includes/stdc++11-includes.qbs", 0);
addTestRow("stdc++11-includes/stdc++11-includes.pro", 0); addTestRow(Tool::ClangStaticAnalyzer, "stdc++11-includes/stdc++11-includes.pro", 0);
addTestRow("qt-widgets-app/qt-widgets-app.qbs", 0); addTestRow(Tool::ClangStaticAnalyzer, "qt-widgets-app/qt-widgets-app.qbs", 0);
addTestRow("qt-widgets-app/qt-widgets-app.pro", 0); addTestRow(Tool::ClangStaticAnalyzer, "qt-widgets-app/qt-widgets-app.pro", 0);
addTestRow("qt-essential-includes/qt-essential-includes.qbs", 0); addTestRow(Tool::ClangStaticAnalyzer, "qt-essential-includes/qt-essential-includes.qbs", 0);
addTestRow("qt-essential-includes/qt-essential-includes.pro", 0); addTestRow(Tool::ClangStaticAnalyzer, "qt-essential-includes/qt-essential-includes.pro", 0);
addTestRow("mingw-includes/mingw-includes.qbs", 0); addTestRow(Tool::ClangStaticAnalyzer, "mingw-includes/mingw-includes.qbs", 0);
addTestRow("mingw-includes/mingw-includes.pro", 0); addTestRow(Tool::ClangStaticAnalyzer, "mingw-includes/mingw-includes.pro", 0);
} }
void ClangStaticAnalyzerUnitTests::addTestRow(const QByteArray &relativeFilePath, void ClangToolsUnitTests::addTestRow(Tool tool, const QByteArray &relativeFilePath,
int expectedDiagCount) int expectedDiagCount)
{ {
const QString absoluteFilePath = m_tmpDir->absolutePath(relativeFilePath); const QString absoluteFilePath = m_tmpDir->absolutePath(relativeFilePath);
const QString fileName = QFileInfo(absoluteFilePath).fileName(); const QString fileName = QFileInfo(absoluteFilePath).fileName();
QTest::newRow(fileName.toUtf8().constData()) << absoluteFilePath << expectedDiagCount; QTest::newRow(fileName.toUtf8().constData()) << tool << absoluteFilePath << expectedDiagCount;
} }
} // namespace Internal } // namespace Internal

View File

@@ -32,12 +32,18 @@ namespace CppTools { namespace Tests { class TemporaryCopiedDir; } }
namespace ClangTools { namespace ClangTools {
namespace Internal { namespace Internal {
class ClangStaticAnalyzerUnitTests : public QObject enum class Tool
{
ClangStaticAnalyzer,
ClangTidyAndClazy
};
class ClangToolsUnitTests : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
ClangStaticAnalyzerUnitTests() {} ClangToolsUnitTests() {}
private slots: private slots:
void initTestCase(); void initTestCase();
@@ -46,7 +52,7 @@ private slots:
void testProject_data(); void testProject_data();
private: private:
void addTestRow(const QByteArray &relativeFilePath, int expectedDiagCount); void addTestRow(Tool tool, const QByteArray &relativeFilePath, int expectedDiagCount);
private: private:
CppTools::Tests::TemporaryCopiedDir *m_tmpDir = nullptr; CppTools::Tests::TemporaryCopiedDir *m_tmpDir = nullptr;
@@ -54,3 +60,5 @@ private:
} // namespace Internal } // namespace Internal
} // namespace ClangTools } // namespace ClangTools
Q_DECLARE_METATYPE(ClangTools::Internal::Tool)