followsymbol_switchmethoddecldef_test.cpp 45.8 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
30
31
/****************************************************************************
**
** 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.
**
****************************************************************************/

#include "cppeditor.h"
#include "cppeditorplugin.h"
32
33
#include "cppelementevaluator.h"
#include "cppvirtualfunctionassistprovider.h"
34

35
#include <coreplugin/plugintestutils.h>
36
37
38
#include <texteditor/codeassist/iassistproposal.h>
#include <texteditor/codeassist/iassistprocessor.h>
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
39
40
41
42
43
44
45
46
#include <utils/fileutils.h>

#include <QDebug>
#include <QDir>
#include <QtTest>


/*!
47
    Tests for Follow Symbol Under Cursor and Switch Between Method Declaration/Definition
48
49
50
51
52
53

    Section numbers refer to

        Working Draft, Standard for Programming Language C++
        Document Number: N3242=11-0012

54
    You can find potential test code for Follow Symbol Under Cursor on the bottom of this file.
55
56
57
58
59
60
61
62
63
64
 */
using namespace CPlusPlus;
using namespace CppEditor;
using namespace CppEditor::Internal;
using namespace CppTools;
using namespace TextEditor;
using namespace Core;

namespace {

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/// A fake virtual functions assist provider that runs processor->perform() already in configure()
class VirtualFunctionTestAssistProvider : public VirtualFunctionAssistProvider
{
public:
    VirtualFunctionTestAssistProvider(CPPEditorWidget *editorWidget)
        : m_editorWidget(editorWidget)
    {}

    // Invoke the processor already here to calculate the proposals. Return false in order to
    // indicate that configure failed, so the actual code assist invocation leading to a pop-up
    // will not happen.
    bool configure(CPlusPlus::Class *startClass, CPlusPlus::Function *function,
                   const CPlusPlus::Snapshot &snapshot, bool openInNextSplit)
    {
        VirtualFunctionAssistProvider::configure(startClass, function, snapshot, openInNextSplit);

        IAssistProcessor *processor = createProcessor();
82
        IAssistInterface *assistInterface = m_editorWidget->createAssistInterface(FollowSymbol,
83
                                                                            ExplicitlyInvoked);
84
85
        IAssistProposal *immediateProposal = processor->immediateProposal(assistInterface);
        IAssistProposal *finalProposal = processor->perform(assistInterface);
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

        m_immediateItems = itemList(immediateProposal->model());
        m_finalItems = itemList(finalProposal->model());

        return false;
    }

    static QStringList itemList(IAssistProposalModel *imodel)
    {
        QStringList immediateItems;
        BasicProposalItemListModel *model = dynamic_cast<BasicProposalItemListModel *>(imodel);
        if (!model)
            return immediateItems;
        if (model->isSortable(QString()))
            model->sort(QString());
        model->removeDuplicates();

        for (int i = 0, size = model->size(); i < size; ++i) {
            const QString text = model->text(i);
            immediateItems.append(text);
        }

        return immediateItems;
    }

public:
    QStringList m_immediateItems;
    QStringList m_finalItems;

private:
    CPPEditorWidget *m_editorWidget;
};

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class TestDocument;
typedef QSharedPointer<TestDocument> TestDocumentPtr;

/**
 * Represents a test document.
 *
 * A TestDocument's source can contain special characters:
 *   - a '@' character denotes the initial text cursor position
 *   - a '$' character denotes the target text cursor position
 */
class TestDocument
{
public:
    TestDocument(const QByteArray &theSource, const QString &fileName)
        : source(theSource)
        , fileName(fileName)
        , initialCursorPosition(source.indexOf('@'))
        , targetCursorPosition(source.indexOf('$'))
        , editor(0)
        , editorWidget(0)
    {
        QVERIFY(initialCursorPosition != targetCursorPosition);
        if (initialCursorPosition > targetCursorPosition) {
            source.remove(initialCursorPosition, 1);
143
144
145
146
            if (targetCursorPosition != -1) {
                source.remove(targetCursorPosition, 1);
                --initialCursorPosition;
            }
147
148
        } else {
            source.remove(targetCursorPosition, 1);
149
150
151
152
            if (initialCursorPosition != -1) {
                source.remove(initialCursorPosition, 1);
                --targetCursorPosition;
            }
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
183
184
185
186
187
188
189
190
        }
    }

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

    bool hasInitialCursorMarker() const { return initialCursorPosition != -1; }
    bool hasTargetCursorMarker() const { return targetCursorPosition != -1; }

    QString filePath() const
    {
        if (directoryPath.isEmpty())
            qDebug() << "directoryPath not set!";
        return directoryPath + QLatin1Char('/') + fileName;
    }

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

    QByteArray source;

    const QString fileName;
    QString directoryPath;
    int initialCursorPosition;
    int targetCursorPosition;

    CPPEditor *editor;
    CPPEditorWidget *editorWidget;
};

/**
 * Encapsulates the whole process of setting up several editors, positioning the cursor,
191
192
 * executing Follow Symbol Under Cursor or Switch Between Method Declaration/Definition
 * and checking the result.
193
 */
194
class TestCase
195
{
196
197
public:
    enum CppEditorAction {
198
199
        FollowSymbolUnderCursorAction,
        SwitchBetweenMethodDeclarationDefinitionAction
200
    };
201

202
203
204
205
206
207
    TestCase(CppEditorAction action, const QByteArray &source,
             const QStringList &expectedVirtualFunctionImmediateProposal = QStringList(),
             const QStringList &expectedVirtualFunctionFinalProposal = QStringList());
    TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles,
             const QStringList &expectedVirtualSymbolsImmediateProposal = QStringList(),
             const QStringList &expectedVirtualSymbolsFinalProposal = QStringList());
208
209
    ~TestCase();

210
    void run(bool expectedFail = false);
211
212
213
214
215
216
217
218
219

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

    void init();

    TestDocumentPtr testFileWithInitialCursorMarker();
    TestDocumentPtr testFileWithTargetCursorMarker();
220
221
222
223

private:
    CppEditorAction m_action;
    QList<TestDocumentPtr> m_testFiles;
224
225
    QStringList m_expectedVirtualSymbolsImmediateProposal; // for virtual functions
    QStringList m_expectedVirtualSymbolsFinalProposals;    // for virtual functions
226
227
228
229
};

/// Convenience function for creating a TestDocument.
/// See TestDocument.
230
231
232
TestCase::TestCase(CppEditorAction action, const QByteArray &source,
                   const QStringList &expectedVirtualFunctionImmediateProposal,
                   const QStringList &expectedVirtualFunctionFinalProposal)
233
    : m_action(action)
234
235
    , m_expectedVirtualSymbolsImmediateProposal(expectedVirtualFunctionImmediateProposal)
    , m_expectedVirtualSymbolsFinalProposals(expectedVirtualFunctionFinalProposal)
236
{
237
    m_testFiles << TestDocument::create(source, QLatin1String("file.cpp"));
238
239
240
241
242
243
244
    init();
}

/// Creates a test case with multiple test files.
/// Exactly one test document must be provided that contains '@', the initial position marker.
/// Exactly one test document must be provided that contains '$', the target position marker.
/// It can be the same document.
245
246
247
TestCase::TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles,
                   const QStringList &expectedVirtualSymbolsImmediateProposal,
                   const QStringList &expectedVirtualSymbolsFinalProposal)
248
249
    : m_action(action)
    , m_testFiles(theTestFiles)
250
251
    , m_expectedVirtualSymbolsImmediateProposal(expectedVirtualSymbolsImmediateProposal)
    , m_expectedVirtualSymbolsFinalProposals(expectedVirtualSymbolsFinalProposal)
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{
    init();
}

void TestCase::init()
{
    // Check if there are initial and target position markers
    QVERIFY2(testFileWithInitialCursorMarker(),
        "No test file with initial cursor marker is provided.");
    QVERIFY2(testFileWithTargetCursorMarker(),
        "No test file with target cursor marker is provided.");

    // Write files to disk
    const QString directoryPath = QDir::tempPath();
266
    foreach (TestDocumentPtr testFile, m_testFiles) {
267
268
269
270
271
272
        testFile->directoryPath = directoryPath;
        testFile->writeToDisk();
    }

    // Update Code Model
    QStringList filePaths;
273
    foreach (const TestDocumentPtr &testFile, m_testFiles)
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
        filePaths << testFile->filePath();
    CppTools::CppModelManagerInterface::instance()->updateSourceFiles(filePaths);

    // Wait for the indexer to process all files.
    // All these files are "Fast Checked", that is the function bodies are not processed.
    QStringList filePathsNotYetInSnapshot(filePaths);
    forever {
        Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
        foreach (const QString &filePath, filePathsNotYetInSnapshot) {
            if (snapshot.contains(filePath))
                filePathsNotYetInSnapshot.removeOne(filePath);
        }
        if (filePathsNotYetInSnapshot.isEmpty())
            break;
        QCoreApplication::processEvents();
    }

    // Open Files
292
    foreach (TestDocumentPtr testFile, m_testFiles) {
293
294
295
296
297
298
299
300
        testFile->editor
            = dynamic_cast<CPPEditor *>(EditorManager::openEditor(testFile->filePath()));
        QVERIFY(testFile->editor);

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

        // Wait until the indexer processed the just opened file.
301
        // The file is "Full Checked" since it is in the working copy now,
302
        // that is the function bodies are processed.
303
304
305
306
307
308
309
        forever {
            Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
            if (Document::Ptr document = snapshot.document(testFile->filePath())) {
                if (document->checkMode() == Document::FullCheck)
                    break;
                QCoreApplication::processEvents();
            }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
        }

        // Rehighlight
        testFile->editorWidget->semanticRehighlight(true);
        // Wait for the semantic info from the future
        while (testFile->editorWidget->semanticInfo().doc.isNull())
            QCoreApplication::processEvents();
    }
}

TestCase::~TestCase()
{
    // Close editors
    QList<Core::IEditor *> editorsToClose;
324
    foreach (const TestDocumentPtr testFile, m_testFiles) {
325
326
327
        if (testFile->editor)
            editorsToClose << testFile->editor;
    }
328
    Core::Tests::closeAndDeleteEditors(editorsToClose);
329
330
331
332
333
334
335
336
337

    // Remove the test files from the code-model
    CppModelManagerInterface *mmi = CppTools::CppModelManagerInterface::instance();
    mmi->GC();
    QCOMPARE(mmi->snapshot().size(), 0);
}

TestDocumentPtr TestCase::testFileWithInitialCursorMarker()
{
338
    foreach (const TestDocumentPtr testFile, m_testFiles) {
339
340
341
342
343
344
345
346
        if (testFile->hasInitialCursorMarker())
            return testFile;
    }
    return TestDocumentPtr();
}

TestDocumentPtr TestCase::testFileWithTargetCursorMarker()
{
347
    foreach (const TestDocumentPtr testFile, m_testFiles) {
348
349
350
351
352
353
        if (testFile->hasTargetCursorMarker())
            return testFile;
    }
    return TestDocumentPtr();
}

354
void TestCase::run(bool expectedFail)
355
356
357
358
359
360
{
    TestDocumentPtr initialTestFile = testFileWithInitialCursorMarker();
    QVERIFY(initialTestFile);
    TestDocumentPtr targetTestFile = testFileWithTargetCursorMarker();
    QVERIFY(targetTestFile);

361
362
363
    // Activate editor of initial test file
    EditorManager::activateEditor(initialTestFile->editor);

364
365
366
    initialTestFile->editor->setCursorPosition(initialTestFile->initialCursorPosition);
//    qDebug() << "Initial line:" << initialTestFile->editor->currentLine();
//    qDebug() << "Initial column:" << initialTestFile->editor->currentColumn() - 1;
367

368
369
370
    QStringList immediateVirtualSymbolResults;
    QStringList finalVirtualSymbolResults;

371
372
    // Trigger the action
    switch (m_action) {
373
374
375
376
377
378
379
380
381
    case FollowSymbolUnderCursorAction: {
        CPPEditorWidget *widget = initialTestFile->editorWidget;
        FollowSymbolUnderCursor *delegate = widget->followSymbolUnderCursorDelegate();
        VirtualFunctionAssistProvider *original = delegate->virtualFunctionAssistProvider();

        // Set test provider, run and get results
        QScopedPointer<VirtualFunctionTestAssistProvider> testProvider(
            new VirtualFunctionTestAssistProvider(widget));
        delegate->setVirtualFunctionAssistProvider(testProvider.data());
382
        initialTestFile->editorWidget->openLinkUnderCursor();
383
384
385
386
387
        immediateVirtualSymbolResults = testProvider->m_immediateItems;
        finalVirtualSymbolResults = testProvider->m_finalItems;

        // Restore original test provider
        delegate->setVirtualFunctionAssistProvider(original);
388
        break;
389
390
    }
    case SwitchBetweenMethodDeclarationDefinitionAction:
391
392
393
394
395
396
        CppEditorPlugin::instance()->switchDeclarationDefinition();
        break;
    default:
        QFAIL("Unknown test action");
        break;
    }
397
398
399
400

    QCoreApplication::processEvents();

    // Compare
401
    IEditor *currentEditor = EditorManager::currentEditor();
402
403
404
    BaseTextEditor *currentTextEditor = dynamic_cast<BaseTextEditor*>(currentEditor);
    QVERIFY(currentTextEditor);

405
    QCOMPARE(currentTextEditor->document()->filePath(), targetTestFile->filePath());
406
407
408
409
410
    int expectedLine, expectedColumn;
    currentTextEditor->convertPosition(targetTestFile->targetCursorPosition,
                                       &expectedLine, &expectedColumn);
//    qDebug() << "Expected line:" << expectedLine;
//    qDebug() << "Expected column:" << expectedColumn;
411

412
413
    if (expectedFail)
        QEXPECT_FAIL("", "Contributor works on a fix.", Abort);
414
415
    QCOMPARE(currentTextEditor->currentLine(), expectedLine);
    QCOMPARE(currentTextEditor->currentColumn() - 1, expectedColumn);
416
417
418
419
420

//    qDebug() << immediateVirtualSymbolResults;
//    qDebug() << finalVirtualSymbolResults;
    QCOMPARE(immediateVirtualSymbolResults, m_expectedVirtualSymbolsImmediateProposal);
    QCOMPARE(finalVirtualSymbolResults, m_expectedVirtualSymbolsFinalProposals);
421
422
423
424
}

} // anonymous namespace

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromFunctionDeclarationSymbol()
{
    QList<TestDocumentPtr> testFiles;

    const QByteArray headerContents =
        "class C\n"
        "{\n"
        "public:\n"
        "    C();\n"
        "    int @function();\n"  // Line 5
        "};\n"
        ;
    testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));

    const QByteArray sourceContents =
        "#include \"file.h\"\n"
        "\n"
        "C::C()\n"
        "{\n"
        "}\n"                   // Line 5
        "\n"
        "int C::$function()\n"
        "{\n"
        "    return 1 + 1;\n"
        "}\n"                   // Line 10
        ;
    testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));

453
    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinitionAction, testFiles);
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
    test.run();
}

void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromFunctionDefinitionSymbol()
{
    QList<TestDocumentPtr> testFiles;

    const QByteArray headerContents =
        "class C\n"
        "{\n"
        "public:\n"
        "    C();\n"
        "    int $function();\n"
        "};\n"
        ;
    testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));

    const QByteArray sourceContents =
        "#include \"file.h\"\n"
        "\n"
        "C::C()\n"
        "{\n"
        "}\n"
        "\n"
        "int C::@function()\n"
        "{\n"
        "    return 1 + 1;\n"
        "}\n"
        ;
    testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));

485
    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinitionAction, testFiles);
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    test.run();
}

void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromFunctionBody()
{
    QList<TestDocumentPtr> testFiles;

    const QByteArray headerContents =
        "class C\n"
        "{\n"
        "public:\n"
        "    C();\n"
        "    int $function();\n"
        "};\n"
        ;
    testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));

    const QByteArray sourceContents =
        "#include \"file.h\"\n"
        "\n"
        "C::C()\n"
        "{\n"
        "}\n"                   // Line 5
        "\n"
        "int C::function()\n"
        "{\n"
        "    return @1 + 1;\n"
        "}\n"                   // Line 10
        ;
    testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));

517
    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinitionAction, testFiles);
518
519
520
    test.run();
}

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
void CppEditorPlugin::test_SwitchMethodDeclarationDefinition_fromReturnType()
{
    QList<TestDocumentPtr> testFiles;

    const QByteArray headerContents =
        "class C\n"
        "{\n"
        "public:\n"
        "    C();\n"
        "    int $function();\n"
        "};\n"
        ;
    testFiles << TestDocument::create(headerContents, QLatin1String("file.h"));

    const QByteArray sourceContents =
        "#include \"file.h\"\n"
        "\n"
        "C::C()\n"
        "{\n"
        "}\n"                   // Line 5
        "\n"
        "@int C::function()\n"
        "{\n"
        "    return 1 + 1;\n"
        "}\n"                   // Line 10
        ;
    testFiles << TestDocument::create(sourceContents, QLatin1String("file.cpp"));

549
    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinitionAction, testFiles);
550
551
552
    test.run();
}

553
554
555
556
557
558
559
560
561
562
563
/// Check ...
void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromFunction()
{
    const QByteArray source =
        "int $j;\n"
        "int main()\n"
        "{\n"
        "    @j = 2;\n"
        "}\n"           // Line 5
        ;

564
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
565
    test.run();
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesClassMember()
{
    // 3.3.10 Name hiding (par 3.), from text
    const QByteArray source =
        "struct C {\n"
        "    void f()\n"
        "    {\n"
        "        int $member; // hides C::member\n"
        "        ++@member;\n"                       // Line 5
        "    }\n"
        "    int member;\n"
        "};\n"
        ;

582
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
583
    test.run();
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesNamespaceMemberIntroducedByUsingDirective()
{
    // 3.3.10 Name hiding (par 4.), from text
    const QByteArray source =
        "namespace N {\n"
        "    int i;\n"
        "}\n"
        "\n"
        "int main()\n"                       // Line 5
        "{\n"
        "    using namespace N;\n"
        "    int $i;\n"
        "    ++i@; // refers to local i;\n"
        "}\n"                                // Line 10
        ;

602
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
603
    test.run();
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVariable1()
{
    // 3.3.3 Block scope (par. 4), from text
    // Same for if, while, switch
    const QByteArray source =
        "int main()\n"
        "{\n"
        "    int i = 1;\n"
        "    for (int $i = 0; i < 10; ++i) { // 'i' refers to for's i\n"
        "        i = @i; // same\n"                                       // Line 5
        "    }\n"
        "}\n";
        ;

620
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
621
    test.run();
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_loopLocalVarHidesOuterScopeVariable2()
{
    // 3.3.3 Block scope (par. 4), from text
    // Same for if, while, switch
    const QByteArray source =
        "int main()\n"
        "{\n"
        "    int i = 1;\n"
        "    for (int $i = 0; @i < 10; ++i) { // 'i' refers to for's i\n"
        "        i = i; // same\n"                                         // Line 5
        "    }\n"
        "}\n";
        ;

638
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
639
    test.run();
640
641
642
643
644
645
646
647
648
649
650
651
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_subsequentDefinedClassMember()
{
    // 3.3.7 Class scope, part of the example
    const QByteArray source =
        "class X {\n"
        "    int f() { return @i; } // i refers to class's i\n"
        "    int $i;\n"
        "};\n"
        ;

652
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
653
    test.run();
654
655
656
657
658
659
660
661
662
663
664
665
666
667
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classMemberHidesOuterTypeDef()
{
    // 3.3.7 Class scope, part of the example
    // Variable name hides type name.
    const QByteArray source =
        "typedef int c;\n"
        "class X {\n"
        "    int f() { return @c; } // c refers to class' c\n"
        "    int $c; // hides typedef name\n"
        "};\n"                                                 // Line 5
        ;

668
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
669
    test.run();
670
671
672
673
674
675
676
677
678
679
680
681
682
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromEnum()
{
    // 3.3.2 Point of declaration (par. 1), copy-paste
    const QByteArray source =
        "const int $x = 12;\n"
        "int main()\n"
        "{\n"
        "    enum { x = @x }; // x refers to global x\n"
        "}\n"                                             // Line 5
        ;

683
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
684
    test.run(/*expectedFail =*/ true);
685
686
687
688
689
690
691
692
693
694
695
696
697
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_selfInitialization()
{
    // 3.3.2 Point of declaration
    const QByteArray source =
        "int x = 12;\n"
        "int main()\n"
        "{\n"
        "   int $x = @x; // Second x refers to local x\n"
        "}\n"                                              // Line 5
        ;

698
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
699
    test.run();
700
701
702
703
704
705
706
707
708
709
710
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_pointerToClassInClassDefinition()
{
    // 3.3.2 Point of declaration (par. 3), from text
    const QByteArray source =
        "class $Foo {\n"
        "    @Foo *p; // Refers to above Foo\n"
        "};\n"
        ;

711
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
712
    test.run();
713
714
715
716
717
718
719
720
721
722
723
724
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_previouslyDefinedMemberFromArrayDefinition()
{
    // 3.3.2 Point of declaration (par. 5), copy-paste
    const QByteArray source =
        "struct X {\n"
        "    enum E { $z = 16 };\n"
        "    int b[X::@z]; // z refers to defined z\n"
        "};\n"
        ;

725
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
726
    test.run();
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_outerStaticMemberVariableFromInsideSubclass()
{
    // 3.3.7 Class scope (par. 2), from text
    const QByteArray source =
        "struct C\n"
        "{\n"
        "   struct I\n"
        "   {\n"
        "       void f()\n"                            // Line 5
        "       {\n"
        "           int i = @c; // refers to C's c\n"
        "       }\n"
        "   };\n"
        "\n"                                           // Line 10
        "   static int $c;\n"
        "};\n"
        ;

747
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
748
    test.run();
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingDotOperator()
{
    // 3.3.7 Class scope (par. 1), part of point 5
    const QByteArray source =
        "struct C\n"
        "{\n"
        "    int $member;\n"
        "};\n"
        "\n"                 // Line 5
        "int main()\n"
        "{\n"
        "    C c;\n"
        "    c.@member++;\n"
        "}\n"                // Line 10
        ;

767
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
768
    test.run();
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_memberVariableFollowingArrowOperator()
{
    // 3.3.7 Class scope (par. 1), part of point 5
    const QByteArray source =
        "struct C\n"
        "{\n"
        "    int $member;\n"
        "};\n"
        "\n"                    // Line 5
        "int main()\n"
        "{\n"
        "    C* c;\n"
        "    c->@member++;\n"
        "}\n"                   // Line 10
        ;

787
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
788
    test.run();
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_staticMemberVariableFollowingScopeOperator()
{
    // 3.3.7 Class scope (par. 1), part of point 5
    const QByteArray source =
        "struct C\n"
        "{\n"
        "    static int $member;\n"
        "};\n"
        "\n"                        // Line 5
        "int main()\n"
        "{\n"
        "    C::@member++;\n"
        "}\n"
        ;

806
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
807
    test.run();
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_staticMemberVariableFollowingDotOperator()
{
    // 3.3.7 Class scope (par. 2), from text
    const QByteArray source =
        "struct C\n"
        "{\n"
        "    static int $member;\n"
        "};\n"
        "\n"                        // Line 5
        "int main()\n"
        "{\n"
        "    C c;\n"
        "    c.@member;\n"
        "}\n"                       // Line 10
        ;

826
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
827
    test.run();
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
}


void CppEditorPlugin::test_FollowSymbolUnderCursor_staticMemberVariableFollowingArrowOperator()
{
    // 3.3.7 Class scope (par. 2), from text
    const QByteArray source =
        "struct C\n"
        "{\n"
        "    static int $member;\n"
        "};\n"
        "\n"                         // Line 5
        "int main()\n"
        "{\n"
        "    C *c;\n"
        "    c->@member++;\n"
        "}\n"                        // Line 10
        ;

847
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
848
    test.run();
849
850
851
852
853
854
855
856
857
858
859
860
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_previouslyDefinedEnumValueFromInsideEnum()
{
    // 3.3.8 Enumeration scope
    const QByteArray source =
        "enum {\n"
        "    $i = 0,\n"
        "    j = @i // refers to i above\n"
        "};\n"
        ;

861
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
862
    test.run();
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_nsMemberHidesNsMemberIntroducedByUsingDirective()
{
    // 3.3.8 Enumeration scope
    const QByteArray source =
        "namespace A {\n"
        "  char x;\n"
        "}\n"
        "\n"
        "namespace B {\n"                               // Line 5
        "  using namespace A;\n"
        "  int $x; // hides A::x\n"
        "}\n"
        "\n"
        "int main()\n"                                  // Line 10
        "{\n"
        "    B::@x++; // refers to B's X, not A::x\n"
        "}\n"
        ;

884
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
885
    test.run();
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_baseClassFunctionIntroducedByUsingDeclaration()
{
    // 3.3.10 Name hiding, from text
    // www.stroustrup.com/bs_faq2.html#overloadderived
    const QByteArray source =
        "struct B {\n"
        "    int $f(int) {}\n"
        "};\n"
        "\n"
        "class D : public B {\n"                              // Line 5
        "public:\n"
        "    using B::f; // make every f from B available\n"
        "    double f(double) {}\n"
        "};\n"
        "\n"                                                  // Line 10
        "int main()\n"
        "{\n"
        "    D* pd = new D;\n"
        "    pd->@f(2); // refers to B::f\n"
        "    pd->f(2.3); // refers to D::f\n"                 // Line 15
        "}\n"
        ;

911
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
912
    test.run();
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_funWithSameNameAsBaseClassFunIntroducedByUsingDeclaration()
{
    // 3.3.10 Name hiding, from text
    // www.stroustrup.com/bs_faq2.html#overloadderived
    const QByteArray source =
        "struct B {\n"
        "    int f(int) {}\n"
        "};\n"
        "\n"
        "class D : public B {\n"                              // Line 5
        "public:\n"
        "    using B::f; // make every f from B available\n"
        "    double $f(double) {}\n"
        "};\n"
        "\n"                                                  // Line 10
        "int main()\n"
        "{\n"
        "    D* pd = new D;\n"
        "    pd->f(2); // refers to B::f\n"
        "    pd->@f(2.3); // refers to D::f\n"                // Line 15
        "}\n"
        ;

938
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
939
    test.run();
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_funLocalVarHidesOuterClass()
{
    // 3.3.10 Name hiding (par 2.), from text
    // A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member,
    // function, or enumerator declared in the same scope.
    const QByteArray source =
        "struct C {};\n"
        "\n"
        "int main()\n"
        "{\n"
        "    int $C; // hides type C\n"  // Line 5
        "    ++@C;\n"
        "}\n"
        ;

957
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
    test.run();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classConstructor()
{
    const QByteArray source =
            "class Foo {\n"
            "    F@oo();"
            "    ~Foo();"
            "};\n\n"
            "Foo::$Foo()\n"
            "{\n"
            "}\n\n"
            "Foo::~Foo()\n"
            "{\n"
            "}\n";

975
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
    test.run();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classDestructor()
{
    const QByteArray source =
            "class Foo {\n"
            "    Foo();"
            "    ~@Foo();"
            "};\n\n"
            "Foo::Foo()\n"
            "{\n"
            "}\n\n"
            "Foo::~$Foo()\n"
            "{\n"
            "}\n";

993
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
Joerg Bornemann's avatar
Joerg Bornemann committed
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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
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
    test.run();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_QObject_connect_data()
{
#define TAG(str) secondQObjectParam ? str : str ", no 2nd QObject"
    QTest::addColumn<char>("start");
    QTest::addColumn<char>("target");
    QTest::addColumn<bool>("secondQObjectParam");
    for (int i = 0; i < 2; ++i) {
        bool secondQObjectParam = (i == 0);
        QTest::newRow(TAG("SIGNAL: before keyword"))
                << '1' << '1' << secondQObjectParam;
        QTest::newRow(TAG("SIGNAL: in keyword"))
                << '2' << '1' << secondQObjectParam;
        QTest::newRow(TAG("SIGNAL: before parenthesis"))
                << '3' << '1' << secondQObjectParam;
        QTest::newRow(TAG("SIGNAL: before identifier"))
                << '4' << '1' << secondQObjectParam;
        QTest::newRow(TAG("SIGNAL: in identifier"))
                << '5' << '1' << secondQObjectParam;
        QTest::newRow(TAG("SIGNAL: before identifier parenthesis"))
                << '6' << '1' << secondQObjectParam;
        QTest::newRow(TAG("SLOT: before keyword"))
                << '7' << '2' << secondQObjectParam;
        QTest::newRow(TAG("SLOT: in keyword"))
                << '8' << '2' << secondQObjectParam;
        QTest::newRow(TAG("SLOT: before parenthesis"))
                << '9' << '2' << secondQObjectParam;
        QTest::newRow(TAG("SLOT: before identifier"))
                << 'A' << '2' << secondQObjectParam;
        QTest::newRow(TAG("SLOT: in identifier"))
                << 'B' << '2' << secondQObjectParam;
        QTest::newRow(TAG("SLOT: before identifier parenthesis"))
                << 'C' << '2' << secondQObjectParam;
    }
#undef TAG
}

static void selectMarker(QByteArray *source, char marker, char number)
{
    int idx = 0;
    forever {
        idx = source->indexOf(marker, idx);
        if (idx == -1)
            break;
        if (source->at(idx + 1) == number) {
            ++idx;
            source->remove(idx, 1);
        } else {
            source->remove(idx, 2);
        }
    }
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_QObject_connect()
{
    QFETCH(char, start);
    QFETCH(char, target);
    QFETCH(bool, secondQObjectParam);
    QByteArray source =
            "class Foo : public QObject\n"
            "{\n"
            "signals:\n"
            "    void $1endOfWorld();\n"
            "public slots:\n"
            "    void $2onWorldEnded()\n"
            "    {\n"
            "    }\n"
            "};\n"
            "\n"
            "void bla()\n"
            "{\n"
            "   Foo foo;\n"
            "   connect(&foo, @1SI@2GNAL@3(@4end@5OfWorld@6()),\n"
            "           &foo, @7SL@8OT@9(@Aon@BWorldEnded@C()));\n"
            "}\n";

    selectMarker(&source, '@', start);
    selectMarker(&source, '$', target);

    if (!secondQObjectParam)
        source.replace(" &foo, ", QByteArray());

    if (start >= '7' && !secondQObjectParam) {
        qWarning("SLOT jump triggers QTCREATORBUG-10265. Skipping.");
        return;
    }

1083
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
1084
    test.run();
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
void CppEditorPlugin::test_FollowSymbolUnderCursor_classOperator_onOperatorToken_data()
{
    QTest::addColumn<bool>("toDeclaration");
    QTest::newRow("forward") << false;
    QTest::newRow("backward") << true;
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classOperator_onOperatorToken()
{
    QFETCH(bool, toDeclaration);

    QByteArray source =
            "class Foo {\n"
            "    void @operator[](size_t idx);\n"
            "};\n\n"
            "void Foo::$operator[](size_t idx)\n"
            "{\n"
            "}\n";
    if (toDeclaration)
        source.replace('@', '#').replace('$', '@').replace('#', '$');
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classOperator_data()
{
    test_FollowSymbolUnderCursor_classOperator_onOperatorToken_data();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classOperator()
{
    QFETCH(bool, toDeclaration);

    QByteArray source =
            "class Foo {\n"
            "    void $2operator@1[](size_t idx);\n"
            "};\n\n"
            "void Foo::$1operator@2[](size_t idx)\n"
            "{\n"
            "}\n";
    if (toDeclaration)
        source.replace("@1", QByteArray()).replace("$1", QByteArray())
                .replace("@2", "@").replace("$2", "$");
    else
        source.replace("@2", QByteArray()).replace("$2", QByteArray())
                .replace("@1", "@").replace("$1", "$");
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classOperator_inOp_data()
{
    test_FollowSymbolUnderCursor_classOperator_onOperatorToken_data();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_classOperator_inOp()
{
    QFETCH(bool, toDeclaration);

    QByteArray source =
            "class Foo {\n"
            "    void $2operator[@1](size_t idx);\n"
            "};\n\n"
            "void Foo::$1operator[@2](size_t idx)\n"
            "{\n"
            "}\n";
    if (toDeclaration)
        source.replace("@1", QByteArray()).replace("$1", QByteArray())
                .replace("@2", "@").replace("$2", "$");
    else
        source.replace("@2", QByteArray()).replace("$2", QByteArray())
                .replace("@1", "@").replace("$1", "$");
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
void CppEditorPlugin::test_FollowSymbolUnderCursor_using_QTCREATORBUG7903_globalNamespace()
{
    const QByteArray source =
            "namespace NS {\n"
            "class Foo {};\n"
            "}\n"
            "using NS::$Foo;\n"
            "void fun()\n"
            "{\n"
            "    @Foo foo;\n"
            "}\n"
        ;

1176
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
1177
    test.run();
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_using_QTCREATORBUG7903_namespace()
{
    const QByteArray source =
            "namespace NS {\n"
            "class Foo {};\n"
            "}\n"
            "namespace NS1 {\n"
            "void fun()\n"
            "{\n"
            "    using NS::$Foo;\n"
            "    @Foo foo;\n"
            "}\n"
            "}\n"
        ;

1195
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
1196
    test.run();
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_using_QTCREATORBUG7903_insideFunction()
{
    const QByteArray source =
            "namespace NS {\n"
            "class Foo {};\n"
            "}\n"
            "void fun()\n"
            "{\n"
            "    using NS::$Foo;\n"
            "    @Foo foo;\n"
            "}\n"
        ;

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
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
    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

/// Check: Static type is base class pointer, all overrides are presented.
void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_allOverrides()
{
    const QByteArray source =
            "struct A { virtual void virt() = 0; };\n"
            "void A::virt() {}\n"
            "\n"
            "struct B : A { void virt(); };\n"
            "void B::virt() {}\n"
            "\n"
            "struct C : B { void virt(); };\n"
            "void C::virt() {}\n"
            "\n"
            "struct CD1 : C { void virt(); };\n"
            "void CD1::virt() {}\n"
            "\n"
            "struct CD2 : C { void virt(); };\n"
            "void CD2::virt() {}\n"
            "\n"
            "int f(A *o)\n"
            "{\n"
            "    o->$@virt();\n"
            "}\n"
            ;

    const QStringList immediateResults = QStringList()
            << QLatin1String("A::virt")
            << QLatin1String("...searching overrides");
    const QStringList finalResults = QStringList()
            << QLatin1String("A::virt")
            << QLatin1String("A::virt") // TODO: Double entry
            << QLatin1String("B::virt")
            << QLatin1String("C::virt")
            << QLatin1String("CD1::virt")
            << QLatin1String("CD2::virt");

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source, immediateResults, finalResults);
    test.run();
}

/// Check: Static type is derived class pointer, only overrides of sub classes are presented.
void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_possibleOverrides1()
{
    const QByteArray source =
            "struct A { virtual void virt() = 0; };\n"
            "void A::virt() {}\n"
            "\n"
            "struct B : A { void virt(); };\n"
            "void B::virt() {}\n"
            "\n"
            "struct C : B { void virt(); };\n"
            "void C::virt() {}\n"
            "\n"
            "struct CD1 : C { void virt(); };\n"
            "void CD1::virt() {}\n"
            "\n"
            "struct CD2 : C { void virt(); };\n"
            "void CD2::virt() {}\n"
            "\n"
            "int f(B *o)\n"
            "{\n"
            "   o->$@virt();\n"
            "}\n"
            ;

    const QStringList immediateResults = QStringList()
            << QLatin1String("B::virt")
            << QLatin1String("...searching overrides");
    const QStringList finalResults = QStringList()
            << QLatin1String("B::virt")
            << QLatin1String("B::virt") // Double entry
            << QLatin1String("C::virt")
            << QLatin1String("CD1::virt")
            << QLatin1String("CD2::virt");

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source, immediateResults, finalResults);
    test.run();
}

/// Check: Virtual function call in member of class hierarchy, only possible overrides are presented.
void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_possibleOverrides2()
{
    const QByteArray source =
            "struct A { virtual void f(); };\n"
            "void A::f() {}\n"
            "\n"
            "struct B : public A { void f(); };\n"
            "void B::f() {}\n"
            "\n"
            "struct C : public B { void g() { f$@(); } }; \n"
            "\n"
            "struct D : public C { void f(); };\n"
            "void D::f() {}\n"
            ;

    const QStringList immediateResults = QStringList()
            << QLatin1String("B::f")
            << QLatin1String("...searching overrides");
    const QStringList finalResults = QStringList()
            << QLatin1String("B::f")
            << QLatin1String("B::f")
            << QLatin1String("D::f");

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source, immediateResults, finalResults);
    test.run();
}

/// Check: Do not trigger on qualified function calls.
void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_notOnQualified()
{
    const QByteArray source =
            "struct A { virtual void f(); };\n"
            "void A::$f() {}\n"
            "\n"
            "struct B : public A {\n"
            "    void f();\n"
            "    void g() { A::@f(); }\n"
            "};\n"
            ;

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

/// Check: Do not trigger on member function declaration.
void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_notOnDeclaration()
{
    const QByteArray source =
            "struct A { virtual void f(); };\n"
            "void A::f() {}\n"
            "\n"
            "struct B : public A { void f@(); };\n"
            "void B::$f() {}\n"
            ;

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

/// Check: Do not trigger on function definition.
void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_notOnDefinition()
{
    const QByteArray source =
            "struct A { virtual void f(); };\n"
            "void A::f() {}\n"
            "\n"
            "struct B : public A { void $f(); };\n"
            "void B::@f() {}\n"
            ;

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
    test.run();
}

void CppEditorPlugin::test_FollowSymbolUnderCursor_virtualFunctionCall_notOnNonPointerNonReference()
{
    const QByteArray source =
            "struct A { virtual void f(); };\n"
            "void A::f() {}\n"
            "\n"
            "struct B : public A { void f(); };\n"
            "void B::$f() {}\n"
            "\n"
            "void client(B b) { b.@f(); }\n"
            ;

    TestCase test(TestCase::FollowSymbolUnderCursorAction, source);
1383
    test.run();
1384
1385
}

1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
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
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
/*
Potential test cases improving name lookup.

If you fix one, add a test and remove the example from here.

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.1 Declarative regions and scopes, copy-paste (main added)
int j = 24;
int main()
{
    int i = j, j; // First j refers to global j, second j refers to just locally defined j
    j = 42; // Refers to locally defined j
}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.2 Point of declaration (par. 2), copy-paste (main added)
const int i = 2;
int main()
{
    int i[i]; // Second i refers to global
}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.2 Point of declaration (par. 9), copy-paste (main added)
typedef unsigned char T;
template<class T
= T // lookup finds the typedef name of unsigned char
, T // lookup finds the template parameter
N = 0> struct A { };

int main() {}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.9 Template parameter scope (par. 3), copy-paste (main added), part 1
template<class T, T* p, class U = T> class X {}; // second and third T refers to first one
template<class T> void f(T* p = new T);

int main() {}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.9 Template parameter scope (par. 3), copy-paste (main added), part 2
template<class T> class X : public Array<T> {};
template<class T> class Y : public T {};

int main() {}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.9 Template parameter scope (par. 4), copy-paste (main added), part 2
typedef int N;
template<N X, typename N, template<N Y> class T> struct A; // N refers to N above

int main() {}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.10 Name hiding (par 1.), from text, example 2a

// www.stroustrup.com/bs_faq2.html#overloadderived
// "In C++, there is no overloading across scopes - derived class scopes are not
// an exception to this general rule. (See D&E or TC++PL3 for details)."
struct B {
    int f(int) {}
};

struct D : public B {
    double f(double) {} // hides B::f
};

int main()
{
    D* pd = new D;
    pd->f(2); // refers to D::f
    pd->f(2.3); // refers to D::f
}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 3.3.10 Name hiding (par 2.), from text
int C; // hides following type C, order is not important
struct C {};

int main()
{
    ++C;
}

*/