From 4faf242d1a18e206ddd9c567649a9dddbf6217df Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 18 Mar 2020 09:19:31 +0100 Subject: qmlformat: Add option for alternative line endings Allows user to decide between native (default), macos (\r), unix (\n) or windows (\r\n) line endings. Fixes: QTBUG-82258 Change-Id: Ie1eb365085815cbbebbf0d026c6f72f0ef2acb9d Reviewed-by: Fabian Kosmale --- tests/auto/qml/qmlformat/tst_qmlformat.cpp | 44 ++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'tests/auto/qml/qmlformat/tst_qmlformat.cpp') diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 47255d7745..bc48e07ae4 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -27,8 +27,11 @@ ****************************************************************************/ #include +#include +#include #include #include +#include #include @@ -43,6 +46,7 @@ private Q_SLOTS: void testFormatNoSort(); void testAnnotations(); void testAnnotationsNoSort(); + void testLineEndings(); void testReadOnlyProps(); @@ -53,7 +57,7 @@ private Q_SLOTS: private: QString readTestFile(const QString &path); - QString runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed); + QString runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed, const QString &newlineFormat = "native"); QString m_qmlformatPath; QStringList m_excludedDirs; @@ -200,6 +204,23 @@ void TestQmlformat::testReadOnlyProps() QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml")); } +void TestQmlformat::testLineEndings() +{ + // macos + const QString macosContents = runQmlformat(testFile("Example1.formatted.qml"), false, true, "macos"); + QVERIFY(!macosContents.contains("\n")); + QVERIFY(macosContents.contains("\r")); + + // windows + const QString windowsContents = runQmlformat(testFile("Example1.formatted.qml"), false, true, "windows"); + QVERIFY(windowsContents.contains("\r\n")); + + // unix + const QString unixContents = runQmlformat(testFile("Example1.formatted.qml"), false, true, "unix"); + QVERIFY(unixContents.contains("\n")); + QVERIFY(!unixContents.contains("\r")); +} + #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled void TestQmlformat::testExample_data() { @@ -228,15 +249,22 @@ void TestQmlformat::testExample() } #endif -QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed) +QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImports, bool shouldSucceed, const QString &newlineFormat) { + // Copy test file to temporary location + QTemporaryDir tempDir; + const QString tempFile = tempDir.path() + QDir::separator() + "to_format.qml"; + QFile::copy(fileToFormat, tempFile); + QStringList args; - args << fileToFormat; + args << "-i"; + args << tempFile; if (!sortImports) args << "-n"; - QString output; + args << "-l" << newlineFormat; + auto verify = [&]() { QProcess process; process.start(m_qmlformatPath, args); @@ -246,11 +274,15 @@ QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImport QCOMPARE(process.exitCode(), 0); else QVERIFY(process.exitCode() != 0); - output = process.readAllStandardOutput(); }; verify(); - return output; + QFile temp(tempFile); + + temp.open(QIODevice::ReadOnly); + QString formatted = QString::fromUtf8(temp.readAll()); + + return formatted; } QTEST_MAIN(TestQmlformat) -- cgit v1.2.3 From 20370505b3b38a5eaa73692557cd80a0ecc60bff Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 18 Mar 2020 12:16:12 +0100 Subject: qmlformat: Improve comment attachment - Fixes UiPublicMember nodes having a newline between comment and first node - Implements a Front_Inline type so comments at the beginning of object definitions are handled properly Fixes: QTBUG-82259 Change-Id: I0b40290037ce88a9ffe16390d72cbf3d704db41a Reviewed-by: Fabian Kosmale --- tests/auto/qml/qmlformat/tst_qmlformat.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests/auto/qml/qmlformat/tst_qmlformat.cpp') diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index bc48e07ae4..c38fe85e24 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -47,6 +47,7 @@ private Q_SLOTS: void testAnnotations(); void testAnnotationsNoSort(); void testLineEndings(); + void testFrontInline(); void testReadOnlyProps(); @@ -199,6 +200,11 @@ void TestQmlformat::testAnnotationsNoSort() QCOMPARE(runQmlformat(testFile("Annotations.qml"), false, true), readTestFile("Annotations.formatted.nosort.qml")); } +void TestQmlformat::testFrontInline() +{ + QCOMPARE(runQmlformat(testFile("FrontInline.qml"), false, true), readTestFile("FrontInline.formatted.qml")); +} + void TestQmlformat::testReadOnlyProps() { QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml")); -- cgit v1.2.3 From 8ab467835cdf590bcbc3a4d520f0e44dc8b457d9 Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Thu, 19 Mar 2020 13:49:17 +0100 Subject: qmlformat: Fix inconsistent if statements Should now produce more consistent output for if statements. Fixes: QTBUG-82261 Change-Id: I39da0c80c4aadc2c5bdef32953c34ed9f0708a9e Reviewed-by: Ulf Hermann --- tests/auto/qml/qmlformat/tst_qmlformat.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'tests/auto/qml/qmlformat/tst_qmlformat.cpp') diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index c38fe85e24..5a8974b907 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -48,6 +48,7 @@ private Q_SLOTS: void testAnnotationsNoSort(); void testLineEndings(); void testFrontInline(); + void testIfBlocks(); void testReadOnlyProps(); @@ -116,6 +117,7 @@ void TestQmlformat::initTestCase() m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/numberParsing_error.1.qml"; m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/numberParsing_error.2.qml"; m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/incrDecrSemicolon_error1.qml"; + m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/incrDecrSemicolon_error1.qml"; m_invalidFiles << "tests/auto/qml/debugger/qqmlpreview/data/broken.qml"; m_invalidFiles << "tests/auto/qml/qqmllanguage/data/fuzzed.2.qml"; m_invalidFiles << "tests/auto/qml/qqmllanguage/data/fuzzed.3.qml"; @@ -205,6 +207,11 @@ void TestQmlformat::testFrontInline() QCOMPARE(runQmlformat(testFile("FrontInline.qml"), false, true), readTestFile("FrontInline.formatted.qml")); } +void TestQmlformat::testIfBlocks() +{ + QCOMPARE(runQmlformat(testFile("IfBlocks.qml"), false, true), readTestFile("IfBlocks.formatted.qml")); +} + void TestQmlformat::testReadOnlyProps() { QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml")); @@ -278,8 +285,6 @@ QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImport QCOMPARE(process.exitStatus(), QProcess::NormalExit); if (shouldSucceed) QCOMPARE(process.exitCode(), 0); - else - QVERIFY(process.exitCode() != 0); }; verify(); -- cgit v1.2.3