cppcodegen_test.cpp 24 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Bill King's avatar
Bill King committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Bill King's avatar
Bill King committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Bill King's avatar
Bill King committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
Bill King's avatar
Bill King committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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
Bill King's avatar
Bill King committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Bill King's avatar
Bill King committed
29

30
#include "cpptoolsplugin.h"
31 32
#include "cpptoolstestcase.h"
#include "insertionpointlocator.h"
33

34
#include <utils/fileutils.h>
35 36

#include <QtTest>
hjk's avatar
hjk committed
37
#include <QDebug>
38
#include <QDir>
39

40 41 42 43
/*!
    Tests for various parts of the code generation. Well, okay, currently it only
    tests the InsertionPointLocator.
 */
44
using namespace CPlusPlus;
45
using namespace CppTools;
46
using namespace CppTools::Internal;
47

48 49 50
/*!
    Should insert at line 3, column 1, with "public:\n" as prefix and without suffix.
 */
51
void CppToolsPlugin::test_codegen_public_in_empty_class()
52 53 54 55
{
    const QByteArray src = "\n"
            "class Foo\n" // line 1
            "{\n"
56
            "};\n"
57 58
            "\n";

59
    Document::Ptr doc = Document::create(QLatin1String("public_in_empty_class"));
60
    doc->setUtf8Source(src);
61 62 63 64 65 66
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 1U);

67 68 69 70 71 72 73
    Class *foo = doc->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);

    Snapshot snapshot;
    snapshot.insert(doc);
74
    CppRefactoringChanges changes(snapshot);
75
    InsertionPointLocator find(changes);
76
    InsertionLocation loc = find.methodDeclarationInClass(
77 78
                doc->fileName(),
                foo,
79
                InsertionPointLocator::Public);
80
    QVERIFY(loc.isValid());
81
    QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
82
    QVERIFY(loc.suffix().isEmpty());
83
    QCOMPARE(loc.line(), 3U);
84 85 86
    QCOMPARE(loc.column(), 1U);
}

87 88 89
/*!
    Should insert at line 3, column 1, without prefix and without suffix.
 */
90
void CppToolsPlugin::test_codegen_public_in_nonempty_class()
91 92 93 94
{
    const QByteArray src = "\n"
            "class Foo\n" // line 1
            "{\n"
95 96
            "public:\n"   // line 3
            "};\n"        // line 4
97 98
            "\n";

99
    Document::Ptr doc = Document::create(QLatin1String("public_in_nonempty_class"));
100
    doc->setUtf8Source(src);
101 102 103 104 105 106
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 1U);

107 108 109 110
    Class *foo = doc->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
111

112 113
    Snapshot snapshot;
    snapshot.insert(doc);
114
    CppRefactoringChanges changes(snapshot);
115
    InsertionPointLocator find(changes);
116
    InsertionLocation loc = find.methodDeclarationInClass(
117 118
                doc->fileName(),
                foo,
119
                InsertionPointLocator::Public);
120
    QVERIFY(loc.isValid());
121
    QVERIFY(loc.prefix().isEmpty());
122
    QVERIFY(loc.suffix().isEmpty());
123
    QCOMPARE(loc.line(), 4U);
124 125 126
    QCOMPARE(loc.column(), 1U);
}

127 128 129
/*!
    Should insert at line 3, column 1, with "public:\n" as prefix and "\n suffix.
 */
130
void CppToolsPlugin::test_codegen_public_before_protected()
131 132 133 134 135 136 137 138
{
    const QByteArray src = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "protected:\n" // line 3
            "};\n"
            "\n";

139
    Document::Ptr doc = Document::create(QLatin1String("public_before_protected"));
140
    doc->setUtf8Source(src);
141 142 143 144 145 146
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 1U);

147 148 149 150
    Class *foo = doc->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
151

152 153
    Snapshot snapshot;
    snapshot.insert(doc);
154
    CppRefactoringChanges changes(snapshot);
155
    InsertionPointLocator find(changes);
156
    InsertionLocation loc = find.methodDeclarationInClass(
157 158
                doc->fileName(),
                foo,
159
                InsertionPointLocator::Public);
160 161 162 163 164 165 166
    QVERIFY(loc.isValid());
    QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
    QCOMPARE(loc.suffix(), QLatin1String("\n"));
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.line(), 3U);
}

167 168 169 170
/*!
    Should insert at line 4, column 1, with "private:\n" as prefix and without
    suffix.
 */
171
void CppToolsPlugin::test_codegen_private_after_protected()
172 173 174 175 176 177 178 179
{
    const QByteArray src = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "protected:\n" // line 3
            "};\n"
            "\n";

180
    Document::Ptr doc = Document::create(QLatin1String("private_after_protected"));
181
    doc->setUtf8Source(src);
182 183 184 185 186 187
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 1U);

188 189 190 191
    Class *foo = doc->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
192

193 194
    Snapshot snapshot;
    snapshot.insert(doc);
195
    CppRefactoringChanges changes(snapshot);
196
    InsertionPointLocator find(changes);
197
    InsertionLocation loc = find.methodDeclarationInClass(
198 199
                doc->fileName(),
                foo,
200
                InsertionPointLocator::Private);
201 202 203 204 205 206 207
    QVERIFY(loc.isValid());
    QCOMPARE(loc.prefix(), QLatin1String("private:\n"));
    QVERIFY(loc.suffix().isEmpty());
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.line(), 4U);
}

208 209 210 211
/*!
    Should insert at line 4, column 1, with "protected:\n" as prefix and without
    suffix.
 */
212
void CppToolsPlugin::test_codegen_protected_in_nonempty_class()
213 214 215 216 217 218 219 220
{
    const QByteArray src = "\n"
            "class Foo\n" // line 1
            "{\n"
            "public:\n"   // line 3
            "};\n"        // line 4
            "\n";

221
    Document::Ptr doc = Document::create(QLatin1String("protected_in_nonempty_class"));
222
    doc->setUtf8Source(src);
223 224 225 226 227 228
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 1U);

229 230 231 232
    Class *foo = doc->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
233

234 235
    Snapshot snapshot;
    snapshot.insert(doc);
236
    CppRefactoringChanges changes(snapshot);
237
    InsertionPointLocator find(changes);
238
    InsertionLocation loc = find.methodDeclarationInClass(
239 240
                doc->fileName(),
                foo,
241
                InsertionPointLocator::Protected);
242 243 244 245 246 247 248
    QVERIFY(loc.isValid());
    QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
    QVERIFY(loc.suffix().isEmpty());
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.line(), 4U);
}

249 250 251
/*!
    Should insert at line 4, column 1, with "protected\n" as prefix and "\n" suffix.
 */
252
void CppToolsPlugin::test_codegen_protected_between_public_and_private()
253 254 255 256 257 258 259 260 261
{
    const QByteArray src = "\n"
            "class Foo\n" // line 1
            "{\n"
            "public:\n"   // line 3
            "private:\n"  // line 4
            "};\n"        // line 5
            "\n";

262
    Document::Ptr doc = Document::create(QLatin1String("protected_betwee_public_and_private"));
263
    doc->setUtf8Source(src);
264 265 266 267 268 269
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 1U);

270 271 272 273
    Class *foo = doc->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
274

275 276
    Snapshot snapshot;
    snapshot.insert(doc);
277
    CppRefactoringChanges changes(snapshot);
278
    InsertionPointLocator find(changes);
279
    InsertionLocation loc = find.methodDeclarationInClass(
280 281
                doc->fileName(),
                foo,
282
                InsertionPointLocator::Protected);
283 284 285 286 287 288 289
    QVERIFY(loc.isValid());
    QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
    QCOMPARE(loc.suffix(), QLatin1String("\n"));
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.line(), 4U);
}

290 291 292 293 294 295 296
/*!
    Should insert at line 18, column 1, with "private slots:\n" as prefix and "\n"
    as suffix.

    This is the typical Qt Designer case, with test-input like what the integration
    generates.
 */
297
void CppToolsPlugin::test_codegen_qtdesigner_integration()
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
{
    const QByteArray src = "/**** Some long (C)opyright notice ****/\n"
            "#ifndef MAINWINDOW_H\n"
            "#define MAINWINDOW_H\n"
            "\n"
            "#include <QMainWindow>\n"
            "\n"
            "namespace Ui {\n"
            "    class MainWindow;\n"
            "}\n"
            "\n"
            "class MainWindow : public QMainWindow\n" // line 10
            "{\n"
            "    Q_OBJECT\n"
            "\n"
            "public:\n" // line 14
            "    explicit MainWindow(QWidget *parent = 0);\n"
            "    ~MainWindow();\n"
            "\n"
            "private:\n" // line 18
            "    Ui::MainWindow *ui;\n"
            "};\n"
            "\n"
            "#endif // MAINWINDOW_H\n";

323
    Document::Ptr doc = Document::create(QLatin1String("qtdesigner_integration"));
324
    doc->setUtf8Source(src);
325 326 327 328 329 330
    doc->parse();
    doc->check();

    QCOMPARE(doc->diagnosticMessages().size(), 0);
    QCOMPARE(doc->globalSymbolCount(), 2U);

331 332 333 334
    Class *foo = doc->globalSymbolAt(1)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 10U);
    QCOMPARE(foo->column(), 7U);
335

336 337
    Snapshot snapshot;
    snapshot.insert(doc);
338
    CppRefactoringChanges changes(snapshot);
339
    InsertionPointLocator find(changes);
340
    InsertionLocation loc = find.methodDeclarationInClass(
341 342
                doc->fileName(),
                foo,
343
                InsertionPointLocator::PrivateSlot);
344 345 346 347 348 349 350
    QVERIFY(loc.isValid());
    QCOMPARE(loc.prefix(), QLatin1String("private slots:\n"));
    QCOMPARE(loc.suffix(), QLatin1String("\n"));
    QCOMPARE(loc.line(), 18U);
    QCOMPARE(loc.column(), 1U);
}

351
void CppToolsPlugin::test_codegen_definition_empty_class()
352 353 354 355 356 357 358 359 360 361 362 363
{
    const QByteArray srcText = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "void foo();\n" // line 3
            "};\n"
            "\n";

    const QByteArray dstText = "\n"
            "int x;\n"  // line 1
            "\n";

364
    Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
365
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
366
    src->setUtf8Source(srcText);
367 368 369 370 371
    src->parse();
    src->check();
    QCOMPARE(src->diagnosticMessages().size(), 0);
    QCOMPARE(src->globalSymbolCount(), 1U);

372
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
373
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
374
    dst->setUtf8Source(dstText);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    dst->parse();
    dst->check();
    QCOMPARE(dst->diagnosticMessages().size(), 0);
    QCOMPARE(dst->globalSymbolCount(), 1U);

    Snapshot snapshot;
    snapshot.insert(src);
    snapshot.insert(dst);

    Class *foo = src->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
    QCOMPARE(foo->memberCount(), 1U);
    Declaration *decl = foo->memberAt(0)->asDeclaration();
    QVERIFY(decl);
    QCOMPARE(decl->line(), 3U);
    QCOMPARE(decl->column(), 6U);

    CppRefactoringChanges changes(snapshot);
    InsertionPointLocator find(changes);
    QList<InsertionLocation> locList = find.methodDefinition(decl);
    QVERIFY(locList.size() == 1);
    InsertionLocation loc = locList.first();
399
    QCOMPARE(loc.fileName(), dst->fileName());
400 401
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
Leandro Melo's avatar
Leandro Melo committed
402 403
    QCOMPARE(loc.line(), 3U);
    QCOMPARE(loc.column(), 1U);
404 405
}

406
void CppToolsPlugin::test_codegen_definition_first_member()
407 408 409 410 411 412 413 414 415
{
    const QByteArray srcText = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "void foo();\n" // line 3
            "void bar();\n" // line 4
            "};\n"
            "\n";

416
    const QByteArray dstText = QString::fromLatin1(
417 418 419 420 421 422 423 424 425
                "\n"
                "#include \"%1/file.h\"\n" // line 1
                "int x;\n"
                "\n"
                "void Foo::bar()\n" // line 4
                "{\n"
                "\n"
                "}\n"
                "\n"
426
                "int y;\n").arg(QDir::tempPath()).toLatin1();
427

428
    Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
429
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
430
    src->setUtf8Source(srcText);
431 432 433 434 435
    src->parse();
    src->check();
    QCOMPARE(src->diagnosticMessages().size(), 0);
    QCOMPARE(src->globalSymbolCount(), 1U);

436
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
437 438
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
439
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
440
    dst->setUtf8Source(dstText);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    dst->parse();
    dst->check();
    QCOMPARE(dst->diagnosticMessages().size(), 0);
    QCOMPARE(dst->globalSymbolCount(), 3U);

    Snapshot snapshot;
    snapshot.insert(src);
    snapshot.insert(dst);

    Class *foo = src->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
    QCOMPARE(foo->memberCount(), 2U);
    Declaration *decl = foo->memberAt(0)->asDeclaration();
    QVERIFY(decl);
    QCOMPARE(decl->line(), 3U);
    QCOMPARE(decl->column(), 6U);

    CppRefactoringChanges changes(snapshot);
    InsertionPointLocator find(changes);
    QList<InsertionLocation> locList = find.methodDefinition(decl);
    QVERIFY(locList.size() == 1);
    InsertionLocation loc = locList.first();
465
    QCOMPARE(loc.fileName(), dst->fileName());
466 467 468 469 470 471
    QCOMPARE(loc.line(), 4U);
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.suffix(), QLatin1String("\n\n"));
    QCOMPARE(loc.prefix(), QString());
}

472
void CppToolsPlugin::test_codegen_definition_last_member()
473 474 475 476 477 478 479 480 481
{
    const QByteArray srcText = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "void foo();\n" // line 3
            "void bar();\n" // line 4
            "};\n"
            "\n";

482
    const QByteArray dstText = QString::fromLatin1(
483 484 485 486 487 488 489 490 491
                "\n"
                "#include \"%1/file.h\"\n" // line 1
                "int x;\n"
                "\n"
                "void Foo::foo()\n" // line 4
                "{\n"
                "\n"
                "}\n" // line 7
                "\n"
492
                "int y;\n").arg(QDir::tempPath()).toLatin1();
493

494
    Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
495
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
496
    src->setUtf8Source(srcText);
497 498 499 500 501
    src->parse();
    src->check();
    QCOMPARE(src->diagnosticMessages().size(), 0);
    QCOMPARE(src->globalSymbolCount(), 1U);

502
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
503 504
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
505
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
506
    dst->setUtf8Source(dstText);
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    dst->parse();
    dst->check();
    QCOMPARE(dst->diagnosticMessages().size(), 0);
    QCOMPARE(dst->globalSymbolCount(), 3U);

    Snapshot snapshot;
    snapshot.insert(src);
    snapshot.insert(dst);

    Class *foo = src->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
    QCOMPARE(foo->memberCount(), 2U);
    Declaration *decl = foo->memberAt(1)->asDeclaration();
    QVERIFY(decl);
    QCOMPARE(decl->line(), 4U);
    QCOMPARE(decl->column(), 6U);

    CppRefactoringChanges changes(snapshot);
    InsertionPointLocator find(changes);
    QList<InsertionLocation> locList = find.methodDefinition(decl);
    QVERIFY(locList.size() == 1);
    InsertionLocation loc = locList.first();
531
    QCOMPARE(loc.fileName(), dst->fileName());
532 533 534 535 536 537
    QCOMPARE(loc.line(), 7U);
    QCOMPARE(loc.column(), 2U);
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
}

538
void CppToolsPlugin::test_codegen_definition_middle_member()
539 540 541 542 543 544 545 546 547 548
{
    const QByteArray srcText = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "void foo();\n" // line 3
            "void bar();\n" // line 4
            "void car();\n" // line 5
            "};\n"
            "\n";

549
    const QByteArray dstText = QString::fromLatin1(
550 551 552 553 554 555 556 557 558 559 560 561 562 563
                "\n"
                "#include \"%1/file.h\"\n" // line 1
                "int x;\n"
                "\n"
                "void Foo::foo()\n" // line 4
                "{\n"
                "\n"
                "}\n" // line 7
                "\n"
                "void Foo::car()\n" // line 9
                "{\n"
                "\n"
                "}\n"
                "\n"
564
                "int y;\n").arg(QDir::tempPath()).toLatin1();
565

566
    Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
567
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
568
    src->setUtf8Source(srcText);
569 570 571 572 573
    src->parse();
    src->check();
    QCOMPARE(src->diagnosticMessages().size(), 0);
    QCOMPARE(src->globalSymbolCount(), 1U);

574
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
575 576
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
577
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
578
    dst->setUtf8Source(dstText);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    dst->parse();
    dst->check();
    QCOMPARE(dst->diagnosticMessages().size(), 0);
    QCOMPARE(dst->globalSymbolCount(), 4U);

    Snapshot snapshot;
    snapshot.insert(src);
    snapshot.insert(dst);

    Class *foo = src->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
    QCOMPARE(foo->memberCount(), 3U);
    Declaration *decl = foo->memberAt(1)->asDeclaration();
    QVERIFY(decl);
    QCOMPARE(decl->line(), 4U);
    QCOMPARE(decl->column(), 6U);

    CppRefactoringChanges changes(snapshot);
    InsertionPointLocator find(changes);
    QList<InsertionLocation> locList = find.methodDefinition(decl);
    QVERIFY(locList.size() == 1);
    InsertionLocation loc = locList.first();
603
    QCOMPARE(loc.fileName(), dst->fileName());
604 605 606 607 608
    QCOMPARE(loc.line(), 7U);
    QCOMPARE(loc.column(), 2U);
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
}
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

void CppToolsPlugin::test_codegen_definition_middle_member_surrounded_by_undefined()
{
    const QByteArray srcText = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "void foo();\n" // line 3
            "void bar();\n" // line 4
            "void baz();\n" // line 5
            "void car();\n" // line 6
            "};\n"
            "\n";

    const QByteArray dstText = QString::fromLatin1(
            "\n"
            "#include \"%1/file.h\"\n" // line 1
            "int x;\n"
            "\n"
            "void Foo::car()\n" // line 4
            "{\n"
            "\n"
            "}\n"
            "\n"
            "int y;\n").arg(QDir::tempPath()).toLatin1();

    Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
635
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
636 637 638 639 640 641 642 643 644
    src->setUtf8Source(srcText);
    src->parse();
    src->check();
    QCOMPARE(src->diagnosticMessages().size(), 0);
    QCOMPARE(src->globalSymbolCount(), 1U);

    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
645
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
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
    dst->setUtf8Source(dstText);
    dst->parse();
    dst->check();
    QCOMPARE(dst->diagnosticMessages().size(), 0);
    QCOMPARE(dst->globalSymbolCount(), 3U);

    Snapshot snapshot;
    snapshot.insert(src);
    snapshot.insert(dst);

    Class *foo = src->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
    QCOMPARE(foo->memberCount(), 4U);
    Declaration *decl = foo->memberAt(1)->asDeclaration();
    QVERIFY(decl);
    QCOMPARE(decl->line(), 4U);
    QCOMPARE(decl->column(), 6U);

    CppRefactoringChanges changes(snapshot);
    InsertionPointLocator find(changes);
    QList<InsertionLocation> locList = find.methodDefinition(decl);
    QVERIFY(locList.size() == 1);
    InsertionLocation loc = locList.first();
    QCOMPARE(loc.fileName(), dst->fileName());
    QCOMPARE(loc.line(), 4U);
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.prefix(), QString());
    QCOMPARE(loc.suffix(), QLatin1String("\n\n"));
}

void CppToolsPlugin::test_codegen_definition_member_specific_file()
{
    const QByteArray srcText = "\n"
            "class Foo\n"  // line 1
            "{\n"
            "void foo();\n" // line 3
            "void bar();\n" // line 4
            "void baz();\n" // line 5
            "};\n"
            "\n"
            "void Foo::bar()\n"
            "{\n"
            "\n"
            "}\n";

    const QByteArray dstText = QString::fromLatin1(
                "\n"
                "#include \"%1/file.h\"\n" // line 1
                "int x;\n"
                "\n"
                "void Foo::foo()\n" // line 4
                "{\n"
                "\n"
                "}\n" // line 7
                "\n"
                "int y;\n").arg(QDir::tempPath()).toLatin1();

    Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
706
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
707 708 709 710 711 712 713 714 715
    src->setUtf8Source(srcText);
    src->parse();
    src->check();
    QCOMPARE(src->diagnosticMessages().size(), 0);
    QCOMPARE(src->globalSymbolCount(), 2U);

    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
716
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
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
    dst->setUtf8Source(dstText);
    dst->parse();
    dst->check();
    QCOMPARE(dst->diagnosticMessages().size(), 0);
    QCOMPARE(dst->globalSymbolCount(), 3U);

    Snapshot snapshot;
    snapshot.insert(src);
    snapshot.insert(dst);

    Class *foo = src->globalSymbolAt(0)->asClass();
    QVERIFY(foo);
    QCOMPARE(foo->line(), 1U);
    QCOMPARE(foo->column(), 7U);
    QCOMPARE(foo->memberCount(), 3U);
    Declaration *decl = foo->memberAt(2)->asDeclaration();
    QVERIFY(decl);
    QCOMPARE(decl->line(), 5U);
    QCOMPARE(decl->column(), 6U);

    CppRefactoringChanges changes(snapshot);
    InsertionPointLocator find(changes);
    QList<InsertionLocation> locList = find.methodDefinition(decl, true, dst->fileName());
    QVERIFY(locList.size() == 1);
    InsertionLocation loc = locList.first();
    QCOMPARE(loc.fileName(), dst->fileName());
    QCOMPARE(loc.line(), 7U);
    QCOMPARE(loc.column(), 2U);
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
}