2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-11-07 15:14:43 +01:00
|
|
|
|
|
|
|
|
#include <qmljs/qmljsdocument.h>
|
2014-01-22 18:38:45 +01:00
|
|
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
2011-11-07 15:14:43 +01:00
|
|
|
#include <qmljs/qmljsreformatter.h>
|
|
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2015-03-04 16:46:23 +01:00
|
|
|
#include <qmljs/parser/qmljsengine_p.h>
|
2023-09-27 15:41:10 +02:00
|
|
|
|
2022-06-20 12:35:13 +02:00
|
|
|
#include <utils/filepath.h>
|
2011-11-07 15:14:43 +01:00
|
|
|
|
2023-09-27 15:41:10 +02:00
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QGraphicsObject>
|
|
|
|
|
#include <QScopedPointer>
|
2011-11-07 15:14:43 +01:00
|
|
|
#include <QtTest>
|
2023-09-27 15:41:10 +02:00
|
|
|
|
2011-11-07 15:14:43 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
|
|
|
|
|
class tst_Reformatter : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
tst_Reformatter();
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void test();
|
|
|
|
|
void test_data();
|
2023-05-06 17:06:17 +02:00
|
|
|
|
|
|
|
|
void reformatter_data();
|
|
|
|
|
void reformatter();
|
|
|
|
|
|
|
|
|
|
private:
|
2011-11-07 15:14:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tst_Reformatter::tst_Reformatter()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define QCOMPARE_NOEXIT(actual, expected) \
|
|
|
|
|
QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
|
|
void tst_Reformatter::test_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("path");
|
|
|
|
|
|
2023-05-06 17:06:17 +02:00
|
|
|
// This test performs line-by-line comparison and fails if reformatting
|
|
|
|
|
// makes a change inline, for example whitespace removal. We omit
|
|
|
|
|
// those files in this test.
|
|
|
|
|
QSet<QString> excludedFiles;
|
2023-11-09 13:44:32 +01:00
|
|
|
excludedFiles << QString::fromLatin1(TESTSRCDIR) + "/typeAnnotations.qml";
|
|
|
|
|
excludedFiles << QString::fromLatin1(TESTSRCDIR) + "/typeAnnotations.formatted.qml";
|
2023-05-06 17:06:17 +02:00
|
|
|
|
2011-11-07 15:14:43 +01:00
|
|
|
QDirIterator it(TESTSRCDIR, QStringList() << QLatin1String("*.qml") << QLatin1String("*.js"), QDir::Files);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
const QString fileName = it.next();
|
2023-05-06 17:06:17 +02:00
|
|
|
if (!excludedFiles.contains(fileName))
|
|
|
|
|
QTest::newRow(fileName.toLatin1()) << it.filePath();
|
2011-11-07 15:14:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Reformatter::test()
|
|
|
|
|
{
|
|
|
|
|
QFETCH(QString, path);
|
2022-06-20 12:35:13 +02:00
|
|
|
Utils::FilePath fPath = Utils::FilePath::fromString(path);
|
2011-11-07 15:14:43 +01:00
|
|
|
|
2022-06-20 12:35:13 +02:00
|
|
|
Document::MutablePtr doc = Document::create(fPath, ModelManagerInterface::guessLanguageOfFile(fPath));
|
|
|
|
|
QFile file(doc->fileName().toString());
|
2011-11-07 15:14:43 +01:00
|
|
|
file.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
|
QString source = QString::fromUtf8(file.readAll());
|
|
|
|
|
doc->setSource(source);
|
|
|
|
|
file.close();
|
|
|
|
|
doc->parse();
|
|
|
|
|
|
|
|
|
|
QVERIFY(!doc->source().isEmpty());
|
|
|
|
|
QVERIFY(doc->diagnosticMessages().isEmpty());
|
|
|
|
|
|
|
|
|
|
QString rewritten = reformat(doc);
|
|
|
|
|
|
2023-04-14 11:38:25 +02:00
|
|
|
QStringList sourceLines = source.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
|
|
|
|
QStringList newLines = rewritten.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
2011-11-07 15:14:43 +01:00
|
|
|
|
|
|
|
|
// compare line by line
|
|
|
|
|
int commonLines = qMin(newLines.size(), sourceLines.size());
|
|
|
|
|
for (int i = 0; i < commonLines; ++i) {
|
|
|
|
|
// names intentional to make 'Actual (sourceLine): ...\nExpected (newLinee): ...' line up
|
|
|
|
|
const QString &sourceLine = sourceLines.at(i);
|
|
|
|
|
const QString &newLinee = newLines.at(i);
|
2018-09-11 15:25:03 +02:00
|
|
|
bool fail = !QCOMPARE_NOEXIT(newLinee, sourceLine);
|
2011-11-07 15:14:43 +01:00
|
|
|
if (fail) {
|
|
|
|
|
qDebug() << "in line" << (i + 1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QCOMPARE(sourceLines.size(), newLines.size());
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 17:06:17 +02:00
|
|
|
void tst_Reformatter::reformatter_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("filePath");
|
|
|
|
|
QTest::addColumn<QString>("formattedFilePath");
|
|
|
|
|
|
|
|
|
|
QTest::newRow("typeAnnotations")
|
2023-11-09 13:44:32 +01:00
|
|
|
<< QString::fromLatin1(TESTSRCDIR) + "/typeAnnotations.qml"
|
|
|
|
|
<< QString::fromLatin1(TESTSRCDIR) +"/typeAnnotations.formatted.qml";
|
2023-05-06 17:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Reformatter::reformatter()
|
|
|
|
|
{
|
|
|
|
|
QFETCH(QString, filePath);
|
|
|
|
|
QFETCH(QString, formattedFilePath);
|
|
|
|
|
|
|
|
|
|
Utils::FilePath fPath = Utils::FilePath::fromString(filePath);
|
|
|
|
|
Document::MutablePtr doc
|
|
|
|
|
= Document::create(fPath, ModelManagerInterface::guessLanguageOfFile(fPath));
|
|
|
|
|
|
|
|
|
|
QString fileContent;
|
|
|
|
|
{
|
|
|
|
|
QFile file(filePath);
|
|
|
|
|
QVERIFY(file.open(QFile::ReadOnly | QFile::Text));
|
|
|
|
|
fileContent = QString::fromUtf8(file.readAll());
|
|
|
|
|
}
|
|
|
|
|
doc->setSource(fileContent);
|
|
|
|
|
doc->parse();
|
|
|
|
|
QString expected;
|
|
|
|
|
{
|
|
|
|
|
QFile file(formattedFilePath);
|
|
|
|
|
QVERIFY(file.open(QFile::ReadOnly | QFile::Text));
|
|
|
|
|
expected = QString::fromUtf8(file.readAll());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString formatted = reformat(doc);
|
|
|
|
|
QCOMPARE(formatted, expected);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 12:21:30 +02:00
|
|
|
QTEST_GUILESS_MAIN(tst_Reformatter);
|
2011-11-07 15:14:43 +01:00
|
|
|
|
|
|
|
|
#include "tst_reformatter.moc"
|