From 08f29f0d8f456eb1f994b05c21fd04468c95329c Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Thu, 4 Aug 2011 17:31:48 -0300 Subject: Included tests for added function signatures very similar to already existing ones. Specifically this causes the situation when there is in C++ a function with an argument that is a reference to a Value Type, and the user adds a very similar function with the same type, but passed as value. Example: C++ : function(const TYPE& a, int b) Added: function(TYPE) The return type of ShibokenGenerator::getArgumentReplacement() method was modified, because the argument object is more useful than its index. --- tests/libsample/samplenamespace.cpp | 13 ++++++- tests/libsample/samplenamespace.h | 8 +++-- tests/samplebinding/addedfunction_test.py | 57 +++++++++++++++++++++++++++++++ tests/samplebinding/typesystem_sample.xml | 20 ++++++++++- 4 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tests/samplebinding/addedfunction_test.py (limited to 'tests') diff --git a/tests/libsample/samplenamespace.cpp b/tests/libsample/samplenamespace.cpp index 0bcc5c2b2..922f26fa3 100644 --- a/tests/libsample/samplenamespace.cpp +++ b/tests/libsample/samplenamespace.cpp @@ -101,5 +101,16 @@ forceDecisorSideB(int a, const Point& pt, const Str& text, ObjectType* object) { } -} // namespace SampleNamespace +double +passReferenceToValueType(const Point& point, double multiplier) +{ + return (point.x() + point.y()) * multiplier; +} + +int +passReferenceToObjectType(const ObjectType& obj, int multiplier) +{ + return obj.objectName().size() * multiplier; +} +} // namespace SampleNamespace diff --git a/tests/libsample/samplenamespace.h b/tests/libsample/samplenamespace.h index 3ac5b2601..e1578dc88 100644 --- a/tests/libsample/samplenamespace.h +++ b/tests/libsample/samplenamespace.h @@ -26,8 +26,7 @@ #include "libsamplemacros.h" #include "str.h" #include "point.h" - -class ObjectType; +#include "objecttype.h" // Anonymous global enum enum { @@ -130,6 +129,11 @@ LIBSAMPLE_API void forceDecisorSideA(const Point& pt, const Str& text, ObjectTyp LIBSAMPLE_API void forceDecisorSideB(int a, ObjectType* object = 0); LIBSAMPLE_API void forceDecisorSideB(int a, const Point& pt, const Str& text, ObjectType* object = 0); +// Add a new signature on type system with only a Point value as parameter. +LIBSAMPLE_API double passReferenceToValueType(const Point& point, double multiplier); +// Add a new signature on type system with only a ObjectType pointer as parameter. +LIBSAMPLE_API int passReferenceToObjectType(const ObjectType& obj, int multiplier); + } // namespace SampleNamespace #endif // SAMPLENAMESPACE_H diff --git a/tests/samplebinding/addedfunction_test.py b/tests/samplebinding/addedfunction_test.py new file mode 100644 index 000000000..7e8db06af --- /dev/null +++ b/tests/samplebinding/addedfunction_test.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of the Shiboken Python Bindings Generator project. +# +# Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +# +# Contact: PySide team +# +# 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 added functions.''' + +import unittest +from sample import SampleNamespace, ObjectType, Point + +class TestAddedFunctionsWithSimilarTypes(unittest.TestCase): + '''Adds new signatures very similar to already existing ones.''' + + def testValueTypeReferenceAndValue(self): + '''In C++ we have "function(const ValueType&, double)", + in Python we add "function(ValueType)".''' + point = Point(10, 20) + multiplier = 4.0 + control = (point.x() + point.y()) * multiplier + self.assertEqual(SampleNamespace.passReferenceToValueType(point, multiplier), control) + control = point.x() + point.y() + self.assertEqual(SampleNamespace.passReferenceToValueType(point), control) + + def testObjectTypeReferenceAndPointer(self): + '''In C++ we have "function(const ObjectType&, int)", + in Python we add "function(ValueType)".''' + obj = ObjectType() + obj.setObjectName('sbrubbles') + multiplier = 3.0 + control = len(obj.objectName()) * multiplier + self.assertEqual(SampleNamespace.passReferenceToObjectType(obj, multiplier), control) + control = len(obj.objectName()) + self.assertEqual(SampleNamespace.passReferenceToObjectType(obj), control) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 547710a52..be0f8fe88 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -137,6 +137,22 @@ %PYARG_0 = %CONVERTTOPYTHON[int](%1 + %2); + + + double %0 = %1.x() + %1.y(); + %PYARG_0 = %CONVERTTOPYTHON[double](%0); + + + + + + + // The dot in "%1." must be replaced by a "->". + double %0 = %1.objectName().size(); + %PYARG_0 = %CONVERTTOPYTHON[int](%0); + + @@ -1711,5 +1727,7 @@ - + + + -- cgit v1.2.3