diff options
Diffstat (limited to 'tests/auto/qml/qmlformat')
-rw-r--r-- | tests/auto/qml/qmlformat/data/FrontInline.formatted.qml | 3 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/FrontInline.qml | 2 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml | 63 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/IfBlocks.qml | 66 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 59 |
5 files changed, 185 insertions, 8 deletions
diff --git a/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml b/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml new file mode 100644 index 0000000000..620fbf4120 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml @@ -0,0 +1,3 @@ +// This comment should be directly above Item after formatting +Item { +} diff --git a/tests/auto/qml/qmlformat/data/FrontInline.qml b/tests/auto/qml/qmlformat/data/FrontInline.qml new file mode 100644 index 0000000000..c63265481c --- /dev/null +++ b/tests/auto/qml/qmlformat/data/FrontInline.qml @@ -0,0 +1,2 @@ +Item { // This comment should be directly above Item after formatting +} diff --git a/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml new file mode 100644 index 0000000000..b8e77ec23a --- /dev/null +++ b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml @@ -0,0 +1,63 @@ +Item { + + function test() { + //// The following if blocks should NOT HAVE braces + // Single branch, no braces + if (true) + console.log("foo"); + + // Single branch, no braces + if (true) + console.log("foo"); + + // Multiple branches, No braces + if (true) + console.log("foo"); + else if (false) + console.log("bar"); + else + console.log("baz"); + // Multiple branches, all braces + if (true) + console.log("foo"); + else if (false) + console.log("bar"); + else + console.log("baz"); + + //// The following if blocks should HAVE braces + // Single branch, braces + if (true) { + console.log("foo"); + console.log("bar"); + } + // Multiple branches, some braces + if (true) { + console.log("foo"); + console.log("foo2"); + } else if (false) { + console.log("bar"); + } else { + console.log("baz"); + } + // Multiple branches, some braces + if (true) { + console.log("foo"); + } else if (false) { + console.log("bar"); + console.log("bar2"); + } else { + console.log("baz"); + } + // Multiple branches, some braces + if (true) { + console.log("foo"); + } else if (false) { + console.log("bar"); + } else { + console.log("baz"); + console.log("baz2"); + } + } + +} diff --git a/tests/auto/qml/qmlformat/data/IfBlocks.qml b/tests/auto/qml/qmlformat/data/IfBlocks.qml new file mode 100644 index 0000000000..505988b238 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/IfBlocks.qml @@ -0,0 +1,66 @@ +Item { + function test() { + //// The following if blocks should NOT HAVE braces + // Single branch, no braces + if (true) + console.log("foo"); + + // Single branch, no braces + if (true) { + console.log("foo"); + } + + + // Multiple branches, No braces + if (true) + console.log("foo"); + else if (false) + console.log("bar"); + else + console.log("baz"); + + // Multiple branches, all braces + if (true) { + console.log("foo"); + } else if (false) { + console.log("bar"); + } else { + console.log("baz"); + } + + //// The following if blocks should HAVE braces + // Single branch, braces + if (true) { + console.log("foo"); + console.log("bar"); + } + + // Multiple branches, some braces + if (true) { + console.log("foo"); + console.log("foo2"); + } else if (false) + console.log("bar"); + else + console.log("baz"); + + // Multiple branches, some braces + if (true) + console.log("foo"); + else if (false) { + console.log("bar"); + console.log("bar2"); + } else + console.log("baz"); + + // Multiple branches, some braces + if (true) + console.log("foo"); + else if (false) + console.log("bar"); + else { + console.log("baz"); + console.log("baz2"); + } + } +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 21d5ae46a9..997a5cc753 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -27,8 +27,11 @@ ****************************************************************************/ #include <QtTest/QtTest> +#include <QDir> +#include <QFile> #include <QProcess> #include <QString> +#include <QTemporaryDir> #include <util.h> @@ -43,6 +46,9 @@ private Q_SLOTS: void testFormatNoSort(); void testAnnotations(); void testAnnotationsNoSort(); + void testLineEndings(); + void testFrontInline(); + void testIfBlocks(); void testReadOnlyProps(); void testStatesAndTransitions(); @@ -55,7 +61,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; @@ -113,6 +119,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"; @@ -197,6 +204,16 @@ 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::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")); @@ -212,6 +229,23 @@ void TestQmlformat::testLargeBindings() QCOMPARE(runQmlformat(testFile("largeBindings.qml"), false, true), readTestFile("largeBindings.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() { @@ -240,15 +274,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); @@ -256,13 +297,15 @@ QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImport QCOMPARE(process.exitStatus(), QProcess::NormalExit); if (shouldSucceed) 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) |