cppcodegen_test.cpp 24 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Bill King's avatar
Bill King committed
2
**
3
** Copyright (C) 2014 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
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
Bill King's avatar
Bill King committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** 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
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Bill King's avatar
Bill King committed
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

291 292 293 294 295 296 297
/*!
    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.
 */
298
void CppToolsPlugin::test_codegen_qtdesigner_integration()
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
{
    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";

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

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

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

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

352
void CppToolsPlugin::test_codegen_definition_empty_class()
353 354 355 356 357 358 359 360 361 362 363 364
{
    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";

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

373
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
374
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
375
    dst->setUtf8Source(dstText);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    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();
400
    QCOMPARE(loc.fileName(), dst->fileName());
401 402
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
Leandro Melo's avatar
Leandro Melo committed
403 404
    QCOMPARE(loc.line(), 3U);
    QCOMPARE(loc.column(), 1U);
405 406
}

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

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

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

437
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
438 439
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
440
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
441
    dst->setUtf8Source(dstText);
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
    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();
466
    QCOMPARE(loc.fileName(), dst->fileName());
467 468 469 470 471 472
    QCOMPARE(loc.line(), 4U);
    QCOMPARE(loc.column(), 1U);
    QCOMPARE(loc.suffix(), QLatin1String("\n\n"));
    QCOMPARE(loc.prefix(), QString());
}

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

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

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

503
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
504 505
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
506
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
507
    dst->setUtf8Source(dstText);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    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();
532
    QCOMPARE(loc.fileName(), dst->fileName());
533 534 535 536 537 538
    QCOMPARE(loc.line(), 7U);
    QCOMPARE(loc.column(), 2U);
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
}

539
void CppToolsPlugin::test_codegen_definition_middle_member()
540 541 542 543 544 545 546 547 548 549
{
    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";

550
    const QByteArray dstText = QString::fromLatin1(
551 552 553 554 555 556 557 558 559 560 561 562 563 564
                "\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"
565
                "int y;\n").arg(QDir::tempPath()).toLatin1();
566

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

575
    Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
576 577
    dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
                                          Client::IncludeLocal));
578
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
579
    dst->setUtf8Source(dstText);
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    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();
604
    QCOMPARE(loc.fileName(), dst->fileName());
605 606 607 608 609
    QCOMPARE(loc.line(), 7U);
    QCOMPARE(loc.column(), 2U);
    QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
    QCOMPARE(loc.suffix(), QString());
}
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

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"));
636
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
637 638 639 640 641 642 643 644 645
    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));
646
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
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
    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"));
707
    QVERIFY(CppTools::Tests::TestCase::writeFile(src->fileName(), srcText));
708 709 710 711 712 713 714 715 716
    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));
717
    QVERIFY(CppTools::Tests::TestCase::writeFile(dst->fileName(), dstText));
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
    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());
}