summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-12-13 21:11:09 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-09 19:10:20 -0300
commite648d0b0398f46afd2d5c5113a3b95a0522f5c2a (patch)
treec189ec4a77c348a4db1f7ce80706d812a729837a
parent5e5009140ec778dd217ce254770ed25c755a906c (diff)
Fixed insert-template tag when used inside a module level inject-code tag.
Also added unit tests. Reviewed by Hugo Parente <hugo.lima@openbossa.org> Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/testinserttemplate.cpp121
-rw-r--r--tests/testinserttemplate.h39
-rw-r--r--typesystem.cpp8
4 files changed, 169 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 01c7deb..dd3a326 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -27,6 +27,7 @@ declare_test(testenum)
declare_test(testextrainclude)
declare_test(testfunctiontag)
declare_test(testimplicitconversions)
+declare_test(testinserttemplate)
declare_test(testmodifyfunction)
declare_test(testmultipleinheritance)
declare_test(testnamespace)
diff --git a/tests/testinserttemplate.cpp b/tests/testinserttemplate.cpp
new file mode 100644
index 0000000..e3aade5
--- /dev/null
+++ b/tests/testinserttemplate.cpp
@@ -0,0 +1,121 @@
+/*
+ * This file is part of the API Extractor project.
+ *
+ * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include "testinserttemplate.h"
+#include <QtTest/QTest>
+#include "testutil.h"
+
+void TestInsertTemplate::testInsertTemplateOnClassInjectCode()
+{
+ const char* cppCode ="struct A{};";
+ const char* xmlCode = "\
+ <typesystem package='Foo'>\
+ <template name='code_template'>\
+ code template content\
+ </template>\
+ <value-type name='A'>\
+ <inject-code class='native'>\
+ <insert-template name='code_template'/>\
+ </inject-code>\
+ </value-type>\
+ </typesystem>";
+ TestUtil t(cppCode, xmlCode, false);
+ AbstractMetaClassList classes = t.builder()->classes();
+ QCOMPARE(classes.count(), 1);
+ AbstractMetaClass* classA = classes.findClass("A");
+ QVERIFY(classA);
+ QCOMPARE(classA->typeEntry()->codeSnips().count(), 1);
+ QString code = classA->typeEntry()->codeSnips().first().code();
+ QVERIFY(code.contains("code template content"));
+}
+
+void TestInsertTemplate::testInsertTemplateOnModuleInjectCode()
+{
+ const char* cppCode ="";
+ const char* xmlCode = "\
+ <typesystem package='Foo'>\
+ <template name='code_template'>\
+ code template content\
+ </template>\
+ <inject-code class='native'>\
+ <insert-template name='code_template'/>\
+ </inject-code>\
+ </typesystem>";
+ TestUtil t(cppCode, xmlCode, false);
+ AbstractMetaClassList classes = t.builder()->classes();
+ QVERIFY(classes.isEmpty());
+
+ TypeEntry* module = TypeDatabase::instance()->findType("Foo");
+ QVERIFY(module);
+ QCOMPARE(module->codeSnips().count(), 1);
+ QString code = module->codeSnips().first().code().trimmed();
+ QVERIFY(code.contains("code template content"));
+}
+
+void TestInsertTemplate::testInvalidTypeSystemTemplate()
+{
+ const char* cppCode ="";
+ const char* xmlCode = "\
+ <typesystem package='Foo'>\
+ <inject-code class='native'>\
+ <insert-template name='this_code_template_does_not_exists'/>\
+ </inject-code>\
+ </typesystem>";
+ TestUtil t(cppCode, xmlCode, false);
+ AbstractMetaClassList classes = t.builder()->classes();
+ QVERIFY(classes.isEmpty());
+
+ TypeEntry* module = TypeDatabase::instance()->findType("Foo");
+ QVERIFY(module);
+ QCOMPARE(module->codeSnips().count(), 1);
+ QString code = module->codeSnips().first().code().trimmed();
+ QVERIFY(code.isEmpty());
+}
+
+void TestInsertTemplate::testValidAndInvalidTypeSystemTemplate()
+{
+ const char* cppCode ="";
+ const char* xmlCode = "\
+ <typesystem package='Foo'>\
+ <template name='code_template'>\
+ code template content\
+ </template>\
+ <inject-code class='native'>\
+ <insert-template name='this_code_template_does_not_exists'/>\
+ <insert-template name='code_template'/>\
+ </inject-code>\
+ </typesystem>";
+ TestUtil t(cppCode, xmlCode, false);
+ AbstractMetaClassList classes = t.builder()->classes();
+ QVERIFY(classes.isEmpty());
+
+ TypeEntry* module = TypeDatabase::instance()->findType("Foo");
+ QVERIFY(module);
+ QCOMPARE(module->codeSnips().count(), 1);
+ QString code = module->codeSnips().first().code().trimmed();
+ QVERIFY(code.contains("code template content"));
+}
+
+QTEST_APPLESS_MAIN(TestInsertTemplate)
+
+#include "testinserttemplate.moc"
diff --git a/tests/testinserttemplate.h b/tests/testinserttemplate.h
new file mode 100644
index 0000000..fd3ca1d
--- /dev/null
+++ b/tests/testinserttemplate.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the API Extractor project.
+ *
+ * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef TESTINSERTTEMPLATE_H
+#define TESTINSERTTEMPLATE_H
+
+#include <QObject>
+
+class TestInsertTemplate : public QObject
+{
+ Q_OBJECT
+ private slots:
+ void testInsertTemplateOnClassInjectCode();
+ void testInsertTemplateOnModuleInjectCode();
+ void testInvalidTypeSystemTemplate();
+ void testValidAndInvalidTypeSystemTemplate();
+};
+
+#endif
diff --git a/typesystem.cpp b/typesystem.cpp
index daffeba..bbf7e86 100644
--- a/typesystem.cpp
+++ b/typesystem.cpp
@@ -224,6 +224,14 @@ bool Handler::endElement(const QString &, const QString &localName, const QStrin
case StackElement::TemplateInstanceEnum:
switch (m_current->parent->type) {
case StackElement::InjectCode:
+ if (m_current->parent->parent->type == StackElement::Root) {
+ CodeSnipList snips = m_current->parent->entry->codeSnips();
+ CodeSnip snip = snips.takeLast();
+ snip.addTemplateInstance(m_current->value.templateInstance);
+ snips.append(snip);
+ m_current->parent->entry->setCodeSnips(snips);
+ break;
+ }
case StackElement::NativeToTarget:
case StackElement::AddConversion:
m_contextStack.top()->codeSnips.last().addTemplateInstance(m_current->value.templateInstance);