cppquickfix_test.cpp 85.7 KB
Newer Older
1
2
/****************************************************************************
**
3
** Copyright (C) 2013 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
#include "cppeditor.h"
31
#include "cppeditorplugin.h"
32
33
#include "cppquickfixassistant.h"
#include "cppquickfixes.h"
34

35
#include <cpptools/cppcodestylepreferences.h>
36
37
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cpppreprocessor.h>
38
#include <cpptools/cpptoolssettings.h>
39
#include <cpptools/includeutils.h>
40

41
42
43
44
#include <utils/fileutils.h>

#include <QDebug>
#include <QDir>
45
46
#include <QtTest>

47
48
49
/*!
    Tests for quick-fixes.
 */
50
using namespace Core;
51
52
53
54
using namespace CPlusPlus;
using namespace CppEditor;
using namespace CppEditor::Internal;
using namespace CppTools;
55
using namespace IncludeUtils;
56
57
58
using namespace TextEditor;

namespace {
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

class TestDocument;
typedef QSharedPointer<TestDocument> TestDocumentPtr;

/**
 * Represents a test document before and after applying the quick fix.
 *
 * A TestDocument's originalSource may contain an '@' character to denote
 * the cursor position. This marker is removed before the Editor reads
 * the document.
 */
class TestDocument
{
public:
    TestDocument(const QByteArray &theOriginalSource, const QByteArray &theExpectedSource,
                 const QString &fileName)
        : originalSource(theOriginalSource)
        , expectedSource(theExpectedSource)
        , fileName(fileName)
        , cursorMarkerPosition(theOriginalSource.indexOf('@'))
        , editor(0)
        , editorWidget(0)
    {
        originalSource.remove(cursorMarkerPosition, 1);
        expectedSource.remove(theExpectedSource.indexOf('@'), 1);
    }

    static TestDocumentPtr create(const QByteArray &theOriginalSource,
        const QByteArray &theExpectedSource,const QString &fileName)
    {
        return TestDocumentPtr(new TestDocument(theOriginalSource, theExpectedSource, fileName));
    }

    bool hasCursorMarkerPosition() const { return cursorMarkerPosition != -1; }
    bool changesExpected() const { return originalSource != expectedSource; }

    QString filePath() const
    {
97
98
99
        if (!QFileInfo(fileName).isAbsolute())
            return QDir::tempPath() + QLatin1Char('/') + fileName;
        return fileName;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    }

    void writeToDisk() const
    {
        Utils::FileSaver srcSaver(filePath());
        srcSaver.write(originalSource);
        srcSaver.finalize();
    }

    QByteArray originalSource;
    QByteArray expectedSource;

    const QString fileName;
    const int cursorMarkerPosition;

    CPPEditor *editor;
    CPPEditorWidget *editorWidget;
};

119
120
121
122
/**
 * Encapsulates the whole process of setting up an editor, getting the
 * quick-fix, applying it, and checking the result.
 */
123
class TestCase
124
{
125
126
127
128
129
public:
    TestCase(const QByteArray &originalSource, const QByteArray &expectedSource,
             const QStringList &includePaths = QStringList());
    TestCase(const QList<TestDocumentPtr> theTestFiles,
             const QStringList &includePaths = QStringList());
130
131
    ~TestCase();

132
133
    QuickFixOperation::Ptr getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget,
                                  int resultIndex = 0);
134
    TestDocumentPtr testFileWithCursorMarker() const;
135

136
    void run(CppQuickFixFactory *factory, int resultIndex = 0);
137
138
139
140

private:
    TestCase(const TestCase &);
    TestCase &operator=(const TestCase &);
141

142
143
144
145
146
147
148
149
150
151
    void init(const QStringList &includePaths);

private:
    QList<TestDocumentPtr> testFiles;

    CppCodeStylePreferences *cppCodeStylePreferences;
    QString cppCodeStylePreferencesOriginalDelegateId;

    QStringList includePathsToRestore;
    bool restoreIncludePaths;
152
153
};

154
155
156
/// Apply the factory on the source and get back the resultIndex'th result or a null pointer.
QuickFixOperation::Ptr TestCase::getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget,
                                        int resultIndex)
157
158
159
160
{
    CppQuickFixInterface qfi(new CppQuickFixAssistInterface(editorWidget, ExplicitlyInvoked));
    TextEditor::QuickFixOperations results;
    factory->match(qfi, results);
161
    return results.isEmpty() ? QuickFixOperation::Ptr() : results.at(resultIndex);
162
163
}

164
/// The '@' in the originalSource is the position from where the quick-fix discovery is triggered.
165
166
167
TestCase::TestCase(const QByteArray &originalSource, const QByteArray &expectedSource,
                   const QStringList &includePaths)
    : cppCodeStylePreferences(0), restoreIncludePaths(false)
168
{
169
    testFiles << TestDocument::create(originalSource, expectedSource, QLatin1String("file.cpp"));
170
    init(includePaths);
171
172
173
}

/// Exactly one TestFile must contain the cursor position marker '@' in the originalSource.
174
175
TestCase::TestCase(const QList<TestDocumentPtr> theTestFiles, const QStringList &includePaths)
    : testFiles(theTestFiles), cppCodeStylePreferences(0), restoreIncludePaths(false)
176
{
177
    init(includePaths);
178
179
}

180
void TestCase::init(const QStringList &includePaths)
181
182
183
184
185
186
{
    // Check if there is exactly one cursor marker
    unsigned cursorMarkersCount = 0;
    foreach (const TestDocumentPtr testFile, testFiles) {
        if (testFile->hasCursorMarkerPosition())
            ++cursorMarkersCount;
187
    }
188
    QVERIFY2(cursorMarkersCount == 1, "Exactly one cursor marker is allowed.");
189

190
    // Write files to disk
191
    foreach (TestDocumentPtr testFile, testFiles)
192
        testFile->writeToDisk();
193
194
195
196
197
198
199
200

    CppTools::Internal::CppModelManager *cmm = CppTools::Internal::CppModelManager::instance();

    // Set appropriate include paths
    if (!includePaths.isEmpty()) {
        restoreIncludePaths = true;
        includePathsToRestore = cmm->includePaths();
        cmm->setIncludePaths(includePaths);
201
    }
202

203
204
205
206
    // Update Code Model
    QStringList filePaths;
    foreach (const TestDocumentPtr &testFile, testFiles)
        filePaths << testFile->filePath();
207
    cmm->updateSourceFiles(filePaths);
208
209
210
211

    // Wait for the parser in the future to give us the document
    QStringList filePathsNotYetInSnapshot(filePaths);
    forever {
212
        Snapshot snapshot = cmm->snapshot();
213
214
215
216
217
218
        foreach (const QString &filePath, filePathsNotYetInSnapshot) {
            if (snapshot.contains(filePath))
                filePathsNotYetInSnapshot.removeOne(filePath);
        }
        if (filePathsNotYetInSnapshot.isEmpty())
            break;
219
        QCoreApplication::processEvents();
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    }

    // Open Files
    foreach (TestDocumentPtr testFile, testFiles) {
        testFile->editor
            = dynamic_cast<CPPEditor *>(EditorManager::openEditor(testFile->filePath()));
        QVERIFY(testFile->editor);

        // Set cursor position
        const int cursorPosition = testFile->hasCursorMarkerPosition()
                ? testFile->cursorMarkerPosition : 0;
        testFile->editor->setCursorPosition(cursorPosition);

        testFile->editorWidget = dynamic_cast<CPPEditorWidget *>(testFile->editor->editorWidget());
        QVERIFY(testFile->editorWidget);

        // Rehighlight
        testFile->editorWidget->semanticRehighlight(true);
        // Wait for the semantic info from the future
        while (testFile->editorWidget->semanticInfo().doc.isNull())
            QCoreApplication::processEvents();
    }
242
243
244
245
246
247
248

    // Enforce the default cpp code style, so we are independent of config file settings.
    // This is needed by e.g. the GenerateGetterSetter quick fix.
    cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
    QVERIFY(cppCodeStylePreferences);
    cppCodeStylePreferencesOriginalDelegateId = cppCodeStylePreferences->currentDelegateId();
    cppCodeStylePreferences->setCurrentDelegate(QLatin1String("qt"));
249
250
251
252
}

TestCase::~TestCase()
{
253
254
255
256
257
258
259
260
261
262
263
    // Restore default cpp code style
    if (cppCodeStylePreferences)
        cppCodeStylePreferences->setCurrentDelegate(cppCodeStylePreferencesOriginalDelegateId);

    // Close editors
    QList<Core::IEditor *> editorsToClose;
    foreach (const TestDocumentPtr testFile, testFiles) {
        if (testFile->editor)
            editorsToClose << testFile->editor;
    }
    EditorManager::instance()->closeEditors(editorsToClose, false);
264
265
    QCoreApplication::processEvents(); // process any pending events

266
    // Remove the test files from the code-model
267
    CppModelManagerInterface *mmi = CppModelManagerInterface::instance();
268
269
    mmi->GC();
    QCOMPARE(mmi->snapshot().size(), 0);
270
271
272
273
274
275
276
277

    // Restore include paths
    if (restoreIncludePaths)
        CppTools::Internal::CppModelManager::instance()->setIncludePaths(includePathsToRestore);

    // Remove created files from file system
    foreach (const TestDocumentPtr &testDocument, testFiles)
        QVERIFY(QFile::remove(testDocument->filePath()));
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
}

/// Leading whitespace is not removed, so we can check if the indetation ranges
/// have been set correctly by the quick-fix.
QByteArray &removeTrailingWhitespace(QByteArray &input)
{
    QList<QByteArray> lines = input.split('\n');
    input.resize(0);
    foreach (QByteArray line, lines) {
        while (line.length() > 0) {
            char lastChar = line[line.length() - 1];
            if (lastChar == ' ' || lastChar == '\t')
                line = line.left(line.length() - 1);
            else
                break;
        }
        input.append(line);
        input.append('\n');
    }
    return input;
}

300
void TestCase::run(CppQuickFixFactory *factory, int resultIndex)
301
{
302
303
304
305
306
307
308
309
    // Run the fix in the file having the cursor marker
    TestDocumentPtr testFile;
    foreach (const TestDocumentPtr file, testFiles) {
        if (file->hasCursorMarkerPosition())
            testFile = file;
    }
    QVERIFY2(testFile, "No test file with cursor marker found");

310
    if (QuickFixOperation::Ptr fix = getFix(factory, testFile->editorWidget, resultIndex))
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
        fix->perform();
    else
        qDebug() << "Quickfix was not triggered";

    // Compare all files
    foreach (const TestDocumentPtr testFile, testFiles) {
        // Check
        QByteArray result = testFile->editorWidget->document()->toPlainText().toUtf8();
        removeTrailingWhitespace(result);
        QCOMPARE(QLatin1String(result), QLatin1String(testFile->expectedSource));

        // Undo the change
        for (int i = 0; i < 100; ++i)
            testFile->editorWidget->undo();
        result = testFile->editorWidget->document()->toPlainText().toUtf8();
        QCOMPARE(result, testFile->originalSource);
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
/// Delegates directly to AddIncludeForUndefinedIdentifierOp for easier testing.
class AddIncludeForUndefinedIdentifierTestFactory : public CppQuickFixFactory
{
public:
    AddIncludeForUndefinedIdentifierTestFactory(const QString &include)
        : m_include(include) {}

    void match(const CppQuickFixInterface &interface, QuickFixOperations &result)
    {
        result += CppQuickFixOperation::Ptr(
            new AddIncludeForUndefinedIdentifierOp(interface, 0, m_include));
    }

private:
    const QString m_include;
};

QString globalIncludePath()
{
    const QString path = QLatin1String(SRCDIR)
        + QLatin1String("/../../../tests/auto/cplusplus/preprocessor/data/include-data/global");
    return QDir::cleanPath(path);
}

QString directoryOfTestFile()
{
    const QString path = QLatin1String(SRCDIR)
        + QLatin1String("/../../../tests/auto/cplusplus/preprocessor/data/include-data/local");
    return QDir::cleanPath(path);
}

361
362
} // anonymous namespace

363
364
365
366
/// Checks:
/// 1. If the name does not start with ("m_" or "_") and does not
///    end with "_", we are forced to prefix the getter with "get".
/// 2. Setter: Use pass by value on integer/float and pointer types.
367
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithPrefix()
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
393
394
395
396
397
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    int @it;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    int it;\n"
        "\n"
        "public:\n"
        "    int getIt() const;\n"
        "    void setIt(int value);\n"
        "};\n"
        "\n"
        "int Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        "void Something::setIt(int value)\n"
        "{\n"
        "    it = value;\n"
        "}\n"
        "\n"
        ;
398

399
    GenerateGetterSetter factory;
400
401
    TestCase data(original, expected);
    data.run(&factory);
402
403
404
405
406
}

/// Checks:
/// 1. Getter: "get" prefix is not necessary.
/// 2. Setter: Parameter name is base name.
407
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithoutPrefix()
408
{
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    int @m_it;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    int m_it;\n"
        "\n"
        "public:\n"
        "    int it() const;\n"
        "    void setIt(int it);\n"
        "};\n"
        "\n"
        "int Something::it() const\n"
        "{\n"
        "    return m_it;\n"
        "}\n"
        "\n"
        "void Something::setIt(int it)\n"
        "{\n"
        "    m_it = it;\n"
        "}\n"
        "\n"
        ;
438

439
    GenerateGetterSetter factory;
440
441
    TestCase data(original, expected);
    data.run(&factory);
442
443
444
445
}

/// Check: Setter: Use pass by reference for parameters which
/// are not integer, float or pointers.
446
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_customType()
447
{
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    MyType @it;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    MyType it;\n"
        "\n"
        "public:\n"
        "    MyType getIt() const;\n"
        "    void setIt(const MyType &value);\n"
        "};\n"
        "\n"
        "MyType Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        "void Something::setIt(const MyType &value)\n"
        "{\n"
        "    it = value;\n"
        "}\n"
        "\n"
        ;
477

478
    GenerateGetterSetter factory;
479
480
    TestCase data(original, expected);
    data.run(&factory);
481
482
483
484
485
}

/// Checks:
/// 1. Setter: No setter is generated for const members.
/// 2. Getter: Return a non-const type since it pass by value anyway.
486
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_constMember()
487
{
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    const int @it;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    const int it;\n"
        "\n"
        "public:\n"
        "    int getIt() const;\n"
        "};\n"
        "\n"
        "int Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        ;
511

512
    GenerateGetterSetter factory;
513
514
    TestCase data(original, expected);
    data.run(&factory);
515
516
517
}

/// Checks: No special treatment for pointer to non const.
518
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_pointerToNonConst()
519
{
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    int *it@;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    int *it;\n"
        "\n"
        "public:\n"
        "    int *getIt() const;\n"
        "    void setIt(int *value);\n"
        "};\n"
        "\n"
        "int *Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        "void Something::setIt(int *value)\n"
        "{\n"
        "    it = value;\n"
        "}\n"
        "\n"
        ;
549

550
    GenerateGetterSetter factory;
551
552
    TestCase data(original, expected);
    data.run(&factory);
553
554
555
}

/// Checks: No special treatment for pointer to const.
556
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_pointerToConst()
557
{
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    const int *it@;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    const int *it;\n"
        "\n"
        "public:\n"
        "    const int *getIt() const;\n"
        "    void setIt(const int *value);\n"
        "};\n"
        "\n"
        "const int *Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        "void Something::setIt(const int *value)\n"
        "{\n"
        "    it = value;\n"
        "}\n"
        "\n"
        ;
587

588
    GenerateGetterSetter factory;
589
590
    TestCase data(original, expected);
    data.run(&factory);
591
592
593
594
595
}

/// Checks:
/// 1. Setter: Setter is a static function.
/// 2. Getter: Getter is a static, non const function.
596
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_staticMember()
597
{
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    static int @m_member;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    static int m_member;\n"
        "\n"
        "public:\n"
        "    static int member();\n"
        "    static void setMember(int member);\n"
        "};\n"
        "\n"
        "int Something::member()\n"
        "{\n"
        "    return m_member;\n"
        "}\n"
        "\n"
        "void Something::setMember(int member)\n"
        "{\n"
        "    m_member = member;\n"
        "}\n"
        "\n"
        ;
627

628
    GenerateGetterSetter factory;
629
630
    TestCase data(original, expected);
    data.run(&factory);
631
632
633
}

/// Check: Check if it works on the second declarator
634
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_secondDeclarator()
635
{
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    int *foo, @it;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    int *foo, it;\n"
        "\n"
        "public:\n"
        "    int getIt() const;\n"
        "    void setIt(int value);\n"
        "};\n"
        "\n"
        "int Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        "void Something::setIt(int value)\n"
        "{\n"
        "    it = value;\n"
        "}\n"
        "\n"
        ;
665

666
    GenerateGetterSetter factory;
667
668
    TestCase data(original, expected);
    data.run(&factory);
669
}
670
671

/// Check: Quick fix is offered for "int *@it;" ('@' denotes the text cursor position)
672
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_triggeringRightAfterPointerSign()
673
{
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
    const QByteArray original =
        "\n"
        "class Something\n"
        "{\n"
        "    int *@it;\n"
        "};\n"
        ;
    const QByteArray expected =
        "\n"
        "class Something\n"
        "{\n"
        "    int *it;\n"
        "\n"
        "public:\n"
        "    int *getIt() const;\n"
        "    void setIt(int *value);\n"
        "};\n"
        "\n"
        "int *Something::getIt() const\n"
        "{\n"
        "    return it;\n"
        "}\n"
        "\n"
        "void Something::setIt(int *value)\n"
        "{\n"
        "    it = value;\n"
        "}\n"
        "\n"
        ;
703

704
    GenerateGetterSetter factory;
705
706
    TestCase data(original, expected);
    data.run(&factory);
707
708
709
}

/// Check: Quick fix is not triggered on a member function.
710
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_notTriggeringOnMemberFunction()
711
{
712
    const QByteArray original = "class Something { void @f(); };";
713

714
    GenerateGetterSetter factory;
715
716
    TestCase data(original, original + "\n");
    data.run(&factory);
717
718
719
}

/// Check: Quick fix is not triggered on an member array;
720
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_notTriggeringOnMemberArray()
721
{
722
    const QByteArray original = "class Something { void @a[10]; };";
723

724
    GenerateGetterSetter factory;
725
726
    TestCase data(original, original + "\n");
    data.run(&factory);
727
728
729
730
}

/// Check: Do not offer the quick fix if there is already a member with the
/// getter or setter name we would generate.
731
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_notTriggeringWhenGetterOrSetterExist()
732
{
733
734
735
736
737
    const QByteArray original =
        "class Something {\n"
        "     int @it;\n"
        "     void setIt();\n"
        "};\n";
738

739
    GenerateGetterSetter factory;
740
741
    TestCase data(original, original + "\n");
    data.run(&factory);
742
}
743
744
745

/// Check: Just a basic test since the main functionality is tested in
/// cpppointerdeclarationformatter_test.cpp
746
void CppEditorPlugin::test_quickfix_ReformatPointerDeclaration()
747
{
748
749
    const QByteArray original = "char@*s;";
    const QByteArray expected = "char *s;\n";
750
751

    ReformatPointerDeclaration factory;
752
753
    TestCase data(original, expected);
    data.run(&factory);
754
}
755
756

/// Check from source file: If there is no header file, insert the definition after the class.
757
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_basic()
758
759
760
761
762
763
764
765
766
767
{
    const QByteArray original =
        "struct Foo\n"
        "{\n"
        "    Foo();@\n"
        "};\n"
        "\n"
        ;
    const QByteArray expected = original +
        "Foo::Foo()\n"
768
        "{\n\n"
769
        "}\n"
770
        "\n\n"
771
772
773
774
775
776
777
778
        ;

    InsertDefFromDecl factory;
    TestCase data(original, expected);
    data.run(&factory);
}

/// Check from header file: If there is a source file, insert the definition in the source file.
779
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic1()
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "struct Foo\n"
        "{\n"
        "    Foo()@;\n"
        "};\n";
    expected = original + "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original.resize(0);
    expected =
        "\n"
        "Foo::Foo()\n"
800
        "{\n\n"
801
802
803
804
805
806
807
808
809
810
811
        "}\n"
        "\n"
        ;
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    InsertDefFromDecl factory;
    TestCase data(testFiles);
    data.run(&factory);
}

/// Check from source file: Insert in source file, not header file.
812
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic2()
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    // Empty Header File
    testFiles << TestDocument::create("", "\n", QLatin1String("file.h"));

    // Source File
    original =
        "struct Foo\n"
        "{\n"
        "    Foo()@;\n"
        "};\n";
    expected = original +
        "\n"
        "Foo::Foo()\n"
831
        "{\n\n"
832
833
834
835
836
837
838
839
840
841
842
843
        "}\n"
        "\n"
        ;
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    InsertDefFromDecl factory;
    TestCase data(testFiles);
    data.run(&factory);
}

/// Check from header file: If the class is in a namespace, the added function definition
/// name must be qualified accordingly.
844
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_namespace1()
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "namespace N {\n"
        "struct Foo\n"
        "{\n"
        "    Foo()@;\n"
        "};\n"
        "}\n";
    expected = original + "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original.resize(0);
    expected =
        "\n"
        "N::Foo::Foo()\n"
867
        "{\n\n"
868
869
870
871
872
873
874
875
876
877
878
879
        "}\n"
        "\n"
        ;
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    InsertDefFromDecl factory;
    TestCase data(testFiles);
    data.run(&factory);
}

/// Check from header file: If the class is in namespace N and the source file has a
/// "using namespace N" line, the function definition name must be qualified accordingly.
880
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_namespace2()
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "namespace N {\n"
        "struct Foo\n"
        "{\n"
        "    Foo()@;\n"
        "};\n"
        "}\n";
    expected = original + "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original =
900
901
902
            "#include \"file.h\"\n"
            "using namespace N;\n"
            ;
903
    expected = original +
904
905
906
907
908
909
910
            "\n"
            "\n"
            "Foo::Foo()\n"
            "{\n\n"
            "}\n"
            "\n"
            ;
911
912
913
914
915
916
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    InsertDefFromDecl factory;
    TestCase data(testFiles);
    data.run(&factory);
}
917

918
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_freeFunction()
919
920
{
    const QByteArray original = "void free()@;\n";
921
    const QByteArray expected =
922
923
        "void free()\n"
        "{\n\n"
924
925
926
927
928
929
930
931
        "}\n"
        "\n"
        ;

    InsertDefFromDecl factory;
    TestCase data(original, expected);
    data.run(&factory);
}
932

933
934
935
936
937
938
939
940
941
/// Check definition insert inside class
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_insideClass()
{
    const QByteArray original =
        "class Foo {\n"
        "    void b@ar();\n"
        "};";
    const QByteArray expected =
        "class Foo {\n"
942
943
        "    void bar()\n"
        "    {\n\n"
944
945
946
947
948
949
950
951
        "    }\n"
        "};\n";

    InsertDefFromDecl factory;
    TestCase data(original, expected);
    data.run(&factory, 1);
}

952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/// Check not triggering when definition exists
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_notTriggeringWhenDefinitionExists()
{
    const QByteArray original =
            "class Foo {\n"
            "    void b@ar();\n"
            "};\n"
            "void Foo::bar() {}\n";
    const QByteArray expected = original + "\n";

    InsertDefFromDecl factory;
    TestCase data(original, expected);
    data.run(&factory, 1);
}

967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/// Check not triggering when it is a statement
void CppEditorPlugin::test_quickfix_InsertDefFromDecl_notTriggeringStatement()
{
    const QByteArray original =
            "class Foo {\n"
            "public:\n"
            "    Foo() {}\n"
            "};\n"
            "void freeFunc() {\n"
            "    Foo @f();"
            "}\n";
    const QByteArray expected = original + "\n";

    InsertDefFromDecl factory;
    TestCase data(original, expected);
    data.run(&factory);
}

985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
// Function for one of InsertDeclDef section cases
void insertToSectionDeclFromDef(const QByteArray &section, int sectionIndex)
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "class Foo\n"
        "{\n"
        "};\n";
    expected =
        "class Foo\n"
        "{\n"
        + section + ":\n" +
        "    Foo();\n"
        "@};\n\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original =
        "#include \"file.h\"\n"
        "\n"
        "Foo::Foo@()\n"
        "{\n"
        "}\n"
        "\n"
        ;
    expected = original + "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    InsertDeclFromDef factory;
    TestCase data(testFiles);
    data.run(&factory, sectionIndex);
}

/// Check from source file: Insert in header file.
1024
void CppEditorPlugin::test_quickfix_InsertDeclFromDef()
1025
1026
1027
1028
1029
1030
1031
1032
1033
{
    insertToSectionDeclFromDef("public", 0);
    insertToSectionDeclFromDef("public slots", 1);
    insertToSectionDeclFromDef("protected", 2);
    insertToSectionDeclFromDef("protected slots", 3);
    insertToSectionDeclFromDef("private", 4);
    insertToSectionDeclFromDef("private slots", 5);
}

1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
QList<Include> includesForSource(const QByteArray &source)
{
    const QString fileName = directoryOfTestFile() + QLatin1String("/file.cpp");
    Utils::FileSaver srcSaver(fileName);
    srcSaver.write(source);
    srcSaver.finalize();

    using namespace CppTools::Internal;

    CppModelManager *cmm = CppModelManager::instance();
    cmm->GC();
    CppPreprocessor pp((QPointer<CppModelManager>(cmm)));
    pp.setIncludePaths(QStringList(globalIncludePath()));
    pp.run(fileName);

    Document::Ptr document = cmm->snapshot().document(fileName);
    return document->includes();
}

/// Check: Detection of include groups separated by new lines
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_detectIncludeGroupsByNewLines()
{
    // Source referencing those files
    QByteArray source =
        "#include \"header.h\"\n"
        "\n"
        "#include \"file.h\"\n"
        "#include \"fileother.h\"\n"
        "\n"
        "#include <lib/fileother.h>\n"
        "#include <lib/file.h>\n"
        "\n"
        "#include \"otherlib/file.h\"\n"
        "#include \"otherlib/fileother.h\"\n"
        "\n"
        "#include \"utils/utils.h\"\n"
        "\n"
        "#include <QDebug>\n"
        "#include <QDir>\n"
        "#include <QString>\n"
        "\n"
        "#include <iostream>\n"
        "#include <string>\n"
        "#include <except>\n"
        "\n"
        "#include <iostream>\n"
        "#include \"stuff\"\n"
        "#include <except>\n"
        "\n"
        ;

    QList<Include> includes = includesForSource(source);
    QCOMPARE(includes.size(), 17);
    QList<IncludeGroup> includeGroups
        = IncludeGroup::detectIncludeGroupsByNewLines(includes);
    QCOMPARE(includeGroups.size(), 8);

    QCOMPARE(includeGroups.at(0).size(), 1);
    QVERIFY(includeGroups.at(0).commonPrefix().isEmpty());
    QVERIFY(includeGroups.at(0).hasOnlyIncludesOfType(Client::IncludeLocal));
    QVERIFY(includeGroups.at(0).isSorted());

    QCOMPARE(includeGroups.at(1).size(), 2);
    QVERIFY(!includeGroups.at(1).commonPrefix().isEmpty());
    QVERIFY(includeGroups.at(1).hasOnlyIncludesOfType(Client::IncludeLocal));
    QVERIFY(includeGroups.at(1).isSorted());

    QCOMPARE(includeGroups.at(2).size(), 2);
    QVERIFY(!includeGroups.at(2).commonPrefix().isEmpty());
    QVERIFY(includeGroups.at(2).hasOnlyIncludesOfType(Client::IncludeGlobal));
    QVERIFY(!includeGroups.at(2).isSorted());

    QCOMPARE(includeGroups.at(6).size(), 3);
    QVERIFY(includeGroups.at(6).commonPrefix().isEmpty());
    QVERIFY(includeGroups.at(6).hasOnlyIncludesOfType(Client::IncludeGlobal));
    QVERIFY(!includeGroups.at(6).isSorted());

    QCOMPARE(includeGroups.at(7).size(), 3);
    QVERIFY(includeGroups.at(7).commonPrefix().isEmpty());
    QVERIFY(!includeGroups.at(7).hasOnlyIncludesOfType(Client::IncludeLocal));
    QVERIFY(!includeGroups.at(7).hasOnlyIncludesOfType(Client::IncludeGlobal));
    QVERIFY(!includeGroups.at(7).isSorted());

    QCOMPARE(IncludeGroup::filterIncludeGroups(includeGroups, Client::IncludeLocal).size(), 4);
    QCOMPARE(IncludeGroup::filterIncludeGroups(includeGroups, Client::IncludeGlobal).size(), 3);
    QCOMPARE(IncludeGroup::filterMixedIncludeGroups(includeGroups).size(), 1);
}

/// Check: Detection of include groups separated by include dirs
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_detectIncludeGroupsByIncludeDir()
{
    QByteArray source =
        "#include \"file.h\"\n"
        "#include \"fileother.h\"\n"
        "#include <lib/file.h>\n"
        "#include <lib/fileother.h>\n"
        "#include \"otherlib/file.h\"\n"
        "#include \"otherlib/fileother.h\"\n"
        "#include <iostream>\n"
        "#include <string>\n"
        "#include <except>\n"
        "\n"
        ;

    QList<Include> includes = includesForSource(source);
    QCOMPARE(includes.size(), 9);
    QList<IncludeGroup> includeGroups
        = IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
    QCOMPARE(includeGroups.size(), 4);

    QCOMPARE(includeGroups.at(0).size(), 2);
    QVERIFY(includeGroups.at(0).commonIncludeDir().isEmpty());

    QCOMPARE(includeGroups.at(1).size(), 2);
    QCOMPARE(includeGroups.at(1).commonIncludeDir(), QLatin1String("lib/"));

    QCOMPARE(includeGroups.at(2).size(), 2);
    QCOMPARE(includeGroups.at(2).commonIncludeDir(), QLatin1String("otherlib/"));

    QCOMPARE(includeGroups.at(3).size(), 3);
    QCOMPARE(includeGroups.at(3).commonIncludeDir(), QLatin1String(""));
}

/// Check: Detection of include groups separated by include types
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_detectIncludeGroupsByIncludeType()
{
    QByteArray source =
        "#include \"file.h\"\n"
        "#include \"fileother.h\"\n"
        "#include <lib/file.h>\n"
        "#include <lib/fileother.h>\n"
        "#include \"otherlib/file.h\"\n"
        "#include \"otherlib/fileother.h\"\n"
        "#include <iostream>\n"
        "#include <string>\n"
        "#include <except>\n"
        "\n"
        ;

    QList<Include> includes = includesForSource(source);
    QCOMPARE(includes.size(), 9);
    QList<IncludeGroup> includeGroups
        = IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
    QCOMPARE(includeGroups.size(), 4);

    QCOMPARE(includeGroups.at(0).size(), 2);
    QVERIFY(includeGroups.at(0).hasOnlyIncludesOfType(Client::IncludeLocal));

    QCOMPARE(includeGroups.at(1).size(), 2);
    QVERIFY(includeGroups.at(1).hasOnlyIncludesOfType(Client::IncludeGlobal));

    QCOMPARE(includeGroups.at(2).size(), 2);
    QVERIFY(includeGroups.at(2).hasOnlyIncludesOfType(Client::IncludeLocal));

    QCOMPARE(includeGroups.at(3).size(), 3);
    QVERIFY(includeGroups.at(3).hasOnlyIncludesOfType(Client::IncludeGlobal));
}

/// Check: Add include if there is already an include
1193
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_normal()
1194
1195
1196
1197
1198
1199
1200
1201
1202
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    // Header File
    original = "class Foo {};\n";
    expected = original + "\n";
1203
1204
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("afile.h"));
1205
1206
1207

    // Source File
    original =
1208
        "#include \"header.h\"\n"
1209
1210
1211
1212
1213
1214
1215
        "\n"
        "void f()\n"
        "{\n"
        "    Fo@o foo;\n"
        "}\n"
        ;
    expected =
1216
1217
        "#include \"afile.h\"\n"
        "#include \"header.h\"\n"
1218
1219
1220
1221
1222
1223
1224
        "\n"
        "void f()\n"
        "{\n"
        "    Foo foo;\n"
        "}\n"
        "\n"
        ;
1225
1226
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("afile.cpp"));
1227

1228
    // Do not use the test factory, at least once we want to go through the "full stack".
1229
    AddIncludeForUndefinedIdentifier factory;
1230
    TestCase data(testFiles, QStringList(globalIncludePath()));
1231
1232
1233
    data.run(&factory);
}

1234
/// Check: Ignore *.moc includes
1235
1236
1237
1238
1239
1240
1241
1242
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_ignoremoc()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
1243
        "void @f();\n"
1244
1245
1246
        "#include \"file.moc\";\n"
        ;
    expected =
1247
1248
        "#include \"file.h\"\n"
        "\n"
1249
        "void f();\n"
1250
1251
1252
        "#include \"file.moc\";\n"
        "\n"
        ;
1253
1254
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1255

1256
1257
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1258
1259
1260
    data.run(&factory);
}

1261
/// Check: Insert include at top for a sorted group
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_sortingTop()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include \"y.h\"\n"
        "#include \"z.h\"\n"
1272
        "\n@"
1273
1274
1275
1276
1277
        ;
    expected =
        "#include \"file.h\"\n"
        "#include \"y.h\"\n"
        "#include \"z.h\"\n"
1278
        "\n\n"
1279
        ;
1280
1281
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1282

1283
1284
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1285
1286
1287
    data.run(&factory);
}

1288
/// Check: Insert include in the middle for a sorted group
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_sortingMiddle()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include \"a.h\"\n"
        "#include \"z.h\"\n"
1299
        "\n@"
1300
1301
1302
1303
1304
        ;
    expected =
        "#include \"a.h\"\n"
        "#include \"file.h\"\n"
        "#include \"z.h\"\n"
1305
        "\n\n"
1306
        ;
1307
1308
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1309

1310
1311
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1312
1313
1314
    data.run(&factory);
}

1315
1316
1317
1318
/// Check: Insert include at bottom for a sorted group
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_sortingBottom()
{
    QList<TestDocumentPtr> testFiles;
1319

1320
1321
    QByteArray original;
    QByteArray expected;
1322

1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
    original =
        "#include \"a.h\"\n"
        "#include \"b.h\"\n"
        "\n@"
        ;
    expected =
        "#include \"a.h\"\n"
        "#include \"b.h\"\n"
        "#include \"file.h\"\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: For an unsorted group the new include is appended
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_appendToUnsorted()
1344
1345
1346
1347
1348
1349
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
    original =
        "#include \"b.h\"\n"
        "#include \"a.h\"\n"
        "\n@"
        ;
    expected =
        "#include \"b.h\"\n"
        "#include \"a.h\"\n"
        "#include \"file.h\"\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Insert a local include at front if there are only global includes
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_firstLocalIncludeAtFront()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include <a.h>\n"
        "#include <b.h>\n"
        "\n@"
        ;
    expected =
        "#include \"file.h\"\n"
        "\n"
        "#include <a.h>\n"
        "#include <b.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Insert a global include at back if there are only local includes
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_firstGlobalIncludeAtBack()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;
1404
1405
1406
1407

    original =
        "#include \"a.h\"\n"
        "#include \"b.h\"\n"
1408
1409
        "\n"
        "void @f();\n"
1410
1411
1412
1413
        ;
    expected =
        "#include \"a.h\"\n"
        "#include \"b.h\"\n"
1414
1415
1416
1417
        "\n"
        "#include <file.h>\n"
        "\n"
        "void f();\n"
1418
1419
        "\n"
        ;
1420
1421
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1422

1423
1424
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("<file.h>"));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1425
1426
1427
    data.run(&factory);
}

1428
1429
/// Check: Prefer group with longest matching prefix
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_preferGroupWithLongerMatchingPrefix()
1430
1431
1432
1433
1434
1435
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
    original =
        "#include \"prefixa.h\"\n"
        "#include \"prefixb.h\"\n"
        "\n"
        "#include \"foo.h\"\n"
        "\n@"
        ;
    expected =
        "#include \"prefixa.h\"\n"
        "#include \"prefixb.h\"\n"
        "#include \"prefixc.h\"\n"
        "\n"
        "#include \"foo.h\"\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"prefixc.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Create a new include group if there are only include groups with a different include dir
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_newGroupIfOnlyDifferentIncludeDirs()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;
1466
1467

    original =
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
        "#include \"lib/file.h\"\n"
        "#include \"lib/fileother.h\"\n"
        "\n@"
        ;
    expected =
        "#include \"lib/file.h\"\n"
        "#include \"lib/fileother.h\"\n"
        "\n"
        "#include \"file.h\"\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Include group with mixed include dirs, sorted --> insert properly
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_mixedDirsSorted()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include <lib/file.h>\n"
        "#include <otherlib/file.h>\n"
        "#include <utils/file.h>\n"
        "\n@"
        ;
    expected =
        "#include <firstlib/file.h>\n"
        "#include <lib/file.h>\n"
        "#include <otherlib/file.h>\n"
        "#include <utils/file.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("<firstlib/file.h>"));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Include group with mixed include dirs, unsorted --> append
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_mixedDirsUnsorted()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include <otherlib/file.h>\n"
        "#include <lib/file.h>\n"
        "#include <utils/file.h>\n"
        "\n@"
        ;
    expected =
        "#include <otherlib/file.h>\n"
        "#include <lib/file.h>\n"
        "#include <utils/file.h>\n"
        "#include <lastlib/file.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("<lastlib/file.h>"));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Include group with mixed include types
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_mixedIncludeTypes1()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include \"a.h\"\n"
        "#include <global.h>\n"
        "\n@"
        ;
    expected =
        "#include \"a.h\"\n"
        "#include \"z.h\"\n"
        "#include <global.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"z.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Include group with mixed include types
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_mixedIncludeTypes2()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include \"z.h\"\n"
        "#include <global.h>\n"
        "\n@"
        ;
    expected =
        "#include \"a.h\"\n"
        "#include \"z.h\"\n"
        "#include <global.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"a.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Include group with mixed include types
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_mixedIncludeTypes3()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include \"z.h\"\n"
        "#include <global.h>\n"
        "\n@"
        ;
    expected =
        "#include \"z.h\"\n"
        "#include \"lib/file.h\"\n"
        "#include <global.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"lib/file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Include group with mixed include types
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_mixedIncludeTypes4()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "#include \"z.h\"\n"
        "#include <global.h>\n"
        "\n@"
        ;
    expected =
        "#include \"z.h\"\n"
        "#include <global.h>\n"
        "#include <lib/file.h>\n"
        "\n\n"
        ;
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));

    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("<lib/file.h>"));
    TestCase data(testFiles, QStringList(globalIncludePath()));
    data.run(&factory);
}

/// Check: Insert very first include
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_noinclude()
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "void @f();\n"
1663
1664
1665
1666
        ;
    expected =
        "#include \"file.h\"\n"
        "\n"
1667
        "void f();\n"
1668
1669
        "\n"
        ;
1670
1671
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1672

1673
1674
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1675
1676
1677
    data.run(&factory);
}

1678
1679
/// Check: Insert very first include if there is a c++ style comment on top
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_veryFirstIncludeCppStyleCommentOnTop()
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "\n"
        "// comment\n"
        "\n"
1690
        "void @f();\n"
1691
1692
1693
1694
1695
1696
1697
        ;
    expected =
        "\n"
        "// comment\n"
        "\n"
        "#include \"file.h\"\n"
        "\n"
1698
        "void @f();\n"
1699
1700
        "\n"
        ;
1701
1702
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1703

1704
1705
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1706
1707
    data.run(&factory);
}
1708
1709
1710

/// Check: Insert very first include if there is a c style comment on top
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_veryFirstIncludeCStyleCommentOnTop()
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
{
    QList<TestDocumentPtr> testFiles;

    QByteArray original;
    QByteArray expected;

    original =
        "\n"
        "/*\n"
        " comment\n"
        " */\n"
        "\n"
1723
        "void @f();\n"
1724
1725
1726
1727
1728
1729
1730
1731
1732
        ;
    expected =
        "\n"
        "/*\n"
        " comment\n"
        " */\n"
        "\n"
        "#include \"file.h\"\n"
        "\n"
1733
        "void @f();\n"
1734
1735
        "\n"
        ;
1736
1737
    testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
                                      + QLatin1String("file.cpp"));
1738

1739
1740
    AddIncludeForUndefinedIdentifierTestFactory factory(QLatin1String("\"file.h\""));
    TestCase data(testFiles, QStringList(globalIncludePath()));
1741
1742
    data.run(&factory);
}
1743
1744

/// Check: Move definition from header to cpp.
1745
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp()
1746
1747
1748
1749
1750
1751
1752
1753
{
    QList<TestDocumentPtr> testFiles;
    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "class Foo {\n"
1754
1755
        "  inline int numbe@r() const\n"
        "  {\n"
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
        "    return 5;\n"
        "  }\n"
        "\n"
        "    void bar();\n"
        "};";
    expected =
        "class Foo {\n"
        "  inline int number() const;\n"
        "\n"
        "    void bar();\n"
        "};\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original =
        "#include \"file.h\"\n"
        "\n";
    expected =
        "#include \"file.h\"\n"
        "\n"
        "\n"
1777
1778
        "int Foo::number() const\n"
        "{\n"
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
        "    return 5;\n"
        "}\n"
        "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    MoveFuncDefOutside factory;
    TestCase data(testFiles);
    data.run(&factory);
}

/// Check: Move definition outside class
1790
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutside()
1791
1792
1793
{
    QByteArray original =
        "class Foo {\n"
1794
1795
        "  inline int numbe@r() const\n"
        "  {\n"
1796
1797
1798
1799
1800
1801
1802
1803
        "    return 5;\n"
        "  }\n"
        "};";
    QByteArray expected =
        "class Foo {\n"
        "  inline int number() const;\n"
        "};\n"
        "\n"
1804
1805
        "int Foo::number() const\n"
        "{\n"
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
        "    return 5;\n"
        "}"
        "\n\n";

    MoveFuncDefOutside factory;
    TestCase data(original, expected);
    data.run(&factory);
}

/// Check: Move definition from header to cpp (with namespace).
1816
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNS()
1817
1818
1819
1820
1821
1822
1823
1824
1825
{
    QList<TestDocumentPtr> testFiles;
    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "namespace MyNs {\n"
        "class Foo {\n"
1826
1827
        "  inline int numbe@r() const\n"
        "  {\n"
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
        "    return 5;\n"
        "  }\n"
        "};\n"
        "}";
    expected =
        "namespace MyNs {\n"
        "class Foo {\n"
        "  inline int number() const;\n"
        "};\n"
        "}\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original =
        "#include \"file.h\"\n"
        "\n";
    expected =
        "#include \"file.h\"\n"
        "\n"
        "\n"
1848
1849
        "int MyNs::Foo::number() const\n"
        "{\n"
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
        "    return 5;\n"
        "}\n"
        "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    MoveFuncDefOutside factory;
    TestCase data(testFiles);
    data.run(&factory);
}

/// Check: Move definition from header to cpp (with namespace + using).
1861
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNSUsing()
1862
1863
1864
1865
1866
1867
1868
1869
1870
{
    QList<TestDocumentPtr> testFiles;
    QByteArray original;
    QByteArray expected;

    // Header File
    original =
        "namespace MyNs {\n"
        "class Foo {\n"
1871
1872
        "  inline int numbe@r() const\n"
        "  {\n"
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
        "    return 5;\n"
        "  }\n"
        "};\n"
        "}";
    expected =
        "namespace MyNs {\n"
        "class Foo {\n"
        "  inline int number() const;\n"
        "};\n"
        "}\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));

    // Source File
    original =
        "#include \"file.h\"\n"
        "using namespace MyNs;\n"
        "\n";
    expected =
        "#include \"file.h\"\n"
        "using namespace MyNs;\n"
        "\n"
        "\n"
1895
1896
        "int Foo::number() const\n"
        "{\n"
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
        "    return 5;\n"
        "}\n"
        "\n";
    testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));

    MoveFuncDefOutside factory;
    TestCase data(testFiles);
    data.run(&factory);
}

/// Check: Move definition outside class with Namespace
1908
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutsideWithNs()
1909
1910
1911
1912
{
    QByteArray original =
        "namespace MyNs {\n"
        "class Foo {\n"
1913
1914
        "  inline int numbe@r() const\n"
        "  {\n"
1915
1916
1917
1918
1919
1920
1921
1922
1923
        "    return 5;\n"
        "  }\n"
        "};}";
    QByteArray expected =
        "namespace MyNs {\n"
        "class Foo {\n"
        "  inline int number() const;\n"
        "};\n"
        "\n"
1924
1925
        "int Foo::number() const\n"
        "{\n"
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
        "    return 5;\n"
        "}"
        "\n}\n";

    MoveFuncDefOutside factory;
    TestCase data(original, expected);
    data.run(&factory);
}

/// Check: Move free function from header to cpp.
1936
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCpp()
1937
1938
1939
1940
1941
1942
1943
{
    QList<TestDocumentPtr> testFiles;
    QByteArray original;
    QByteArray expected;

    // Header File
    original =