aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/qml/qqmlparser/tst_qqmlparser.cpp')
-rw-r--r--tests/auto/qml/qqmlparser/tst_qqmlparser.cpp143
1 files changed, 141 insertions, 2 deletions
diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
index f16e96a385..9d8818d01e 100644
--- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
+++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp
@@ -32,12 +32,14 @@
#include <private/qqmljsastvisitor_p.h>
#include <private/qqmljsast_p.h>
+#include "../../shared/util.h"
+
#include <qtest.h>
#include <QDir>
#include <QDebug>
#include <cstdlib>
-class tst_qqmlparser : public QObject
+class tst_qqmlparser : public QQmlDataTest
{
Q_OBJECT
public:
@@ -55,6 +57,11 @@ private slots:
void templateLiteral();
void leadingSemicolonInClass();
void templatedReadonlyProperty();
+ void qmlImportInJSRequiresFullVersion();
+ void typeAnnotations_data();
+ void typeAnnotations();
+ void disallowedTypeAnnotations_data();
+ void disallowedTypeAnnotations();
private:
QStringList excludedDirs;
@@ -87,7 +94,7 @@ public:
qDebug() << "first source loc failed: node:" << node->kind << "at" << node->firstSourceLocation().startLine << "/" << node->firstSourceLocation().startColumn
<< "parent" << parent->kind << "at" << parent->firstSourceLocation().startLine << "/" << parent->firstSourceLocation().startColumn;
if (node->lastSourceLocation().end() > parentEnd)
- qDebug() << "first source loc failed: node:" << node->kind << "at" << node->lastSourceLocation().startLine << "/" << node->lastSourceLocation().startColumn
+ qDebug() << "last source loc failed: node:" << node->kind << "at" << node->lastSourceLocation().startLine << "/" << node->lastSourceLocation().startColumn
<< "parent" << parent->kind << "at" << parent->lastSourceLocation().startLine << "/" << parent->lastSourceLocation().startColumn;
QVERIFY(node->firstSourceLocation().begin() >= parentBegin);
@@ -113,6 +120,27 @@ public:
}
};
+struct TypeAnnotationObserver: public AST::Visitor
+{
+ bool typeAnnotationSeen = false;
+
+ void operator()(AST::Node *node)
+ {
+ AST::Node::accept(node, this);
+ }
+
+ virtual bool visit(AST::TypeAnnotation *)
+ {
+ typeAnnotationSeen = true;
+ return true;
+ }
+
+ void throwRecursionDepthError() final
+ {
+ QFAIL("Maximum statement or expression depth exceeded");
+ }
+};
+
}
tst_qqmlparser::tst_qqmlparser()
@@ -121,6 +149,7 @@ tst_qqmlparser::tst_qqmlparser()
void tst_qqmlparser::initTestCase()
{
+ QQmlDataTest::initTestCase();
// Add directories you want excluded here
// These snippets are not expected to run on their own.
@@ -299,6 +328,116 @@ void tst_qqmlparser::templatedReadonlyProperty()
QVERIFY(parser.parse());
}
+void tst_qqmlparser::qmlImportInJSRequiresFullVersion()
+{
+ {
+ QQmlJS::Engine engine;
+ QQmlJS::Lexer lexer(&engine);
+ lexer.setCode(QLatin1String(".import Test 1.0 as T"), 0, false);
+ QQmlJS::Parser parser(&engine);
+ bool b = parser.parseProgram();
+ qDebug() << parser.errorMessage();
+ QVERIFY(b);
+ }
+ {
+ QQmlJS::Engine engine;
+ QQmlJS::Lexer lexer(&engine);
+ lexer.setCode(QLatin1String(".import Test 1 as T"), 0, false);
+ QQmlJS::Parser parser(&engine);
+ QVERIFY(!parser.parseProgram());
+ }
+ {
+ QQmlJS::Engine engine;
+ QQmlJS::Lexer lexer(&engine);
+ lexer.setCode(QLatin1String(".import Test 1 as T"), 0, false);
+ QQmlJS::Parser parser(&engine);
+ QVERIFY(!parser.parseProgram());
+ }
+ {
+ QQmlJS::Engine engine;
+ QQmlJS::Lexer lexer(&engine);
+ lexer.setCode(QLatin1String(".import Test as T"), 0, false);
+ QQmlJS::Parser parser(&engine);
+ QVERIFY(!parser.parseProgram());
+ }
+}
+
+void tst_qqmlparser::typeAnnotations_data()
+{
+ QTest::addColumn<QString>("file");
+
+ QString tests = dataDirectory() + "/typeannotations/";
+
+ QStringList files;
+ files << findFiles(QDir(tests));
+
+ for (const QString &file: qAsConst(files))
+ QTest::newRow(qPrintable(file)) << file;
+}
+
+void tst_qqmlparser::typeAnnotations()
+{
+ using namespace QQmlJS;
+
+ QFETCH(QString, file);
+
+ QString code;
+
+ QFile f(file);
+ if (f.open(QFile::ReadOnly))
+ code = QString::fromUtf8(f.readAll());
+
+ const bool qmlMode = file.endsWith(QLatin1String(".qml"));
+
+ Engine engine;
+ Lexer lexer(&engine);
+ lexer.setCode(code, 1, qmlMode);
+ Parser parser(&engine);
+ bool ok = qmlMode ? parser.parse() : parser.parseProgram();
+ QVERIFY(ok);
+
+ check::TypeAnnotationObserver observer;
+ observer(parser.rootNode());
+
+ QVERIFY(observer.typeAnnotationSeen);
+}
+
+void tst_qqmlparser::disallowedTypeAnnotations_data()
+{
+ QTest::addColumn<QString>("file");
+
+ QString tests = dataDirectory() + "/disallowedtypeannotations/";
+
+ QStringList files;
+ files << findFiles(QDir(tests));
+
+ for (const QString &file: qAsConst(files))
+ QTest::newRow(qPrintable(file)) << file;
+}
+
+void tst_qqmlparser::disallowedTypeAnnotations()
+{
+ using namespace QQmlJS;
+
+ QFETCH(QString, file);
+
+ QString code;
+
+ QFile f(file);
+ if (f.open(QFile::ReadOnly))
+ code = QString::fromUtf8(f.readAll());
+
+ const bool qmlMode = file.endsWith(QLatin1String(".qml"));
+
+ Engine engine;
+ Lexer lexer(&engine);
+ lexer.setCode(code, 1, qmlMode);
+ Parser parser(&engine);
+ bool ok = qmlMode ? parser.parse() : parser.parseProgram();
+ QVERIFY(!ok);
+ QVERIFY2(parser.errorMessage().startsWith("Type annotations are not permitted "), qPrintable(parser.errorMessage()));
+}
+
QTEST_MAIN(tst_qqmlparser)
#include "tst_qqmlparser.moc"