aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/tests/testabstractmetatype.cpp
blob: 2c320c87434ee5f48dd3432cf5c1a76deafd0a1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "testabstractmetatype.h"
#include "testutil.h"
#include <abstractmetaargument.h>
#include <abstractmetafunction.h>
#include <abstractmetalang.h>
#include <abstractmetatype.h>
#include <typesystem.h>
#include <parser/codemodel.h>
#include <typeparser.h>

#include <qtcompat.h>

#include <QtTest/QTest>

using namespace Qt::StringLiterals;

void TestAbstractMetaType::parsing_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<QString>("output");
    QTest::newRow("primitive")
        << QString::fromLatin1("int") << QString::fromLatin1("int");
    QTest::newRow("ref")
        << QString::fromLatin1("int &") << QString::fromLatin1("int&");
    QTest::newRow("pointer")
        << QString::fromLatin1("int **") << QString::fromLatin1("int**");
    QTest::newRow("const ref")
        << QString::fromLatin1("const int &") << QString::fromLatin1("const int&");
    QTest::newRow("const pointer")
        << QString::fromLatin1("const int **") << QString::fromLatin1("const int**");
    QTest::newRow("const pointer const")
        << QString::fromLatin1("const int *const*") << QString::fromLatin1("const int*const*");
}

void TestAbstractMetaType::parsing()
{
    QFETCH(QString, input);
    QFETCH(QString, output);
    QString errorMessage;
    const TypeInfo ti = TypeParser::parse(input, &errorMessage);
    QVERIFY2(errorMessage.isEmpty(), qPrintable(errorMessage));
    const QString actual = ti.toString();
    QCOMPARE(actual, output);
}

void TestAbstractMetaType::testConstCharPtrType()
{
    const char cppCode[] = "const char* justAtest();\n";
    const char xmlCode[] = "<typesystem package=\"Foo\">\n\
        <primitive-type name='char'/>\n\
        <function signature='justAtest()' />\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode));
    QVERIFY(builder);
    QCOMPARE(builder->globalFunctions().size(), 1);
    const auto func = builder->globalFunctions().constFirst();
    AbstractMetaType rtype = func->type();
    // Test properties of const char*
    QVERIFY(!rtype.isVoid());
    QCOMPARE(rtype.package(), u"Foo");
    QCOMPARE(rtype.name(), u"char");
    QVERIFY(rtype.isConstant());
    QVERIFY(!rtype.isArray());
    QVERIFY(!rtype.isContainer());
    QVERIFY(!rtype.isObject());
    QVERIFY(!rtype.isPrimitive()); // const char* differs from char, so it's not considered a primitive type by apiextractor
    QVERIFY(rtype.isNativePointer());
    QCOMPARE(rtype.referenceType(), NoReference);
    QVERIFY(!rtype.isValue());
    QVERIFY(!rtype.isValuePointer());
}

void TestAbstractMetaType::testApiVersionSupported()
{
    const char cppCode[] = "class foo {}; class foo2 {};\n\
                          void justAtest(); void justAtest3();\n";
    const char xmlCode[] = "<typesystem package='Foo'>\n\
        <value-type name='foo' since='0.1'/>\n\
        <value-type name='foo2' since='1.0'/>\n\
        <value-type name='foo3' since='1.1'/>\n\
        <function signature='justAtest()' since='0.1'/>\n\
        <function signature='justAtest2()' since='1.1'/>\n\
        <function signature='justAtest3()'/>\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode,
                                                                false, u"1.0"_s));
    QVERIFY(builder);

    AbstractMetaClassList classes = builder->classes();
    QCOMPARE(classes.size(), 2);


    QCOMPARE(builder->globalFunctions().size(), 2);
}


void TestAbstractMetaType::testApiVersionNotSupported()
{
    const char cppCode[] = "class object {};\n";
    const char xmlCode[] = "<typesystem package='Foo'>\n\
        <value-type name='object' since='0.1'/>\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode,
                                                                true, u"0.1"_s));
    QVERIFY(builder);

    AbstractMetaClassList classes = builder->classes();
    QCOMPARE(classes.size(), 1);
}

void TestAbstractMetaType::testCharType()
{
    const char cppCode[] = "char justAtest(); class A {};\n";
    const char xmlCode[] = "<typesystem package=\"Foo\">\n\
    <primitive-type name='char'/>\n\
    <value-type name='A'/>\n\
    <function signature='justAtest()'/>\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode));
    QVERIFY(builder);

    AbstractMetaClassList classes = builder->classes();
    QCOMPARE(classes.size(), 1);
    QCOMPARE(classes.constFirst()->package(), u"Foo");

    const auto functions = builder->globalFunctions();
    QCOMPARE(functions.size(), 1);
    const auto func = functions.constFirst();
    AbstractMetaType rtype = func->type();
    // Test properties of const char*
    QVERIFY(!rtype.isVoid());
    QCOMPARE(rtype.package(), u"Foo");
    QCOMPARE(rtype.name(), u"char");
    QVERIFY(!rtype.isConstant());
    QVERIFY(!rtype.isArray());
    QVERIFY(!rtype.isContainer());
    QVERIFY(!rtype.isObject());
    QVERIFY(rtype.isPrimitive());
    QVERIFY(!rtype.isNativePointer());
    QCOMPARE(rtype.referenceType(), NoReference);
    QVERIFY(!rtype.isValue());
    QVERIFY(!rtype.isValuePointer());
}

void TestAbstractMetaType::testTypedef()
{
    const char cppCode[] = "\
    struct A {\n\
        void someMethod();\n\
    };\n\
    typedef A B;\n\
    typedef B C;\n";
    const char xmlCode[] = "<typesystem package=\"Foo\">\n\
    <value-type name='C' />\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode));
    QVERIFY(builder);

    AbstractMetaClassList classes = builder->classes();
    QCOMPARE(classes.size(), 1);
    const auto c = AbstractMetaClass::findClass(classes, "C");
    QVERIFY(c);
    QVERIFY(c->isTypeDef());
}

void TestAbstractMetaType::testTypedefWithTemplates()
{
    const char cppCode[] = "\
    template<typename T>\n\
    class A {};\n\
    \n\
    class B {};\n\
    typedef A<B> C;\n\
    \n\
    void func(C c);\n";
    const char xmlCode[] = "<typesystem package=\"Foo\">\n\
    <container-type name='A' type='list'/>\n\
    <value-type name='B' />\n\
    <function signature='func(A&lt;B&gt;)'/>\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode));
    QVERIFY(builder);

    AbstractMetaClassList classes = builder->classes();
    QCOMPARE(classes.size(), 1);
    const auto functions = builder->globalFunctions();
    QCOMPARE(functions.size(), 1);
    const auto function = functions.constFirst();
    AbstractMetaArgumentList args = function->arguments();
    QCOMPARE(args.size(), 1);
    const AbstractMetaArgument &arg = args.constFirst();
    AbstractMetaType metaType = arg.type();
    QCOMPARE(metaType.cppSignature(), u"A<B>");
}


void TestAbstractMetaType::testObjectTypeUsedAsValue()
{
    const char cppCode[] = "\
    class A {\n\
        void method(A);\n\
    };\n";
    const char xmlCode[] = "<typesystem package='Foo'>\n\
    <object-type name='A'/>\n\
    </typesystem>\n";
    QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode));
    QVERIFY(builder);

    AbstractMetaClassList classes = builder->classes();
    QCOMPARE(classes.size(), 1);
    const auto classA = AbstractMetaClass::findClass(classes, "A");
    QVERIFY(classA);
    const auto overloads = classA->queryFunctionsByName(u"method"_s);
    QCOMPARE(overloads.size(), 1);
    const auto method = overloads.constFirst();
    QVERIFY(method);
    AbstractMetaArgumentList args = method->arguments();
    QCOMPARE(args.size(), 1);
    const AbstractMetaArgument &arg = args.constFirst();
    AbstractMetaType metaType = arg.type();
    QCOMPARE(metaType.cppSignature(), u"A");
    QVERIFY(metaType.isValue());
    QVERIFY(metaType.typeEntry()->isObject());
}

QTEST_APPLESS_MAIN(TestAbstractMetaType)