1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of Qt for Python.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "testcodeinjection.h"
30 #include <QFileInfo>
31 #include <QDir>
32 #include <QtTest/QTest>
33 #include "testutil.h"
34 #include <abstractmetalang.h>
35 #include <typesystem.h>
36
testReadFile_data()37 void TestCodeInjections::testReadFile_data()
38 {
39 QTest::addColumn<QString>("filePath");
40 QTest::addColumn<QString>("snippet");
41 QTest::addColumn<QString>("expected");
42
43 QTest::newRow("utf8")
44 << QString::fromLatin1(":/utf8code.txt")
45 << QString()
46 << QString::fromUtf8("\xC3\xA1\xC3\xA9\xC3\xAD\xC3\xB3\xC3\xBA");
47
48 QTest::newRow("snippet")
49 << QString::fromLatin1(":/injectedcode.txt")
50 << QString::fromLatin1("label")
51 << QString::fromLatin1("code line");
52 }
53
testReadFile()54 void TestCodeInjections::testReadFile()
55 {
56 QFETCH(QString, filePath);
57 QFETCH(QString, snippet);
58 QFETCH(QString, expected);
59
60 const char* cppCode ="struct A {};\n";
61 int argc = 0;
62 char *argv[] = {nullptr};
63 QCoreApplication app(argc, argv);
64
65 QString attribute = QLatin1String("file='") + filePath + QLatin1Char('\'');
66 if (!snippet.isEmpty())
67 attribute += QLatin1String(" snippet='") + snippet + QLatin1Char('\'');
68
69 QString xmlCode = QLatin1String("\
70 <typesystem package=\"Foo\">\n\
71 <value-type name='A'>\n\
72 <conversion-rule ") + attribute + QLatin1String("/>\n\
73 <inject-code class='target' ") + attribute + QLatin1String("/>\n\
74 <value-type name='B'/>\n\
75 </value-type>\n\
76 </typesystem>\n");
77 QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode.toLocal8Bit().constData()));
78 QVERIFY(!builder.isNull());
79 AbstractMetaClassList classes = builder->classes();
80 const AbstractMetaClass *classA = AbstractMetaClass::findClass(classes, QLatin1String("A"));
81 QCOMPARE(classA->typeEntry()->codeSnips().count(), 1);
82 QString code = classA->typeEntry()->codeSnips().constFirst().code();
83 QVERIFY(code.indexOf(expected) != -1);
84 code = classA->typeEntry()->conversionRule();
85 QVERIFY(code.indexOf(expected) != -1);
86 }
87
testInjectWithValidApiVersion()88 void TestCodeInjections::testInjectWithValidApiVersion()
89 {
90 const char* cppCode ="struct A {};\n";
91 const char* xmlCode = "\
92 <typesystem package='Foo'>\n\
93 <value-type name='A'>\n\
94 <inject-code class='target' since='1.0'>\n\
95 test Inject code\n\
96 </inject-code>\n\
97 </value-type>\n\
98 </typesystem>\n";
99
100 QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode,
101 true, QLatin1String("1.0")));
102 QVERIFY(!builder.isNull());
103 AbstractMetaClassList classes = builder->classes();
104 AbstractMetaClass* classA = AbstractMetaClass::findClass(classes, QLatin1String("A"));
105 QCOMPARE(classA->typeEntry()->codeSnips().count(), 1);
106 }
107
testInjectWithInvalidApiVersion()108 void TestCodeInjections::testInjectWithInvalidApiVersion()
109 {
110 const char* cppCode ="struct A {};\n";
111 const char* xmlCode = "\
112 <typesystem package=\"Foo\">\n\
113 <value-type name='A'>\n\
114 <inject-code class='target' since='1.0'>\n\
115 test Inject code\n\
116 </inject-code>\n\
117 </value-type>\n\
118 </typesystem>\n";
119
120 QScopedPointer<AbstractMetaBuilder> builder(TestUtil::parse(cppCode, xmlCode,
121 true, QLatin1String("0.1")));
122 QVERIFY(!builder.isNull());
123
124 AbstractMetaClassList classes = builder->classes();
125 const AbstractMetaClass *classA = AbstractMetaClass::findClass(classes, QLatin1String("A"));
126 QCOMPARE(classA->typeEntry()->codeSnips().count(), 0);
127 }
128
129
130
131 QTEST_APPLESS_MAIN(TestCodeInjections)
132