Fix tst_qml_reformatter auto test

Add source location to pragma library writer so that the comments before
.pragma line are also written.

Fix tst_qml_reformatter by skipping the blankline comparisons. Line by
line comparison with the non-formatted code is not a good idea to test
 reformatting. Ideally test should have compared the formatted file with
the original one character wise, yet it is not worth changing into that
at this point.

Amends 0ce57fcf5e90f8bf8cfbe681f2954a0c1ef0e945

Change-Id: I39bcee2c881e1a0928c17ebb45aa1c85e6cf3b99
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Semih Yavuz
2023-04-14 11:38:25 +02:00
committed by Eike Ziller
parent e2bccfde24
commit 15a06b77c0
2 changed files with 5 additions and 15 deletions

View File

@@ -101,16 +101,16 @@ public:
_hadEmptyLine = false; _hadEmptyLine = false;
_binaryExpDepth = 0; _binaryExpDepth = 0;
const QString &source = _doc->source();
// emit directives // emit directives
if (_doc->bind()->isJsLibrary()) { if (_doc->bind()->isJsLibrary()) {
out(QLatin1String(".pragma library")); const QLatin1String pragmaLine(".pragma library");
out(pragmaLine, SourceLocation(source.indexOf(".pragma"), pragmaLine.length()));
newLine(); newLine();
} }
const QList<SourceLocation> &directives = _doc->jsDirectives(); const QList<SourceLocation> &directives = _doc->jsDirectives();
for (const auto &d: directives) { for (const auto &d: directives) {
quint32 line = 0; quint32 line = 0;
const QString &source = _doc->source();
int i = -1; int i = -1;
while (++line < d.startLine) while (++line < d.startLine)
i = source.indexOf(QChar('\n'), i + 1); i = source.indexOf(QChar('\n'), i + 1);

View File

@@ -68,25 +68,15 @@ void tst_Reformatter::test()
QString rewritten = reformat(doc); QString rewritten = reformat(doc);
QStringList sourceLines = source.split(QLatin1Char('\n')); QStringList sourceLines = source.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
QStringList newLines = rewritten.split(QLatin1Char('\n')); QStringList newLines = rewritten.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
// compare line by line // compare line by line
int commonLines = qMin(newLines.size(), sourceLines.size()); int commonLines = qMin(newLines.size(), sourceLines.size());
bool insideMultiLineComment = false;
for (int i = 0; i < commonLines; ++i) { for (int i = 0; i < commonLines; ++i) {
// names intentional to make 'Actual (sourceLine): ...\nExpected (newLinee): ...' line up // names intentional to make 'Actual (sourceLine): ...\nExpected (newLinee): ...' line up
const QString &sourceLine = sourceLines.at(i); const QString &sourceLine = sourceLines.at(i);
const QString &newLinee = newLines.at(i); const QString &newLinee = newLines.at(i);
if (!insideMultiLineComment && sourceLine.trimmed().startsWith("/*")) {
insideMultiLineComment = true;
sourceLines.insert(i, "\n");
continue;
}
if (sourceLine.trimmed().endsWith("*/"))
insideMultiLineComment = false;
if (sourceLine.trimmed().isEmpty() && newLinee.trimmed().isEmpty())
continue;
bool fail = !QCOMPARE_NOEXIT(newLinee, sourceLine); bool fail = !QCOMPARE_NOEXIT(newLinee, sourceLine);
if (fail) { if (fail) {
qDebug() << "in line" << (i + 1); qDebug() << "in line" << (i + 1);