diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-01-02 16:26:03 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-01-11 14:53:53 +0000 |
commit | 842fa74658be458ef7c51f00901220d232ad8330 (patch) | |
tree | 77e0a1a3fa0f329838bf833797c49005f9f25646 | |
parent | fd7106f51d846cc02e500a5b36e846448665a47c (diff) |
tst_sanity: check for internal IDs
Task-number: QTBUG-65341
Change-Id: I51bd3390a18e50e37dfdd880dfa8df262d478b04
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | tests/auto/sanity/BLACKLIST | 6 | ||||
-rw-r--r-- | tests/auto/sanity/tst_sanity.cpp | 67 |
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/auto/sanity/BLACKLIST b/tests/auto/sanity/BLACKLIST index 42ad3448..112c9fba 100644 --- a/tests/auto/sanity/BLACKLIST +++ b/tests/auto/sanity/BLACKLIST @@ -2,3 +2,9 @@ * [attachedObjects:material/SwitchDelegate.qml] * +[ids:controls/Tumbler.qml] +* +[ids:material/Tumbler.qml] +* +[ids:universal/Tumbler.qml] +* diff --git a/tests/auto/sanity/tst_sanity.cpp b/tests/auto/sanity/tst_sanity.cpp index ae442b4a..c99c42cb 100644 --- a/tests/auto/sanity/tst_sanity.cpp +++ b/tests/auto/sanity/tst_sanity.cpp @@ -74,6 +74,8 @@ private slots: void anchors_data(); void attachedObjects(); void attachedObjects_data(); + void ids(); + void ids_data(); private: QQmlEngine engine; @@ -262,6 +264,71 @@ void tst_Sanity::anchors_data() QTest::newRow(qPrintable(it.key())) << it.key() << it.value(); } +class IdValidator : public BaseValidator +{ +public: + IdValidator() : m_depth(0) { } + +protected: + bool visit(QQmlJS::AST::UiObjectBinding *) override + { + ++m_depth; + return true; + } + + void endVisit(QQmlJS::AST::UiObjectBinding *) override + { + --m_depth; + } + + bool visit(QQmlJS::AST::UiScriptBinding *node) override + { + if (m_depth == 0) + return true; + + QQmlJS::AST::UiQualifiedId *id = node->qualifiedId; + if (id && id->name == QStringLiteral("id")) + addError(QString("Internal IDs are not allowed (%1)").arg(extractName(node->statement)), node); + return true; + } + +private: + QString extractName(QQmlJS::AST::Statement *statement) + { + QQmlJS::AST::ExpressionStatement *expressionStatement = static_cast<QQmlJS::AST::ExpressionStatement *>(statement); + if (!expressionStatement) + return QString(); + + QQmlJS::AST::IdentifierExpression *expression = static_cast<QQmlJS::AST::IdentifierExpression *>(expressionStatement->expression); + if (!expression) + return QString(); + + return expression->name.toString(); + } + + int m_depth; +}; + +void tst_Sanity::ids() +{ + QFETCH(QString, control); + QFETCH(QString, filePath); + + IdValidator validator; + if (!validator.validate(filePath)) + QFAIL(qPrintable(validator.errors())); +} + +void tst_Sanity::ids_data() +{ + QTest::addColumn<QString>("control"); + QTest::addColumn<QString>("filePath"); + + QMap<QString, QString>::const_iterator it; + for (it = files.constBegin(); it != files.constEnd(); ++it) + QTest::newRow(qPrintable(it.key())) << it.key() << it.value(); +} + static void addTestRows(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skiplist = QStringList()) { // We cannot use QQmlComponent to load QML files directly from the source tree. |