Commit f0aea93b authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen

CppEditor: Tests: Move doxygen tests in separate test class

Change-Id: If27b4e11deafaefd95a5815466fc5fdac23ba30a
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@theqtcompany.com>
parent 4f7eb4e6
/****************************************************************************
**
** 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
**
** This file is part of Qt Creator.
......@@ -28,107 +28,43 @@
**
****************************************************************************/
#include "cppeditorplugin.h"
#include "cppdoxygen_test.h"
#include "cppeditortestcase.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cpptools/commentssettings.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cpptoolssettings.h>
#include <cplusplus/CppDocument.h>
#include <utils/fileutils.h>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QKeyEvent>
#include <QString>
#include <QtTest>
/*!
Tests for inserting doxygen comments.
*/
using namespace Core;
using namespace CPlusPlus;
namespace { typedef QByteArray _; }
/**
* Encapsulates the whole process of setting up an editor,
* pressing ENTER and checking the result.
*/
namespace CppEditor {
namespace Internal {
namespace Tests {
class DoxygenTestCase : public Internal::Tests::TestCase
void DoxygenTest::initTestCase()
{
QScopedPointer<CppTools::CommentsSettings> oldSettings;
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
// 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);
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);
}
verifyCleanState();
}
~DoxygenTestCase()
{
if (oldSettings)
CppTools::CppToolsSettings::instance()->setCommentsSettings(*oldSettings);
}
};
void DoxygenTest::cleanTestCase()
{
verifyCleanState();
}
} // namespace Tests
void DoxygenTest::cleanup()
{
if (oldSettings)
CppTools::CppToolsSettings::instance()->setCommentsSettings(*oldSettings);
QVERIFY(Core::EditorManager::closeAllEditors(false));
QVERIFY(TestCase::garbageCollectGlobalSnapshot());
}
void CppEditorPlugin::test_doxygen_comments_data()
void DoxygenTest::testBasic_data()
{
QTest::addColumn<QByteArray>("given");
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, 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>("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, expected);
......@@ -314,8 +250,65 @@ void CppEditorPlugin::test_doxygen_comments_no_leading_asterisks()
injection.m_enableDoxygen = true;
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 CppEditor
/****************************************************************************
**
** 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
......@@ -74,6 +74,7 @@ RESOURCES += \
equals(TEST, 1) {
HEADERS += \
cppeditortestcase.h \
cppdoxygen_test.h \
cppquickfix_test.h
SOURCES += \
cppdoxygen_test.cpp \
......
......@@ -56,7 +56,7 @@ QtcPlugin {
name: "Tests"
condition: project.testsEnabled
files: [
"cppdoxygen_test.cpp",
"cppdoxygen_test.cpp", "cppdoxygen_test.h",
"cppeditortestcase.cpp", "cppeditortestcase.h",
"cppincludehierarchy_test.cpp",
"cppquickfix_test.cpp",
......
......@@ -44,6 +44,10 @@
#include "cppsnippetprovider.h"
#include "cpptypehierarchy.h"
#ifdef WITH_TESTS
# include "cppdoxygen_test.h"
#endif
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.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()
{
if (currentCppEditorWidget()) {
......
......@@ -85,6 +85,9 @@ private slots:
void inspectCppCodeModel();
#ifdef WITH_TESTS
private:
QList<QObject *> createTestObjects() const override;
private slots:
// The following tests expect that no projects are loaded on start-up.
void test_SwitchMethodDeclarationDefinition_data();
......@@ -115,11 +118,6 @@ private slots:
void test_FollowSymbolUnderCursor_virtualFunctionCall();
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();
......
......@@ -344,18 +344,32 @@ IAssistProposalScopedPointer::~IAssistProposalScopedPointer()
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();
QVERIFY(mm);
QVERIFY(mm->projectInfos().isEmpty());
QVERIFY(mm->headerPaths().isEmpty());
QVERIFY(mm->definedMacros().isEmpty());
QVERIFY(mm->projectFiles().isEmpty());
QVERIFY(mm->snapshot().isEmpty());
QCOMPARE(mm->workingCopy().size(), 1);
QVERIFY(mm->workingCopy().contains(mm->configurationFileName()));
RETURN_FALSE_IF_NOT(mm);
RETURN_FALSE_IF_NOT(mm->projectInfos().isEmpty());
RETURN_FALSE_IF_NOT(mm->headerPaths().isEmpty());
RETURN_FALSE_IF_NOT(mm->definedMacros().isEmpty());
RETURN_FALSE_IF_NOT(mm->projectFiles().isEmpty());
RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty());
RETURN_FALSE_IF_NOT(mm->workingCopy().size() == 1);
RETURN_FALSE_IF_NOT(mm->workingCopy().contains(mm->configurationFileName()));
return true;
}
#undef RETURN_FALSE_IF_NOT
} // namespace Tests
} // namespace CppTools
......@@ -158,9 +158,9 @@ private:
class CPPTOOLS_EXPORT VerifyCleanCppModelManager
{
public:
VerifyCleanCppModelManager() { verify(); }
~VerifyCleanCppModelManager() { verify(); }
static void verify();
VerifyCleanCppModelManager();
~VerifyCleanCppModelManager();
static bool isClean();
};
class FileWriterAndRemover
......
......@@ -70,13 +70,13 @@ ModelManagerTestHelper::ModelManagerTestHelper(QObject *parent) :
this, &ModelManagerTestHelper::gcFinished);
cleanup();
Tests::VerifyCleanCppModelManager::verify();
QVERIFY(Tests::VerifyCleanCppModelManager::isClean());
}
ModelManagerTestHelper::~ModelManagerTestHelper()
{
cleanup();
Tests::VerifyCleanCppModelManager::verify();
QVERIFY(Tests::VerifyCleanCppModelManager::isClean());
}
void ModelManagerTestHelper::cleanup()
......
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