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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*
* This file is part of KDevelop
* Copyright 2013 Milian Wolff <mail@milianw.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "test_qmljsdeclarations.h"
#include "../helper.h"
#include "../parsesession.h"
#include "../declarationbuilder.h"
#include "../cache.h"
#include <tests/testcore.h>
#include <tests/autotestshell.h>
#include <tests/testhelpers.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/types/integraltype.h>
#include <language/duchain/problem.h>
#include <language/duchain/classdeclaration.h>
#include <language/editor/documentrange.h>
#include <QTest>
QTEST_GUILESS_MAIN(TestDeclarations)
using namespace KDevelop;
void TestDeclarations::initTestCase()
{
AutoTestShell::init({"kdevqmljs"});
TestCore::initialize(Core::NoUi);
QmlJS::registerDUChainItems();
}
void TestDeclarations::cleanupTestCase()
{
TestCore::shutdown();
}
void TestDeclarations::testJSProblems()
{
const IndexedString file(QUrl(QStringLiteral("file:///internal/jsproblems.js")));
ParseSession session(file,
"function f(a) {}\n"
"f(2);\n"
"f(true);\n",
0);
QVERIFY(session.ast());
DeclarationBuilder builder(&session);
builder.build(file, session.ast());
auto problems = session.problems();
QCOMPARE(problems.count(), 1);
QCOMPARE((KTextEditor::Range)problems.at(0)->finalLocation(), KTextEditor::Range(2, 2, 2, 6));
}
void TestDeclarations::testFunction()
{
const IndexedString file(QUrl(QStringLiteral("file:///internal/functionArgs.js")));
// 0 1 2 3
// 01234567890123456789012345678901234567890
ParseSession session(file, "/**\n * some comment\n */\n"
"function foo(arg1, arg2, arg3) { var i = 0; }", 0);
QVERIFY(session.ast());
QVERIFY(session.problems().isEmpty());
QCOMPARE(session.language().dialect(), QmlJS::Dialect::JavaScript);
DeclarationBuilder builder(&session);
ReferencedTopDUContext top = builder.build(file, session.ast());
QVERIFY(top);
DUChainReadLocker lock;
QCOMPARE(top->localDeclarations().size(), 3); // module, exports and foo
FunctionDeclaration* fooDec = dynamic_cast<FunctionDeclaration*>(top->localDeclarations().at(2));
QVERIFY(fooDec);
QCOMPARE(fooDec->range(), RangeInRevision(3, 9, 3, 12));
QCOMPARE(QString::fromUtf8(fooDec->comment()), QString("some comment"));
QVERIFY(fooDec->internalContext());
DUContext* argCtx = fooDec->internalContext();
QCOMPARE(argCtx->localDeclarations().size(), 3);
Declaration* arg1 = argCtx->localDeclarations().at(0);
QCOMPARE(arg1->identifier().toString(), QString("arg1"));
QCOMPARE(arg1->range(), RangeInRevision(3, 13, 3, 17));
Declaration* arg2 = argCtx->localDeclarations().at(1);
QCOMPARE(arg2->identifier().toString(), QString("arg2"));
QCOMPARE(arg2->range(), RangeInRevision(3, 19, 3, 23));
Declaration* arg3 = argCtx->localDeclarations().at(2);
QCOMPARE(arg3->identifier().toString(), QString("arg3"));
QCOMPARE(arg3->range(), RangeInRevision(3, 25, 3, 29));
FunctionType::Ptr funType = fooDec->type<FunctionType>();
QVERIFY(funType);
QVERIFY(funType->returnType().cast<IntegralType>());
QCOMPARE(funType->returnType().cast<IntegralType>()->dataType(), (uint) IntegralType::TypeVoid);
QCOMPARE(argCtx->childContexts().size(), 2);
DUContext* bodyCtx = argCtx->childContexts().at(1);
QVERIFY(bodyCtx);
QVERIFY(bodyCtx->findDeclarations(arg1->identifier()).contains(arg1));
QVERIFY(bodyCtx->findDeclarations(arg2->identifier()).contains(arg2));
QVERIFY(bodyCtx->findDeclarations(arg3->identifier()).contains(arg3));
QCOMPARE(bodyCtx->localDeclarations().count(), 1);
}
void TestDeclarations::testQMLId()
{
const IndexedString file(QUrl(QStringLiteral("file:///internal/qmlId.qml")));
ReferencedTopDUContext top;
DeclarationPointer oldDec;
{
// 0 1 2 3
// 01234567890123456789012345678901234567890
ParseSession session(file, "/** file comment **/\n"
"import QtQuick 1.0\n"
"/**\n * some comment\n */\n"
"Text { id: test; Text { id: child; } }", 0);
QVERIFY(session.ast());
QVERIFY(session.problems().isEmpty());
QCOMPARE(session.language().dialect(), QmlJS::Dialect::Qml);
DeclarationBuilder builder(&session);
top = builder.build(file, session.ast());
QVERIFY(top);
DUChainReadLocker lock;
QCOMPARE(top->localDeclarations().size(), 5); // module, exports, Text, test and child are all in the global scope
// First declaration, the anonymous class
ClassDeclaration* classDecl = dynamic_cast<ClassDeclaration *>(top->localDeclarations().at(2));
QVERIFY(classDecl);
QCOMPARE(classDecl->abstractType()->toString(), QString("qmlId"));
QCOMPARE(classDecl->range(), RangeInRevision(5, 0, 5, 0));
QVERIFY(classDecl->internalContext());
QCOMPARE(classDecl->internalContext()->range(), RangeInRevision(5, 6, 5, 37));
// Second declaration: test
Declaration* dec = top->localDeclarations().at(3);
QVERIFY(dec);
QCOMPARE(dec->identifier().toString(), QString("test"));
QCOMPARE(dec->abstractType()->toString(), QString("qmlId"));
oldDec = dec;
}
// test recompile
{
// 0 1 2 3
// 01234567890123456789012345678901234567890
ParseSession session(file, "/** file comment **/\n"
"import QtQuick 1.0\n"
"/**\n * some comment\n */\n"
"Text { id: test; Text { id: child;}\n"
" Text {id: foo;} }", 0);
QVERIFY(session.ast());
QVERIFY(session.problems().isEmpty());
QCOMPARE(session.language().dialect(), QmlJS::Dialect::Qml);
DeclarationBuilder builder(&session);
ReferencedTopDUContext top2 = builder.build(file, session.ast(), top);
QVERIFY(top2);
QCOMPARE(top2.data(), top.data());
DUChainReadLocker lock;
QCOMPARE(top->localDeclarations().size(), 6); // module, exports, Text, test, child and foo
// First declaration, the anonymous class
ClassDeclaration* classDecl = dynamic_cast<ClassDeclaration *>(top->localDeclarations().at(2));
QVERIFY(classDecl);
QCOMPARE(classDecl->abstractType()->toString(), QString("qmlId"));
QCOMPARE(classDecl->range(), RangeInRevision(5, 0, 5, 0));
QVERIFY(classDecl->internalContext());
QCOMPARE(classDecl->internalContext()->range(), RangeInRevision(5, 6, 6, 17));
// Second declaration: test
Declaration* dec = top->localDeclarations().at(3);
QVERIFY(dec);
QCOMPARE(dec->identifier().toString(), QString("test"));
QCOMPARE(dec->abstractType()->toString(), QString("qmlId"));
}
}
void TestDeclarations::testProperty()
{
const IndexedString file(QUrl(QStringLiteral("file:///internal/qmlProperty.qml")));
// 0 1 2 3
// 01234567890123456789012345678901234567890
ParseSession session(file, "Text {\n"
" /// some comment\n"
" property int foo;\n"
"}", 0);
QVERIFY(session.ast());
QVERIFY(session.problems().isEmpty());
QCOMPARE(session.language().dialect(), QmlJS::Dialect::Qml);
DeclarationBuilder builder(&session);
ReferencedTopDUContext top = builder.build(file, session.ast());
QVERIFY(top);
DUChainReadLocker lock;
QCOMPARE(top->localDeclarations().size(), 3); // module, exports, Text
ClassDeclaration* text = dynamic_cast<ClassDeclaration*>(top->localDeclarations().at(2));
QVERIFY(text);
QVERIFY(text->internalContext());
QCOMPARE(text->internalContext()->type(), DUContext::Class);
QCOMPARE(text->internalContext()->localDeclarations().size(), 1);
ClassMemberDeclaration* foo = dynamic_cast<ClassMemberDeclaration*>(text->internalContext()->localDeclarations().first());
QVERIFY(foo);
QCOMPARE(foo->identifier().toString(), QString("foo"));
QVERIFY(foo->abstractType());
QCOMPARE(foo->abstractType()->toString(), QString("int"));
QCOMPARE(QString::fromUtf8(foo->comment()), QString("some comment"));
}
/**
* Test that all qmltypes files for built-in QtQuick modules are found on the system.
* These files are also available on CI machines, since an installed Qt5 is assumed
*/
void TestDeclarations::testQMLtypesImportPaths()
{
KDevelop::IndexedString stubPath;
QString path;
// QtQuick QML modules
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick"), QStringLiteral("2.0"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtTest"), QStringLiteral("1.1"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.Layouts"), QStringLiteral("1.1"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.Controls"), QStringLiteral("1.1"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.Dialogs"), QStringLiteral("1.1"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.LocalStorage"), QStringLiteral("2.0"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.Particles"), QStringLiteral("2.0"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.Window"), QStringLiteral("2.2"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQuick.XmlListModel"), QStringLiteral("2.0"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
// QtQml QML modules
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtQml.Models"), QStringLiteral("2.3"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
// QtMultimedia QML modules
path = QmlJS::Cache::instance().modulePath(stubPath, QStringLiteral("QtMultimedia"), QStringLiteral("5.6"));
QVERIFY(QFileInfo::exists(path + "/plugins.qmltypes"));
}
|