File: test_refactoring.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (123 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2017 Kevin Funk <kfunk@kde.org>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "test_refactoring.h"

#include <tests/testcore.h>
#include <tests/autotestshell.h>
#include <tests/testfile.h>
#include <tests/testproject.h>
#include <interfaces/ilanguagecontroller.h>

#include "codegen/clangrefactoring.h"

#include <language/duchain/duchainlock.h>
#include <language/duchain/declaration.h>
#include <language/interfaces/ilanguagesupport.h>

#include <QTemporaryDir>
#include <QLoggingCategory>
#include <QTest>

QTEST_MAIN(TestRefactoring)

using namespace KDevelop;

TestRefactoring::~TestRefactoring() = default;

void TestRefactoring::initTestCase()
{
    QLoggingCategory::setFilterRules(QStringLiteral("*.debug=false\ndefault.debug=true\nkdevelop.plugins.clang.debug=true\n"));

    QVERIFY(qputenv("KDEV_CLANG_DISPLAY_DIAGS", "1"));

    AutoTestShell::init({QStringLiteral("kdevclangsupport")});

    auto core = TestCore::initialize();
    delete core->projectController();
    m_projectController = new TestProjectController(core);
    core->setProjectController(m_projectController);
}

void TestRefactoring::cleanupTestCase()
{
    TestCore::shutdown();
}

void TestRefactoring::testClassRename()
{
    const QString codeBefore(QStringLiteral(R"(
class Foo {
public:
    Foo();
    ~Foo();
};
Foo::Foo() {
}
Foo::~Foo() {
}
    )"));

    const QString codeAfter(QStringLiteral(R"(
class FooNew {
public:
    FooNew();
    ~FooNew();
};
FooNew::FooNew() {
}
FooNew::~FooNew() {
}
    )"));

    QTemporaryDir dir;
    auto project = new TestProject(Path(dir.path()), this);
    m_projectController->addProject(project);

    TestFile file(codeBefore, QStringLiteral("cpp"), project, dir.path());
    QVERIFY(file.parseAndWait(TopDUContext::AllDeclarationsContextsAndUses));

    DUChainReadLocker lock;

    auto top = file.topContext();
    QVERIFY(top);

    auto declaration = top->localDeclarations().first();
    QVERIFY(declaration);

    const QString originalName = declaration->identifier().identifier().str();
    const QString newName = QStringLiteral("FooNew");

    QSharedPointer<BasicRefactoringCollector> collector(new BasicRefactoringCollector(declaration));

    // TODO: Do this without GUI?
    UsesWidget uses(declaration, collector);
    lock.unlock();

    for (int i = 0; i < 30000; i += 1000) {
        if (collector->isReady()) {
            break;
        }
        QTest::qWait(1000);
    }
    QVERIFY(collector->isReady());

    BasicRefactoring::NameAndCollector nameAndCollector{newName, collector};

    auto languages = ICore::self()->languageController()->languagesForUrl(file.url().toUrl());
    QVERIFY(!languages.isEmpty());
    auto clangLanguageSupport = languages.first();
    QVERIFY(clangLanguageSupport);
    auto clangRefactoring = qobject_cast<ClangRefactoring*>(clangLanguageSupport->refactoring());
    QVERIFY(clangRefactoring);

    clangRefactoring->renameCollectedDeclarations(nameAndCollector.collector.data(), newName, originalName);
    QCOMPARE(file.fileContents(), codeAfter);

    m_projectController->closeAllProjects();
}

#include "moc_test_refactoring.cpp"