Changeset View
Changeset View
Standalone View
Standalone View
languages/clang/tests/test_assistants.cpp
Show All 13 Lines | 1 | /* This file is part of KDevelop | |||
---|---|---|---|---|---|
14 | You should have received a copy of the GNU Library General Public License | 14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to | ||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | Boston, MA 02110-1301, USA. | 17 | Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "test_assistants.h" | 20 | #include "test_assistants.h" | ||
21 | 21 | | |||
22 | #include "codegen/simplerefactoring.h" | ||||
23 | | ||||
22 | #include <QtTest/QtTest> | 24 | #include <QtTest/QtTest> | ||
23 | #include <QTemporaryDir> | 25 | #include <QTemporaryDir> | ||
24 | 26 | | |||
25 | #include <ktexteditor/view.h> | 27 | #include <ktexteditor/view.h> | ||
26 | #include <ktexteditor/document.h> | 28 | #include <ktexteditor/document.h> | ||
27 | 29 | | |||
28 | #include <tests/autotestshell.h> | 30 | #include <tests/autotestshell.h> | ||
29 | #include <tests/testcore.h> | 31 | #include <tests/testcore.h> | ||
32 | #include <tests/testfile.h> | ||||
30 | 33 | | |||
31 | #include <util/foregroundlock.h> | 34 | #include <util/foregroundlock.h> | ||
32 | 35 | | |||
33 | #include <interfaces/idocumentcontroller.h> | 36 | #include <interfaces/idocumentcontroller.h> | ||
34 | #include <interfaces/ilanguagecontroller.h> | 37 | #include <interfaces/ilanguagecontroller.h> | ||
35 | #include <interfaces/iplugincontroller.h> | 38 | #include <interfaces/iplugincontroller.h> | ||
36 | #include <interfaces/isourceformattercontroller.h> | 39 | #include <interfaces/isourceformattercontroller.h> | ||
37 | 40 | | |||
38 | #include <language/assistant/staticassistant.h> | 41 | #include <language/assistant/staticassistant.h> | ||
39 | #include <language/assistant/staticassistantsmanager.h> | 42 | #include <language/assistant/staticassistantsmanager.h> | ||
40 | #include <language/assistant/renameaction.h> | 43 | #include <language/assistant/renameaction.h> | ||
41 | #include <language/backgroundparser/backgroundparser.h> | 44 | #include <language/backgroundparser/backgroundparser.h> | ||
42 | #include <language/duchain/duchain.h> | 45 | #include <language/duchain/duchain.h> | ||
46 | #include <language/duchain/duchainlock.h> | ||||
43 | #include <language/duchain/duchainutils.h> | 47 | #include <language/duchain/duchainutils.h> | ||
44 | #include <language/codegen/coderepresentation.h> | 48 | #include <language/codegen/coderepresentation.h> | ||
45 | 49 | | |||
46 | #include <shell/documentcontroller.h> | 50 | #include <shell/documentcontroller.h> | ||
47 | 51 | | |||
48 | using namespace KDevelop; | 52 | using namespace KDevelop; | ||
49 | using namespace KTextEditor; | 53 | using namespace KTextEditor; | ||
50 | 54 | | |||
▲ Show 20 Lines • Show All 476 Lines • ▼ Show 20 Line(s) | 530 | { | |||
527 | auto fileName = testbed.includeFileName(); | 531 | auto fileName = testbed.includeFileName(); | ||
528 | fileName = fileName.mid(fileName.lastIndexOf('/') + 1); | 532 | fileName = fileName.mid(fileName.lastIndexOf('/') + 1); | ||
529 | const auto description = QObject::tr("Insert \'%1\'") | 533 | const auto description = QObject::tr("Insert \'%1\'") | ||
530 | .arg(QStringLiteral("#include \"%1\"").arg(fileName)); | 534 | .arg(QStringLiteral("#include \"%1\"").arg(fileName)); | ||
531 | const bool hasMissingInclude = actionDescriptions.contains(description); | 535 | const bool hasMissingInclude = actionDescriptions.contains(description); | ||
532 | QCOMPARE(hasMissingInclude, static_cast<bool>(actions & MissingInclude)); | 536 | QCOMPARE(hasMissingInclude, static_cast<bool>(actions & MissingInclude)); | ||
533 | } | 537 | } | ||
534 | } | 538 | } | ||
539 | | ||||
540 | void TestAssistants::testMoveIntoSource() | ||||
541 | { | ||||
542 | QFETCH(QString, origHeader); | ||||
543 | QFETCH(QString, origImpl); | ||||
544 | QFETCH(QString, newHeader); | ||||
545 | QFETCH(QString, newImpl); | ||||
546 | QFETCH(QualifiedIdentifier, id); | ||||
547 | | ||||
548 | TestFile header(origHeader, "h"); | ||||
549 | TestFile impl("#include \"" + header.url().byteArray() + "\"\n" + origImpl, "cpp", &header); | ||||
550 | | ||||
551 | impl.parse(KDevelop::TopDUContext::AllDeclarationsContextsAndUses); | ||||
552 | QVERIFY(impl.waitForParsed()); | ||||
553 | | ||||
554 | IndexedDeclaration declaration; | ||||
555 | { | ||||
556 | DUChainReadLocker lock; | ||||
557 | auto headerCtx = DUChain::self()->chainForDocument(header.url()); | ||||
558 | QVERIFY(headerCtx); | ||||
559 | auto decls = headerCtx->findDeclarations(id); | ||||
560 | Q_ASSERT(!decls.isEmpty()); | ||||
561 | declaration = IndexedDeclaration(decls.first()); | ||||
562 | QVERIFY(declaration.isValid()); | ||||
563 | } | ||||
564 | CodeRepresentation::setDiskChangesForbidden(false); | ||||
565 | SimpleRefactoring refactoring; | ||||
566 | QCOMPARE(refactoring.moveIntoSource(declaration), QString()); | ||||
567 | CodeRepresentation::setDiskChangesForbidden(true); | ||||
568 | | ||||
569 | QCOMPARE(header.fileContents(), newHeader); | ||||
570 | QVERIFY(impl.fileContents().endsWith(newImpl)); | ||||
571 | } | ||||
572 | | ||||
573 | void TestAssistants::testMoveIntoSource_data() | ||||
574 | { | ||||
575 | QTest::addColumn<QString>("origHeader"); | ||||
576 | QTest::addColumn<QString>("origImpl"); | ||||
577 | QTest::addColumn<QString>("newHeader"); | ||||
578 | QTest::addColumn<QString>("newImpl"); | ||||
579 | QTest::addColumn<QualifiedIdentifier>("id"); | ||||
580 | | ||||
581 | const QualifiedIdentifier fooId("foo"); | ||||
582 | | ||||
583 | QTest::newRow("globalfunction") << QString("int foo()\n{\n int i = 0;\n return 0;\n}\n") | ||||
584 | << QString() | ||||
585 | << QString("int foo();\n") | ||||
586 | << QString("\nint foo()\n{\n int i = 0;\n return 0;\n}\n") | ||||
587 | << fooId; | ||||
588 | | ||||
589 | QTest::newRow("staticfunction") << QString("static int foo()\n{\n int i = 0;\n return 0;\n}\n") | ||||
590 | << QString() | ||||
591 | << QString("static int foo();\n") | ||||
592 | << QString("\nint foo()\n{\n int i = 0;\n return 0;\n}\n") | ||||
593 | << fooId; | ||||
594 | | ||||
595 | QTest::newRow("funcsameline") << QString("int foo() {\n int i = 0;\n return 0;\n}\n") | ||||
596 | << QString() | ||||
597 | << QString("int foo();\n") | ||||
598 | << QString("\nint foo() {\n int i = 0;\n return 0;\n}\n") | ||||
599 | << fooId; | ||||
600 | | ||||
601 | QTest::newRow("func-comment") << QString("int foo()\n/* foobar */ {\n int i = 0;\n return 0;\n}\n") | ||||
602 | << QString() | ||||
603 | << QString("int foo()\n/* foobar */;\n") | ||||
604 | << QString("\nint foo() {\n int i = 0;\n return 0;\n}\n") | ||||
605 | << fooId; | ||||
606 | | ||||
607 | QTest::newRow("func-comment2") << QString("int foo()\n/*asdf*/\n{\n int i = 0;\n return 0;\n}\n") | ||||
608 | << QString() | ||||
609 | << QString("int foo()\n/*asdf*/;\n") | ||||
610 | << QString("\nint foo()\n{\n int i = 0;\n return 0;\n}\n") | ||||
611 | << fooId; | ||||
612 | | ||||
613 | const QualifiedIdentifier aFooId("a::foo"); | ||||
614 | QTest::newRow("class-method") << QString("class a {\n int foo(){\n return 0;\n }\n};\n") | ||||
615 | << QString() | ||||
616 | << QString("class a {\n int foo();\n};\n") | ||||
617 | << QString("\nint a::foo() {\n return 0;\n }\n") | ||||
618 | << aFooId; | ||||
619 | | ||||
620 | QTest::newRow("class-method-const") << QString("class a {\n int foo() const\n {\n return 0;\n }\n};\n") | ||||
621 | << QString() | ||||
622 | << QString("class a {\n int foo() const;\n};\n") | ||||
623 | << QString("\nint a::foo() const\n {\n return 0;\n }\n") | ||||
624 | << aFooId; | ||||
625 | | ||||
626 | QTest::newRow("class-method-const-sameline") << QString("class a {\n int foo() const{\n return 0;\n }\n};\n") | ||||
627 | << QString() | ||||
628 | << QString("class a {\n int foo() const;\n};\n") | ||||
629 | << QString("\nint a::foo() const {\n return 0;\n }\n") | ||||
630 | << aFooId; | ||||
631 | } |