diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-22 08:12:06 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-25 15:13:31 +0200 |
commit | f863ce7cc68a4def0fe9929c7f50e4e101fd8a1e (patch) | |
tree | dd1fb01e1e544e3aa8b84070e6128f22986e95c5 | |
parent | 3e466bd4b4b21e288c04da61c5d44f6f0288e0d4 (diff) |
shiboken6: Add a test for fixing of default values in AbstractMetaBuilder
Extract it to AbstractMetaBuilder and add a test similar to the enum
test added by 5888236a23f277e1f795052d5ed93a0430d834a4.
Task-number: PYSIDE-1691
Pick-to: 6.2
Change-Id: I636c1ec82486f84da9ccd4bf941054ec47d41290
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
5 files changed, 115 insertions, 10 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp b/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp index 92ffe054d..d365a285a 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp @@ -2065,7 +2065,7 @@ AbstractMetaFunction *AbstractMetaBuilderPrivate::traverseFunction(const Functio AbstractMetaArgument &metaArg = metaArguments[i]; const QString originalDefaultExpression = - fixDefaultValue(arg, metaArg.type(), currentClass, i); + fixDefaultValue(arg->defaultValueExpression(), metaArg.type(), currentClass); metaArg.setOriginalDefaultValueExpression(originalDefaultExpression); metaArg.setDefaultValueExpression(originalDefaultExpression); @@ -2538,12 +2538,10 @@ QString AbstractMetaBuilder::fixEnumDefault(const AbstractMetaType &type, return d->fixEnumDefault(type, expr); } -QString AbstractMetaBuilderPrivate::fixDefaultValue(const ArgumentModelItem &item, - const AbstractMetaType &type, - const AbstractMetaClass *implementingClass, - int /* argumentIndex */) const +// see TestResolveType::testFixDefaultArguments() +QString AbstractMetaBuilderPrivate::fixDefaultValue(QString expr, const AbstractMetaType &type, + const AbstractMetaClass *implementingClass) const { - QString expr = item->defaultValueExpression(); if (expr.isEmpty() || expr == u"{}" || expr == u"nullptr" || expr == u"NULL") return expr; @@ -2614,6 +2612,12 @@ QString AbstractMetaBuilderPrivate::fixDefaultValue(const ArgumentModelItem &ite return expr; } +QString AbstractMetaBuilder::fixDefaultValue(const QString &expr, const AbstractMetaType &type, + const AbstractMetaClass *c) const +{ + return d->fixDefaultValue(expr, type, c); +} + bool AbstractMetaBuilderPrivate::isEnum(const FileModelItem &dom, const QStringList& qualified_name) { CodeModelItem item = dom->model()->findItem(qualified_name, dom); diff --git a/sources/shiboken6/ApiExtractor/abstractmetabuilder.h b/sources/shiboken6/ApiExtractor/abstractmetabuilder.h index 022c29cdd..06c8c0bc4 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetabuilder.h +++ b/sources/shiboken6/ApiExtractor/abstractmetabuilder.h @@ -113,8 +113,11 @@ public: QStringView value); // For testing purposes + QString fixDefaultValue(const QString &expr, const AbstractMetaType &type, + const AbstractMetaClass *) const; QString fixEnumDefault(const AbstractMetaType &type, const QString &expr) const; + #ifndef QT_NO_DEBUG_STREAM void formatDebug(QDebug &d) const; #endif diff --git a/sources/shiboken6/ApiExtractor/abstractmetabuilder_p.h b/sources/shiboken6/ApiExtractor/abstractmetabuilder_p.h index a52b4bf9d..06203ba5a 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetabuilder_p.h +++ b/sources/shiboken6/ApiExtractor/abstractmetabuilder_p.h @@ -149,10 +149,8 @@ public: static void setupFunctionDefaults(AbstractMetaFunction *metaFunction, AbstractMetaClass *metaClass); - QString fixDefaultValue(const ArgumentModelItem &item, - const AbstractMetaType &type, - const AbstractMetaClass *, - int argumentIndex) const; + QString fixDefaultValue(QString expr, const AbstractMetaType &type, + const AbstractMetaClass *) const; QString fixEnumDefault(const AbstractMetaType &type, const QString &expr) const; std::optional<AbstractMetaType> diff --git a/sources/shiboken6/ApiExtractor/tests/testresolvetype.cpp b/sources/shiboken6/ApiExtractor/tests/testresolvetype.cpp index 4c2930234..89d050a21 100644 --- a/sources/shiboken6/ApiExtractor/tests/testresolvetype.cpp +++ b/sources/shiboken6/ApiExtractor/tests/testresolvetype.cpp @@ -29,7 +29,9 @@ #include "testresolvetype.h" #include <QtTest/QTest> #include "testutil.h" +#include <abstractmetafunction.h> #include <abstractmetalang.h> +#include <abstractmetatype.h> #include <typesystem.h> void TestResolveType::testResolveReturnTypeFromParentScope() @@ -63,5 +65,101 @@ void TestResolveType::testResolveReturnTypeFromParentScope() QVERIFY(meth); } +// Helper classes and functions for testing default value fixing. +// Put the AbstractMetaBuilder into test fixture struct to avoid having +// to re-parse for each data row. + +struct DefaultValuesFixture +{ + QSharedPointer<AbstractMetaBuilder> builder; + + AbstractMetaType intType; + AbstractMetaType stringType; + AbstractMetaType classType; + const AbstractMetaClass *klass{}; +}; + +Q_DECLARE_METATYPE(DefaultValuesFixture) +Q_DECLARE_METATYPE(AbstractMetaType) + +static int populateDefaultValuesFixture(DefaultValuesFixture *fixture) +{ + static const char cppCode[] =R"( +#include <string> +namespace Namespace { +class Test +{ +public: + explicit Test(int x = INT_FIELD_1); + explicit Test(const std::string &t = std::string(CHAR_FIELD_1)); + + static const int INT_FIELD_1 = 42; + static const char *CHAR_FIELD_1; +}; +} // Namespace +)"; + static const char xmlCode[] = R"( +<typesystem package="Foo"> + <namespace-type name='Namespace'> + <value-type name='Test'/> + </namespace-type> +</typesystem> +)"; + + fixture->builder.reset(TestUtil::parse(cppCode, xmlCode, false)); + if (fixture->builder.isNull()) + return -1; + + for (const auto &klass : fixture->builder->classes()) { + if (klass->name() == u"Test") { + fixture->klass = klass; + break; + } + } + if (!fixture->klass) + return -2; + + fixture->classType = AbstractMetaType(fixture->klass->typeEntry()); + fixture->classType.decideUsagePattern(); + + for (const auto &f : fixture->klass->findFunctions(u"Test"_qs)) { + if (f->arguments().size() != 1) + return -3; + const auto type = f->arguments().constFirst().type(); + if (type.name() == u"int") + fixture->intType = type; + else + fixture->stringType = type; + } + return 0; +} + +void TestResolveType::testFixDefaultArguments_data() +{ + DefaultValuesFixture fixture; + const int setupOk = populateDefaultValuesFixture(&fixture); + + QTest::addColumn<DefaultValuesFixture>("fixture"); + QTest::addColumn<int>("setupOk"); // To verify setup + QTest::addColumn<AbstractMetaType>("metaType"); // Type and parameters for fixup + QTest::addColumn<QString>("input"); + QTest::addColumn<QString>("expected"); + + QTest::newRow("int") << fixture << setupOk + << fixture.intType << "1" << "1"; +} + +void TestResolveType::testFixDefaultArguments() +{ + QFETCH(DefaultValuesFixture, fixture); + QFETCH(int, setupOk); + QFETCH(AbstractMetaType, metaType); + QFETCH(QString, input); + QFETCH(QString, expected); + QCOMPARE(setupOk, 0); + const QString actual = fixture.builder->fixDefaultValue(input, metaType, fixture.klass); + QCOMPARE(actual, expected); +} + QTEST_APPLESS_MAIN(TestResolveType) diff --git a/sources/shiboken6/ApiExtractor/tests/testresolvetype.h b/sources/shiboken6/ApiExtractor/tests/testresolvetype.h index 62c08bcd7..1c6f654d0 100644 --- a/sources/shiboken6/ApiExtractor/tests/testresolvetype.h +++ b/sources/shiboken6/ApiExtractor/tests/testresolvetype.h @@ -36,6 +36,8 @@ class TestResolveType : public QObject Q_OBJECT private slots: void testResolveReturnTypeFromParentScope(); + void testFixDefaultArguments_data(); + void testFixDefaultArguments(); }; #endif |