aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmlformat
diff options
context:
space:
mode:
authorDmitrii Akshintsev <dmitrii.akshintsev@qt.io>2023-11-20 14:19:41 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2024-01-25 17:35:04 +0000
commita0f946d41e8584d90de6a9fbd0d8ea6fd832094f (patch)
tree49098afdc896cb93504d66a7b1f84fe4ff8b95f1 /tests/auto/qml/qmlformat
parentade897c021be7089a86702de3e9e626ab8cb7648 (diff)
QmlFormat: Support .js files (CLI tool)
This commit contains last pieces of changes necessary for the formatting of .js files using qmlformat CLI. What's not included: normalize && function spacing for .js files Task-number: QTBUG-117849 Pick-to: 6.7 Change-Id: I6a4260f074ce05ce07cdcff185a1cf7737d3a44c Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qmlformat')
-rw-r--r--tests/auto/qml/qmlformat/data/threeFunctions.formattedFuncSpacing.js11
-rw-r--r--tests/auto/qml/qmlformat/data/threeFunctions.formattedTabs.js9
-rw-r--r--tests/auto/qml/qmlformat/data/threeFunctions.formattedW2.js9
-rw-r--r--tests/auto/qml/qmlformat/data/threeFunctionsOneLine.js1
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp32
5 files changed, 58 insertions, 4 deletions
diff --git a/tests/auto/qml/qmlformat/data/threeFunctions.formattedFuncSpacing.js b/tests/auto/qml/qmlformat/data/threeFunctions.formattedFuncSpacing.js
new file mode 100644
index 0000000000..592db7a02b
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/threeFunctions.formattedFuncSpacing.js
@@ -0,0 +1,11 @@
+function one() {
+ var a=1;
+}
+
+function two(a, b) {
+ console.log(a,b);
+}
+
+function three(c) {
+ var a=c;
+}
diff --git a/tests/auto/qml/qmlformat/data/threeFunctions.formattedTabs.js b/tests/auto/qml/qmlformat/data/threeFunctions.formattedTabs.js
new file mode 100644
index 0000000000..6575a9fbd8
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/threeFunctions.formattedTabs.js
@@ -0,0 +1,9 @@
+function one() {
+ var a = 1;
+}
+function two(a, b) {
+ console.log(a, b);
+}
+function three(c) {
+ var a = c;
+}
diff --git a/tests/auto/qml/qmlformat/data/threeFunctions.formattedW2.js b/tests/auto/qml/qmlformat/data/threeFunctions.formattedW2.js
new file mode 100644
index 0000000000..afcf8acf02
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/threeFunctions.formattedW2.js
@@ -0,0 +1,9 @@
+function one() {
+ var a = 1;
+}
+function two(a, b) {
+ console.log(a, b);
+}
+function three(c) {
+ var a = c;
+}
diff --git a/tests/auto/qml/qmlformat/data/threeFunctionsOneLine.js b/tests/auto/qml/qmlformat/data/threeFunctionsOneLine.js
new file mode 100644
index 0000000000..648591f221
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/threeFunctionsOneLine.js
@@ -0,0 +1 @@
+function one() {var a=1;}function two(a, b) {console.log(a,b);}function three(c){var a=c;}
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index 11f8f0240b..1148388bc3 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -27,6 +27,7 @@ public:
private Q_SLOTS:
void initTestCase() override;
+ //actually testFormat tests CLI of qmlformat
void testFormat();
void testFormat_data();
@@ -47,8 +48,9 @@ private Q_SLOTS:
void plainJS();
private:
QString readTestFile(const QString &path);
+ //TODO(QTBUG-117849) refactor this helper function
QString runQmlformat(const QString &fileToFormat, QStringList args, bool shouldSucceed = true,
- RunOption rOption = RunOption::OnCopy);
+ RunOption rOption = RunOption::OnCopy, bool isQml = true);
QString formatInMemory(const QString &fileToFormat, bool *didSucceed = nullptr,
LineWriterOptions options = LineWriterOptions(),
WriteOutChecks extraChecks = WriteOutCheck::ReparseCompare,
@@ -365,6 +367,23 @@ void TestQmlformat::testFormat_data()
QTest::newRow("javascriptBlock")
<< "javascriptBlock.qml"
<< "javascriptBlock.formatted.qml" << QStringList{} << RunOption::OnCopy;
+ //plainJS
+ QTest::newRow("nestedLambdaWithIfElse")
+ << "lambdaWithIfElseInsideLambda.js"
+ << "lambdaWithIfElseInsideLambda.formatted.js" << QStringList{} << RunOption::OnCopy;
+
+ QTest::newRow("indentEquals2")
+ << "threeFunctionsOneLine.js"
+ << "threeFunctions.formattedW2.js" << QStringList{"-w=2"} << RunOption::OnCopy;
+
+ QTest::newRow("tabIndents")
+ << "threeFunctionsOneLine.js"
+ << "threeFunctions.formattedTabs.js" << QStringList{"-t"} << RunOption::OnCopy;
+
+ QTest::newRow("normalizedFunctionSpacing")
+ << "threeFunctionsOneLine.js"
+ << "threeFunctions.formattedFuncSpacing.js"
+ << QStringList{ "-n", "--functions-spacing" } << RunOption::OnCopy;
}
void TestQmlformat::testFormat()
@@ -374,7 +393,11 @@ void TestQmlformat::testFormat()
QFETCH(QStringList, args);
QFETCH(RunOption, runOption);
- QCOMPARE(runQmlformat(testFile(file), args, true, runOption), readTestFile(fileFormatted));
+ bool isQml = file.endsWith(QLatin1String(".qml"));
+ auto formatted = runQmlformat(testFile(file), args, true, runOption, isQml);
+ QEXPECT_FAIL("normalizedFunctionSpacing",
+ "Normalize && function spacing are not yet supported for JS", Abort);
+ QCOMPARE(formatted, readTestFile(fileFormatted));
}
void TestQmlformat::plainJS_data()
@@ -599,11 +622,12 @@ void TestQmlformat::testFilesOption()
}
QString TestQmlformat::runQmlformat(const QString &fileToFormat, QStringList args,
- bool shouldSucceed, RunOption rOptions)
+ bool shouldSucceed, RunOption rOptions, bool isQml)
{
// Copy test file to temporary location
QTemporaryDir tempDir;
- const QString tempFile = tempDir.path() + QDir::separator() + "to_format.qml";
+ const QString ext = isQml ? ".qml" : ".js";
+ const QString tempFile = tempDir.path() + QDir::separator() + "to_format" + ext;
if (rOptions == RunOption::OnCopy) {
QFile::copy(fileToFormat, tempFile);