CppEditor: Tests: Move doxygen tests in separate test class

Change-Id: If27b4e11deafaefd95a5815466fc5fdac23ba30a
Reviewed-by: Erik Verbruggen <erik.verbruggen@theqtcompany.com>
This commit is contained in:
Nikolai Kosjar
2015-01-15 14:58:16 +01:00
committed by Erik Verbruggen
parent 4f7eb4e6b8
commit f0aea93b44
9 changed files with 197 additions and 107 deletions

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal ** Contact: http://www.qt-project.org/legal
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.
@@ -28,107 +28,43 @@
** **
****************************************************************************/ ****************************************************************************/
#include "cppeditorplugin.h" #include "cppdoxygen_test.h"
#include "cppeditortestcase.h" #include "cppeditortestcase.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cpptools/commentssettings.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cpptoolssettings.h> #include <cpptools/cpptoolssettings.h>
#include <cplusplus/CppDocument.h>
#include <utils/fileutils.h>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QKeyEvent> #include <QKeyEvent>
#include <QString>
#include <QtTest> #include <QtTest>
/*!
Tests for inserting doxygen comments.
*/
using namespace Core;
using namespace CPlusPlus;
namespace { typedef QByteArray _; } namespace { typedef QByteArray _; }
/**
* Encapsulates the whole process of setting up an editor,
* pressing ENTER and checking the result.
*/
namespace CppEditor { namespace CppEditor {
namespace Internal { namespace Internal {
namespace Tests { namespace Tests {
class DoxygenTestCase : public Internal::Tests::TestCase void DoxygenTest::initTestCase()
{ {
QScopedPointer<CppTools::CommentsSettings> oldSettings; verifyCleanState();
public:
/// The '|' in the input denotes the cursor position.
DoxygenTestCase(const QByteArray &original, const QByteArray &expected,
CppTools::CommentsSettings *settings = 0)
{
QVERIFY(succeededSoFar());
// Write files to disk
CppTools::Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
TestDocument testDocument("file.cpp", original, '|');
QVERIFY(testDocument.hasCursorMarker());
testDocument.m_source.remove(testDocument.m_cursorPosition, 1);
testDocument.setBaseDirectory(temporaryDir.path());
QVERIFY(testDocument.writeToDisk());
// Update Code Model
QVERIFY(parseFiles(testDocument.filePath()));
// Open Editor
QVERIFY(openCppEditor(testDocument.filePath(), &testDocument.m_editor,
&testDocument.m_editorWidget));
closeEditorAtEndOfTestCase(testDocument.m_editor);
if (settings) {
auto *cts = CppTools::CppToolsSettings::instance();
oldSettings.reset(new CppTools::CommentsSettings(cts->commentsSettings()));
cts->setCommentsSettings(*settings);
} }
// We want to test documents that start with a comment. By default, the void DoxygenTest::cleanTestCase()
// editor will fold the very first comment it encounters, assuming {
// it is a license header. Currently unfoldAll() does not work as verifyCleanState();
// expected (some blocks are still hidden in some test cases, so the
// cursor movements are not as expected). For the time being, we just
// prepend a declaration before the initial test comment.
// testDocument.m_editorWidget->unfoldAll();
testDocument.m_editor->setCursorPosition(testDocument.m_cursorPosition);
waitForRehighlightedSemanticDocument(testDocument.m_editorWidget);
// Send 'ENTER' key press
QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QCoreApplication::sendEvent(testDocument.m_editorWidget, &event);
const QByteArray result = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
QCOMPARE(QLatin1String(result), QLatin1String(expected));
testDocument.m_editorWidget->undo();
const QString contentsAfterUndo = testDocument.m_editorWidget->document()->toPlainText();
QCOMPARE(contentsAfterUndo, testDocument.m_source);
} }
~DoxygenTestCase() void DoxygenTest::cleanup()
{ {
if (oldSettings) if (oldSettings)
CppTools::CppToolsSettings::instance()->setCommentsSettings(*oldSettings); CppTools::CppToolsSettings::instance()->setCommentsSettings(*oldSettings);
QVERIFY(Core::EditorManager::closeAllEditors(false));
QVERIFY(TestCase::garbageCollectGlobalSnapshot());
} }
};
} // namespace Tests void DoxygenTest::testBasic_data()
void CppEditorPlugin::test_doxygen_comments_data()
{ {
QTest::addColumn<QByteArray>("given"); QTest::addColumn<QByteArray>("given");
QTest::addColumn<QByteArray>("expected"); QTest::addColumn<QByteArray>("expected");
@@ -285,14 +221,14 @@ void CppEditorPlugin::test_doxygen_comments_data()
); );
} }
void CppEditorPlugin::test_doxygen_comments() void DoxygenTest::testBasic()
{ {
QFETCH(QByteArray, given); QFETCH(QByteArray, given);
QFETCH(QByteArray, expected); QFETCH(QByteArray, expected);
Tests::DoxygenTestCase(given, expected); runTest(given, expected);
} }
void CppEditorPlugin::test_doxygen_comments_no_leading_asterisks_data() void DoxygenTest::testNoLeadingAsterisks_data()
{ {
QTest::addColumn<QByteArray>("given"); QTest::addColumn<QByteArray>("given");
QTest::addColumn<QByteArray>("expected"); QTest::addColumn<QByteArray>("expected");
@@ -305,7 +241,7 @@ void CppEditorPlugin::test_doxygen_comments_no_leading_asterisks_data()
); );
} }
void CppEditorPlugin::test_doxygen_comments_no_leading_asterisks() void DoxygenTest::testNoLeadingAsterisks()
{ {
QFETCH(QByteArray, given); QFETCH(QByteArray, given);
QFETCH(QByteArray, expected); QFETCH(QByteArray, expected);
@@ -314,8 +250,65 @@ void CppEditorPlugin::test_doxygen_comments_no_leading_asterisks()
injection.m_enableDoxygen = true; injection.m_enableDoxygen = true;
injection.m_leadingAsterisks = false; injection.m_leadingAsterisks = false;
Tests::DoxygenTestCase(given, expected, &injection); runTest(given, expected, &injection);
} }
void DoxygenTest::verifyCleanState() const
{
QVERIFY(CppTools::Tests::VerifyCleanCppModelManager::isClean());
QVERIFY(Core::DocumentModel::openedDocuments().isEmpty());
QVERIFY(Core::EditorManager::visibleEditors().isEmpty());
}
/// The '|' in the input denotes the cursor position.
void DoxygenTest::runTest(const QByteArray &original, const QByteArray &expected,
CppTools::CommentsSettings *settings)
{
// Write files to disk
CppTools::Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
TestDocument testDocument("file.cpp", original, '|');
QVERIFY(testDocument.hasCursorMarker());
testDocument.m_source.remove(testDocument.m_cursorPosition, 1);
testDocument.setBaseDirectory(temporaryDir.path());
QVERIFY(testDocument.writeToDisk());
// Update Code Model
QVERIFY(TestCase::parseFiles(testDocument.filePath()));
// Open Editor
QVERIFY(TestCase::openCppEditor(testDocument.filePath(), &testDocument.m_editor,
&testDocument.m_editorWidget));
if (settings) {
auto *cts = CppTools::CppToolsSettings::instance();
oldSettings.reset(new CppTools::CommentsSettings(cts->commentsSettings()));
cts->setCommentsSettings(*settings);
}
// We want to test documents that start with a comment. By default, the
// editor will fold the very first comment it encounters, assuming
// it is a license header. Currently unfoldAll() does not work as
// expected (some blocks are still hidden in some test cases, so the
// cursor movements are not as expected). For the time being, we just
// prepend a declaration before the initial test comment.
// testDocument.m_editorWidget->unfoldAll();
testDocument.m_editor->setCursorPosition(testDocument.m_cursorPosition);
TestCase::waitForRehighlightedSemanticDocument(testDocument.m_editorWidget);
// Send 'ENTER' key press
QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
QCoreApplication::sendEvent(testDocument.m_editorWidget, &event);
const QByteArray result = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
QCOMPARE(QLatin1String(result), QLatin1String(expected));
testDocument.m_editorWidget->undo();
const QString contentsAfterUndo = testDocument.m_editorWidget->document()->toPlainText();
QCOMPARE(contentsAfterUndo, testDocument.m_source);
}
} // namespace Tests
} // namespace Internal } // namespace Internal
} // namespace CppEditor } // namespace CppEditor

View File

@@ -0,0 +1,71 @@
/****************************************************************************
**
** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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 Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPPDOXYGEN_TEST_H
#define CPPDOXYGEN_TEST_H
#include <cpptools/commentssettings.h>
#include <QObject>
#include <QScopedPointer>
namespace CppEditor {
namespace Internal {
namespace Tests {
/// Tests for inserting doxygen comments.
class DoxygenTest: public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanTestCase();
void cleanup();
void testBasic_data();
void testBasic();
void testNoLeadingAsterisks_data();
void testNoLeadingAsterisks();
private:
void verifyCleanState() const;
void runTest(const QByteArray &original, const QByteArray &expected,
CppTools::CommentsSettings *settings = 0);
QScopedPointer<CppTools::CommentsSettings> oldSettings;
};
} // namespace Tests
} // namespace Internal
} // namespace CppEditor
#endif // CPPDOXYGEN_TEST_H

View File

@@ -74,6 +74,7 @@ RESOURCES += \
equals(TEST, 1) { equals(TEST, 1) {
HEADERS += \ HEADERS += \
cppeditortestcase.h \ cppeditortestcase.h \
cppdoxygen_test.h \
cppquickfix_test.h cppquickfix_test.h
SOURCES += \ SOURCES += \
cppdoxygen_test.cpp \ cppdoxygen_test.cpp \

View File

@@ -56,7 +56,7 @@ QtcPlugin {
name: "Tests" name: "Tests"
condition: project.testsEnabled condition: project.testsEnabled
files: [ files: [
"cppdoxygen_test.cpp", "cppdoxygen_test.cpp", "cppdoxygen_test.h",
"cppeditortestcase.cpp", "cppeditortestcase.h", "cppeditortestcase.cpp", "cppeditortestcase.h",
"cppincludehierarchy_test.cpp", "cppincludehierarchy_test.cpp",
"cppquickfix_test.cpp", "cppquickfix_test.cpp",

View File

@@ -44,6 +44,10 @@
#include "cppsnippetprovider.h" #include "cppsnippetprovider.h"
#include "cpptypehierarchy.h" #include "cpptypehierarchy.h"
#ifdef WITH_TESTS
# include "cppdoxygen_test.h"
#endif
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
@@ -350,6 +354,15 @@ void CppEditorPlugin::inspectCppCodeModel()
} }
} }
#ifdef WITH_TESTS
QList<QObject *> CppEditorPlugin::createTestObjects() const
{
return QList<QObject *>()
<< new Tests::DoxygenTest
;
}
#endif
void CppEditorPlugin::openTypeHierarchy() void CppEditorPlugin::openTypeHierarchy()
{ {
if (currentCppEditorWidget()) { if (currentCppEditorWidget()) {

View File

@@ -85,6 +85,9 @@ private slots:
void inspectCppCodeModel(); void inspectCppCodeModel();
#ifdef WITH_TESTS #ifdef WITH_TESTS
private:
QList<QObject *> createTestObjects() const override;
private slots: private slots:
// The following tests expect that no projects are loaded on start-up. // The following tests expect that no projects are loaded on start-up.
void test_SwitchMethodDeclarationDefinition_data(); void test_SwitchMethodDeclarationDefinition_data();
@@ -115,11 +118,6 @@ private slots:
void test_FollowSymbolUnderCursor_virtualFunctionCall(); void test_FollowSymbolUnderCursor_virtualFunctionCall();
void test_FollowSymbolUnderCursor_virtualFunctionCall_multipleDocuments(); void test_FollowSymbolUnderCursor_virtualFunctionCall_multipleDocuments();
void test_doxygen_comments_data();
void test_doxygen_comments();
void test_doxygen_comments_no_leading_asterisks_data();
void test_doxygen_comments_no_leading_asterisks();
void test_quickfix_data(); void test_quickfix_data();
void test_quickfix(); void test_quickfix();

View File

@@ -344,18 +344,32 @@ IAssistProposalScopedPointer::~IAssistProposalScopedPointer()
delete d->model(); delete d->model();
} }
void VerifyCleanCppModelManager::verify() VerifyCleanCppModelManager::VerifyCleanCppModelManager()
{
QVERIFY(isClean());
}
VerifyCleanCppModelManager::~VerifyCleanCppModelManager() {
QVERIFY(isClean());
}
#define RETURN_FALSE_IF_NOT(check) if (!(check)) return false;
bool VerifyCleanCppModelManager::isClean()
{ {
CppModelManager *mm = CppModelManager::instance(); CppModelManager *mm = CppModelManager::instance();
QVERIFY(mm); RETURN_FALSE_IF_NOT(mm);
QVERIFY(mm->projectInfos().isEmpty()); RETURN_FALSE_IF_NOT(mm->projectInfos().isEmpty());
QVERIFY(mm->headerPaths().isEmpty()); RETURN_FALSE_IF_NOT(mm->headerPaths().isEmpty());
QVERIFY(mm->definedMacros().isEmpty()); RETURN_FALSE_IF_NOT(mm->definedMacros().isEmpty());
QVERIFY(mm->projectFiles().isEmpty()); RETURN_FALSE_IF_NOT(mm->projectFiles().isEmpty());
QVERIFY(mm->snapshot().isEmpty()); RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty());
QCOMPARE(mm->workingCopy().size(), 1); RETURN_FALSE_IF_NOT(mm->workingCopy().size() == 1);
QVERIFY(mm->workingCopy().contains(mm->configurationFileName())); RETURN_FALSE_IF_NOT(mm->workingCopy().contains(mm->configurationFileName()));
return true;
} }
#undef RETURN_FALSE_IF_NOT
} // namespace Tests } // namespace Tests
} // namespace CppTools } // namespace CppTools

View File

@@ -158,9 +158,9 @@ private:
class CPPTOOLS_EXPORT VerifyCleanCppModelManager class CPPTOOLS_EXPORT VerifyCleanCppModelManager
{ {
public: public:
VerifyCleanCppModelManager() { verify(); } VerifyCleanCppModelManager();
~VerifyCleanCppModelManager() { verify(); } ~VerifyCleanCppModelManager();
static void verify(); static bool isClean();
}; };
class FileWriterAndRemover class FileWriterAndRemover

View File

@@ -70,13 +70,13 @@ ModelManagerTestHelper::ModelManagerTestHelper(QObject *parent) :
this, &ModelManagerTestHelper::gcFinished); this, &ModelManagerTestHelper::gcFinished);
cleanup(); cleanup();
Tests::VerifyCleanCppModelManager::verify(); QVERIFY(Tests::VerifyCleanCppModelManager::isClean());
} }
ModelManagerTestHelper::~ModelManagerTestHelper() ModelManagerTestHelper::~ModelManagerTestHelper()
{ {
cleanup(); cleanup();
Tests::VerifyCleanCppModelManager::verify(); QVERIFY(Tests::VerifyCleanCppModelManager::isClean());
} }
void ModelManagerTestHelper::cleanup() void ModelManagerTestHelper::cleanup()