2011-09-09 15:49:05 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2011-09-09 15:49:05 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2011-09-09 15:49:05 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <QScopedPointer>
|
|
|
|
|
#include <QLatin1String>
|
|
|
|
|
#include <QGraphicsObject>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
|
|
#include <qmljs/qmljsinterpreter.h>
|
|
|
|
|
#include <qmljs/qmljsdocument.h>
|
|
|
|
|
#include <qmljs/qmljsbind.h>
|
|
|
|
|
#include <qmljs/qmljslink.h>
|
|
|
|
|
#include <qmljs/qmljscheck.h>
|
|
|
|
|
#include <qmljs/qmljscontext.h>
|
|
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
|
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
2011-09-28 15:16:00 +02:00
|
|
|
using namespace QmlJS::StaticAnalysis;
|
2011-09-09 15:49:05 +02:00
|
|
|
|
|
|
|
|
class tst_Check : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
tst_Check();
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void test();
|
|
|
|
|
void test_data();
|
|
|
|
|
|
|
|
|
|
void initTestCase();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tst_Check::tst_Check()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
|
# define SHARE_PATH "/Resources"
|
|
|
|
|
#else
|
|
|
|
|
# define SHARE_PATH "/share/qtcreator"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
QString resourcePath()
|
|
|
|
|
{
|
|
|
|
|
return QDir::cleanPath(QTCREATORDIR + QLatin1String(SHARE_PATH));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Check::initTestCase()
|
|
|
|
|
{
|
|
|
|
|
// the resource path is wrong, have to load things manually
|
|
|
|
|
QFileInfo builtins(resourcePath() + "/qml-type-descriptions/builtins.qmltypes");
|
|
|
|
|
QStringList errors, warnings;
|
|
|
|
|
CppQmlTypesLoader::defaultQtObjects = CppQmlTypesLoader::loadQmlTypes(QFileInfoList() << builtins, &errors, &warnings);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
static bool offsetComparator(const Message &lhs, const Message &rhs)
|
2011-09-09 15:49:05 +02:00
|
|
|
{
|
2011-09-28 15:16:00 +02:00
|
|
|
return lhs.location.offset < rhs.location.offset;
|
2011-09-09 15:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define QCOMPARE_NOEXIT(actual, expected) \
|
|
|
|
|
QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
|
|
void tst_Check::test_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("path");
|
|
|
|
|
|
|
|
|
|
QDirIterator it(TESTSRCDIR, QStringList("*.qml"), QDir::Files);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
const QString fileName = it.next();
|
|
|
|
|
QTest::newRow(fileName.toLatin1()) << it.filePath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Check::test()
|
|
|
|
|
{
|
|
|
|
|
QFETCH(QString, path);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot;
|
2011-11-09 13:53:28 +01:00
|
|
|
Document::MutablePtr doc = Document::create(path, Document::QmlLanguage);
|
2011-09-09 15:49:05 +02:00
|
|
|
QFile file(doc->fileName());
|
|
|
|
|
file.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
|
doc->setSource(file.readAll());
|
|
|
|
|
file.close();
|
|
|
|
|
doc->parse();
|
|
|
|
|
snapshot.insert(doc);
|
|
|
|
|
|
|
|
|
|
QVERIFY(!doc->source().isEmpty());
|
|
|
|
|
QVERIFY(doc->diagnosticMessages().isEmpty());
|
|
|
|
|
|
|
|
|
|
ContextPtr context = Link(snapshot, QStringList(), LibraryInfo())();
|
|
|
|
|
|
|
|
|
|
Check checker(doc, context);
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> messages = checker();
|
2011-09-09 15:49:05 +02:00
|
|
|
std::sort(messages.begin(), messages.end(), &offsetComparator);
|
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
const QRegExp messagePattern(" (\\d+) (\\d+) (\\d+)");
|
2011-09-09 15:49:05 +02:00
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
QList<Message> expectedMessages;
|
2011-09-09 15:49:05 +02:00
|
|
|
foreach (const AST::SourceLocation &comment, doc->engine()->comments()) {
|
|
|
|
|
const QString text = doc->source().mid(comment.begin(), comment.end() - comment.begin());
|
|
|
|
|
|
2011-09-28 15:16:00 +02:00
|
|
|
if (messagePattern.indexIn(text) == -1)
|
2011-09-09 15:49:05 +02:00
|
|
|
continue;
|
2011-09-28 15:16:00 +02:00
|
|
|
const int type = messagePattern.cap(1).toInt();
|
|
|
|
|
const int columnStart = messagePattern.cap(2).toInt();
|
|
|
|
|
const int columnEnd = messagePattern.cap(3).toInt() + 1;
|
|
|
|
|
|
|
|
|
|
Message message;
|
|
|
|
|
message.location = SourceLocation(
|
|
|
|
|
comment.offset - comment.startColumn + columnStart,
|
|
|
|
|
columnEnd - columnStart,
|
|
|
|
|
comment.startLine,
|
|
|
|
|
columnStart),
|
|
|
|
|
message.type = static_cast<Type>(type);
|
|
|
|
|
expectedMessages += message;
|
2011-09-09 15:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < messages.size(); ++i) {
|
2011-09-28 15:16:00 +02:00
|
|
|
Message expected;
|
2011-09-12 10:44:11 +02:00
|
|
|
if (i < expectedMessages.size())
|
|
|
|
|
expected = expectedMessages.at(i);
|
2011-09-28 15:16:00 +02:00
|
|
|
Message actual = messages.at(i);
|
2011-09-09 15:49:05 +02:00
|
|
|
bool fail = false;
|
2011-09-28 15:16:00 +02:00
|
|
|
fail |= !QCOMPARE_NOEXIT(actual.location.startLine, expected.location.startLine);
|
2011-09-09 15:49:05 +02:00
|
|
|
if (fail)
|
|
|
|
|
return;
|
2011-09-30 10:39:00 +02:00
|
|
|
fail |= !QCOMPARE_NOEXIT((int)actual.type, (int)expected.type);
|
2011-09-28 15:16:00 +02:00
|
|
|
fail |= !QCOMPARE_NOEXIT(actual.location.startColumn, expected.location.startColumn);
|
|
|
|
|
fail |= !QCOMPARE_NOEXIT(actual.location.offset, expected.location.offset);
|
|
|
|
|
fail |= !QCOMPARE_NOEXIT(actual.location.length, expected.location.length);
|
2011-09-09 15:49:05 +02:00
|
|
|
if (fail) {
|
2011-09-28 15:16:00 +02:00
|
|
|
qDebug() << "Failed for message on line" << actual.location.startLine << actual.message;
|
2011-09-09 15:49:05 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-12 10:44:11 +02:00
|
|
|
if (expectedMessages.size() > messages.size()) {
|
2011-09-28 15:16:00 +02:00
|
|
|
Message missingMessage = expectedMessages.at(messages.size());
|
|
|
|
|
qDebug() << "expected more messages: " << missingMessage.location.startLine << missingMessage.message;
|
2011-09-12 10:44:11 +02:00
|
|
|
QFAIL("more messages expected");
|
|
|
|
|
}
|
2011-09-09 15:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_Check);
|
|
|
|
|
|
|
|
|
|
#include "tst_check.moc"
|