diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-10-30 13:49:14 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-10-30 14:24:21 -0300 |
commit | c5bae38411471448cd226fc91a116034e2757ab3 (patch) | |
tree | 86ff44669ab2b2cb8eed3a0ba71ef1676994aeb1 /tests | |
parent | 74b9a717eb0a4e375b91f8cb3a4cf6eebb08486a (diff) |
added the Str class to libsample and test cases that check the __str__
implementation and a method that receives a C++ class reference
through a Python type implictly convertible to said C++ class
Reviewed by Hugo Lima <hugo.lima@openbossa.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/libsample/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/libsample/str.cpp | 123 | ||||
-rw-r--r-- | tests/libsample/str.h | 62 | ||||
-rw-r--r-- | tests/samplebinding/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/samplebinding/global.h | 22 | ||||
-rwxr-xr-x | tests/samplebinding/str_test.py | 56 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 8 |
7 files changed, 263 insertions, 10 deletions
diff --git a/tests/libsample/CMakeLists.txt b/tests/libsample/CMakeLists.txt index 3672faf26..82c4b1bbb 100644 --- a/tests/libsample/CMakeLists.txt +++ b/tests/libsample/CMakeLists.txt @@ -19,6 +19,7 @@ reference.cpp samplenamespace.cpp simplefile.cpp size.cpp +str.cpp virtualmethods.cpp ) diff --git a/tests/libsample/str.cpp b/tests/libsample/str.cpp new file mode 100644 index 000000000..fcf70fde8 --- /dev/null +++ b/tests/libsample/str.cpp @@ -0,0 +1,123 @@ +/* + * This file is part of the Shiboken Python Binding 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. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * 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 + */ + +#include "str.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +Str::Str(const Str& s) +{ + init(s.cstring()); +} + +Str::Str(const char* cstr) +{ + init(cstr); +} + +void +Str::init(const char* cstr) +{ + m_size = strlen(cstr); + m_str = (char *) malloc(m_size + 1); + if (m_size == 0) + m_str[0] = '\0'; + else + strncpy(m_str, cstr, m_size + 1); +} + +Str::~Str() +{ + free(m_str); +} + +Str +Str::arg(const Str& s) const +{ + char* var = strstr(m_str, "%VAR"); + if (!var) + return Str(m_str); + + char* newstr = (char*) malloc (m_size + s.size() - 3); + + int var_pos = 0; + for (const char* ptr = m_str; ptr != var; ptr++) + var_pos++; + + strncpy(newstr, m_str, var_pos); + char* ptr = newstr + var_pos; + strncpy(ptr, s.cstring(), s.size()); + ptr = ptr + s.size(); + strcpy(ptr, m_str + var_pos + 4); + + Str result(newstr); + free(newstr); + + return result; +} + +const char* +Str::cstring() const +{ + return m_str; +} + +void +Str::show() const +{ + printf("%s", m_str); +} + +char +Str::get_char(int pos) const +{ + if (pos < 0) + pos = m_size - pos; + if (pos < 0 || pos >= m_size) + return -1; + return m_str[pos]; +} + +bool +Str::set_char(int pos, char ch) +{ + if (pos < 0) + pos = m_size - pos; + if (pos < 0 || pos >= m_size) + return false; + m_str[pos] = ch; + return true; +} + diff --git a/tests/libsample/str.h b/tests/libsample/str.h new file mode 100644 index 000000000..43396e633 --- /dev/null +++ b/tests/libsample/str.h @@ -0,0 +1,62 @@ +/* + * This file is part of the Shiboken Python Binding 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. + * + * As a special exception to the GNU Lesser General Public License + * version 2.1, the object code form of a "work that uses the Library" + * may incorporate material from a header file that is part of the + * Library. You may distribute such object code under terms of your + * choice, provided that the incorporated material (i) does not exceed + * more than 5% of the total size of the Library; and (ii) is limited to + * numerical parameters, data structure layouts, accessors, macros, + * inline functions and templates. + * + * 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 + */ + +#ifndef STR_H +#define STR_H + +class Str +{ +public: + Str(const Str& s); + Str(const char* cstr = 0); + ~Str(); + + Str arg(const Str& s) const; + + const char* cstring() const; + char get_char(int pos) const; + bool set_char(int pos, char ch); + + void show() const; + + int size() const { return m_size; } + +private: + void init(const char* cstr); + char* m_str; + int m_size; +}; + +#endif // STR_H + diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt index 94de71102..2405f3bf6 100644 --- a/tests/samplebinding/CMakeLists.txt +++ b/tests/samplebinding/CMakeLists.txt @@ -27,6 +27,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/sample_module_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/simplefile_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/size_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/str_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/virtualmethods_wrapper.cpp ) diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h index 13fd4f0e7..ba36da17a 100644 --- a/tests/samplebinding/global.h +++ b/tests/samplebinding/global.h @@ -1,22 +1,24 @@ #include "abstract.h" #include "collector.h" -#include "derived.h" -#include "point.h" -#include "size.h" #include "complex.h" +#include "derived.h" #include "functions.h" +#include "implicitconv.h" #include "kindergarten.h" -#include "pairuser.h" #include "listuser.h" #include "mapuser.h" +#include "modifications.h" #include "multiple_derived.h" +#include "nondefaultctor.h" +#include "oddbool.h" #include "overload.h" +#include "pairuser.h" +#include "point.h" +#include "privatedtor.h" +#include "reference.h" #include "samplenamespace.h" #include "simplefile.h" -#include "modifications.h" -#include "implicitconv.h" -#include "reference.h" +#include "size.h" +#include "str.h" #include "virtualmethods.h" -#include "nondefaultctor.h" -#include "oddbool.h" -#include "privatedtor.h" + diff --git a/tests/samplebinding/str_test.py b/tests/samplebinding/str_test.py new file mode 100755 index 000000000..841e840b1 --- /dev/null +++ b/tests/samplebinding/str_test.py @@ -0,0 +1,56 @@ +#!/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 a method that receives a reference to class that is implicitly convertible from a Python native type.''' + +import sys +import unittest + +from sample import Str + +class StrTest(unittest.TestCase): + '''Test cases for thr Str class.''' + + def test__str__Method(self): + '''Test if the binding correcly implements the Python __str__ method.''' + s1 = 'original string' + s2 = Str(s1) + self.assertNotEqual(s1, s2) + self.assertEqual(s1, str(s2)) + + def testPassExactClassAsReferenceToArgument(self): + '''Test passing the expected class as an argument to a method that expects a reference.''' + s1 = Str('This is %VAR!').arg(Str('Sparta')) + self.assertEqual(str(s1), 'This is Sparta!') + + def testPassPythonTypeImplictlyConvertibleToAClassUsedAsReference(self): + '''Test passing a Python class implicitly convertible to a wrapped class that is expected to be passed as reference.''' + s1 = Str('This is %VAR!').arg('Athens') + self.assertEqual(str(s1), 'This is Athens!') + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 9f7a47b41..5878c4ee6 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -236,6 +236,14 @@ <value-type name="Overload" /> <value-type name="Collector" /> + <value-type name="Str"> + <add-function signature="__str__()" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + %0 = PyString_FromString(%CPPSELF.cstring()); + </inject-code> + </add-function> + </value-type> + <value-type name="SimpleFile"> <modify-function signature="open()"> <modify-argument index="return"> |