aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/followsymbol_switchmethoddecldef_test.cpp
blob: 428b6e20793d6dc4ebb2bef4de68858b0bed329b (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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
438
439
440
441
442
443
444
445
446
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
477
478
479
480
481
482
483
484
485
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
517
518
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
549
550
551
552
553
554
555
556
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
587
588
589
590
591
592
593
594
595
596
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
627
628
629
630
631
632
633
634
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
665
666
667
668
669
670
671
672
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
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
911
912
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
/****************************************************************************
**
** 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"

#include <utils/fileutils.h>

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


/*!
    Tests for Follow Symbol Under Cursor and Switch Between Method Declaration/Definition

    Section numbers refer to

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

    You can find potential test code for Follow Symbol Under Cursor on the bottom of this file.
 */
using namespace CPlusPlus;
using namespace CppEditor;
using namespace CppEditor::Internal;
using namespace CppTools;
using namespace TextEditor;
using namespace Core;

namespace {

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);
            if (targetCursorPosition != -1) {
                source.remove(targetCursorPosition, 1);
                --initialCursorPosition;
            }
        } else {
            source.remove(targetCursorPosition, 1);
            if (initialCursorPosition != -1) {
                source.remove(initialCursorPosition, 1);
                --targetCursorPosition;
            }
        }
    }

    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,
 * executing Follow Symbol Under Cursor or Switch Between Method Declaration/Definition
 * and checking the result.
 */
class TestCase
{
public:
    enum CppEditorAction {
        FollowSymbolUnderCursor,
        SwitchBetweenMethodDeclarationDefinition
    };

    TestCase(CppEditorAction action, const QByteArray &source);
    TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles);
    ~TestCase();

    void run();

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

    void init();

    TestDocumentPtr testFileWithInitialCursorMarker();
    TestDocumentPtr testFileWithTargetCursorMarker();

private:
    CppEditorAction m_action;
    QList<TestDocumentPtr> m_testFiles;
};

/// Convenience function for creating a TestDocument.
/// See TestDocument.
TestCase::TestCase(CppEditorAction action, const QByteArray &source)
    : m_action(action)
{
    m_testFiles << TestDocument::create(source, QLatin1String("file.cpp"));
    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.
TestCase::TestCase(CppEditorAction action, const QList<TestDocumentPtr> theTestFiles)
    : m_action(action)
    , m_testFiles(theTestFiles)
{
    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();
    foreach (TestDocumentPtr testFile, m_testFiles) {
        testFile->directoryPath = directoryPath;
        testFile->writeToDisk();
    }

    // Update Code Model
    QStringList filePaths;
    foreach (const TestDocumentPtr &testFile, m_testFiles)
        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
    foreach (TestDocumentPtr testFile, m_testFiles) {
        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.
        // The file is "Full Checked" since it is in the working copy now,
        // that is the function bodies are processed.
        forever {
            Snapshot snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
            if (Document::Ptr document = snapshot.document(testFile->filePath())) {
                if (document->checkMode() == Document::FullCheck)
                    break;
                QCoreApplication::processEvents();
            }
        }

        // 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;
    foreach (const TestDocumentPtr testFile, m_testFiles) {
        if (testFile->editor)
            editorsToClose << testFile->editor;
    }
    EditorManager::instance()->closeEditors(editorsToClose, false);
    QCoreApplication::processEvents(); // process any pending events

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

TestDocumentPtr TestCase::testFileWithInitialCursorMarker()
{
    foreach (const TestDocumentPtr testFile, m_testFiles) {
        if (testFile->hasInitialCursorMarker())
            return testFile;
    }
    return TestDocumentPtr();
}

TestDocumentPtr TestCase::testFileWithTargetCursorMarker()
{
    foreach (const TestDocumentPtr testFile, m_testFiles) {
        if (testFile->hasTargetCursorMarker())
            return testFile;
    }
    return TestDocumentPtr();
}

void TestCase::run()
{
    TestDocumentPtr initialTestFile = testFileWithInitialCursorMarker();
    QVERIFY(initialTestFile);
    TestDocumentPtr targetTestFile = testFileWithTargetCursorMarker();
    QVERIFY(targetTestFile);

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

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

    // Trigger the action
    switch (m_action) {
    case FollowSymbolUnderCursor:
        initialTestFile->editorWidget->openLinkUnderCursor();
        break;
    case SwitchBetweenMethodDeclarationDefinition:
        CppEditorPlugin::instance()->switchDeclarationDefinition();
        break;
    default:
        QFAIL("Unknown test action");
        break;
    }

    QCoreApplication::processEvents();

    // Compare
    IEditor *currentEditor = EditorManager::instance()->currentEditor();
    BaseTextEditor *currentTextEditor = dynamic_cast<BaseTextEditor*>(currentEditor);
    QVERIFY(currentTextEditor);

    QCOMPARE(currentTextEditor->document()->fileName(), targetTestFile->filePath());
    int expectedLine, expectedColumn;
    currentTextEditor->convertPosition(targetTestFile->targetCursorPosition,
                                       &expectedLine, &expectedColumn);
//    qDebug() << "Expected line:" << expectedLine;
//    qDebug() << "Expected column:" << expectedColumn;
    QCOMPARE(currentTextEditor->currentLine(), expectedLine);
    QCOMPARE(currentTextEditor->currentColumn() - 1, expectedColumn);
}

} // anonymous namespace

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"));

    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
    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"));

    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
    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"));

    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
    test.run();
}

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"));

    TestCase test(TestCase::SwitchBetweenMethodDeclarationDefinition, testFiles);
    test.run();
}

/// Check ...
void CppEditorPlugin::test_FollowSymbolUnderCursor_globalVarFromFunction()
{
    const QByteArray source =
        "int $j;\n"
        "int main()\n"
        "{\n"
        "    @j = 2;\n"
        "}\n"           // Line 5
        ;

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

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"
        ;

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

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
        ;

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

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";
        ;

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

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";
        ;

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

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"
        ;

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

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
        ;

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

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
        ;

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

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
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

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
        ;

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

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
        ;

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

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"
        ;

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

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
        ;

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


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
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

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"
        ;

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

/*
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;
}

*/