cppdoxygen_test.cpp 7.3 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
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
** 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 31
#include "cppeditorplugin.h"
#include "cppeditortestcase.h"
32

33
#include <coreplugin/editormanager/editormanager.h>
34
#include <cpptools/cppmodelmanager.h>
35 36

#include <cplusplus/CppDocument.h>
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#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;

52
namespace { typedef QByteArray _; }
53

54 55 56 57
/**
 * Encapsulates the whole process of setting up an editor,
 * pressing ENTER and checking the result.
 */
58 59 60 61 62 63

namespace CppEditor {
namespace Internal {
namespace Tests {

class DoxygenTestCase : public Internal::Tests::TestCase
64
{
65
public:
66 67 68 69 70
    /// The '|' in the input denotes the cursor position.
    DoxygenTestCase(const QByteArray &original, const QByteArray &expected)
    {
        QVERIFY(succeededSoFar());

71
        TestDocument testDocument("file.cpp", original, '|');
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        QVERIFY(testDocument.hasCursorMarker());
        testDocument.m_source.remove(testDocument.m_cursorPosition, 1);
        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);

        // 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();
103
        const QString contentsAfterUndo = testDocument.m_editorWidget->document()->toPlainText();
104 105
        QCOMPARE(contentsAfterUndo, testDocument.m_source);
    }
106 107
};

108
} // namespace Tests
109

110
void CppEditorPlugin::test_doxygen_comments_data()
111
{
112 113 114 115
    QTest::addColumn<QByteArray>("given");
    QTest::addColumn<QByteArray>("expected");

    QTest::newRow("qt_style") << _(
116 117 118
        "bool preventFolding;\n"
        "/*!|\n"
        "int a;\n"
119
        ) << _(
120 121 122 123 124
        "bool preventFolding;\n"
        "/*!\n"
        " * \\brief a\n"
        " */\n"
        "int a;\n"
125
    );
126

127
    QTest::newRow("qt_style_continuation") << _(
128 129 130 131 132
        "bool preventFolding;\n"
        "/*!\n"
        " * \\brief a|\n"
        " */\n"
        "int a;\n"
133
        ) << _(
134 135 136
        "bool preventFolding;\n"
        "/*!\n"
        " * \\brief a\n"
137
        " * \n"
138 139
        " */\n"
        "int a;\n"
140
    );
141

142
    QTest::newRow("java_style") << _(
143 144 145
        "bool preventFolding;\n"
        "/**|\n"
        "int a;\n"
146
        ) << _(
147 148 149 150 151
        "bool preventFolding;\n"
        "/**\n"
        " * @brief a\n"
        " */\n"
        "int a;\n"
152
    );
153

154
    QTest::newRow("java_style_continuation") << _(
155 156 157 158 159
        "bool preventFolding;\n"
        "/**\n"
        " * @brief a|\n"
        " */\n"
        "int a;\n"
160
        ) << _(
161 162 163
        "bool preventFolding;\n"
        "/**\n"
        " * @brief a\n"
164
        " * \n"
165 166
        " */\n"
        "int a;\n"
167
    );
168

169
    QTest::newRow("cpp_styleA") << _(
170 171 172
         "bool preventFolding;\n"
         "///|\n"
         "int a;\n"
173
        ) << _(
174 175 176 177 178
         "bool preventFolding;\n"
         "///\n"
         "/// \\brief a\n"
         "///\n"
         "int a;\n"
179
    );
180

181
    QTest::newRow("cpp_styleB") << _(
182 183 184
         "bool preventFolding;\n"
         "//!|\n"
         "int a;\n"
185
        ) << _(
186 187 188 189 190
         "bool preventFolding;\n"
         "//!\n"
         "//! \\brief a\n"
         "//!\n"
         "int a;\n"
191
    );
192

193
    QTest::newRow("cpp_styleA_continuation") << _(
194 195 196 197 198
         "bool preventFolding;\n"
         "///\n"
         "/// \\brief a|\n"
         "///\n"
         "int a;\n"
199
        ) << _(
200 201 202
         "bool preventFolding;\n"
         "///\n"
         "/// \\brief a\n"
203
         "/// \n"
204 205
         "///\n"
         "int a;\n"
206
     );
207

208 209
    /// test cpp style doxygen comment when inside a indented scope
    QTest::newRow("cpp_styleA_indented") << _(
210 211 212
         "    bool preventFolding;\n"
         "    ///|\n"
         "    int a;\n"
213
        ) << _(
214 215 216 217 218
         "    bool preventFolding;\n"
         "    ///\n"
         "    /// \\brief a\n"
         "    ///\n"
         "    int a;\n"
219
    );
220

221 222
    /// test cpp style doxygen comment continuation when inside a indented scope
    QTest::newRow("cpp_styleA_indented_continuation") << _(
223 224 225 226 227
         "    bool preventFolding;\n"
         "    ///\n"
         "    /// \\brief a|\n"
         "    ///\n"
         "    int a;\n"
228
        ) << _(
229 230 231
         "    bool preventFolding;\n"
         "    ///\n"
         "    /// \\brief a\n"
232
         "    /// \n"
233 234
         "    ///\n"
         "    int a;\n"
235
    );
236

237
    QTest::newRow("cpp_styleA_corner_case") << _(
238 239 240
          "bool preventFolding;\n"
          "///\n"
          "void d(); ///|\n"
241
        ) << _(
242 243 244 245
            "bool preventFolding;\n"
            "///\n"
            "void d(); ///\n"
            "\n"
246 247
    );
}
248

249 250 251 252
void CppEditorPlugin::test_doxygen_comments()
{
    QFETCH(QByteArray, given);
    QFETCH(QByteArray, expected);
253
    Tests::DoxygenTestCase(given, expected);
254
}
255 256 257

} // namespace Internal
} // namespace CppEditor