File: test_qmljscontexts.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (137 lines) | stat: -rw-r--r-- 4,697 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
    SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de>

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

#include "test_qmljscontexts.h"

#include "../helper.h"
#include "../parsesession.h"
#include "../declarationbuilder.h"

#include <tests/testcore.h>
#include <tests/autotestshell.h>
#include <tests/testhelpers.h>

#include <QTest>
#include <QRandomGenerator>

QTEST_GUILESS_MAIN(TestContexts)

using namespace KDevelop;

void TestContexts::initTestCase()
{
    AutoTestShell::init();
    TestCore::initialize(Core::NoUi);

    QmlJS::registerDUChainItems();
}

void TestContexts::cleanupTestCase()
{
    QmlJS::unregisterDUChainItems();

    TestCore::shutdown();
}

void TestContexts::testFunctionContext()
{
    QFETCH(QString, code);
    QFETCH(RangeInRevision, argCtxRange);
    QFETCH(RangeInRevision, bodyCtxRange);

    const auto random = QRandomGenerator::global()->generate();
    const IndexedString file(QUrl(QStringLiteral("file:///internal/%1-functionContext.js").arg(random)));
    ParseSession session(file, code, 0);
    QVERIFY(session.ast());
    QCOMPARE(session.language().dialect(), QmlJS::Dialect::JavaScript);

    DeclarationBuilder builder(&session);
    ReferencedTopDUContext top = builder.build(file, session.ast());
    QVERIFY(top);

    DUChainReadLocker lock;

    QCOMPARE(top->type(), DUContext::Global);

    // the function arguments (containing the prototype context and the function body)
    QCOMPARE(top->childContexts().count(), 3);        // module, exports, the function

    DUContext* argCtx = top->childContexts().at(2);
    QCOMPARE(argCtx->type(), DUContext::Function);
    QCOMPARE(argCtx->range(), argCtxRange);
    QCOMPARE(argCtx->childContexts().size(), 2);    // The prototype context then the body context

    DUContext* bodyCtx = argCtx->childContexts().at(1);
    QVERIFY(bodyCtx);
    QCOMPARE(bodyCtx->type(), DUContext::Other);
    QCOMPARE(bodyCtx->range(), bodyCtxRange);
}

void TestContexts::testFunctionContext_data()
{
    QTest::addColumn<QString>("code");
    QTest::addColumn<RangeInRevision>("argCtxRange");
    QTest::addColumn<RangeInRevision>("bodyCtxRange");
    //                         0         1
    //                         012345678901234567890
    QTest::newRow("empty") << "function foo() {;}"
                           << RangeInRevision(0, 12, 0, 18)
                           << RangeInRevision(0, 15, 0, 18);

    //                        0         1         2         3
    //                        01234567890123456789012345678901234567890
    QTest::newRow("args") << "function foo(arg1, arg2, arg3) {;}"
                           << RangeInRevision(0, 12, 0, 34)
                           << RangeInRevision(0, 31, 0, 34);

    //                           0         1         2
    //                           0123456789012345678901234567890
    QTest::newRow("newline") << "function foo() {;\n}"
                           << RangeInRevision(0, 12, 1, 1)
                           << RangeInRevision(0, 15, 1, 1);
}

void TestContexts::testQMLContext()
{
    const IndexedString file(QUrl(QStringLiteral("file:///internal/testQMLContext.qml")));
    ParseSession session(file, "Text {\n"
                               "  id: main\n"
                               "  Text {\n"
                               "    id: child1\n"
                               "  }\n"
                               "  Text {\n"
                               "    id: child2\n"
                               "  }\n"
                               "}\n", 0);
    QVERIFY(session.ast());
    QCOMPARE(session.language().dialect(), QmlJS::Dialect::Qml);

    DeclarationBuilder builder(&session);
    ReferencedTopDUContext top = builder.build(file, session.ast());
    QVERIFY(top);

    DUChainReadLocker lock;

    QCOMPARE(top->type(), DUContext::Global);

    QCOMPARE(top->childContexts().count(), 3);        // module, exports, Text
    DUContext* mainCtx = top->childContexts().at(2);
    QCOMPARE(mainCtx->type(), DUContext::Class);
    QCOMPARE(mainCtx->range(), RangeInRevision(0, 6, 8, 0));
    QCOMPARE(mainCtx->childContexts().size(), 2);

    DUContext* child1Ctx = mainCtx->childContexts().first();
    QCOMPARE(child1Ctx->type(), DUContext::Class);
    QCOMPARE(child1Ctx->range(), RangeInRevision(2, 8, 4, 2));
    QCOMPARE(child1Ctx->childContexts().size(), 0);

    DUContext* child2Ctx = mainCtx->childContexts().last();
    QCOMPARE(child2Ctx->type(), DUContext::Class);
    QCOMPARE(child2Ctx->range(), RangeInRevision(5, 8, 7, 2));
    QCOMPARE(child2Ctx->childContexts().size(), 0);
}

#include "moc_test_qmljscontexts.cpp"