diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2009-11-13 15:20:54 -0200 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2009-11-16 15:58:01 -0200 |
commit | d2b0d52b53d31ca4b026a1982bb322fcb58a73cd (patch) | |
tree | 8cb47c1d982ab9bdf7ea93ac987866d761123cd9 | |
parent | 6db0e13380d4818a3ab14975e5c30d50c2e4d0e2 (diff) | |
download | shiboken-d2b0d52b53d31ca4b026a1982bb322fcb58a73cd.tar.gz shiboken-d2b0d52b53d31ca4b026a1982bb322fcb58a73cd.tar.xz shiboken-d2b0d52b53d31ca4b026a1982bb322fcb58a73cd.zip |
Added tests for various inject-code use cases.
-rw-r--r-- | tests/libsample/injectcode.cpp | 55 | ||||
-rw-r--r-- | tests/libsample/injectcode.h | 23 | ||||
-rwxr-xr-x | tests/samplebinding/injectcode_test.py | 65 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 94 |
4 files changed, 218 insertions, 19 deletions
diff --git a/tests/libsample/injectcode.cpp b/tests/libsample/injectcode.cpp index a53f17b5..f418b25f 100644 --- a/tests/libsample/injectcode.cpp +++ b/tests/libsample/injectcode.cpp @@ -33,32 +33,63 @@ */ #include "injectcode.h" +#include <sstream> + +using namespace std; InjectCode::InjectCode() { } -int -InjectCode::simpleMethod(int arg0, int arg1) +InjectCode::~InjectCode() +{ +} + +template<typename T> +const char* InjectCode::toStr(const T& value) +{ + std::ostringstream s; + s << value; + m_valueHolder = s.str(); + return m_valueHolder.c_str(); +} + +const char* InjectCode::simpleMethod1(int arg0, int arg1) +{ + return toStr(arg0 + arg1); +} + +const char* InjectCode::simpleMethod2() +{ + return "_"; +} + +const char* InjectCode::simpleMethod3(int argc, char** argv) +{ + for (int i = 0; i < argc; ++i) + m_valueHolder += argv[i]; + return m_valueHolder.c_str(); +} + +const char* InjectCode::overloadedMethod(int arg0, bool arg1) { - return arg0 + arg1; + toStr(arg0); + m_valueHolder += arg1 ? "true" : "false"; + return m_valueHolder.c_str(); } -double -InjectCode::overloadedMethod(int arg) +const char* InjectCode::overloadedMethod(int arg0, double arg1) { - return arg * 2; + return toStr(arg0 + arg1); } -double -InjectCode::overloadedMethod(double arg) +const char* InjectCode::overloadedMethod(int argc, char** argv) { - return arg * 1.5; + return simpleMethod3(argc, argv); } -int -InjectCode::virtualMethod(int arg) +const char* InjectCode::virtualMethod(int arg) { - return arg * 10; + return toStr(arg); } diff --git a/tests/libsample/injectcode.h b/tests/libsample/injectcode.h index dca46db6..52d41719 100644 --- a/tests/libsample/injectcode.h +++ b/tests/libsample/injectcode.h @@ -35,21 +35,32 @@ #ifndef INJECTCODE_H #define INJECTCODE_H -#include "complex.h" #include <utility> +#include <string> class InjectCode { public: InjectCode(); - ~InjectCode() {} + virtual ~InjectCode(); - int simpleMethod(int arg0, int arg1); + const char* simpleMethod1(int arg0, int arg1); + const char* simpleMethod2(); + const char* simpleMethod3(int argc, char** argv); - double overloadedMethod(int arg); - double overloadedMethod(double arg); + const char* overloadedMethod(int argc, char** argv); + const char* overloadedMethod(int arg0, double arg1); + const char* overloadedMethod(int arg0, bool arg1); - virtual int virtualMethod(int arg); + virtual const char* virtualMethod(int arg); +private: + // This attr is just to retain the memory pointed by all return values, + // So, the memory returned by all methods will be valid until someone call + // another method of this class. + std::string m_valueHolder; + + template<typename T> + const char* toStr(const T& value); }; #endif // INJECTCODE_H diff --git a/tests/samplebinding/injectcode_test.py b/tests/samplebinding/injectcode_test.py new file mode 100755 index 00000000..32204791 --- /dev/null +++ b/tests/samplebinding/injectcode_test.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of the Shiboken Python Bindings Generator project. +# +# Copyright (C) 2009 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 Lesser General Public License +# version 2.1 as published by the Free Software Foundation. Please +# review the following information to ensure the GNU Lesser General +# Public License version 2.1 requirements will be met: +# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +# # +# 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser 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 + +'''Test cases for std::list container conversions''' + +import unittest +from sample import InjectCode + +class InjectCodeTest(unittest.TestCase): + + def testTypeNativeBeginning_TypeTargetBeginning(self): + ic = InjectCode() + self.assertEqual(str(ic), "Hi! I'm the inject code dummy class.") + + def testFunctionTargetBeginning_FunctionTargetEnd(self): + ic = InjectCode() + ret = ic.simpleMethod1(2, 1) + self.assertEqual(ret, "4end") + ret = ic.simpleMethod1(4, 2) + self.assertEqual(ret, "7end") + + def testFunctionTargetBeginning(self): + ic = InjectCode() + ret = ic.simpleMethod2() + self.assertEqual(ret, "_end") + + def testArgsModification(self): + ic = InjectCode() + ret = ic.overloadedMethod(["1", "2", "3", "4"]) + self.assertEqual(ret, "1234") + ret = ic.overloadedMethod(2, 0.5) + self.assertEqual(ret, "2.5") + ret = ic.overloadedMethod(6, True) + self.assertEqual(ret, "6true") + + def testArgsModification2(self): + ic = InjectCode() + ret = ic.simpleMethod3(["1", "2", "3", "4"]) + self.assertEqual(ret, "1234") + +if __name__ == '__main__': + unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 976a9135..1da279b1 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -218,7 +218,95 @@ <value-type name="VirtualMethods"/> <value-type name="ImplicitConv"/> - <value-type name="InjectCode"/> + <value-type name="InjectCode"> + <!-- + Various tests for inject codes. + Note: Some uses of inject code here are used just for testing purposes, consider using the add-function tag. + --> + <!-- + Inject the tp_str method using this alternative way + Tested in InjectCodeTest.testTypeNativeBeginning_TypeTargetBeginning: + --> + <inject-code class="native" position="beginning"> + PyObject* InjectCode_tpstr(PyObject*) { return PyString_FromString("Hi! I'm the inject code dummy class."); } + </inject-code> + <!-- + Register our tp_str class using another inject code + Tested in InjectCodeTest.testTypeNativeBeginning_TypeTargetBeginning: + --> + <inject-code class="target" position="beginning"> + PyInjectCode_Type.tp_str = InjectCode_tpstr; + </inject-code> + + <!-- Tested in InjectCodeTest.testFunctionTargetBeginning_FunctionTargetEnd --> + <modify-function signature="simpleMethod1(int, int)"> + <inject-code class="target" position="beginning"> + %1 += 1; + </inject-code> + <inject-code class="target" position="end"> + PyObject* tmp = PyString_FromString("end"); + PyString_Concat(&%0, tmp); + Py_DECREF(tmp); + </inject-code> + </modify-function> + + <!-- Tested in InjectCodeTest.testFunctionTargetBeginning --> + <modify-function signature="simpleMethod2()"> + <inject-code class="target" position="end"> + PyObject* tmp = PyString_FromString("end"); + PyString_Concat(&%0, tmp); + Py_DECREF(tmp); + </inject-code> + </modify-function> + + <!-- Tested in InjectCodeTest.testArgsModification --> + <modify-function signature="overloadedMethod(int, char**)"> + <modify-argument index="1"> + <replace-type modified-type="PySequence" /> + </modify-argument> + <modify-argument index="2"> + <remove-argument /> + </modify-argument> + <inject-code class="target" position="beginning"> + int argc; + char** argv; + if (!PySequence_to_argc_argv(%PYARG_1, &argc, &argv)) { + PyErr_SetString(PyExc_TypeError, "error"); + return 0; + } + %RETURN_TYPE foo = %CPPSELF.%FUNCTION_NAME(argc, argv); + %0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo); + + for (int i = 0; i < argc; ++i) + delete[] argv[i]; + delete[] argv; + </inject-code> + </modify-function> + + <!-- Tested in InjectCodeTest.testArgsModification2 --> + <modify-function signature="simpleMethod3(int, char**)"> + <modify-argument index="1"> + <replace-type modified-type="PySequence" /> + </modify-argument> + <modify-argument index="2"> + <remove-argument /> + </modify-argument> + <inject-code class="target" position="beginning"> + int argc; + char** argv; + if (!PySequence_to_argc_argv(%PYARG_1, &argc, &argv)) { + PyErr_SetString(PyExc_TypeError, "error"); + return 0; + } + %RETURN_TYPE foo = %CPPSELF.%FUNCTION_NAME(argc, argv); + %0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo); + + for (int i = 0; i < argc; ++i) + delete[] argv[i]; + delete[] argv; + </inject-code> + </modify-function> + </value-type> <value-type name="Point"> <add-function signature="__str__()" return-type="PyObject*"> @@ -322,5 +410,9 @@ <rejection class="" function-name="gimmeComplexList()"/> <rejection class="" function-name="makeCString()"/> <rejection class="" function-name="returnCString()"/> + + <suppress-warning text="::*" /> + <suppress-warning text="horribly broken type '__off64_t'" /> + <suppress-warning text="enum '__codecvt_result' does not have a type entry or is not an enum" /> </typesystem> |