From 15a06b77c01c3bcbbc69ca0e80035e0c3824d62c Mon Sep 17 00:00:00 2001 From: Semih Yavuz Date: Fri, 14 Apr 2023 11:38:25 +0200 Subject: [PATCH] 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 Reviewed-by: Jarek Kobus Reviewed-by: Eike Ziller --- src/libs/qmljs/qmljsreformatter.cpp | 6 +++--- tests/auto/qml/reformatter/tst_reformatter.cpp | 14 ++------------ 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/libs/qmljs/qmljsreformatter.cpp b/src/libs/qmljs/qmljsreformatter.cpp index 097119eb270..bad58e414ca 100644 --- a/src/libs/qmljs/qmljsreformatter.cpp +++ b/src/libs/qmljs/qmljsreformatter.cpp @@ -101,16 +101,16 @@ public: _hadEmptyLine = false; _binaryExpDepth = 0; - + const QString &source = _doc->source(); // emit directives if (_doc->bind()->isJsLibrary()) { - out(QLatin1String(".pragma library")); + const QLatin1String pragmaLine(".pragma library"); + out(pragmaLine, SourceLocation(source.indexOf(".pragma"), pragmaLine.length())); newLine(); } const QList &directives = _doc->jsDirectives(); for (const auto &d: directives) { quint32 line = 0; - const QString &source = _doc->source(); int i = -1; while (++line < d.startLine) i = source.indexOf(QChar('\n'), i + 1); diff --git a/tests/auto/qml/reformatter/tst_reformatter.cpp b/tests/auto/qml/reformatter/tst_reformatter.cpp index 42d98e02bed..68b192fa600 100644 --- a/tests/auto/qml/reformatter/tst_reformatter.cpp +++ b/tests/auto/qml/reformatter/tst_reformatter.cpp @@ -68,25 +68,15 @@ void tst_Reformatter::test() QString rewritten = reformat(doc); - QStringList sourceLines = source.split(QLatin1Char('\n')); - QStringList newLines = rewritten.split(QLatin1Char('\n')); + QStringList sourceLines = source.split(QLatin1Char('\n'), Qt::SkipEmptyParts); + QStringList newLines = rewritten.split(QLatin1Char('\n'), Qt::SkipEmptyParts); // compare line by line int commonLines = qMin(newLines.size(), sourceLines.size()); - bool insideMultiLineComment = false; 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); - 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); if (fail) { qDebug() << "in line" << (i + 1);