diff --git a/src/plugins/cppeditor/cppdoxygen_test.cpp b/src/plugins/cppeditor/cppdoxygen_test.cpp index d57e2b51140..07ac2e2492e 100644 --- a/src/plugins/cppeditor/cppdoxygen_test.cpp +++ b/src/plugins/cppeditor/cppdoxygen_test.cpp @@ -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 ** ** This file is part of Qt Creator. @@ -28,107 +28,43 @@ ** ****************************************************************************/ -#include "cppeditorplugin.h" +#include "cppdoxygen_test.h" + #include "cppeditortestcase.h" -#include -#include -#include #include -#include -#include - #include #include #include #include -#include #include -/*! - 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 oldSettings; -public: - /// The '|' in the input denotes the cursor position. - DoxygenTestCase(const QByteArray &original, const QByteArray &expected, - CppTools::CommentsSettings *settings = 0) - { - QVERIFY(succeededSoFar()); + verifyCleanState(); +} - // 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()); +void DoxygenTest::cleanTestCase() +{ + verifyCleanState(); +} - // Update Code Model - QVERIFY(parseFiles(testDocument.filePath())); +void DoxygenTest::cleanup() +{ + if (oldSettings) + CppTools::CppToolsSettings::instance()->setCommentsSettings(*oldSettings); + QVERIFY(Core::EditorManager::closeAllEditors(false)); + QVERIFY(TestCase::garbageCollectGlobalSnapshot()); +} - // 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); - } - - ~DoxygenTestCase() - { - if (oldSettings) - CppTools::CppToolsSettings::instance()->setCommentsSettings(*oldSettings); - } -}; - -} // namespace Tests - -void CppEditorPlugin::test_doxygen_comments_data() +void DoxygenTest::testBasic_data() { QTest::addColumn("given"); QTest::addColumn("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("given"); QTest::addColumn("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 diff --git a/src/plugins/cppeditor/cppdoxygen_test.h b/src/plugins/cppeditor/cppdoxygen_test.h new file mode 100644 index 00000000000..740d9dc18af --- /dev/null +++ b/src/plugins/cppeditor/cppdoxygen_test.h @@ -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 + +#include +#include + +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 oldSettings; +}; + +} // namespace Tests +} // namespace Internal +} // namespace CppEditor + +#endif // CPPDOXYGEN_TEST_H diff --git a/src/plugins/cppeditor/cppeditor.pro b/src/plugins/cppeditor/cppeditor.pro index 52dddae171a..b616682dce1 100644 --- a/src/plugins/cppeditor/cppeditor.pro +++ b/src/plugins/cppeditor/cppeditor.pro @@ -74,6 +74,7 @@ RESOURCES += \ equals(TEST, 1) { HEADERS += \ cppeditortestcase.h \ + cppdoxygen_test.h \ cppquickfix_test.h SOURCES += \ cppdoxygen_test.cpp \ diff --git a/src/plugins/cppeditor/cppeditor.qbs b/src/plugins/cppeditor/cppeditor.qbs index e6f6769fd9c..cb1248508e1 100644 --- a/src/plugins/cppeditor/cppeditor.qbs +++ b/src/plugins/cppeditor/cppeditor.qbs @@ -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", diff --git a/src/plugins/cppeditor/cppeditorplugin.cpp b/src/plugins/cppeditor/cppeditorplugin.cpp index 1ae3c964998..884b31e3ee6 100644 --- a/src/plugins/cppeditor/cppeditorplugin.cpp +++ b/src/plugins/cppeditor/cppeditorplugin.cpp @@ -44,6 +44,10 @@ #include "cppsnippetprovider.h" #include "cpptypehierarchy.h" +#ifdef WITH_TESTS +# include "cppdoxygen_test.h" +#endif + #include #include #include @@ -350,6 +354,15 @@ void CppEditorPlugin::inspectCppCodeModel() } } +#ifdef WITH_TESTS +QList CppEditorPlugin::createTestObjects() const +{ + return QList() + << new Tests::DoxygenTest + ; +} +#endif + void CppEditorPlugin::openTypeHierarchy() { if (currentCppEditorWidget()) { diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index 3bf01566785..53d85fffa50 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -85,6 +85,9 @@ private slots: void inspectCppCodeModel(); #ifdef WITH_TESTS +private: + QList 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(); diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index 4c3fbb9679f..f8b5f0475f1 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -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 diff --git a/src/plugins/cpptools/cpptoolstestcase.h b/src/plugins/cpptools/cpptoolstestcase.h index 6cf3ff054aa..c3f477d0a20 100644 --- a/src/plugins/cpptools/cpptoolstestcase.h +++ b/src/plugins/cpptools/cpptoolstestcase.h @@ -158,9 +158,9 @@ private: class CPPTOOLS_EXPORT VerifyCleanCppModelManager { public: - VerifyCleanCppModelManager() { verify(); } - ~VerifyCleanCppModelManager() { verify(); } - static void verify(); + VerifyCleanCppModelManager(); + ~VerifyCleanCppModelManager(); + static bool isClean(); }; class FileWriterAndRemover diff --git a/src/plugins/cpptools/modelmanagertesthelper.cpp b/src/plugins/cpptools/modelmanagertesthelper.cpp index 366a540ed9e..db96ab3709a 100644 --- a/src/plugins/cpptools/modelmanagertesthelper.cpp +++ b/src/plugins/cpptools/modelmanagertesthelper.cpp @@ -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()