cppdoxygen_test.cpp 7.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/****************************************************************************
**
** Copyright (C) 2013 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://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: 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.
**
****************************************************************************/

30
#include "cppeditor.h"
31 32
#include "cppeditorplugin.h"
#include "cppeditortestcase.h"
33

34 35
#include <coreplugin/editormanager/editormanager.h>
#include <cpptools/cppmodelmanagerinterface.h>
36 37

#include <cplusplus/CppDocument.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#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;
using namespace CppEditor::Internal;

namespace {
55 56 57

typedef QByteArray _;

58 59 60 61
/**
 * Encapsulates the whole process of setting up an editor,
 * pressing ENTER and checking the result.
 */
62
class TestCase : public CppEditor::Internal::Tests::TestCase
63
{
64
public:
65 66 67 68
    TestCase(const QByteArray &input);
    void run(const QByteArray &expected, int undoCount = 1);

private:
69
    CppEditor::Internal::Tests::TestDocument testDocument;
70 71 72 73
};

/// The '|' in the input denotes the cursor position.
TestCase::TestCase(const QByteArray &input)
74
    : testDocument("file.cpp", input, '|')
75
{
76 77 78
    QVERIFY(testDocument.hasCursorMarker());
    testDocument.m_source.remove(testDocument.m_cursorPosition, 1);
    QVERIFY(testDocument.writeToDisk());
79

80
    // Update Code Model
81
    QVERIFY(parseFiles(testDocument.filePath()));
82

83
    // Open Editor
84 85 86
    QVERIFY(openCppEditor(testDocument.filePath(), &testDocument.m_editor,
                          &testDocument.m_editorWidget));
    closeEditorAtEndOfTestCase(testDocument.m_editor);
87 88 89 90 91 92 93

    // 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.
94 95
//    testDocument.m_editorWidget->unfoldAll();
    testDocument.m_editor->setCursorPosition(testDocument.m_cursorPosition);
96

97
    waitForRehighlightedSemanticDocument(testDocument.m_editorWidget);
98 99 100 101 102 103
}

void TestCase::run(const QByteArray &expected, int undoCount)
{
    // Send 'ENTER' key press
    QKeyEvent event(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
104 105
    QCoreApplication::sendEvent(testDocument.m_editorWidget, &event);
    const QByteArray result = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
106 107 108 109

    QCOMPARE(QLatin1String(result), QLatin1String(expected));

    for (int i = 0; i < undoCount; ++i)
110 111 112 113
        testDocument.m_editorWidget->undo();
    const QByteArray contentsAfterUndo
        = testDocument.m_editorWidget->document()->toPlainText().toUtf8();
    QCOMPARE(contentsAfterUndo, testDocument.m_source);
114 115 116
}
} // anonymous namespace

117
void CppEditorPlugin::test_doxygen_comments_data()
118
{
119 120 121 122
    QTest::addColumn<QByteArray>("given");
    QTest::addColumn<QByteArray>("expected");

    QTest::newRow("qt_style") << _(
123 124 125
        "bool preventFolding;\n"
        "/*!|\n"
        "int a;\n"
126
        ) << _(
127 128 129 130 131
        "bool preventFolding;\n"
        "/*!\n"
        " * \\brief a\n"
        " */\n"
        "int a;\n"
132
    );
133

134
    QTest::newRow("qt_style_continuation") << _(
135 136 137 138 139
        "bool preventFolding;\n"
        "/*!\n"
        " * \\brief a|\n"
        " */\n"
        "int a;\n"
140
        ) << _(
141 142 143
        "bool preventFolding;\n"
        "/*!\n"
        " * \\brief a\n"
144
        " * \n"
145 146
        " */\n"
        "int a;\n"
147
    );
148

149
    QTest::newRow("java_style") << _(
150 151 152
        "bool preventFolding;\n"
        "/**|\n"
        "int a;\n"
153
        ) << _(
154 155 156 157 158
        "bool preventFolding;\n"
        "/**\n"
        " * @brief a\n"
        " */\n"
        "int a;\n"
159
    );
160

161
    QTest::newRow("java_style_continuation") << _(
162 163 164 165 166
        "bool preventFolding;\n"
        "/**\n"
        " * @brief a|\n"
        " */\n"
        "int a;\n"
167
        ) << _(
168 169 170
        "bool preventFolding;\n"
        "/**\n"
        " * @brief a\n"
171
        " * \n"
172 173
        " */\n"
        "int a;\n"
174
    );
175

176
    QTest::newRow("cpp_styleA") << _(
177 178 179
         "bool preventFolding;\n"
         "///|\n"
         "int a;\n"
180
        ) << _(
181 182 183 184 185
         "bool preventFolding;\n"
         "///\n"
         "/// \\brief a\n"
         "///\n"
         "int a;\n"
186
    );
187

188
    QTest::newRow("cpp_styleB") << _(
189 190 191
         "bool preventFolding;\n"
         "//!|\n"
         "int a;\n"
192
        ) << _(
193 194 195 196 197
         "bool preventFolding;\n"
         "//!\n"
         "//! \\brief a\n"
         "//!\n"
         "int a;\n"
198
    );
199

200
    QTest::newRow("cpp_styleA_continuation") << _(
201 202 203 204 205
         "bool preventFolding;\n"
         "///\n"
         "/// \\brief a|\n"
         "///\n"
         "int a;\n"
206
        ) << _(
207 208 209
         "bool preventFolding;\n"
         "///\n"
         "/// \\brief a\n"
210
         "/// \n"
211 212
         "///\n"
         "int a;\n"
213
     );
214

215 216
    /// test cpp style doxygen comment when inside a indented scope
    QTest::newRow("cpp_styleA_indented") << _(
217 218 219
         "    bool preventFolding;\n"
         "    ///|\n"
         "    int a;\n"
220
        ) << _(
221 222 223 224 225
         "    bool preventFolding;\n"
         "    ///\n"
         "    /// \\brief a\n"
         "    ///\n"
         "    int a;\n"
226
    );
227

228 229
    /// test cpp style doxygen comment continuation when inside a indented scope
    QTest::newRow("cpp_styleA_indented_continuation") << _(
230 231 232 233 234
         "    bool preventFolding;\n"
         "    ///\n"
         "    /// \\brief a|\n"
         "    ///\n"
         "    int a;\n"
235
        ) << _(
236 237 238
         "    bool preventFolding;\n"
         "    ///\n"
         "    /// \\brief a\n"
239
         "    /// \n"
240 241
         "    ///\n"
         "    int a;\n"
242
    );
243

244
    QTest::newRow("cpp_styleA_corner_case") << _(
245 246 247
          "bool preventFolding;\n"
          "///\n"
          "void d(); ///|\n"
248
        ) << _(
249 250 251 252
            "bool preventFolding;\n"
            "///\n"
            "void d(); ///\n"
            "\n"
253 254
    );
}
255

256 257 258 259
void CppEditorPlugin::test_doxygen_comments()
{
    QFETCH(QByteArray, given);
    QFETCH(QByteArray, expected);
260 261 262
    TestCase data(given);
    data.run(expected);
}