Skip to content

Commit e648d0b

Browse files
Marcelo Lirahugopl
Marcelo Lira
authored andcommitted
Fixed insert-template tag when used inside a module level inject-code tag.
Also added unit tests. Reviewed by Hugo Parente <[email protected]> Reviewed by Luciano Wolf <[email protected]>
1 parent 5e50091 commit e648d0b

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ declare_test(testenum)
2727
declare_test(testextrainclude)
2828
declare_test(testfunctiontag)
2929
declare_test(testimplicitconversions)
30+
declare_test(testinserttemplate)
3031
declare_test(testmodifyfunction)
3132
declare_test(testmultipleinheritance)
3233
declare_test(testnamespace)

tests/testinserttemplate.cpp

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* This file is part of the API Extractor project.
3+
*
4+
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5+
*
6+
* Contact: PySide team <[email protected]>
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* version 2 as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful, but
13+
* WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA
21+
*
22+
*/
23+
24+
#include "testinserttemplate.h"
25+
#include <QtTest/QTest>
26+
#include "testutil.h"
27+
28+
void TestInsertTemplate::testInsertTemplateOnClassInjectCode()
29+
{
30+
const char* cppCode ="struct A{};";
31+
const char* xmlCode = "\
32+
<typesystem package='Foo'>\
33+
<template name='code_template'>\
34+
code template content\
35+
</template>\
36+
<value-type name='A'>\
37+
<inject-code class='native'>\
38+
<insert-template name='code_template'/>\
39+
</inject-code>\
40+
</value-type>\
41+
</typesystem>";
42+
TestUtil t(cppCode, xmlCode, false);
43+
AbstractMetaClassList classes = t.builder()->classes();
44+
QCOMPARE(classes.count(), 1);
45+
AbstractMetaClass* classA = classes.findClass("A");
46+
QVERIFY(classA);
47+
QCOMPARE(classA->typeEntry()->codeSnips().count(), 1);
48+
QString code = classA->typeEntry()->codeSnips().first().code();
49+
QVERIFY(code.contains("code template content"));
50+
}
51+
52+
void TestInsertTemplate::testInsertTemplateOnModuleInjectCode()
53+
{
54+
const char* cppCode ="";
55+
const char* xmlCode = "\
56+
<typesystem package='Foo'>\
57+
<template name='code_template'>\
58+
code template content\
59+
</template>\
60+
<inject-code class='native'>\
61+
<insert-template name='code_template'/>\
62+
</inject-code>\
63+
</typesystem>";
64+
TestUtil t(cppCode, xmlCode, false);
65+
AbstractMetaClassList classes = t.builder()->classes();
66+
QVERIFY(classes.isEmpty());
67+
68+
TypeEntry* module = TypeDatabase::instance()->findType("Foo");
69+
QVERIFY(module);
70+
QCOMPARE(module->codeSnips().count(), 1);
71+
QString code = module->codeSnips().first().code().trimmed();
72+
QVERIFY(code.contains("code template content"));
73+
}
74+
75+
void TestInsertTemplate::testInvalidTypeSystemTemplate()
76+
{
77+
const char* cppCode ="";
78+
const char* xmlCode = "\
79+
<typesystem package='Foo'>\
80+
<inject-code class='native'>\
81+
<insert-template name='this_code_template_does_not_exists'/>\
82+
</inject-code>\
83+
</typesystem>";
84+
TestUtil t(cppCode, xmlCode, false);
85+
AbstractMetaClassList classes = t.builder()->classes();
86+
QVERIFY(classes.isEmpty());
87+
88+
TypeEntry* module = TypeDatabase::instance()->findType("Foo");
89+
QVERIFY(module);
90+
QCOMPARE(module->codeSnips().count(), 1);
91+
QString code = module->codeSnips().first().code().trimmed();
92+
QVERIFY(code.isEmpty());
93+
}
94+
95+
void TestInsertTemplate::testValidAndInvalidTypeSystemTemplate()
96+
{
97+
const char* cppCode ="";
98+
const char* xmlCode = "\
99+
<typesystem package='Foo'>\
100+
<template name='code_template'>\
101+
code template content\
102+
</template>\
103+
<inject-code class='native'>\
104+
<insert-template name='this_code_template_does_not_exists'/>\
105+
<insert-template name='code_template'/>\
106+
</inject-code>\
107+
</typesystem>";
108+
TestUtil t(cppCode, xmlCode, false);
109+
AbstractMetaClassList classes = t.builder()->classes();
110+
QVERIFY(classes.isEmpty());
111+
112+
TypeEntry* module = TypeDatabase::instance()->findType("Foo");
113+
QVERIFY(module);
114+
QCOMPARE(module->codeSnips().count(), 1);
115+
QString code = module->codeSnips().first().code().trimmed();
116+
QVERIFY(code.contains("code template content"));
117+
}
118+
119+
QTEST_APPLESS_MAIN(TestInsertTemplate)
120+
121+
#include "testinserttemplate.moc"

tests/testinserttemplate.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the API Extractor project.
3+
*
4+
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5+
*
6+
* Contact: PySide team <[email protected]>
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* version 2 as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful, but
13+
* WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA
21+
*
22+
*/
23+
24+
#ifndef TESTINSERTTEMPLATE_H
25+
#define TESTINSERTTEMPLATE_H
26+
27+
#include <QObject>
28+
29+
class TestInsertTemplate : public QObject
30+
{
31+
Q_OBJECT
32+
private slots:
33+
void testInsertTemplateOnClassInjectCode();
34+
void testInsertTemplateOnModuleInjectCode();
35+
void testInvalidTypeSystemTemplate();
36+
void testValidAndInvalidTypeSystemTemplate();
37+
};
38+
39+
#endif

typesystem.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ bool Handler::endElement(const QString &, const QString &localName, const QStrin
224224
case StackElement::TemplateInstanceEnum:
225225
switch (m_current->parent->type) {
226226
case StackElement::InjectCode:
227+
if (m_current->parent->parent->type == StackElement::Root) {
228+
CodeSnipList snips = m_current->parent->entry->codeSnips();
229+
CodeSnip snip = snips.takeLast();
230+
snip.addTemplateInstance(m_current->value.templateInstance);
231+
snips.append(snip);
232+
m_current->parent->entry->setCodeSnips(snips);
233+
break;
234+
}
227235
case StackElement::NativeToTarget:
228236
case StackElement::AddConversion:
229237
m_contextStack.top()->codeSnips.last().addTemplateInstance(m_current->value.templateInstance);

0 commit comments

Comments
 (0)