diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-11-15 15:49:23 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-11-27 18:50:14 +0100 |
commit | f6e90ba533b9b4b98cfcae3d31731c1cb794ee77 (patch) | |
tree | dbde1d4f79cb8f7be325ba69b058eb7530c7394b /tests | |
parent | c6c34db202ce504cff1e6cc71d9f809a8365e066 (diff) |
Add (and ignore for now) type assertions to QML
You can write "(something as Foo)" to give hints to any tools that you
expect something to be a Foo at this place. This is not a conversion and
ignored at runtime for now. Eventually the compiler will verify that the
type assertions are plausible and error out if they aren't.
Change-Id: I21c8705bb387f7ab2cbc153293dbf477663afe87
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlparser/tst_qqmlparser.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp index 4ba6a709df..76b56bd303 100644 --- a/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp +++ b/tests/auto/qml/qqmlparser/tst_qqmlparser.cpp @@ -63,6 +63,8 @@ private slots: void disallowedTypeAnnotations_data(); void disallowedTypeAnnotations(); void semicolonPartOfExpressionStatement(); + void typeAssertion_data(); + void typeAssertion(); private: QStringList excludedDirs; @@ -479,6 +481,46 @@ void tst_qqmlparser::semicolonPartOfExpressionStatement() QVERIFY(observer.endsWithSemicolon); } +void tst_qqmlparser::typeAssertion_data() +{ + QTest::addColumn<QString>("expression"); + QTest::addRow("as A") + << QString::fromLatin1("A { onStuff: (b as A).happen() }"); + QTest::addRow("as double paren") + << QString::fromLatin1("A { onStuff: console.log((12 as double)); }"); + QTest::addRow("as double noparen") + << QString::fromLatin1("A { onStuff: console.log(12 as double); }"); + QTest::addRow("property as double") + << QString::fromLatin1("A { prop: (12 as double); }"); + QTest::addRow("property noparen as double") + << QString::fromLatin1("A { prop: 12 as double; }"); + + // rabbits cannot be discerned from types on a syntactical level. + // We could detect this on a semantical level, once we implement type assertions there. + + QTest::addRow("as rabbit") + << QString::fromLatin1("A { onStuff: (b as rabbit).happen() }"); + QTest::addRow("as rabbit paren") + << QString::fromLatin1("A { onStuff: console.log((12 as rabbit)); }"); + QTest::addRow("as rabbit noparen") + << QString::fromLatin1("A { onStuff: console.log(12 as rabbit); }"); + QTest::addRow("property as rabbit") + << QString::fromLatin1("A { prop: (12 as rabbit); }"); + QTest::addRow("property noparen as rabbit") + << QString::fromLatin1("A { prop: 12 as rabbit; }"); +} + +void tst_qqmlparser::typeAssertion() +{ + QFETCH(QString, expression); + + QQmlJS::Engine engine; + QQmlJS::Lexer lexer(&engine); + lexer.setCode(expression, 1); + QQmlJS::Parser parser(&engine); + QVERIFY(parser.parse()); +} + QTEST_MAIN(tst_qqmlparser) #include "tst_qqmlparser.moc" |