clangcompletion_test.cpp 14 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
** 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.
**
****************************************************************************/

/**
 * @file clangcompletion_test.cpp
 * @brief Performs test for C/C++ code completion
 *
 * All test cases given as strings with @ character that points to completion
 * location.
 */

#ifdef WITH_TESTS

// Disabled because there still no tool to detect system Objective-C headers
#define ENABLE_OBJC_TESTS 0

#include <QtTest>
#include <QDebug>
#undef interface // Canceling "#DEFINE interface struct" on Windows

#include "completiontesthelper.h"
#include "../clangcodemodelplugin.h"

using namespace ClangCodeModel;
using namespace ClangCodeModel::Internal;

////////////////////////////////////////////////////////////////////////////////
// Test cases

/**
 * \defgroup Regression tests
 *
 * This group tests possible regressions in non-standard completion chunks
 * handling: for example, macro arguments and clang's code snippets.
 *
 * @{
 */

void ClangCodeModelPlugin::test_CXX_regressions()
{
    QFETCH(QString, file);
    QFETCH(QStringList, unexpected);
    QFETCH(QStringList, mustHave);

    CompletionTestHelper helper;
    helper << file;

    QStringList proposals = helper.codeCompleteTexts();

    foreach (const QString &p, unexpected)
        QTEST_ASSERT(false == proposals.contains(p));

    foreach (const QString &p, mustHave)
        QTEST_ASSERT(true == proposals.contains(p));
}

void ClangCodeModelPlugin::test_CXX_regressions_data()
{
    QTest::addColumn<QString>("file");
    QTest::addColumn<QStringList>("unexpected");
    QTest::addColumn<QStringList>("mustHave");

    QString file;
    QStringList unexpected;
    QStringList mustHave;

    file = QLatin1String("cxx_regression_1.cpp");
    mustHave << QLatin1String("sqr");
    mustHave << QLatin1String("~Math");
    unexpected << QLatin1String("operator=");
    QTest::newRow("case 1: method call completion") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_2.cpp");
    unexpected << QLatin1String("i_second");
    unexpected << QLatin1String("c_second");
    unexpected << QLatin1String("f_second");
    mustHave << QLatin1String("i_first");
    mustHave << QLatin1String("c_first");
    QTest::newRow("case 2: multiple anonymous structs") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_3.cpp");
    mustHave << QLatin1String("i8");
    mustHave << QLatin1String("i64");
    mustHave << QLatin1String("~Priv");
    unexpected << QLatin1String("operator=");
    QTest::newRow("case 3: nested class resolution") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_4.cpp");
    mustHave << QLatin1String("action");
    QTest::newRow("case 4: local (in function) class resolution") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_5.cpp");
    mustHave << QLatin1String("doB");
    unexpected << QLatin1String("doA");
    QTest::newRow("case 5: nested template class resolution") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_6.cpp");
    mustHave << QLatin1String("OwningPtr");
    QTest::newRow("case 6: using particular symbol from namespace") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_7.cpp");
    mustHave << QLatin1String("dataMember");
    mustHave << QLatin1String("anotherMember");
    QTest::newRow("case 7: template class inherited from template parameter") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_8.cpp");
    mustHave << QLatin1String("utils::");
    unexpected << QLatin1String("utils");
    QTest::newRow("case 8: namespace completion in function body") << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();

    file = QLatin1String("cxx_regression_9.cpp");
    mustHave << QLatin1String("EnumScoped::Value1");
    mustHave << QLatin1String("EnumScoped::Value2");
    mustHave << QLatin1String("EnumScoped::Value3");
    unexpected << QLatin1String("Value1");
    unexpected << QLatin1String("EnumScoped");
    QTest::newRow("case 9: c++11 enum class, value used in switch and 'case' completed")
            << file << unexpected << mustHave;
    mustHave.clear();
    unexpected.clear();
}

void ClangCodeModelPlugin::test_CXX_snippets()
{
    QFETCH(QString, file);
    QFETCH(QStringList, texts);
    QFETCH(QStringList, snippets);
    Q_ASSERT(texts.size() == snippets.size());

    CompletionTestHelper helper;
    helper << file;

    QList<CodeCompletionResult> proposals = helper.codeComplete();

    for (int i = 0, n = texts.size(); i < n; ++i) {
        const QString &text = texts[i];
        const QString &snippet = snippets[i];
        const QString snippetError =
                QLatin1String("Text and snippet mismatch: text '") + text
                + QLatin1String("', snippet '") + snippet
183
                + QLatin1String("', got snippet \"%1\"");
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

        bool hasText = false;
        foreach (const CodeCompletionResult &ccr, proposals) {
            if (ccr.text() != text)
                continue;
            hasText = true;
            QVERIFY2(snippet == ccr.snippet(), snippetError.arg(ccr.snippet()).toAscii());
        }
        const QString textError(QLatin1String("Text not found:") + text);
        QVERIFY2(hasText, textError.toAscii());
    }
}

void ClangCodeModelPlugin::test_CXX_snippets_data()
{
    QTest::addColumn<QString>("file");
    QTest::addColumn<QStringList>("texts");
    QTest::addColumn<QStringList>("snippets");

    QString file;
    QStringList texts;
    QStringList snippets;

    file = QLatin1String("cxx_snippets_1.cpp");
    texts << QLatin1String("reinterpret_cast<type>(expression)");
    snippets << QLatin1String("reinterpret_cast<$type$>($expression$)");

    texts << QLatin1String("static_cast<type>(expression)");
    snippets << QLatin1String("static_cast<$type$>($expression$)");

    texts << QLatin1String("new type(expressions)");
    snippets << QLatin1String("new $type$($expressions$)");

    QTest::newRow("case: snippets for var declaration") << file << texts << snippets;
    texts.clear();
    snippets.clear();

    file = QLatin1String("cxx_snippets_2.cpp");
    texts << QLatin1String("private");
    snippets << QLatin1String("");

    texts << QLatin1String("protected");
    snippets << QLatin1String("");

    texts << QLatin1String("public");
    snippets << QLatin1String("");

    texts << QLatin1String("friend");
    snippets << QLatin1String("");

    texts << QLatin1String("virtual");
    snippets << QLatin1String("");

    texts << QLatin1String("typedef type name");
    snippets << QLatin1String("typedef $type$ $name$");

    QTest::newRow("case: snippets inside class declaration") << file << texts << snippets;
    texts.clear();
    snippets.clear();

    file = QLatin1String("cxx_snippets_3.cpp");
    texts << QLatin1String("List");
    snippets << QLatin1String("List<$class Item$>");

    texts << QLatin1String("Tuple");
    snippets << QLatin1String("Tuple<$class First$, $class Second$, $typename Third$>");

    QTest::newRow("case: template class insertion as snippet") << file << texts << snippets;
    texts.clear();
    snippets.clear();

    file = QLatin1String("cxx_snippets_4.cpp");
    texts << QLatin1String("clamp");
    snippets << QLatin1String("");

    texts << QLatin1String("perform");
    snippets << QLatin1String("perform<$class T$>");

    QTest::newRow("case: template function insertion as snippet") << file << texts << snippets;
    texts.clear();
    snippets.clear();
}

void ClangCodeModelPlugin::test_ObjC_hints()
{
    QFETCH(QString, file);
    QFETCH(QStringList, texts);
    QFETCH(QStringList, snippets);
    QFETCH(QStringList, hints);
    Q_ASSERT(texts.size() == snippets.size());
    Q_ASSERT(texts.size() == hints.size());

    CompletionTestHelper helper;
    helper << file;

    QList<CodeCompletionResult> proposals = helper.codeComplete();

    for (int i = 0, n = texts.size(); i < n; ++i) {
        const QString &text = texts[i];
        const QString &snippet = snippets[i];
        const QString &hint = hints[i];
        const QString snippetError =
                QLatin1String("Text and snippet mismatch: text '") + text
                + QLatin1String("', snippet '") + snippet
288
                + QLatin1String("', got snippet \"%1\"");
289 290 291
        const QString hintError =
                QLatin1String("Text and hint mismatch: text '") + text
                + QLatin1String("', hint\n'") + hint
292
                + QLatin1String(", got hint\n\"%1\"");
293 294 295 296 297 298 299 300 301 302 303

        bool hasText = false;
        QStringList texts;
        foreach (const CodeCompletionResult &ccr, proposals) {
            texts << ccr.text();
            if (ccr.text() != text)
                continue;
            hasText = true;
            QVERIFY2(snippet == ccr.snippet(), snippetError.arg(ccr.snippet()).toAscii());
            QVERIFY2(hint == ccr.hint(), hintError.arg(ccr.hint()).toAscii());
        }
304
        const QString textError(QString::fromLatin1("Text \"%1\" not found in set %2")
Nikolai Kosjar's avatar
Nikolai Kosjar committed
305
                                .arg(text).arg(texts.join(QLatin1String(","))));
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        QVERIFY2(hasText, textError.toAscii());
    }
}

static QString makeObjCHint(const char *cHintPattern)
{
    QString hintPattern(QString::fromUtf8(cHintPattern));
    QStringList lines = hintPattern.split(QLatin1Char('\n'));
    QString hint = QLatin1String("<p>");
    bool prependNewline = false;
    foreach (const QString &line, lines) {
        if (prependNewline)
            hint += QLatin1String("<br/>");
        prependNewline = true;
        int i = 0;
        while (i < line.size() && line[i] == QLatin1Char(' ')) {
            ++i;
            hint += QLatin1String("&nbsp;");
        }
        hint += line.mid(i);
    }
    hint += QLatin1String("</p>");
    return hint;
}

void ClangCodeModelPlugin::test_ObjC_hints_data()
{
    QTest::addColumn<QString>("file");
    QTest::addColumn<QStringList>("texts");
    QTest::addColumn<QStringList>("snippets");
    QTest::addColumn<QStringList>("hints");

    QString file;
    QStringList texts;
    QStringList snippets;
    QStringList hints;

    file = QLatin1String("objc_messages_1.mm");
    texts << QLatin1String("spectacleQuality:");
    snippets << QLatin1String("spectacleQuality:$(bool)$");
    hints << makeObjCHint("-(int) spectacleQuality:<b>(bool)</b>");
    texts << QLatin1String("desiredAmountForDramaDose:andPersonsCount:");
    snippets << QLatin1String("desiredAmountForDramaDose:$(int)$ andPersonsCount:$(int)$");
    hints << makeObjCHint("-(int) desiredAmountForDramaDose:<b>(int)</b> \n"
                          "                 andPersonsCount:<b>(int)</b>");

    QTest::newRow("case: objective-c instance messages call") << file << texts << snippets << hints;
    texts.clear();
    snippets.clear();
    hints.clear();

    file = QLatin1String("objc_messages_2.mm");
    texts << QLatin1String("eatenAmount");
    snippets << QLatin1String("(int) eatenAmount");
    hints << makeObjCHint("+(int) eatenAmount");
    texts << QLatin1String("desiredAmountForDramaDose:andPersonsCount:");
    snippets << QLatin1String("(int) desiredAmountForDramaDose:(int)dose andPersonsCount:(int)count");
    hints << makeObjCHint("+(int) desiredAmountForDramaDose:(int)dose \n"
                          "                 andPersonsCount:(int)count");

    QTest::newRow("case: objective-c class messages in @implementation") << file << texts << snippets << hints;
    texts.clear();
    snippets.clear();
    hints.clear();

    file = QLatin1String("objc_messages_3.mm");
    texts << QLatin1String("eatenAmount");
    snippets << QLatin1String("(int) eatenAmount");
    hints << makeObjCHint("-(int) eatenAmount");
    texts << QLatin1String("spectacleQuality");
    snippets << QLatin1String("(int) spectacleQuality");
    hints << makeObjCHint("-(int) spectacleQuality");
    texts << QLatin1String("desiredAmountForDramaDose:andPersonsCount:");
    snippets << QLatin1String("(int) desiredAmountForDramaDose:(int)dose andPersonsCount:(int)count");
    hints << makeObjCHint("-(int) desiredAmountForDramaDose:(int)dose \n"
                          "                 andPersonsCount:(int)count");
    texts << QLatin1String("initWithOldTracker:");
    snippets << QLatin1String("(id) initWithOldTracker:(Bbbb<Aaaa> *)aabb");
    hints << makeObjCHint("-(id) initWithOldTracker:(Bbbb&lt;Aaaa&gt; *)aabb");

    QTest::newRow("case: objective-c class messages from base class") << file << texts << snippets << hints;
    texts.clear();
    snippets.clear();
    hints.clear();
}

#endif