2021-11-23 12:44:58 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
#include <QtQuickTestUtils/private/qmlutils_p.h>
|
|
|
|
|
|
|
|
#include <QtCore/qfileinfo.h>
|
|
|
|
#include <QtCore/qdir.h>
|
|
|
|
#include <QtCore/qdiriterator.h>
|
|
|
|
#include <QtCore/qurl.h>
|
|
|
|
#include <QtCore/qlibraryinfo.h>
|
2022-03-30 10:50:46 +00:00
|
|
|
#include <QtCore/qscopedpointer.h>
|
2021-11-23 12:44:58 +00:00
|
|
|
#include <QtQml/qqmlengine.h>
|
|
|
|
#include <QtQml/qqmlcomponent.h>
|
2022-03-30 10:50:46 +00:00
|
|
|
#include <QtGui/qfont.h>
|
2021-11-23 12:44:58 +00:00
|
|
|
|
|
|
|
#include <QtQml/private/qqmlirbuilder_p.h>
|
|
|
|
#include <private/qqmljscompiler_p.h>
|
|
|
|
#include <private/qqmljsscope_p.h>
|
|
|
|
#include <private/qqmljsimporter_p.h>
|
|
|
|
#include <private/qqmljslogger_p.h>
|
|
|
|
#include <private/qqmljsimportvisitor_p.h>
|
|
|
|
#include <private/qqmljstyperesolver_p.h>
|
2022-03-18 13:16:09 +00:00
|
|
|
#include <QtQml/private/qqmljslexer_p.h>
|
|
|
|
#include <QtQml/private/qqmljsparser_p.h>
|
2021-11-23 12:44:58 +00:00
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2021-11-23 12:44:58 +00:00
|
|
|
class tst_qqmljsscope : public QQmlDataTest
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
QString loadUrl(const QString &url)
|
|
|
|
{
|
|
|
|
const QFileInfo fi(url);
|
|
|
|
QFile f(fi.absoluteFilePath());
|
|
|
|
f.open(QIODevice::ReadOnly);
|
|
|
|
QByteArray data(fi.size(), Qt::Uninitialized);
|
|
|
|
f.read(data.data(), data.length());
|
|
|
|
return QString::fromUtf8(data);
|
|
|
|
}
|
|
|
|
|
2022-05-04 10:35:58 +00:00
|
|
|
QQmlJSScope::ConstPtr run(QString url, bool expectErrorsOrWarnings = false)
|
2022-03-31 15:29:24 +00:00
|
|
|
{
|
|
|
|
QmlIR::Document document(false);
|
2022-05-04 10:35:58 +00:00
|
|
|
return run(url, &document, expectErrorsOrWarnings);
|
2022-03-31 15:29:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 10:35:58 +00:00
|
|
|
QQmlJSScope::ConstPtr run(QString url, QmlIR::Document *document,
|
|
|
|
bool expectErrorsOrWarnings = false)
|
2021-11-23 12:44:58 +00:00
|
|
|
{
|
|
|
|
url = testFile(url);
|
|
|
|
const QString sourceCode = loadUrl(url);
|
|
|
|
if (sourceCode.isEmpty())
|
|
|
|
return QQmlJSScope::ConstPtr();
|
|
|
|
|
|
|
|
// NB: JS unit generated here is ignored, so use noop function
|
|
|
|
QQmlJSSaveFunction noop([](auto &&...) { return true; });
|
|
|
|
QQmlJSCompileError error;
|
|
|
|
[&]() {
|
2022-03-31 15:29:24 +00:00
|
|
|
QVERIFY2(qCompileQmlFile(*document, url, noop, nullptr, &error),
|
2021-11-23 12:44:58 +00:00
|
|
|
qPrintable(error.message));
|
|
|
|
}();
|
|
|
|
if (!error.message.isEmpty())
|
|
|
|
return QQmlJSScope::ConstPtr();
|
|
|
|
|
|
|
|
|
2021-11-18 13:26:29 +00:00
|
|
|
QQmlJSLogger logger;
|
|
|
|
logger.setFileName(url);
|
|
|
|
logger.setCode(sourceCode);
|
2022-05-04 10:35:58 +00:00
|
|
|
logger.setSilent(expectErrorsOrWarnings);
|
2022-03-31 09:26:09 +00:00
|
|
|
QQmlJSScope::Ptr target = QQmlJSScope::create();
|
|
|
|
QQmlJSImportVisitor visitor(target, &m_importer, &logger, dataDirectory());
|
2022-03-11 15:03:18 +00:00
|
|
|
QQmlJSTypeResolver typeResolver { &m_importer };
|
2022-03-31 15:29:24 +00:00
|
|
|
typeResolver.init(&visitor, document->program);
|
2022-05-04 10:35:58 +00:00
|
|
|
if (!expectErrorsOrWarnings) {
|
|
|
|
[&]() {
|
|
|
|
QVERIFY2(!logger.hasWarnings(), "Expected no warnings in this test");
|
|
|
|
QVERIFY2(!logger.hasErrors(), "Expected no errors in this test");
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
if (QTest::currentTestFailed())
|
|
|
|
return QQmlJSScope::ConstPtr();
|
2021-11-23 12:44:58 +00:00
|
|
|
return visitor.result();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase() override;
|
|
|
|
|
|
|
|
void orderedBindings();
|
2022-01-27 14:02:00 +00:00
|
|
|
void signalCreationDifferences();
|
2022-02-03 13:39:58 +00:00
|
|
|
void allTypesAvailable();
|
2022-03-11 15:03:18 +00:00
|
|
|
void shadowing();
|
2022-03-18 13:16:09 +00:00
|
|
|
|
2022-03-15 15:43:38 +00:00
|
|
|
#ifdef LABS_QML_MODELS_PRESENT
|
|
|
|
void componentWrappedObjects();
|
2022-03-28 11:45:35 +00:00
|
|
|
void labsQmlModelsSanity();
|
2022-03-15 15:43:38 +00:00
|
|
|
#endif
|
|
|
|
void unknownCppBase();
|
2022-03-18 13:16:09 +00:00
|
|
|
void groupedProperties();
|
2022-04-06 09:52:25 +00:00
|
|
|
void descriptiveNameOfNull();
|
2022-03-30 10:50:46 +00:00
|
|
|
void groupedPropertiesConsistency();
|
2022-03-30 12:15:32 +00:00
|
|
|
void groupedPropertySyntax();
|
2022-03-30 15:16:11 +00:00
|
|
|
void attachedProperties();
|
2022-04-06 07:56:17 +00:00
|
|
|
void scriptIndices();
|
2022-05-03 15:08:37 +00:00
|
|
|
void extensions();
|
2022-05-19 13:41:24 +00:00
|
|
|
void emptyBlockBinding();
|
2022-05-19 08:35:20 +00:00
|
|
|
void qualifiedName();
|
2022-05-25 13:18:56 +00:00
|
|
|
void resolvedNonUniqueScopes();
|
2021-11-23 12:44:58 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-11 15:03:18 +00:00
|
|
|
tst_qqmljsscope()
|
|
|
|
: QQmlDataTest(QT_QMLTEST_DATADIR),
|
|
|
|
m_importer(
|
|
|
|
{
|
|
|
|
QLibraryInfo::path(QLibraryInfo::QmlImportsPath),
|
|
|
|
dataDirectory(),
|
2022-05-04 12:13:09 +00:00
|
|
|
// Note: to be able to import the QQmlJSScopeTests
|
|
|
|
// correctly, we need an additional import path. Use
|
|
|
|
// this application's binary directory as done by
|
|
|
|
// QQmlImportDatabase
|
|
|
|
QCoreApplication::applicationDirPath(),
|
2022-03-11 15:03:18 +00:00
|
|
|
},
|
|
|
|
nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QQmlJSImporter m_importer;
|
2021-11-23 12:44:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void tst_qqmljsscope::initTestCase()
|
|
|
|
{
|
|
|
|
QQmlDataTest::initTestCase();
|
|
|
|
|
|
|
|
QDirIterator it(dataDirectory(), QDirIterator::FollowSymlinks | QDirIterator::Subdirectories);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
const QString url = it.next();
|
2022-03-21 09:21:18 +00:00
|
|
|
if (!url.endsWith(u".qml"_s)) // not interesting
|
2021-11-23 12:44:58 +00:00
|
|
|
continue;
|
|
|
|
const QFileInfo fi(url);
|
|
|
|
QVERIFY(fi.exists());
|
|
|
|
QFile f(fi.absoluteFilePath());
|
|
|
|
QVERIFY(f.open(QIODevice::ReadOnly));
|
|
|
|
}
|
2022-05-04 12:13:09 +00:00
|
|
|
|
|
|
|
// test that we can import the module shipped with this test: if we have no
|
|
|
|
// errors / warnings, it is a success
|
|
|
|
QVERIFY(run(u"importOwnModule.qml"_s));
|
2021-11-23 12:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_qqmljsscope::orderedBindings()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"orderedBindings.qml"_s);
|
2021-11-23 12:44:58 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
auto [pBindingsBegin, pBindingsEnd] = root->ownPropertyBindings(u"p"_s);
|
2022-05-19 13:41:24 +00:00
|
|
|
QCOMPARE(std::distance(pBindingsBegin, pBindingsEnd), 2);
|
2021-11-23 12:44:58 +00:00
|
|
|
|
|
|
|
// check that the bindings are properly ordered
|
|
|
|
QCOMPARE(pBindingsBegin->bindingType(), QQmlJSMetaPropertyBinding::Object);
|
|
|
|
QCOMPARE(std::next(pBindingsBegin)->bindingType(), QQmlJSMetaPropertyBinding::Interceptor);
|
2021-11-24 08:53:10 +00:00
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
auto [itemsBindingsBegin, itemsBindingsEnd] = root->ownPropertyBindings(u"items"_s);
|
2022-05-19 13:41:24 +00:00
|
|
|
QCOMPARE(std::distance(itemsBindingsBegin, itemsBindingsEnd), 2);
|
2021-11-24 08:53:10 +00:00
|
|
|
|
|
|
|
QCOMPARE(itemsBindingsBegin->bindingType(), QQmlJSMetaPropertyBinding::Object);
|
|
|
|
QCOMPARE(std::next(itemsBindingsBegin)->bindingType(), QQmlJSMetaPropertyBinding::Object);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(itemsBindingsBegin->objectType()->baseTypeName(), u"Item"_s);
|
|
|
|
QCOMPARE(std::next(itemsBindingsBegin)->objectType()->baseTypeName(), u"Text"_s);
|
2021-11-23 12:44:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 14:02:00 +00:00
|
|
|
void tst_qqmljsscope::signalCreationDifferences()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"signalCreationDifferences.qml"_s);
|
2022-01-27 14:02:00 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QVERIFY(root->hasOwnProperty(u"myProperty"_s));
|
|
|
|
QVERIFY(root->hasOwnProperty(u"conflictingProperty"_s));
|
|
|
|
QCOMPARE(root->ownMethods(u"mySignal"_s).size(), 1);
|
2022-01-27 14:02:00 +00:00
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
const auto conflicting = root->ownMethods(u"conflictingPropertyChanged"_s);
|
2022-01-27 14:02:00 +00:00
|
|
|
QCOMPARE(conflicting.size(), 2);
|
|
|
|
QCOMPARE(conflicting[0].methodType(), QQmlJSMetaMethod::Signal);
|
|
|
|
QCOMPARE(conflicting[1].methodType(), QQmlJSMetaMethod::Signal);
|
|
|
|
|
|
|
|
const QQmlJSMetaMethod *explicitMethod = nullptr;
|
|
|
|
if (conflicting[0].isImplicitQmlPropertyChangeSignal())
|
|
|
|
explicitMethod = &conflicting[1];
|
|
|
|
else
|
|
|
|
explicitMethod = &conflicting[0];
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(explicitMethod->parameterNames(), QStringList({ u"a"_s, u"c"_s }));
|
2022-01-27 14:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 13:39:58 +00:00
|
|
|
void tst_qqmljsscope::allTypesAvailable()
|
|
|
|
{
|
|
|
|
const QStringList importPaths = {
|
|
|
|
QLibraryInfo::path(QLibraryInfo::QmlImportsPath),
|
|
|
|
dataDirectory(),
|
|
|
|
};
|
|
|
|
|
|
|
|
QQmlJSImporter importer { importPaths, /* resource file mapper */ nullptr };
|
2022-03-21 09:21:18 +00:00
|
|
|
const auto types = importer.importModule(u"QtQml"_s);
|
|
|
|
QVERIFY(types.contains(u"$internal$.QObject"_s));
|
|
|
|
QVERIFY(types.contains(u"QtObject"_s));
|
|
|
|
QCOMPARE(types[u"$internal$.QObject"_s].scope, types[u"QtObject"_s].scope);
|
2022-02-03 13:39:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 15:03:18 +00:00
|
|
|
void tst_qqmljsscope::shadowing()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"shadowing.qml"_s);
|
2022-03-11 15:03:18 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
|
|
|
QVERIFY(root->baseType());
|
|
|
|
|
|
|
|
// Check whether properties are properly shadowed
|
|
|
|
const auto properties = root->properties();
|
2022-03-21 09:21:18 +00:00
|
|
|
QVERIFY(properties.contains(u"property_not_shadowed"_s));
|
|
|
|
QVERIFY(properties.contains(u"property_shadowed"_s));
|
2022-03-11 15:03:18 +00:00
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(properties[u"property_not_shadowed"_s].typeName(), u"QString"_s);
|
|
|
|
QCOMPARE(properties[u"property_shadowed"_s].typeName(), u"int"_s);
|
2022-03-11 15:03:18 +00:00
|
|
|
|
|
|
|
// Check whether methods are properly shadowed
|
|
|
|
const auto methods = root->methods();
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(methods.count(u"method_not_shadowed"_s), 1);
|
|
|
|
QCOMPARE(methods.count(u"method_shadowed"_s), 1);
|
2022-03-11 15:03:18 +00:00
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(methods[u"method_not_shadowed"_s].parameterNames().size(), 1);
|
|
|
|
QCOMPARE(methods[u"method_shadowed"_s].parameterNames().size(), 0);
|
2022-03-11 15:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 15:43:38 +00:00
|
|
|
#ifdef LABS_QML_MODELS_PRESENT
|
|
|
|
void tst_qqmljsscope::componentWrappedObjects()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"componentWrappedObjects.qml"_s);
|
2022-03-15 15:43:38 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
|
|
|
auto children = root->childScopes();
|
|
|
|
QCOMPARE(children.size(), 4);
|
|
|
|
|
|
|
|
const auto isGoodType = [](const QQmlJSScope::ConstPtr &type, const QString &propertyName,
|
|
|
|
bool isWrapped) {
|
|
|
|
return type->hasOwnProperty(propertyName)
|
|
|
|
&& type->isWrappedInImplicitComponent() == isWrapped;
|
|
|
|
};
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QVERIFY(isGoodType(children[0], u"nonWrapped1"_s, false));
|
|
|
|
QVERIFY(isGoodType(children[1], u"nonWrapped2"_s, false));
|
|
|
|
QVERIFY(isGoodType(children[2], u"nonWrapped3"_s, false));
|
|
|
|
QVERIFY(isGoodType(children[3], u"wrapped"_s, true));
|
2022-03-15 15:43:38 +00:00
|
|
|
}
|
2022-03-28 11:45:35 +00:00
|
|
|
|
|
|
|
void tst_qqmljsscope::labsQmlModelsSanity()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"labsQmlModelsSanity.qml"_s);
|
2022-03-28 11:45:35 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
auto children = root->childScopes();
|
|
|
|
QCOMPARE(children.size(), 1);
|
|
|
|
|
|
|
|
// DelegateChooser: it inherits QQmlAbstractDelegateComponent (from
|
|
|
|
// QmlModels) which inherits QQmlComponent. While
|
|
|
|
// QQmlAbstractDelegateComponent has no properties, QQmlComponent does. If
|
|
|
|
// the QmlModels dependency is lost, we don't "see" that DelegateChooser
|
|
|
|
// inherits QQmlComponent - and so has no properties from it, hence, we can
|
|
|
|
// test exactly that:
|
2022-03-21 09:21:18 +00:00
|
|
|
QVERIFY(children[0]->hasProperty(u"progress"_s));
|
|
|
|
QVERIFY(children[0]->hasProperty(u"status"_s));
|
|
|
|
QVERIFY(children[0]->hasProperty(u"url"_s));
|
2022-03-28 11:45:35 +00:00
|
|
|
}
|
2022-03-15 15:43:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void tst_qqmljsscope::unknownCppBase()
|
|
|
|
{
|
2022-05-04 10:35:58 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"unknownCppBaseAssigningToVar.qml"_s, true);
|
2022-03-15 15:43:38 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
// we should not crash here, then it is a success
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:16:09 +00:00
|
|
|
void tst_qqmljsscope::groupedProperties()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"groupProperties.qml"_s);
|
2022-03-18 13:16:09 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QVERIFY(root->hasProperty(u"anchors"_s));
|
|
|
|
const auto anchorBindings = root->propertyBindings(u"anchors"_s);
|
2022-03-18 13:16:09 +00:00
|
|
|
QVERIFY(!anchorBindings.isEmpty());
|
2022-03-29 15:19:36 +00:00
|
|
|
QCOMPARE(anchorBindings.size(), 2); // from type itself and from the base type
|
|
|
|
|
|
|
|
const auto getBindingsWithinGroup =
|
|
|
|
[&](QMultiHash<QString, QQmlJSMetaPropertyBinding> *bindings, qsizetype index) -> void {
|
|
|
|
const auto &binding = anchorBindings[index];
|
|
|
|
QCOMPARE(binding.bindingType(), QQmlJSMetaPropertyBinding::GroupProperty);
|
|
|
|
auto anchorScope = binding.groupType();
|
|
|
|
QVERIFY(anchorScope);
|
|
|
|
*bindings = anchorScope->ownPropertyBindings();
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto value = [](const QMultiHash<QString, QQmlJSMetaPropertyBinding> &bindings,
|
|
|
|
const QString &key) {
|
|
|
|
return bindings.value(key, QQmlJSMetaPropertyBinding(QQmlJS::SourceLocation {}));
|
|
|
|
};
|
|
|
|
|
|
|
|
QMultiHash<QString, QQmlJSMetaPropertyBinding> bindingsOfType;
|
|
|
|
getBindingsWithinGroup(&bindingsOfType, 0);
|
|
|
|
QCOMPARE(bindingsOfType.size(), 2);
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(bindingsOfType, u"left"_s).bindingType(), QQmlJSMetaPropertyBinding::Script);
|
|
|
|
QCOMPARE(value(bindingsOfType, u"leftMargin"_s).bindingType(),
|
2022-03-29 15:19:36 +00:00
|
|
|
QQmlJSMetaPropertyBinding::NumberLiteral);
|
|
|
|
|
|
|
|
QMultiHash<QString, QQmlJSMetaPropertyBinding> bindingsOfBaseType;
|
|
|
|
getBindingsWithinGroup(&bindingsOfBaseType, 1);
|
|
|
|
QCOMPARE(bindingsOfBaseType.size(), 1);
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(bindingsOfBaseType, u"top"_s).bindingType(), QQmlJSMetaPropertyBinding::Script);
|
2022-03-18 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 09:52:25 +00:00
|
|
|
void tst_qqmljsscope::descriptiveNameOfNull()
|
|
|
|
{
|
|
|
|
QQmlJSRegisterContent nullContent;
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(nullContent.descriptiveName(), u"(invalid type)"_s);
|
2022-04-06 09:52:25 +00:00
|
|
|
|
|
|
|
QQmlJSScope::Ptr stored = QQmlJSScope::create();
|
2022-03-21 09:21:18 +00:00
|
|
|
stored->setInternalName(u"bar"_s);
|
2022-04-06 09:52:25 +00:00
|
|
|
QQmlJSMetaProperty property;
|
2022-03-21 09:21:18 +00:00
|
|
|
property.setPropertyName(u"foo"_s);
|
|
|
|
property.setTypeName(u"baz"_s);
|
2022-04-06 09:52:25 +00:00
|
|
|
QQmlJSRegisterContent unscoped = QQmlJSRegisterContent::create(
|
|
|
|
stored, property, QQmlJSRegisterContent::ScopeProperty, QQmlJSScope::ConstPtr());
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(unscoped.descriptiveName(), u"bar of (invalid type)::foo with type baz"_s);
|
2022-04-06 09:52:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 10:50:46 +00:00
|
|
|
void tst_qqmljsscope::groupedPropertiesConsistency()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
QQmlEngine engine;
|
|
|
|
QQmlComponent component(&engine);
|
2022-03-21 09:21:18 +00:00
|
|
|
component.loadUrl(testFileUrl(u"groupPropertiesConsistency.qml"_s));
|
2022-03-30 10:50:46 +00:00
|
|
|
QVERIFY2(component.isReady(), qPrintable(component.errorString()));
|
|
|
|
QScopedPointer<QObject> root(component.create());
|
|
|
|
QVERIFY2(root, qPrintable(component.errorString()));
|
|
|
|
QFont font = qvariant_cast<QFont>(root->property("font"));
|
|
|
|
QCOMPARE(font.pixelSize(), 22);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"groupPropertiesConsistency.qml"_s);
|
2022-03-30 10:50:46 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
const auto fontBindings = root->propertyBindings(u"font"_s);
|
2022-03-30 10:50:46 +00:00
|
|
|
QCOMPARE(fontBindings.size(), 2);
|
|
|
|
|
|
|
|
// The binding order in QQmlJSScope case is "reversed": first come
|
|
|
|
// bindings on the leaf type, followed by the bindings on the base type
|
|
|
|
QCOMPARE(fontBindings[0].bindingType(), QQmlJSMetaPropertyBinding::GroupProperty);
|
|
|
|
QCOMPARE(fontBindings[1].bindingType(), QQmlJSMetaPropertyBinding::Script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:15:32 +00:00
|
|
|
void tst_qqmljsscope::groupedPropertySyntax()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"groupPropertySyntax.qml"_s);
|
2022-03-30 12:15:32 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
const auto fontBindings = root->propertyBindings(u"font"_s);
|
2022-03-30 12:15:32 +00:00
|
|
|
QCOMPARE(fontBindings.size(), 1);
|
|
|
|
|
|
|
|
// The binding order in QQmlJSScope case is "reversed": first come
|
|
|
|
// bindings on the leaf type, followed by the bindings on the base type
|
|
|
|
QCOMPARE(fontBindings[0].bindingType(), QQmlJSMetaPropertyBinding::GroupProperty);
|
|
|
|
auto fontScope = fontBindings[0].groupType();
|
|
|
|
QVERIFY(fontScope);
|
2022-05-25 14:24:24 +00:00
|
|
|
QCOMPARE(fontScope->accessSemantics(), QQmlJSScope::AccessSemantics::Value);
|
2022-03-30 12:15:32 +00:00
|
|
|
auto subbindings = fontScope->ownPropertyBindings();
|
|
|
|
QCOMPARE(subbindings.size(), 2);
|
|
|
|
|
|
|
|
const auto value = [](const QMultiHash<QString, QQmlJSMetaPropertyBinding> &bindings,
|
|
|
|
const QString &key) {
|
|
|
|
return bindings.value(key, QQmlJSMetaPropertyBinding(QQmlJS::SourceLocation {}));
|
|
|
|
};
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(subbindings, u"pixelSize"_s).bindingType(),
|
2022-03-30 12:15:32 +00:00
|
|
|
QQmlJSMetaPropertyBinding::NumberLiteral);
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(subbindings, u"bold"_s).bindingType(), QQmlJSMetaPropertyBinding::BoolLiteral);
|
2022-03-30 12:15:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:16:11 +00:00
|
|
|
void tst_qqmljsscope::attachedProperties()
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"attachedProperties.qml"_s);
|
2022-03-30 15:16:11 +00:00
|
|
|
QVERIFY(root);
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
const auto keysBindings = root->propertyBindings(u"Keys"_s);
|
2022-03-30 15:16:11 +00:00
|
|
|
QVERIFY(!keysBindings.isEmpty());
|
|
|
|
QCOMPARE(keysBindings.size(), 2); // from type itself and from the base type
|
|
|
|
|
|
|
|
const auto getBindingsWithinAttached =
|
|
|
|
[&](QMultiHash<QString, QQmlJSMetaPropertyBinding> *bindings, qsizetype index) -> void {
|
|
|
|
const auto &binding = keysBindings[index];
|
|
|
|
QCOMPARE(binding.bindingType(), QQmlJSMetaPropertyBinding::AttachedProperty);
|
|
|
|
auto keysScope = binding.attachingType();
|
|
|
|
QVERIFY(keysScope);
|
2022-05-25 14:24:24 +00:00
|
|
|
QCOMPARE(keysScope->accessSemantics(), QQmlJSScope::AccessSemantics::Reference);
|
2022-03-30 15:16:11 +00:00
|
|
|
*bindings = keysScope->ownPropertyBindings();
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto value = [](const QMultiHash<QString, QQmlJSMetaPropertyBinding> &bindings,
|
|
|
|
const QString &key) {
|
|
|
|
return bindings.value(key, QQmlJSMetaPropertyBinding(QQmlJS::SourceLocation {}));
|
|
|
|
};
|
|
|
|
|
|
|
|
QMultiHash<QString, QQmlJSMetaPropertyBinding> bindingsOfType;
|
|
|
|
getBindingsWithinAttached(&bindingsOfType, 0);
|
|
|
|
QCOMPARE(bindingsOfType.size(), 2);
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(bindingsOfType, u"enabled"_s).bindingType(),
|
2022-03-30 15:16:11 +00:00
|
|
|
QQmlJSMetaPropertyBinding::BoolLiteral);
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(bindingsOfType, u"forwardTo"_s).bindingType(),
|
2022-03-30 15:16:11 +00:00
|
|
|
QQmlJSMetaPropertyBinding::Script);
|
|
|
|
|
|
|
|
QMultiHash<QString, QQmlJSMetaPropertyBinding> bindingsOfBaseType;
|
|
|
|
getBindingsWithinAttached(&bindingsOfBaseType, 1);
|
|
|
|
QCOMPARE(bindingsOfBaseType.size(), 1);
|
2022-03-21 09:21:18 +00:00
|
|
|
QCOMPARE(value(bindingsOfBaseType, u"priority"_s).bindingType(),
|
2022-03-30 15:16:11 +00:00
|
|
|
QQmlJSMetaPropertyBinding::Script);
|
|
|
|
}
|
|
|
|
|
2022-03-31 15:29:24 +00:00
|
|
|
inline QString getScopeName(const QQmlJSScope::ConstPtr &scope)
|
|
|
|
{
|
|
|
|
Q_ASSERT(scope);
|
|
|
|
QQmlJSScope::ScopeType type = scope->scopeType();
|
|
|
|
if (type == QQmlJSScope::GroupedPropertyScope || type == QQmlJSScope::AttachedPropertyScope)
|
|
|
|
return scope->internalName();
|
|
|
|
return scope->baseTypeName();
|
|
|
|
}
|
|
|
|
|
2022-06-01 14:47:37 +00:00
|
|
|
struct FunctionOrExpressionIdentifier
|
|
|
|
{
|
|
|
|
QString name;
|
|
|
|
QQmlJS::SourceLocation loc = QQmlJS::SourceLocation {}; // source location of owning scope
|
|
|
|
int index = -1; // relative or absolute script index
|
|
|
|
FunctionOrExpressionIdentifier() = default;
|
|
|
|
FunctionOrExpressionIdentifier(const QString &name, const QQmlJS::SourceLocation &l, int i)
|
|
|
|
: name(name), loc(l), index(i)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
FunctionOrExpressionIdentifier(const QString &name, const QV4::CompiledData::Location &l, int i)
|
|
|
|
: name(name), loc(QQmlJS::SourceLocation { 0, 0, l.line(), l.column() }), index(i)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator<(const FunctionOrExpressionIdentifier &x,
|
|
|
|
const FunctionOrExpressionIdentifier &y)
|
|
|
|
{
|
|
|
|
// source location is taken from the owner scope so would be non-unique,
|
|
|
|
// while name must be unique within that scope. so: if source locations
|
|
|
|
// match, compare by name
|
|
|
|
if (x.loc == y.loc)
|
|
|
|
return x.name < y.name;
|
|
|
|
|
|
|
|
// otherwise, compare by start line first, if they match, then by column
|
|
|
|
if (x.loc.startLine == y.loc.startLine)
|
|
|
|
return x.loc.startColumn < y.loc.startColumn;
|
|
|
|
return x.loc.startLine < y.loc.startLine;
|
|
|
|
}
|
|
|
|
friend bool operator==(const FunctionOrExpressionIdentifier &x,
|
|
|
|
const FunctionOrExpressionIdentifier &y)
|
|
|
|
{
|
|
|
|
// equal-compare by name and index
|
|
|
|
return x.index == y.index && x.name == y.name;
|
|
|
|
}
|
|
|
|
friend bool operator!=(const FunctionOrExpressionIdentifier &x,
|
|
|
|
const FunctionOrExpressionIdentifier &y)
|
|
|
|
{
|
|
|
|
return !(x == y);
|
|
|
|
}
|
|
|
|
friend QDebug &operator<<(QDebug &stream, const FunctionOrExpressionIdentifier &x)
|
|
|
|
{
|
|
|
|
const QString dump = u"(%1, %2, loc %3:%4)"_s.arg(x.name, QString::number(x.index),
|
|
|
|
QString::number(x.loc.startLine),
|
|
|
|
QString::number(x.loc.startColumn));
|
|
|
|
stream << dump;
|
|
|
|
return stream.maybeSpace();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-06 07:56:17 +00:00
|
|
|
void tst_qqmljsscope::scriptIndices()
|
2022-03-31 15:29:24 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
QQmlEngine engine;
|
|
|
|
QQmlComponent component(&engine);
|
2022-03-21 09:21:18 +00:00
|
|
|
component.loadUrl(testFileUrl(u"functionAndBindingIndices.qml"_s));
|
2022-03-31 15:29:24 +00:00
|
|
|
QVERIFY2(component.isReady(), qPrintable(component.errorString()));
|
|
|
|
QScopedPointer<QObject> root(component.create());
|
|
|
|
QVERIFY2(root, qPrintable(component.errorString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QmlIR::Document document(false); // we need QmlIR information here
|
2022-03-21 09:21:18 +00:00
|
|
|
QQmlJSScope::ConstPtr root = run(u"functionAndBindingIndices.qml"_s, &document);
|
2022-03-31 15:29:24 +00:00
|
|
|
QVERIFY(root);
|
2022-04-06 07:56:17 +00:00
|
|
|
QVERIFY(document.javaScriptCompilationUnit.unitData());
|
2022-03-31 15:29:24 +00:00
|
|
|
|
2022-04-06 07:56:17 +00:00
|
|
|
// compare QQmlJSScope and QmlIR:
|
|
|
|
|
|
|
|
// {property, function}Name and relative (per-object) function table index
|
2022-06-01 14:47:37 +00:00
|
|
|
QList<FunctionOrExpressionIdentifier> orderedJSScopeExpressionsRelative;
|
|
|
|
QList<FunctionOrExpressionIdentifier> orderedQmlIrExpressionsRelative;
|
2022-04-06 07:56:17 +00:00
|
|
|
// {property, function}Name and absolute (per-document) function table index
|
2022-06-01 14:47:37 +00:00
|
|
|
QList<FunctionOrExpressionIdentifier> orderedJSScopeExpressionsAbsolute;
|
|
|
|
QList<FunctionOrExpressionIdentifier> orderedQmlIrExpressionsAbsolute;
|
2022-04-06 07:56:17 +00:00
|
|
|
|
|
|
|
const auto populateQQmlJSScopeArrays =
|
|
|
|
[&](const QQmlJSScope::ConstPtr &scope, const QString &name,
|
|
|
|
QQmlJSMetaMethod::RelativeFunctionIndex relativeIndex) {
|
2022-06-01 14:47:37 +00:00
|
|
|
orderedJSScopeExpressionsRelative.emplaceBack(name, scope->sourceLocation(),
|
2022-04-06 07:56:17 +00:00
|
|
|
static_cast<int>(relativeIndex));
|
|
|
|
auto absoluteIndex = scope->ownRuntimeFunctionIndex(relativeIndex);
|
2022-06-01 14:47:37 +00:00
|
|
|
orderedJSScopeExpressionsAbsolute.emplaceBack(name, scope->sourceLocation(),
|
2022-04-06 07:56:17 +00:00
|
|
|
static_cast<int>(absoluteIndex));
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto populateQmlIRArrays = [&](const QmlIR::Object *irObject, const QString &name,
|
|
|
|
int relative) {
|
2022-06-01 14:47:37 +00:00
|
|
|
orderedQmlIrExpressionsRelative.emplaceBack(name, irObject->location, relative);
|
2022-04-06 07:56:17 +00:00
|
|
|
auto absolute = irObject->runtimeFunctionIndices.at(relative);
|
2022-06-01 14:47:37 +00:00
|
|
|
orderedQmlIrExpressionsAbsolute.emplaceBack(name, irObject->location, absolute);
|
2022-04-06 07:56:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto suitableScope = [](const QQmlJSScope::ConstPtr &scope) {
|
|
|
|
const auto type = scope->scopeType();
|
|
|
|
return type == QQmlJSScope::QMLScope || type == QQmlJSScope::GroupedPropertyScope
|
|
|
|
|| type == QQmlJSScope::AttachedPropertyScope;
|
|
|
|
};
|
2022-03-31 15:29:24 +00:00
|
|
|
|
|
|
|
QList<QQmlJSScope::ConstPtr> queue;
|
|
|
|
queue.push_back(root);
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
auto current = queue.front();
|
|
|
|
queue.pop_front();
|
|
|
|
|
2022-04-06 07:56:17 +00:00
|
|
|
if (suitableScope(current)) {
|
|
|
|
const auto methods = current->ownMethods();
|
|
|
|
for (const auto &method : methods) {
|
|
|
|
if (method.methodType() == QQmlJSMetaMethod::Signal)
|
|
|
|
continue;
|
|
|
|
QString name = method.methodName();
|
|
|
|
auto relativeIndex = method.jsFunctionIndex();
|
|
|
|
QVERIFY2(static_cast<int>(relativeIndex) >= 0,
|
|
|
|
qPrintable(QStringLiteral("Method %1 from %2 has no index")
|
|
|
|
.arg(name, getScopeName(current))));
|
|
|
|
|
|
|
|
populateQQmlJSScopeArrays(current, name, relativeIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto bindings = current->ownPropertyBindings();
|
|
|
|
for (const auto &binding : bindings) {
|
|
|
|
if (binding.bindingType() != QQmlJSMetaPropertyBinding::Script)
|
|
|
|
continue;
|
|
|
|
QString name = binding.propertyName();
|
|
|
|
auto relativeIndex = binding.scriptIndex();
|
|
|
|
QVERIFY2(static_cast<int>(relativeIndex) >= 0,
|
|
|
|
qPrintable(QStringLiteral("Binding on property %1 from %2 has no index")
|
|
|
|
.arg(name, getScopeName(current))));
|
|
|
|
|
|
|
|
populateQQmlJSScopeArrays(current, name, relativeIndex);
|
|
|
|
}
|
2022-03-31 15:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto children = current->childScopes();
|
|
|
|
for (const auto &c : children)
|
|
|
|
queue.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const QmlIR::Object *irObject : qAsConst(document.objects)) {
|
|
|
|
const QString objectName = document.stringAt(irObject->inheritedTypeNameIndex);
|
|
|
|
for (auto it = irObject->functionsBegin(); it != irObject->functionsEnd(); ++it) {
|
|
|
|
QString name = document.stringAt(it->nameIndex);
|
2022-04-06 07:56:17 +00:00
|
|
|
populateQmlIRArrays(irObject, name, it->index);
|
2022-03-31 15:29:24 +00:00
|
|
|
}
|
|
|
|
for (auto it = irObject->bindingsBegin(); it != irObject->bindingsEnd(); ++it) {
|
2022-05-04 13:26:30 +00:00
|
|
|
if (it->type() != QmlIR::Binding::Type_Script)
|
2022-03-31 15:29:24 +00:00
|
|
|
continue;
|
|
|
|
QString name = document.stringAt(it->propertyNameIndex);
|
|
|
|
int index = it->value.compiledScriptIndex;
|
2022-04-06 07:56:17 +00:00
|
|
|
populateQmlIRArrays(irObject, name, index);
|
2022-03-31 15:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 14:47:37 +00:00
|
|
|
std::sort(orderedJSScopeExpressionsRelative.begin(), orderedJSScopeExpressionsRelative.end());
|
|
|
|
std::sort(orderedQmlIrExpressionsRelative.begin(), orderedQmlIrExpressionsRelative.end());
|
2022-03-31 15:29:24 +00:00
|
|
|
|
2022-06-01 14:47:37 +00:00
|
|
|
std::sort(orderedJSScopeExpressionsAbsolute.begin(), orderedJSScopeExpressionsAbsolute.end());
|
|
|
|
std::sort(orderedQmlIrExpressionsAbsolute.begin(), orderedQmlIrExpressionsAbsolute.end());
|
2022-04-06 07:56:17 +00:00
|
|
|
QCOMPARE(orderedJSScopeExpressionsRelative, orderedQmlIrExpressionsRelative);
|
|
|
|
QCOMPARE(orderedJSScopeExpressionsAbsolute, orderedQmlIrExpressionsAbsolute);
|
2022-03-31 15:29:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 15:08:37 +00:00
|
|
|
void tst_qqmljsscope::extensions()
|
|
|
|
{
|
|
|
|
QQmlJSScope::ConstPtr root = run(u"extensions.qml"_s);
|
|
|
|
QVERIFY(root);
|
|
|
|
QVERIFY(root->isFullyResolved());
|
|
|
|
|
|
|
|
const auto childScopes = root->childScopes();
|
2022-05-06 12:48:22 +00:00
|
|
|
QCOMPARE(childScopes.size(), 5);
|
2022-05-03 15:08:37 +00:00
|
|
|
|
|
|
|
QCOMPARE(childScopes[0]->baseTypeName(), u"Extended"_s);
|
|
|
|
QCOMPARE(childScopes[1]->baseTypeName(), u"ExtendedIndirect"_s);
|
|
|
|
QCOMPARE(childScopes[2]->baseTypeName(), u"ExtendedTwice"_s);
|
2022-05-06 12:48:22 +00:00
|
|
|
QCOMPARE(childScopes[3]->baseTypeName(), u"NamespaceExtended"_s);
|
|
|
|
QCOMPARE(childScopes[4]->baseTypeName(), u"NonNamespaceExtended"_s);
|
2022-05-03 15:08:37 +00:00
|
|
|
QVERIFY(childScopes[0]->isFullyResolved());
|
|
|
|
QVERIFY(childScopes[1]->isFullyResolved());
|
|
|
|
QVERIFY(childScopes[2]->isFullyResolved());
|
2022-05-06 12:48:22 +00:00
|
|
|
QVERIFY(childScopes[3]->isFullyResolved());
|
|
|
|
QVERIFY(childScopes[4]->isFullyResolved());
|
2022-05-03 15:08:37 +00:00
|
|
|
|
|
|
|
QCOMPARE(childScopes[0]->property(u"count"_s).typeName(), u"int"_s);
|
|
|
|
QCOMPARE(childScopes[1]->property(u"count"_s).typeName(), u"double"_s);
|
|
|
|
QCOMPARE(childScopes[2]->property(u"count"_s).typeName(), u"int"_s);
|
|
|
|
QCOMPARE(childScopes[2]->property(u"str"_s).typeName(), u"QString"_s);
|
2022-05-06 12:48:22 +00:00
|
|
|
|
|
|
|
QVERIFY(!childScopes[3]->hasProperty(u"count"_s));
|
|
|
|
QVERIFY(!childScopes[3]->property(u"count"_s).isValid());
|
|
|
|
QVERIFY(!childScopes[3]->hasProperty(u"p"_s));
|
|
|
|
QVERIFY(!childScopes[3]->property(u"p"_s).isValid());
|
|
|
|
QVERIFY(!childScopes[3]->hasMethod(u"someMethod"_s));
|
|
|
|
QVERIFY(childScopes[3]->hasEnumeration(u"ExtensionEnum"_s));
|
|
|
|
QVERIFY(childScopes[3]->hasEnumerationKey(u"Value1"_s));
|
|
|
|
QVERIFY(childScopes[3]->enumeration(u"ExtensionEnum"_s).isValid());
|
|
|
|
QCOMPARE(childScopes[3]->defaultPropertyName(), u"objectName"_s);
|
|
|
|
QCOMPARE(childScopes[3]->parentPropertyName(), u"p"_s);
|
|
|
|
QVERIFY(!childScopes[3]->hasInterface(u"QQmlParserStatus"_s));
|
|
|
|
QCOMPARE(childScopes[3]->attachedTypeName(), QString());
|
|
|
|
QVERIFY(!childScopes[3]->attachedType());
|
|
|
|
|
|
|
|
QVERIFY(!childScopes[3]->extensionIsNamespace());
|
|
|
|
QVERIFY(childScopes[3]->baseType()->extensionIsNamespace());
|
|
|
|
|
|
|
|
QVERIFY(childScopes[4]->hasProperty(u"count"_s));
|
|
|
|
QVERIFY(childScopes[4]->property(u"count"_s).isValid());
|
|
|
|
QVERIFY(childScopes[4]->hasProperty(u"p"_s));
|
|
|
|
QVERIFY(childScopes[4]->property(u"p"_s).isValid());
|
|
|
|
QVERIFY(childScopes[4]->hasMethod(u"someMethod"_s));
|
|
|
|
QVERIFY(childScopes[4]->hasEnumeration(u"ExtensionEnum"_s));
|
|
|
|
QVERIFY(childScopes[4]->hasEnumerationKey(u"Value1"_s));
|
|
|
|
QVERIFY(childScopes[4]->enumeration(u"ExtensionEnum"_s).isValid());
|
|
|
|
QCOMPARE(childScopes[4]->defaultPropertyName(), u"objectName"_s);
|
|
|
|
QCOMPARE(childScopes[4]->parentPropertyName(), u"p"_s);
|
|
|
|
QVERIFY(!childScopes[4]->hasInterface(u"QQmlParserStatus"_s));
|
|
|
|
QCOMPARE(childScopes[4]->attachedTypeName(), QString());
|
|
|
|
QVERIFY(!childScopes[4]->attachedType());
|
|
|
|
|
|
|
|
auto [owner, ownerKind] = QQmlJSScope::ownerOfProperty(childScopes[4], u"count"_s);
|
|
|
|
QVERIFY(owner);
|
|
|
|
QCOMPARE(ownerKind, QQmlJSScope::ExtensionType);
|
|
|
|
QCOMPARE(owner, childScopes[4]->baseType()->extensionType().scope);
|
2022-05-03 15:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 13:41:24 +00:00
|
|
|
void tst_qqmljsscope::emptyBlockBinding()
|
|
|
|
{
|
|
|
|
QQmlJSScope::ConstPtr root = run(u"emptyBlockBinding.qml"_s);
|
|
|
|
QVERIFY(root);
|
|
|
|
QVERIFY(root->hasOwnPropertyBindings(u"x"_s));
|
|
|
|
QVERIFY(root->hasOwnPropertyBindings(u"y"_s));
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:35:20 +00:00
|
|
|
void tst_qqmljsscope::qualifiedName()
|
|
|
|
{
|
|
|
|
QQmlJSScope::ConstPtr root = run(u"qualifiedName.qml"_s);
|
|
|
|
|
|
|
|
auto qualifiedNameOf = [](const QQmlJSScope::ConstPtr &ptr) {
|
|
|
|
return ptr->baseType()->qualifiedName();
|
|
|
|
};
|
|
|
|
|
|
|
|
QQmlJSScope::ConstPtr item = root;
|
|
|
|
|
|
|
|
// normal case
|
|
|
|
QCOMPARE(qualifiedNameOf(item), "QtQuick/Item 2.0-6.3");
|
|
|
|
|
|
|
|
QCOMPARE(item->childScopes().size(), 4);
|
|
|
|
QQmlJSScope::ConstPtr textInItem = item->childScopes()[0];
|
|
|
|
QQmlJSScope::ConstPtr nonQualifiedComponentInItem = item->childScopes()[1];
|
|
|
|
QQmlJSScope::ConstPtr qualifiedComponentInItem = item->childScopes()[2];
|
|
|
|
QQmlJSScope::ConstPtr componentInItem = item->childScopes()[3];
|
|
|
|
|
|
|
|
// qualified case
|
|
|
|
QCOMPARE(qualifiedNameOf(nonQualifiedComponentInItem), "QtQuick/TextEdit 2.0-6.3");
|
|
|
|
|
|
|
|
// qualified case
|
|
|
|
QCOMPARE(qualifiedNameOf(qualifiedComponentInItem), "QtQuick/TextEdit 2.0-6.3");
|
|
|
|
|
|
|
|
// normal case
|
|
|
|
QCOMPARE(qualifiedNameOf(textInItem), "QtQuick/Text 2.0-6.3");
|
|
|
|
// qualified import of builtin variable
|
|
|
|
QCOMPARE(qualifiedNameOf(componentInItem), "QML/Component 1.0");
|
|
|
|
QCOMPARE(componentInItem->baseType()->moduleName(), "QML");
|
|
|
|
|
|
|
|
QCOMPARE(componentInItem->childScopes().size(), 1);
|
|
|
|
|
|
|
|
QQmlJSScope::ConstPtr itemInComponent = componentInItem->childScopes()[0];
|
|
|
|
|
|
|
|
QCOMPARE(qualifiedNameOf(itemInComponent), "QtQuick/Item 2.0-6.3");
|
|
|
|
|
|
|
|
QCOMPARE(itemInComponent->childScopes().size(), 5);
|
|
|
|
QQmlJSScope::ConstPtr qualifiedImportTextInItemInComponent = itemInComponent->childScopes()[0];
|
|
|
|
QQmlJSScope::ConstPtr textInItemInComponent = itemInComponent->childScopes()[1];
|
|
|
|
QQmlJSScope::ConstPtr qualifiedImportTextInputInItemInComponent =
|
|
|
|
itemInComponent->childScopes()[2];
|
|
|
|
QQmlJSScope::ConstPtr indirectImportTimerInItemInComponent = itemInComponent->childScopes()[3];
|
|
|
|
QQmlJSScope::ConstPtr qualifiedImportTimerInItemInComponent = itemInComponent->childScopes()[4];
|
|
|
|
|
|
|
|
QCOMPARE(qualifiedNameOf(qualifiedImportTextInItemInComponent), "QtQuick/Text 2.0-6.3");
|
|
|
|
QCOMPARE(qualifiedNameOf(textInItemInComponent), "QtQuick/Text 2.0-6.3");
|
|
|
|
QCOMPARE(textInItemInComponent->baseType()->moduleName(), "QtQuick");
|
|
|
|
QCOMPARE(qualifiedImportTextInItemInComponent->baseType()->moduleName(), "QtQuick");
|
|
|
|
|
|
|
|
QCOMPARE(qualifiedNameOf(qualifiedImportTextInputInItemInComponent),
|
|
|
|
"QtQuick/TextInput 2.0-6.3");
|
|
|
|
|
|
|
|
QCOMPARE(qualifiedNameOf(indirectImportTimerInItemInComponent), "QtQml/Timer 2.0-6.0");
|
|
|
|
QCOMPARE(qualifiedNameOf(qualifiedImportTimerInItemInComponent), "QtQml/Timer 2.0-6.0");
|
|
|
|
QCOMPARE(indirectImportTimerInItemInComponent->baseType()->moduleName(), "QtQml");
|
|
|
|
QCOMPARE(qualifiedImportTimerInItemInComponent->baseType()->moduleName(), "QtQml");
|
|
|
|
}
|
|
|
|
|
2022-05-25 13:18:56 +00:00
|
|
|
void tst_qqmljsscope::resolvedNonUniqueScopes()
|
|
|
|
{
|
|
|
|
QQmlJSScope::ConstPtr root = run(u"resolvedNonUniqueScope.qml"_s);
|
|
|
|
QVERIFY(root);
|
|
|
|
|
|
|
|
const auto value = [](const QMultiHash<QString, QQmlJSMetaPropertyBinding> &bindings,
|
|
|
|
const QString &key) {
|
|
|
|
return bindings.value(key, QQmlJSMetaPropertyBinding(QQmlJS::SourceLocation {}));
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
auto topLevelBindings = root->propertyBindings(u"Component"_s);
|
|
|
|
QCOMPARE(topLevelBindings.size(), 1);
|
|
|
|
QCOMPARE(topLevelBindings[0].bindingType(), QQmlJSMetaPropertyBinding::AttachedProperty);
|
|
|
|
auto componentScope = topLevelBindings[0].attachingType();
|
|
|
|
auto componentBindings = componentScope->ownPropertyBindings();
|
|
|
|
QCOMPARE(componentBindings.size(), 2);
|
|
|
|
auto onCompletedBinding = value(componentBindings, u"onCompleted"_s);
|
|
|
|
QVERIFY(onCompletedBinding.isValid());
|
|
|
|
QCOMPARE(onCompletedBinding.bindingType(), QQmlJSMetaPropertyBinding::Script);
|
|
|
|
QCOMPARE(onCompletedBinding.scriptKind(), QQmlJSMetaPropertyBinding::Script_SignalHandler);
|
|
|
|
auto onDestructionBinding = value(componentBindings, u"onDestruction"_s);
|
|
|
|
QVERIFY(onDestructionBinding.isValid());
|
|
|
|
QCOMPARE(onDestructionBinding.bindingType(), QQmlJSMetaPropertyBinding::Script);
|
|
|
|
QCOMPARE(onDestructionBinding.scriptKind(),
|
|
|
|
QQmlJSMetaPropertyBinding::Script_SignalHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto topLevelBindings = root->propertyBindings(u"p"_s);
|
|
|
|
QCOMPARE(topLevelBindings.size(), 1);
|
|
|
|
QCOMPARE(topLevelBindings[0].bindingType(), QQmlJSMetaPropertyBinding::GroupProperty);
|
|
|
|
auto pScope = topLevelBindings[0].groupType();
|
|
|
|
auto pBindings = pScope->ownPropertyBindings();
|
|
|
|
QCOMPARE(pBindings.size(), 1);
|
|
|
|
auto onXChangedBinding = value(pBindings, u"onXChanged"_s);
|
|
|
|
QVERIFY(onXChangedBinding.isValid());
|
|
|
|
QCOMPARE(onXChangedBinding.bindingType(), QQmlJSMetaPropertyBinding::Script);
|
|
|
|
QCOMPARE(onXChangedBinding.scriptKind(), QQmlJSMetaPropertyBinding::Script_SignalHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:44:58 +00:00
|
|
|
QTEST_MAIN(tst_qqmljsscope)
|
|
|
|
#include "tst_qqmljsscope.moc"
|