diff options
Diffstat (limited to 'sources/shiboken2/tests')
310 files changed, 27489 insertions, 0 deletions
diff --git a/sources/shiboken2/tests/CMakeLists.txt b/sources/shiboken2/tests/CMakeLists.txt new file mode 100644 index 000000000..c06c56f6d --- /dev/null +++ b/sources/shiboken2/tests/CMakeLists.txt @@ -0,0 +1,102 @@ +find_package(Qt5 REQUIRED COMPONENTS Core) +include_directories(${Qt5Core_INCLUDE_DIRS}) + +add_subdirectory(libminimal) +if(NOT DEFINED MINIMAL_TESTS) + add_subdirectory(libsample) + add_subdirectory(libsmart) + add_subdirectory(libother) +endif() + +if(WIN32 OR DEFINED AVOID_PROTECTED_HACK) + message(STATUS "Tests will be generated avoiding the protected hack!") + set(GENERATOR_EXTRA_FLAGS --avoid-protected-hack) +else() + message(STATUS "Tests will be generated using the protected hack!") + set(GENERATOR_EXTRA_FLAGS ) +endif() + +add_subdirectory(minimalbinding) +if(NOT DEFINED MINIMAL_TESTS) + add_subdirectory(samplebinding) + add_subdirectory(smartbinding) + add_subdirectory(otherbinding) +endif() + +if(DEFINED MINIMAL_TESTS) + file(GLOB TEST_FILES minimalbinding/*_test.py) +else() + file(GLOB TEST_FILES minimalbinding/*_test.py + samplebinding/*_test.py + otherbinding/*_test.py + smartbinding/*_test.py + shibokenmodule/*_test.py) +endif() +list(SORT TEST_FILES) + +set(test_blacklist "") + +find_package(PythonInterp REQUIRED) +if(NOT CTEST_TESTING_TIMEOUT) + set(CTEST_TESTING_TIMEOUT 60) +endif() + +if(CMAKE_VERSION VERSION_LESS 2.8) + message("CMake version greater than 2.8 necessary to run tests") +else() + if(WIN32) + set(TEST_PYTHONPATH "${smart_BINARY_DIR};${minimal_BINARY_DIR};${sample_BINARY_DIR};${other_BINARY_DIR};${shibokenmodule_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}") + set(TEST_LIBRARY_PATH "$ENV{PATH};${libminimal_BINARY_DIR};${libsample_BINARY_DIR};${libother_BINARY_DIR};${libsmart_BINARY_DIR};${libshiboken_BINARY_DIR}") + set(LIBRARY_PATH_VAR "PATH") + string(REPLACE "\\" "/" TEST_PYTHONPATH "${TEST_PYTHONPATH}") + string(REPLACE "\\" "/" TEST_LIBRARY_PATH "${TEST_LIBRARY_PATH}") + string(REPLACE ";" "\\;" TEST_PYTHONPATH "${TEST_PYTHONPATH}") + string(REPLACE ";" "\\;" TEST_LIBRARY_PATH "${TEST_LIBRARY_PATH}") + else() + set(TEST_PYTHONPATH "${smart_BINARY_DIR}:${minimal_BINARY_DIR}:${sample_BINARY_DIR}:${other_BINARY_DIR}:${shibokenmodule_BINARY_DIR}:${CMAKE_CURRENT_SOURCE_DIR}") + set(TEST_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}:${libminimal_BINARY_DIR}:${libsample_BINARY_DIR}:${libother_BINARY_DIR}:${libsmart_BINARY_DIR}:${libshiboken_BINARY_DIR}") + set(LIBRARY_PATH_VAR "LD_LIBRARY_PATH") + endif() + foreach(test_file ${TEST_FILES}) + string(REGEX MATCH "/([^/]+)(binding|module)/([^/]+)_test.py" tmp ${test_file}) + set(test_name "${CMAKE_MATCH_1}_${CMAKE_MATCH_3}") + list(FIND test_blacklist ${test_name} expect_fail) + add_test(${test_name} ${PYTHON_EXECUTABLE} ${test_file}) + set_tests_properties(${test_name} PROPERTIES ENVIRONMENT "PYTHONPATH=${TEST_PYTHONPATH};${LIBRARY_PATH_VAR}=${TEST_LIBRARY_PATH}") + set_tests_properties(${test_name} PROPERTIES TIMEOUT ${CTEST_TESTING_TIMEOUT}) + if (${expect_fail} GREATER -1) + set_tests_properties(${test_name} PROPERTIES WILL_FAIL TRUE) + endif() + endforeach() +endif() + +add_subdirectory(dumpcodemodel) + +# FIXME Skipped until add an option to choose the generator +# add_subdirectory(test_generator) + +if (NOT APIEXTRACTOR_DOCSTRINGS_DISABLED) +# project(sphinxtabletest) +# +# # TODO +# set(sphinxtabletest_SRC sphinxtabletest.cpp) +# qt4_automoc(${sphinxtabletest_SRC}) +# +# include_directories(${QT_INCLUDE_DIR} +# ${QT_QTCORE_INCLUDE_DIR} +# ${CMAKE_CURRENT_BINARY_DIR} +# ${qtdoc_generator_SOURCE_DIR}) +# +# add_executable(sphinxtabletest ${sphinxtabletest_SRC}) +# +# target_link_libraries(sphinxtabletest +# ${QT_QTTEST_LIBRARY} +# ${APIEXTRACTOR_LIBRARY} +# qtdoc_generator +# genrunner) +# +# add_test("sphinxtable" sphinxtabletest) +# if (INSTALL_TESTS) +# install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/sphinxtabletest DESTINATION ${TEST_INSTALL_DIR}) +# endif() +endif() diff --git a/sources/shiboken2/tests/dumpcodemodel/CMakeLists.txt b/sources/shiboken2/tests/dumpcodemodel/CMakeLists.txt new file mode 100644 index 000000000..af18cb3c8 --- /dev/null +++ b/sources/shiboken2/tests/dumpcodemodel/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(dumpcodemodel main.cpp) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${apiextractor_SOURCE_DIR} ${Qt5Core_INCLUDE_DIRS}) + +target_link_libraries(dumpcodemodel apiextractor ${Qt5Core_LIBRARIES}) diff --git a/sources/shiboken2/tests/dumpcodemodel/main.cpp b/sources/shiboken2/tests/dumpcodemodel/main.cpp new file mode 100644 index 000000000..9e62faa16 --- /dev/null +++ b/sources/shiboken2/tests/dumpcodemodel/main.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <abstractmetabuilder_p.h> +#include <parser/codemodel.h> + +#include <QtCore/QCoreApplication> +#include <QtCore/QCommandLineOption> +#include <QtCore/QCommandLineParser> +#include <QtCore/QDebug> +#include <QtCore/QDir> +#include <QtCore/QFile> + +#include <iostream> + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + QCommandLineParser parser; + parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); + parser.setApplicationDescription(QStringLiteral("Code model tester")); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption verboseOption(QStringLiteral("d"), + QStringLiteral("Display verbose output about types")); + parser.addOption(verboseOption); + parser.addPositionalArgument(QStringLiteral("file"), QStringLiteral("C++ source file")); + + parser.process(app); + if (parser.positionalArguments().isEmpty()) + parser.showHelp(1); + + const QString sourceFileName = parser.positionalArguments().at(0); + QFile sourceFile(sourceFileName); + if (!sourceFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + QString message = QLatin1String("Cannot open \"") + QDir::toNativeSeparators(sourceFileName) + + QLatin1String("\": ") + sourceFile.errorString(); + std::cerr << qPrintable(message) << '\n'; + return -1; + } + + const FileModelItem dom = AbstractMetaBuilderPrivate::buildDom(&sourceFile); + sourceFile.close(); + if (dom.isNull()) + return -2; + + QString output; + { + QDebug debug(&output); + if (parser.isSet(verboseOption)) + debug.setVerbosity(3); + debug << dom.data(); + } + std::cout << qPrintable(output) << '\n'; + + return 0; +} diff --git a/sources/shiboken2/tests/libminimal/CMakeLists.txt b/sources/shiboken2/tests/libminimal/CMakeLists.txt new file mode 100644 index 000000000..a25892571 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/CMakeLists.txt @@ -0,0 +1,13 @@ +project(libminimal) + +set(libminimal_SRC +obj.cpp +listuser.cpp +typedef.cpp +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_definitions("-DLIBMINIMAL_BUILD") +add_library(libminimal SHARED ${libminimal_SRC}) +set_property(TARGET libminimal PROPERTY PREFIX "") + diff --git a/sources/shiboken2/tests/libminimal/libminimalmacros.h b/sources/shiboken2/tests/libminimal/libminimalmacros.h new file mode 100644 index 000000000..5c9e62e8a --- /dev/null +++ b/sources/shiboken2/tests/libminimal/libminimalmacros.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIBMINIMALMACROS_H +#define LIBMINIMALMACROS_H + +#if defined _WIN32 || defined __CYGWIN__ + #if LIBMINIMAL_BUILD + #define LIBMINIMAL_API __declspec(dllexport) + #else + #define LIBMINIMAL_API __declspec(dllimport) + #endif +#else +#if __GNUC__ >= 4 + #define LIBMINIMAL_API __attribute__ ((visibility("default"))) +#else + #define LIBMINIMAL_API +#endif +#endif + +#endif diff --git a/sources/shiboken2/tests/libminimal/listuser.cpp b/sources/shiboken2/tests/libminimal/listuser.cpp new file mode 100644 index 000000000..ec2eb3923 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/listuser.cpp @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <numeric> +#include <cstdlib> +#include "listuser.h" + +std::list<int> +ListUser::createIntList(int num) +{ + std::list<int> retval; + for (int i = 0; i < num; ++i) + retval.push_back(i); + return retval; +} + +int +ListUser::sumIntList(std::list<int> intList) +{ + int total = 0; + for (std::list<int>::iterator iter = intList.begin(); iter != intList.end(); iter++) + total += *iter; + return total; +} + +std::list<MinBool> +ListUser::createMinBoolList(MinBool mb1, MinBool mb2) +{ + std::list<MinBool> retval; + retval.push_back(mb1); + retval.push_back(mb2); + return retval; +} + +MinBool +ListUser::oredMinBoolList(std::list<MinBool> minBoolList) +{ + MinBool result(false); + for (std::list<MinBool>::iterator iter = minBoolList.begin(); iter != minBoolList.end(); iter++) + result |= *iter; + return result; +} + +std::list<Val> +ListUser::createValList(int num) +{ + std::list<Val> retval; + for (int i = 0; i < num; ++i) + retval.push_back(Val(i)); + return retval; +} + +int +ListUser::sumValList(std::list<Val> valList) +{ + int total = 0; + for (std::list<Val>::iterator iter = valList.begin(); iter != valList.end(); iter++) + total += iter->valId(); + return total; +} + +std::list<Obj*> +ListUser::createObjList(Obj* o1, Obj* o2) +{ + std::list<Obj*> retval; + retval.push_back(o1); + retval.push_back(o2); + return retval; +} + +int +ListUser::sumObjList(std::list<Obj*> objList) +{ + int total = 0; + for (std::list<Obj*>::iterator iter = objList.begin(); iter != objList.end(); iter++) + total += (*iter)->objId(); + return total; +} + +std::list<std::list<int> > +ListUser::createListOfIntLists(int num) +{ + std::list<std::list<int> > retval; + for (int i = 0; i < num; ++i) + retval.push_back(createIntList(num)); + return retval; +} + +int +ListUser::sumListOfIntLists(std::list<std::list<int> > intListList) +{ + int total = 0; + for (std::list<std::list<int> >::iterator it0 = intListList.begin(); it0 != intListList.end(); it0++) { + for (std::list<int>::iterator it1 = (*it0).begin(); it1 != (*it0).end(); it1++) + total += *it1; + } + return total; +} + diff --git a/sources/shiboken2/tests/libminimal/listuser.h b/sources/shiboken2/tests/libminimal/listuser.h new file mode 100644 index 000000000..4138b9f3c --- /dev/null +++ b/sources/shiboken2/tests/libminimal/listuser.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LISTUSER_H +#define LISTUSER_H + +#include <list> +#include "obj.h" +#include "val.h" +#include "minbool.h" + +#include "libminimalmacros.h" + +struct LIBMINIMAL_API ListUser +{ + virtual ~ListUser() {} + + // List of C++ primitive type items + virtual std::list<int> createIntList(int num); + std::list<int> callCreateIntList(int num) { return createIntList(num); } + virtual int sumIntList(std::list<int> intList); + int callSumIntList(std::list<int> intList) { return sumIntList(intList); } + + // List of C++ MinBool objects used as primitives in Python + virtual std::list<MinBool> createMinBoolList(MinBool mb1, MinBool mb2); + std::list<MinBool> callCreateMinBoolList(MinBool mb1, MinBool mb2) { return createMinBoolList(mb1, mb2); } + virtual MinBool oredMinBoolList(std::list<MinBool> minBoolList); + MinBool callOredMinBoolList(std::list<MinBool> minBoolList) { return oredMinBoolList(minBoolList); } + + // List of C++ value types + virtual std::list<Val> createValList(int num); + std::list<Val> callCreateValList(int num) { return createValList(num); } + virtual int sumValList(std::list<Val> valList); + int callSumValList(std::list<Val> valList) { return sumValList(valList); } + + // List of C++ object types + virtual std::list<Obj*> createObjList(Obj* o1, Obj* o2); + std::list<Obj*> callCreateObjList(Obj* o1, Obj* o2) { return createObjList(o1, o2); } + virtual int sumObjList(std::list<Obj*> objList); + int callSumObjList(std::list<Obj*> objList) { return sumObjList(objList); } + + // List of lists of C++ primitive type items + virtual std::list<std::list<int> > createListOfIntLists(int num); + std::list<std::list<int> > callCreateListOfIntLists(int num) { return createListOfIntLists(num); } + virtual int sumListOfIntLists(std::list<std::list<int> > intListList); + int callSumListOfIntLists(std::list<std::list<int> > intListList) { return sumListOfIntLists(intListList); } +}; + +#endif // LISTUSER_H + diff --git a/sources/shiboken2/tests/libminimal/minbool.h b/sources/shiboken2/tests/libminimal/minbool.h new file mode 100644 index 000000000..ebd2f71e2 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/minbool.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MINBOOL_H +#define MINBOOL_H + +#include "libminimalmacros.h" + +class LIBMINIMAL_API MinBool +{ +public: + inline explicit MinBool(bool b) : m_value(b) {} + bool value() const { return m_value; } + inline MinBool operator!() const { return MinBool(!m_value); } + inline MinBool& operator|=(const MinBool& other) { + m_value = m_value | other.m_value; + return *this; + } +private: + bool m_value; +}; + +inline bool operator==(MinBool b1, bool b2) { return (!b1).value() == !b2; } +inline bool operator==(bool b1, MinBool b2) { return (!b1) == (!b2).value(); } +inline bool operator==(MinBool b1, MinBool b2) { return (!b1).value() == (!b2).value(); } +inline bool operator!=(MinBool b1, bool b2) { return (!b1).value() != !b2; } +inline bool operator!=(bool b1, MinBool b2) { return (!b1) != (!b2).value(); } +inline bool operator!=(MinBool b1, MinBool b2) { return (!b1).value() != (!b2).value(); } + +class LIBMINIMAL_API MinBoolUser +{ +public: + MinBoolUser() : m_minbool(MinBool(false)) {} + virtual ~MinBoolUser() {} + inline MinBool minBool() { return m_minbool; } + inline void setMinBool(MinBool minBool) { m_minbool = minBool; } + virtual MinBool invertedMinBool() { return !m_minbool; } + inline MinBool callInvertedMinBool() { return invertedMinBool(); } +private: + MinBool m_minbool; +}; + +#endif diff --git a/sources/shiboken2/tests/libminimal/obj.cpp b/sources/shiboken2/tests/libminimal/obj.cpp new file mode 100644 index 000000000..a1d18d0e3 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/obj.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "obj.h" + +Obj::Obj(int objId) : m_objId(objId) +{ +} + +Obj::~Obj() +{ +} + +bool +Obj::virtualMethod(int val) +{ + return !bool(val%2); +} + diff --git a/sources/shiboken2/tests/libminimal/obj.h b/sources/shiboken2/tests/libminimal/obj.h new file mode 100644 index 000000000..16f838e19 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/obj.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJ_H +#define OBJ_H + +#include "libminimalmacros.h" + +class LIBMINIMAL_API Obj +{ +public: + explicit Obj(int objId); + virtual ~Obj(); + + int objId() { return m_objId; } + void setObjId(int objId) { m_objId = objId; } + + virtual bool virtualMethod(int val); + bool callVirtualMethod(int val) { return virtualMethod(val); } + + virtual Obj* passObjectType(Obj* obj) { return obj; } + Obj* callPassObjectType(Obj* obj) { return passObjectType(obj); } + + virtual Obj* passObjectTypeReference(Obj& obj) { return &obj; } + Obj* callPassObjectTypeReference(Obj& obj) { return passObjectTypeReference(obj); } + +private: + Obj(const Obj&); + Obj& operator=(const Obj&); + int m_objId; +}; + +#endif // OBJ_H + diff --git a/sources/shiboken2/tests/libminimal/typedef.cpp b/sources/shiboken2/tests/libminimal/typedef.cpp new file mode 100644 index 000000000..4948d9944 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/typedef.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "typedef.h" + +// +// Test wrapping of a typedef +// +bool arrayFuncInt(std::vector<int> a) +{ + return a.empty(); +} + +bool arrayFuncIntTypedef(MyArray a) +{ + return arrayFuncInt(a); +} + +std::vector<int> arrayFuncIntReturn(int size) +{ + return std::vector<int>(size); +} + +MyArray arrayFuncIntReturnTypedef(int size) +{ + return arrayFuncIntReturn(size); +} + +// +// Test wrapping of a typedef of a typedef +// +bool arrayFunc(std::vector<int> a) +{ + return a.empty(); +} + +bool arrayFuncTypedef(MyArray a) +{ + return arrayFunc(a); +} + +std::vector<int> arrayFuncReturn(int size) +{ + return std::vector<int>(size); +} + +MyArray arrayFuncReturnTypedef(int size) +{ + return arrayFuncReturn(size); +} diff --git a/sources/shiboken2/tests/libminimal/typedef.h b/sources/shiboken2/tests/libminimal/typedef.h new file mode 100644 index 000000000..f54a6ed18 --- /dev/null +++ b/sources/shiboken2/tests/libminimal/typedef.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TYPEDEF_H +#define TYPEDEF_H + +#include "libminimalmacros.h" + +#include <vector> + +// Test wrapping of a typedef +typedef std::vector<int> MyArrayInt; + +LIBMINIMAL_API bool arrayFuncInt(std::vector<int> a); +LIBMINIMAL_API bool arrayFuncIntTypedef(MyArrayInt a); + +LIBMINIMAL_API std::vector<int> arrayFuncIntReturn(int size); +LIBMINIMAL_API MyArrayInt arrayFuncIntReturnTypedef(int size); + +// Test wrapping of a typedef of a typedef +typedef MyArrayInt MyArray; + +LIBMINIMAL_API bool arrayFunc(std::vector<int> a); +LIBMINIMAL_API bool arrayFuncTypedef(MyArray a); + +LIBMINIMAL_API std::vector<int> arrayFuncReturn(int size); +LIBMINIMAL_API MyArray arrayFuncReturnTypedef(int size); + +#endif diff --git a/sources/shiboken2/tests/libminimal/val.h b/sources/shiboken2/tests/libminimal/val.h new file mode 100644 index 000000000..5ba4b3aff --- /dev/null +++ b/sources/shiboken2/tests/libminimal/val.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VAL_H +#define VAL_H + +#include "libminimalmacros.h" + +class LIBMINIMAL_API Val +{ +public: + explicit Val(int valId) : m_valId(valId) {} + virtual ~Val() {} + + int valId() { return m_valId; } + void setValId(int valId) { m_valId = valId; } + + virtual Val passValueType(Val val) { return val; } + Val callPassValueType(Val val) { return passValueType(val); } + + virtual Val* passValueTypePointer(Val* val) { return val; } + Val* callPassValueTypePointer(Val* val) { return passValueTypePointer(val); } + + virtual Val* passValueTypeReference(Val& val) { return &val; } + Val* callPassValueTypeReference(Val& val) { return passValueTypeReference(val); } + + enum ValEnum { One, Other }; + ValEnum oneOrTheOtherEnumValue(ValEnum enumValue) { return enumValue == One ? Other : One; } +private: + int m_valId; +}; + +#endif // VAL_H + diff --git a/sources/shiboken2/tests/libother/CMakeLists.txt b/sources/shiboken2/tests/libother/CMakeLists.txt new file mode 100644 index 000000000..9b3cf5552 --- /dev/null +++ b/sources/shiboken2/tests/libother/CMakeLists.txt @@ -0,0 +1,18 @@ +project(libother) + +set(libother_SRC +number.cpp +otherderived.cpp +otherobjecttype.cpp +othermultiplederived.cpp +) + +add_definitions("-DLIBOTHER_BUILD") +add_library(libother SHARED ${libother_SRC}) +set_property(TARGET libother PROPERTY PREFIX "") + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${libsample_SOURCE_DIR} + ${libsample_SOURCE_DIR}/..) +target_link_libraries(libother libsample) + diff --git a/sources/shiboken2/tests/libother/extendsnoimplicitconversion.h b/sources/shiboken2/tests/libother/extendsnoimplicitconversion.h new file mode 100644 index 000000000..744bfdc0d --- /dev/null +++ b/sources/shiboken2/tests/libother/extendsnoimplicitconversion.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EXTENDSNOIMPLICITCONVERSION_H +#define EXTENDSNOIMPLICITCONVERSION_H + +#include "libothermacros.h" +#include "noimplicitconversion.h" + +class ExtendsNoImplicitConversion +{ +public: + explicit ExtendsNoImplicitConversion(int objId) : m_objId(objId) {}; + inline int objId() const { return m_objId; } + inline operator NoImplicitConversion() const { return NoImplicitConversion(m_objId); } +private: + int m_objId; +}; +#endif // EXTENDSNOIMPLICITCONVERSION_H + diff --git a/sources/shiboken2/tests/libother/libothermacros.h b/sources/shiboken2/tests/libother/libothermacros.h new file mode 100644 index 000000000..41a4fb13e --- /dev/null +++ b/sources/shiboken2/tests/libother/libothermacros.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIBOTHERMACROS_H +#define LIBOTHERMACROS_H + +#if defined _WIN32 || defined __CYGWIN__ + #if LIBOTHER_BUILD + #define LIBOTHER_API __declspec(dllexport) + #else + #define LIBOTHER_API __declspec(dllimport) + #endif +#else +#if __GNUC__ >= 4 + #define LIBOTHER_API __attribute__ ((visibility("default"))) +#else + #define LIBOTHER_API +#endif +#endif + +#endif diff --git a/sources/shiboken2/tests/libother/number.cpp b/sources/shiboken2/tests/libother/number.cpp new file mode 100644 index 000000000..4da254286 --- /dev/null +++ b/sources/shiboken2/tests/libother/number.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "number.h" +#include <cstring> +#include <sstream> + +using namespace std; + +Str +Number::toStr() const +{ + ostringstream in; + in << m_value; + return in.str().c_str(); +} + +Point +operator*(const Point& p, const Number& n) +{ + return Point(p.x() * n.value(), p.y() * n.value()); +} + +Complex +Number::toComplex() const +{ + return Complex(m_value); +} + +Number +Number::fromComplex(Complex cpx) +{ + return Number(cpx.real()); +} diff --git a/sources/shiboken2/tests/libother/number.h b/sources/shiboken2/tests/libother/number.h new file mode 100644 index 000000000..4286068d5 --- /dev/null +++ b/sources/shiboken2/tests/libother/number.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef NUMBER_H +#define NUMBER_H + +#include "libothermacros.h" +#include "str.h" +#include "point.h" +#include "complex.h" + +class LIBOTHER_API Number +{ +public: + explicit Number(int value) : m_value(value) {}; + inline int value() const { return m_value; } + + Str toStr() const; + inline operator Str() const { return toStr(); } + + friend LIBOTHER_API Point operator*(const Point&, const Number&); + + Complex toComplex() const; + static Number fromComplex(Complex cpx); + +private: + int m_value; +}; + +LIBOTHER_API Point operator*(const Point&, const Number&); + +#endif // NUMBER_H diff --git a/sources/shiboken2/tests/libother/otherderived.cpp b/sources/shiboken2/tests/libother/otherderived.cpp new file mode 100644 index 000000000..4128d73ff --- /dev/null +++ b/sources/shiboken2/tests/libother/otherderived.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "otherderived.h" + +OtherDerived::OtherDerived(int id) : Abstract(id) +{ +} + +OtherDerived::~OtherDerived() +{ +} + +Abstract* +OtherDerived::createObject() +{ + static int id = 100; + return new OtherDerived(id++); +} + +void +OtherDerived::pureVirtual() +{ +} + +void* +OtherDerived::pureVirtualReturningVoidPtr() +{ + return 0; +} + +void +OtherDerived::unpureVirtual() +{ +} + diff --git a/sources/shiboken2/tests/libother/otherderived.h b/sources/shiboken2/tests/libother/otherderived.h new file mode 100644 index 000000000..792b7c408 --- /dev/null +++ b/sources/shiboken2/tests/libother/otherderived.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OTHERDERIVED_H +#define OTHERDERIVED_H + +#include "libothermacros.h" +#include "abstract.h" +#include "derived.h" +#include "objecttype.h" +#include "complex.h" + +class ObjectType; + +class LIBOTHER_API OtherDerived : public Abstract +{ +public: + OtherDerived(int id = -1); + virtual ~OtherDerived(); + virtual void pureVirtual(); + virtual void* pureVirtualReturningVoidPtr(); + virtual void unpureVirtual(); + virtual PrintFormat returnAnEnum() { return Short; } + + inline void useObjectTypeFromOtherModule(ObjectType*) {} + inline Event useValueTypeFromOtherModule(const Event& e) { return e; } + inline Complex useValueTypeFromOtherModule(const Complex& c) { return c; } + inline void useEnumTypeFromOtherModule(OverloadedFuncEnum) {} + + // factory method + static Abstract* createObject(); + + void hideFunction(HideType*) {} + +protected: + inline const char* getClassName() { return className(); } + virtual const char* className() { return "OtherDerived"; } +}; +#endif // OTHERDERIVED_H + diff --git a/sources/shiboken2/tests/libother/othermultiplederived.cpp b/sources/shiboken2/tests/libother/othermultiplederived.cpp new file mode 100644 index 000000000..77fa44b83 --- /dev/null +++ b/sources/shiboken2/tests/libother/othermultiplederived.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "othermultiplederived.h" + +VirtualMethods OtherMultipleDerived::returnUselessClass() +{ + return VirtualMethods(); +} + +Base1* OtherMultipleDerived::createObject(const std::string& objName) +{ + if (objName == "Base1") + return new Base1; + else if (objName == "MDerived1") + return new MDerived1; + else if (objName == "SonOfMDerived1") + return new SonOfMDerived1; + else if (objName == "MDerived3") + return new MDerived3; + else if (objName == "OtherMultipleDerived") + return new OtherMultipleDerived; + return 0; +} + diff --git a/sources/shiboken2/tests/libother/othermultiplederived.h b/sources/shiboken2/tests/libother/othermultiplederived.h new file mode 100644 index 000000000..e75504320 --- /dev/null +++ b/sources/shiboken2/tests/libother/othermultiplederived.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OTHERMULTIPLEDERIVED_H +#define OTHERMULTIPLEDERIVED_H + +#include "libothermacros.h" +#include "multiple_derived.h" +#include "virtualmethods.h" + +class ObjectType; + +class LIBOTHER_API OtherMultipleDerived : public MDerived1 +{ +public: + // this will use CppCopier from other module (bug#142) + VirtualMethods returnUselessClass(); + static Base1* createObject(const std::string& objName); +}; + +#endif diff --git a/sources/shiboken2/tests/libother/otherobjecttype.cpp b/sources/shiboken2/tests/libother/otherobjecttype.cpp new file mode 100644 index 000000000..266b2583e --- /dev/null +++ b/sources/shiboken2/tests/libother/otherobjecttype.cpp @@ -0,0 +1,36 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "otherobjecttype.h" + +Collector& +operator<<(Collector& collector, const OtherObjectType& obj) +{ + collector << obj.identifier()*2; + return collector; +} diff --git a/sources/shiboken2/tests/libother/otherobjecttype.h b/sources/shiboken2/tests/libother/otherobjecttype.h new file mode 100644 index 000000000..2bba912b6 --- /dev/null +++ b/sources/shiboken2/tests/libother/otherobjecttype.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OTHEROBJECTTYPE_H +#define OTHEROBJECTTYPE_H + +#include <list> +#include "str.h" + +#include "libothermacros.h" +#include "objecttype.h" +#include "collector.h" + +class OtherObjectType : public ObjectType +{ +public: + +}; + + +LIBOTHER_API Collector& operator<<(Collector&, const OtherObjectType&); + +#endif // OTHEROBJECTTYPE_H + diff --git a/sources/shiboken2/tests/libsample/CMakeLists.txt b/sources/shiboken2/tests/libsample/CMakeLists.txt new file mode 100644 index 000000000..7bbc0c3dd --- /dev/null +++ b/sources/shiboken2/tests/libsample/CMakeLists.txt @@ -0,0 +1,57 @@ +project(libsample) + +set(libsample_SRC +abstract.cpp +blackbox.cpp +bytearray.cpp +bucket.cpp +collector.cpp +complex.cpp +onlycopy.cpp +derived.cpp +echo.cpp +functions.cpp +handle.cpp +implicitconv.cpp +injectcode.cpp +listuser.cpp +modifications.cpp +mapuser.cpp +modified_constructor.cpp +multiple_derived.cpp +objectmodel.cpp +objecttype.cpp +objecttypeholder.cpp +objecttypelayout.cpp +objecttypeoperators.cpp +objectview.cpp +overload.cpp +overloadsort.cpp +pairuser.cpp +pen.cpp +photon.cpp +point.cpp +pointf.cpp +polygon.cpp +protected.cpp +reference.cpp +sample.cpp +samplenamespace.cpp +sbkdate.cpp +simplefile.cpp +size.cpp +sometime.cpp +str.cpp +strlist.cpp +templateptr.cpp +transform.cpp +virtualmethods.cpp +expression.cpp +filter.cpp +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_definitions("-DLIBSAMPLE_BUILD") +add_library(libsample SHARED ${libsample_SRC}) +set_property(TARGET libsample PROPERTY PREFIX "") + diff --git a/sources/shiboken2/tests/libsample/abstract.cpp b/sources/shiboken2/tests/libsample/abstract.cpp new file mode 100644 index 000000000..ab00a3bcc --- /dev/null +++ b/sources/shiboken2/tests/libsample/abstract.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "abstract.h" +#include "objecttype.h" + +using namespace std; + +const int Abstract::staticPrimitiveField = 0; + +Abstract::Abstract(int id) : m_id(id) +{ + primitiveField = 123; + valueTypeField = Point(12, 34); + objectTypeField = 0; + bitField = 0; +} + +Abstract::~Abstract() +{ +} + +void +Abstract::unpureVirtual() +{ +} + +void +Abstract::callUnpureVirtual() +{ + this->unpureVirtual(); +} + + +void +Abstract::callPureVirtual() +{ + this->pureVirtual(); +} + +void +Abstract::show(PrintFormat format) +{ + cout << '<'; + switch(format) { + case Short: + cout << this; + break; + case Verbose: + cout << "class " << className() << " | cptr: " << this; + cout << ", id: " << m_id; + break; + case OnlyId: + cout << "id: " << m_id; + break; + case ClassNameAndId: + cout << className() << " - id: " << m_id; + break; + } + cout << '>'; +} + +void Abstract::callVirtualGettingEnum(PrintFormat p) +{ + virtualGettingAEnum(p); +} + +void Abstract::virtualGettingAEnum(Abstract::PrintFormat p) +{ +} + diff --git a/sources/shiboken2/tests/libsample/abstract.h b/sources/shiboken2/tests/libsample/abstract.h new file mode 100644 index 000000000..77a43f3d1 --- /dev/null +++ b/sources/shiboken2/tests/libsample/abstract.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ABSTRACT_H +#define ABSTRACT_H + +#include "libsamplemacros.h" +#include "point.h" +#include "complex.h" + +class ObjectType; + +// this class is not exported to python +class HideType +{ +}; + +class LIBSAMPLE_API Abstract +{ +private: + enum PrivateEnum { + PrivValue0, + PrivValue1, + PrivValue2 = PrivValue1 + 2 + }; +public: + enum PrintFormat { + Short, + Verbose, + OnlyId, + ClassNameAndId, + DummyItemToTestPrivateEnum1 = Abstract::PrivValue1, + DummyItemToTestPrivateEnum2 = PrivValue2, + }; + + enum Type { + TpAbstract, TpDerived + }; + + static const int staticPrimitiveField; + int primitiveField; + Complex userPrimitiveField; + Point valueTypeField; + ObjectType* objectTypeField; + + Abstract(int id = -1); + virtual ~Abstract(); + + inline int id() { return m_id; } + + // factory method + inline static Abstract* createObject() { return 0; } + + // method that receives an Object Type + inline static int getObjectId(Abstract* obj) { return obj->id(); } + + virtual void pureVirtual() = 0; + virtual void* pureVirtualReturningVoidPtr() = 0; + virtual void unpureVirtual(); + + virtual PrintFormat returnAnEnum() = 0; + void callVirtualGettingEnum(PrintFormat p); + virtual void virtualGettingAEnum(PrintFormat p); + + void callPureVirtual(); + void callUnpureVirtual(); + + void show(PrintFormat format = Verbose); + virtual Type type() const { return TpAbstract; } + + virtual void hideFunction(HideType* arg) = 0; + +protected: + virtual const char* className() { return "Abstract"; } + + // Protected bit-field structure member. + unsigned int bitField: 1; + +private: + int m_id; +}; +#endif // ABSTRACT_H diff --git a/sources/shiboken2/tests/libsample/blackbox.cpp b/sources/shiboken2/tests/libsample/blackbox.cpp new file mode 100644 index 000000000..9a10850f3 --- /dev/null +++ b/sources/shiboken2/tests/libsample/blackbox.cpp @@ -0,0 +1,131 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "blackbox.h" + +using namespace std; + +BlackBox::~BlackBox() +{ + // Free all maps. + while (!m_objects.empty()) { + delete (*m_objects.begin()).second; + m_objects.erase(m_objects.begin()); + } + while (!m_points.empty()) { + delete (*m_points.begin()).second; + m_points.erase(m_points.begin()); + } +} + +int +BlackBox::keepObjectType(ObjectType* object) +{ + m_ticket++; + std::pair<int, ObjectType*> item(m_ticket, object); + m_objects.insert(item); + object->setParent(0); + + return m_ticket; +} + +ObjectType* +BlackBox::retrieveObjectType(int ticket) +{ + map<int, ObjectType*>::iterator it = m_objects.find(ticket); + if (it != m_objects.end()) { + ObjectType* second = it->second; + m_objects.erase(it); + return second; + } + return 0; +} + +void +BlackBox::disposeObjectType(int ticket) +{ + ObjectType* object = retrieveObjectType(ticket); + if (object) + delete object; +} + +int +BlackBox::keepPoint(Point* point) +{ + m_ticket++; + std::pair<int, Point*> item(m_ticket, point); + m_points.insert(item); + + return m_ticket; +} + +Point* +BlackBox::retrievePoint(int ticket) +{ + map<int, Point*>::iterator it = m_points.find(ticket); + if (it != m_points.end()) { + Point* second = it->second; + m_points.erase(it); + return second; + } + return 0; +} + +void +BlackBox::disposePoint(int ticket) +{ + Point* point = retrievePoint(ticket); + if (point) + delete point; +} + + +std::list<ObjectType*> +BlackBox::objects() +{ + std::list<ObjectType*> l; + map<int, ObjectType*>::iterator it; + + for ( it = m_objects.begin() ; it != m_objects.end(); it++ ) + l.push_back((*it).second); + + return l; +} + +std::list<Point*> +BlackBox::points() +{ + std::list<Point*> l; + map<int, Point*>::iterator it; + + for ( it = m_points.begin() ; it != m_points.end(); it++ ) + l.push_back((*it).second); + + return l; +} + diff --git a/sources/shiboken2/tests/libsample/blackbox.h b/sources/shiboken2/tests/libsample/blackbox.h new file mode 100644 index 000000000..de776668c --- /dev/null +++ b/sources/shiboken2/tests/libsample/blackbox.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BLACKBOX_H +#define BLACKBOX_H + +#include "libsamplemacros.h" +#include <map> +#include "objecttype.h" +#include "point.h" + +class LIBSAMPLE_API BlackBox +{ +public: + typedef std::map<int, ObjectType*> ObjectTypeMap; + typedef std::map<int, Point*> PointMap; + + BlackBox() { m_ticket = -1;} + ~BlackBox(); + + int keepObjectType(ObjectType* object); + ObjectType* retrieveObjectType(int ticket); + void disposeObjectType(int ticket); + + int keepPoint(Point* point); + Point* retrievePoint(int ticket); + void disposePoint(int ticket); + + std::list<ObjectType*> objects(); + std::list<Point*> points(); + + inline void referenceToValuePointer(Point*&) {} + inline void referenceToObjectPointer(ObjectType*&) {} + +private: + ObjectTypeMap m_objects; + PointMap m_points; + int m_ticket; +}; + +#endif // BLACKBOX_H + diff --git a/sources/shiboken2/tests/libsample/bucket.cpp b/sources/shiboken2/tests/libsample/bucket.cpp new file mode 100644 index 000000000..50a1310e7 --- /dev/null +++ b/sources/shiboken2/tests/libsample/bucket.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "bucket.h" +#include <iostream> + +#ifdef _WIN32 // _WIN32 is defined by all Windows 32 and 64 bit compilers, but not by others. +#include <windows.h> +#define SLEEP(x) Sleep(x) +#else +#include <unistd.h> +#define SLEEP(x) usleep(x) +#endif + + +using namespace std; + +Bucket::Bucket() : m_locked(false) +{ +} + +void Bucket::push(int x) +{ + m_data.push_back(x); +} + +int Bucket::pop(void) +{ + int x = 0; + + if (m_data.size() > 0) { + x = m_data.front(); + m_data.pop_front(); + } + + return x; +} + +bool Bucket::empty() +{ + return m_data.empty(); +} + +void Bucket::lock() +{ + m_locked = true; + while (m_locked) { SLEEP(300); } +} + +void Bucket::unlock() +{ + m_locked = false; +} + +bool Bucket::virtualBlockerMethod() +{ + lock(); + // The return value was added just for diversity sake. + return true; +} + diff --git a/sources/shiboken2/tests/libsample/bucket.h b/sources/shiboken2/tests/libsample/bucket.h new file mode 100644 index 000000000..bd8648b6b --- /dev/null +++ b/sources/shiboken2/tests/libsample/bucket.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BUCKET_H +#define BUCKET_H + +#include "libsamplemacros.h" +#include "objecttype.h" +#include <list> + +class ObjectType; + +class LIBSAMPLE_API Bucket : public ObjectType +{ +public: + Bucket(); + void push(int); + int pop(); + bool empty(); + void lock(); + inline bool locked() { return m_locked; } + void unlock(); + + virtual bool virtualBlockerMethod(); + inline bool callVirtualBlockerMethodButYouDontKnowThis() { return virtualBlockerMethod(); } + +private: + std::list<int> m_data; + + volatile bool m_locked; +}; + +#endif // BUCKET_H + diff --git a/sources/shiboken2/tests/libsample/bytearray.cpp b/sources/shiboken2/tests/libsample/bytearray.cpp new file mode 100644 index 000000000..c86ada124 --- /dev/null +++ b/sources/shiboken2/tests/libsample/bytearray.cpp @@ -0,0 +1,214 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <cstring> +#include "bytearray.h" + +ByteArray::ByteArray() +{ + m_data = std::vector<char>(1); + m_data[0] = '\0'; +} + +ByteArray::ByteArray(char c) +{ + m_data = std::vector<char>(2); + m_data[0] = c; + m_data[1] = '\0'; +} + +ByteArray::ByteArray(const char* data) +{ + size_t len = strlen(data); + m_data = std::vector<char>(len + 1); + memcpy(&m_data[0], data, len); + m_data[len] = '\0'; +} + +ByteArray::ByteArray(const char* data, int len) +{ + m_data = std::vector<char>(len + 1); + memcpy(&m_data[0], data, len); + m_data[len] = '\0'; +} + +ByteArray::ByteArray(const ByteArray& other) +{ + m_data = std::vector<char>(other.size() + 1); + memcpy(&m_data[0], &other.m_data[0], other.size()); + m_data[other.size()] = '\0'; +} + +int +ByteArray::size() const +{ + return m_data.size() - 1; +} + +char +ByteArray::at(int pos) const +{ + return m_data[pos]; +} + +const char* +ByteArray::data() const +{ + return &(m_data[0]); +} + +ByteArray& +ByteArray::append(char c) +{ + m_data.pop_back(); + m_data.push_back(c); + m_data.push_back('\0'); + return *this; +} + +ByteArray& +ByteArray::append(const char* data) +{ + m_data.pop_back(); + for (int i = 0; i < (int)strlen(data); ++i) + m_data.push_back(data[i]); + m_data.push_back('\0'); + return *this; +} + +ByteArray& +ByteArray::append(const char* data, int len) +{ + m_data.pop_back(); + for (int i = 0; i < len; ++i) + m_data.push_back(data[i]); + m_data.push_back('\0'); + return *this; +} + +ByteArray& +ByteArray::append(const ByteArray& other) +{ + m_data.pop_back(); + for (int i = 0; i < (int)other.m_data.size(); ++i) + m_data.push_back(other.m_data[i]); + m_data.push_back('\0'); + return *this; +} + +static bool compare(const std::vector<char>& mine, const char* other) +{ + for (int i = 0; i < (int)mine.size() - 1; ++i) { + if (mine[i] != other[i]) + return false; + } + return true; +} + +bool +ByteArray::operator==(const ByteArray& other) const +{ + return compare(m_data, &other.m_data[0]); +} +bool +operator==(const ByteArray& ba1, const char* ba2) +{ + return compare(ba1.m_data, ba2); +} +bool +operator==(const char* ba1, const ByteArray& ba2) +{ + return compare(ba2.m_data, ba1); +} + +bool +ByteArray::operator!=(const ByteArray& other) const +{ + return !(m_data == other.m_data); +} +bool +operator!=(const ByteArray& ba1, const char* ba2) +{ + return !(ba1 == ba2); +} +bool +operator!=(const char* ba1, const ByteArray& ba2) +{ + return !(ba1 == ba2); +} + +ByteArray& +ByteArray::operator+=(char c) +{ + return append(c); +} +ByteArray& +ByteArray::operator+=(const char* data) +{ + return append(data); +} +ByteArray& +ByteArray::operator+=(const ByteArray& other) +{ + return append(other); +} + +ByteArray +operator+(const ByteArray& ba1, const ByteArray& ba2) +{ + return ByteArray(ba1) += ba2; +} +ByteArray +operator+(const ByteArray& ba1, const char* ba2) +{ + return ByteArray(ba1) += ByteArray(ba2); +} +ByteArray +operator+(const char* ba1, const ByteArray& ba2) +{ + return ByteArray(ba1) += ba2; +} +ByteArray +operator+(const ByteArray& ba1, char ba2) +{ + return ByteArray(ba1) += ByteArray(ba2); +} +ByteArray +operator+(char ba1, const ByteArray& ba2) +{ + return ByteArray(ba1) += ba2; +} + +unsigned int +ByteArray::hash(const ByteArray& byteArray) +{ + unsigned int result = 0; + for (int i = 0; i < (int)byteArray.m_data.size(); ++i) + result = 5 * result + byteArray.m_data[i]; + return result; +} diff --git a/sources/shiboken2/tests/libsample/bytearray.h b/sources/shiboken2/tests/libsample/bytearray.h new file mode 100644 index 000000000..d2219ee5d --- /dev/null +++ b/sources/shiboken2/tests/libsample/bytearray.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BYTEARRAY_H +#define BYTEARRAY_H + +#include "str.h" +#include "libsamplemacros.h" +#include <vector> + +class LIBSAMPLE_API ByteArray +{ +public: + ByteArray(); + ByteArray(char data); + ByteArray(const char* data); + ByteArray(const char* data, int len); + ByteArray(const ByteArray& other); + + int size() const; + char at(int i) const; + char operator[](int i) const; + + const char* data() const; + + ByteArray& append(char c); + ByteArray& append(const char* data); + ByteArray& append(const char* data, int len); + ByteArray& append(const ByteArray& other); + + bool operator==(const ByteArray& other) const; + bool operator!=(const ByteArray& other) const; + + ByteArray& operator+=(char c); + ByteArray& operator+=(const char* data); + ByteArray& operator+=(const ByteArray& other); + + static unsigned int hash(const ByteArray& byteArray); +private: + std::vector<char> m_data; + friend LIBSAMPLE_API bool operator==(const ByteArray& ba1, const char* ba2); + friend LIBSAMPLE_API bool operator==(const char* ba1, const ByteArray& ba2); + friend LIBSAMPLE_API bool operator!=(const ByteArray& ba1, const char* ba2); + friend LIBSAMPLE_API bool operator!=(const char* ba1, const ByteArray& ba2); + + friend LIBSAMPLE_API ByteArray operator+(const ByteArray& ba1, const ByteArray& ba2); + friend LIBSAMPLE_API ByteArray operator+(const ByteArray& ba1, const char* ba2); + friend LIBSAMPLE_API ByteArray operator+(const char* ba1, const ByteArray& ba2); + friend LIBSAMPLE_API ByteArray operator+(const ByteArray& ba1, char ba2); + friend LIBSAMPLE_API ByteArray operator+(char ba1, const ByteArray& ba2); +}; + +LIBSAMPLE_API bool operator==(const ByteArray& ba1, const char* ba2); +LIBSAMPLE_API bool operator==(const char* ba1, const ByteArray& ba2); +LIBSAMPLE_API bool operator!=(const ByteArray& ba1, const char* ba2); +LIBSAMPLE_API bool operator!=(const char* ba1, const ByteArray& ba2); + +LIBSAMPLE_API ByteArray operator+(const ByteArray& ba1, const ByteArray& ba2); +LIBSAMPLE_API ByteArray operator+(const ByteArray& ba1, const char* ba2); +LIBSAMPLE_API ByteArray operator+(const char* ba1, const ByteArray& ba2); +LIBSAMPLE_API ByteArray operator+(const ByteArray& ba1, char ba2); +LIBSAMPLE_API ByteArray operator+(char ba1, const ByteArray& ba2); + +#endif // BYTEARRAY_H diff --git a/sources/shiboken2/tests/libsample/collector.cpp b/sources/shiboken2/tests/libsample/collector.cpp new file mode 100644 index 000000000..e60ed3af3 --- /dev/null +++ b/sources/shiboken2/tests/libsample/collector.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "collector.h" + +void Collector::clear() +{ + m_items.clear(); +} + +Collector& Collector::operator<<(ObjectType::Identifier item) +{ + m_items.push_back(item); + return *this; +} + +Collector& Collector::operator<<(const ObjectType *obj) +{ + m_items.push_back(obj->identifier()); + return *this; +} + +std::list<ObjectType::Identifier> Collector::items() +{ + return m_items; +} + +int Collector::size() +{ + return (int) m_items.size(); +} + +Collector &operator<<(Collector &s, const IntWrapper &w) +{ + s << w.value; + return s; +} diff --git a/sources/shiboken2/tests/libsample/collector.h b/sources/shiboken2/tests/libsample/collector.h new file mode 100644 index 000000000..fe82909cb --- /dev/null +++ b/sources/shiboken2/tests/libsample/collector.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COLLECTOR_H +#define COLLECTOR_H + +#include <list> +#include "libsamplemacros.h" + +#include "objecttype.h" + +class LIBSAMPLE_API Collector +{ +public: + Collector() {} + virtual ~Collector() {} + + void clear(); + + Collector& operator<<(ObjectType::Identifier item); + + Collector& operator<<(const ObjectType *); + + std::list<ObjectType::Identifier> items(); + int size(); + +private: + std::list<ObjectType::Identifier> m_items; + + Collector(const Collector&); + Collector& operator=(const Collector&); +}; + +/* Helper for testing external operators */ +class IntWrapper +{ +public: + IntWrapper(int x=0):value(x){} + + int value; +}; + +LIBSAMPLE_API Collector &operator<<(Collector&, const IntWrapper&); + +#endif // COLLECTOR_H + diff --git a/sources/shiboken2/tests/libsample/complex.cpp b/sources/shiboken2/tests/libsample/complex.cpp new file mode 100644 index 000000000..10e4b3f58 --- /dev/null +++ b/sources/shiboken2/tests/libsample/complex.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "complex.h" + +using namespace std; + +Complex::Complex(double real, double imag) + : m_real(real), m_imag(imag) +{ +} + +Complex +Complex::operator+(Complex& other) +{ + Complex result; + result.setReal(m_real + other.real()); + result.setImaginary(m_imag + other.imag()); + return result; +} + +void +Complex::show() +{ + cout << "(real: " << m_real << ", imag: " << m_imag << ")"; +} + + diff --git a/sources/shiboken2/tests/libsample/complex.h b/sources/shiboken2/tests/libsample/complex.h new file mode 100644 index 000000000..cb167bde9 --- /dev/null +++ b/sources/shiboken2/tests/libsample/complex.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COMPLEX_H +#define COMPLEX_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Complex +{ +public: + Complex(double real = 0.0, double imag = 0.0); + ~Complex() {} + + inline double real() const { return m_real; } + inline void setReal(double real) { m_real = real; } + inline double imag() const { return m_imag; } + inline void setImaginary(double imag) { m_imag = imag; } + + Complex operator+(Complex& other); + + void show(); + +private: + double m_real; + double m_imag; +}; + +#endif // COMPLEX_H + diff --git a/sources/shiboken2/tests/libsample/ctorconvrule.h b/sources/shiboken2/tests/libsample/ctorconvrule.h new file mode 100644 index 000000000..776f4744c --- /dev/null +++ b/sources/shiboken2/tests/libsample/ctorconvrule.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CTORCONVRULE_H +#define CTORCONVRULE_H + +#include "libsamplemacros.h" + +class CtorConvRule +{ +public: + explicit CtorConvRule(long value) : m_value(value) {} + virtual ~CtorConvRule() {} + virtual void dummyVirtualMethod() {} + long value() { return m_value; } +private: + long m_value; +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/cvlist.h b/sources/shiboken2/tests/libsample/cvlist.h new file mode 100644 index 000000000..c74007945 --- /dev/null +++ b/sources/shiboken2/tests/libsample/cvlist.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CONSTVALUELIST_H +#define CONSTVALUELIST_H + +#include <list> +#include "libsamplemacros.h" + +class CVValueType +{ + CVValueType(); +}; + +typedef std::list<const CVValueType*> const_ptr_value_list; + +// This tests binding generation for a container of a const value type. The +// class doesn't need to do anything; this is just to verify that the generated +// binding code (the container conversion in particular) is const-valid. + +class CVListUser +{ +public: + static const_ptr_value_list produce() { return const_ptr_value_list(); } + static void consume(const const_ptr_value_list& l) { (void)l; } +}; + +#endif // LIST_H diff --git a/sources/shiboken2/tests/libsample/derived.cpp b/sources/shiboken2/tests/libsample/derived.cpp new file mode 100644 index 000000000..00ac8ebe5 --- /dev/null +++ b/sources/shiboken2/tests/libsample/derived.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "derived.h" + +using namespace std; + +Derived::Derived(int id) : Abstract(id) +{ +} + +Derived::~Derived() +{ +} + +Abstract* +Derived::createObject() +{ + static int id = 100; + return new Derived(id++); +} + +void +Derived::pureVirtual() +{ +} + +void* +Derived::pureVirtualReturningVoidPtr() +{ + return 0; +} + +void +Derived::unpureVirtual() +{ +} + +bool +Derived::singleArgument(bool b) +{ + return !b; +} + +double +Derived::defaultValue(int n) +{ + return ((double) n) + 0.1; +} + +OverloadedFuncEnum +Derived::overloaded(int i, int d) +{ + return OverloadedFunc_ii; +} + +OverloadedFuncEnum +Derived::overloaded(double n) +{ + return OverloadedFunc_d; +} + +Derived::OtherOverloadedFuncEnum +Derived::otherOverloaded(int a, int b, bool c, double d) +{ + return OtherOverloadedFunc_iibd; +} + +Derived::OtherOverloadedFuncEnum +Derived::otherOverloaded(int a, double b) +{ + return OtherOverloadedFunc_id; +} + +struct SecretClass : public Abstract { + virtual void pureVirtual() {} + virtual void* pureVirtualReturningVoidPtr() { return 0; } + virtual PrintFormat returnAnEnum() { return Short; } + void hideFunction(HideType*){}; +}; + +Abstract* Derived::triggerImpossibleTypeDiscovery() +{ + return new SecretClass; +} + +struct AnotherSecretClass : public Derived { +}; + +Abstract* Derived::triggerAnotherImpossibleTypeDiscovery() +{ + return new AnotherSecretClass; +} diff --git a/sources/shiboken2/tests/libsample/derived.h b/sources/shiboken2/tests/libsample/derived.h new file mode 100644 index 000000000..5c57e4691 --- /dev/null +++ b/sources/shiboken2/tests/libsample/derived.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DERIVED_H +#define DERIVED_H + +#include "libsamplemacros.h" +#include "abstract.h" + +enum OverloadedFuncEnum { + OverloadedFunc_ii, + OverloadedFunc_d +}; + +class LIBSAMPLE_API Derived : public Abstract +{ +public: + enum OtherOverloadedFuncEnum { + OtherOverloadedFunc_iibd, + OtherOverloadedFunc_id + }; + + class SomeInnerClass { + public: + void uselessMethod() {} + SomeInnerClass operator+(const SomeInnerClass& other) { return other; } + bool operator==(const SomeInnerClass& other) { return true; } + }; + + Derived(int id = -1); + virtual ~Derived(); + virtual void pureVirtual(); + virtual void* pureVirtualReturningVoidPtr(); + virtual void unpureVirtual(); + + virtual PrintFormat returnAnEnum() { return Short; } + virtual Type type() const { return TpDerived; } + + // factory method + static Abstract* createObject(); + + // single argument + bool singleArgument(bool b); + + // method with default value + double defaultValue(int n = 0); + + // overloads + OverloadedFuncEnum overloaded(int i = 0, int d = 0); + OverloadedFuncEnum overloaded(double n); + + // more overloads + OtherOverloadedFuncEnum otherOverloaded(int a, int b, bool c, double d); + OtherOverloadedFuncEnum otherOverloaded(int a, double b); + + inline SomeInnerClass returnMyParameter(const SomeInnerClass& s) { return s; } + + static Abstract* triggerImpossibleTypeDiscovery(); + static Abstract* triggerAnotherImpossibleTypeDiscovery(); + + void hideFunction(HideType*) {} +protected: + const char* getClassName() { return className(); } + virtual const char* className() { return "Derived"; } +}; +#endif // DERIVED_H + diff --git a/sources/shiboken2/tests/libsample/echo.cpp b/sources/shiboken2/tests/libsample/echo.cpp new file mode 100644 index 000000000..00fec78b0 --- /dev/null +++ b/sources/shiboken2/tests/libsample/echo.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "echo.h" diff --git a/sources/shiboken2/tests/libsample/echo.h b/sources/shiboken2/tests/libsample/echo.h new file mode 100644 index 000000000..a976c9cbc --- /dev/null +++ b/sources/shiboken2/tests/libsample/echo.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ECHO_H +#define ECHO_H + +#include "libsamplemacros.h" +#include "str.h" + +class ObjectType; + +class Echo +{ +public: + Echo(){} + ~Echo(){} + + void doNothingWithConstBool(const bool hi) {} + void methodWithNamedArg(const Str& string = Str("")) {} + + Str operator()(const Str& s, const int i) { return s + i; } + + // These method are here just for compilation test purposes + Echo& operator<<(unsigned int item) { return *this; } + Echo& operator<<(signed int item) { return *this; } + Echo& operator<<(const ObjectType* item) { return *this; } + Echo& operator<<(Str str) { return *this; } +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/expression.cpp b/sources/shiboken2/tests/libsample/expression.cpp new file mode 100644 index 000000000..ff87e2ed2 --- /dev/null +++ b/sources/shiboken2/tests/libsample/expression.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include "expression.h" +#include <sstream> + +Expression::Expression() : m_value(0), m_operation(None), m_operand1(0), m_operand2(0) +{ +} + +Expression::Expression(int number) : m_value(number), m_operation(None), m_operand1(0), m_operand2(0) +{ +} + +Expression::Expression(const Expression& other) +{ + m_operand1 = other.m_operand1 ? new Expression(*other.m_operand1) : 0; + m_operand2 = other.m_operand2 ? new Expression(*other.m_operand2) : 0; + m_value = other.m_value; + m_operation = other.m_operation; +} + +Expression& Expression::operator=(const Expression& other) +{ + delete m_operand1; + delete m_operand2; + m_operand1 = other.m_operand1 ? new Expression(*other.m_operand1) : 0; + m_operand2 = other.m_operand2 ? new Expression(*other.m_operand2) : 0; + m_operation = other.m_operation; + m_value = other.m_value; + return *this; +} + +Expression::~Expression() +{ + delete m_operand1; + delete m_operand2; +} + +Expression Expression::operator+(const Expression& other) +{ + Expression expr; + expr.m_operation = Add; + expr.m_operand1 = new Expression(*this); + expr.m_operand2 = new Expression(other); + return expr; +} + +Expression Expression::operator-(const Expression& other) +{ + Expression expr; + expr.m_operation = Add; + expr.m_operand1 = new Expression(*this); + expr.m_operand2 = new Expression(other); + return expr; +} + +Expression Expression::operator<(const Expression& other) +{ + Expression expr; + expr.m_operation = LessThan; + expr.m_operand1 = new Expression(*this); + expr.m_operand2 = new Expression(other); + return expr; +} + +Expression Expression::operator>(const Expression& other) +{ + Expression expr; + expr.m_operation = GreaterThan; + expr.m_operand1 = new Expression(*this); + expr.m_operand2 = new Expression(other); + return expr; +} + +std::string Expression::toString() const +{ + if (m_operation == None) { + std::ostringstream s; + s << m_value; + return s.str(); + } + + std::string result; + result += '('; + result += m_operand1->toString(); + char op; + switch (m_operation) { + case Add: + op = '+'; + break; + case Sub: + op = '-'; + break; + case LessThan: + op = '<'; + break; + case GreaterThan: + op = '<'; + break; + case None: // just to avoid the compiler warning + default: + op = '?'; + break; + } + result += op; + result += m_operand2->toString(); + result += ')'; + return result; +} + diff --git a/sources/shiboken2/tests/libsample/expression.h b/sources/shiboken2/tests/libsample/expression.h new file mode 100644 index 000000000..c4bb1a1f5 --- /dev/null +++ b/sources/shiboken2/tests/libsample/expression.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include "libsamplemacros.h" +#include <string> + +class LIBSAMPLE_API Expression +{ +public: + enum Operation { + None, Add, Sub, LessThan, GreaterThan + }; + + Expression(int number); + Expression(const Expression& other); + Expression& operator=(const Expression& other); + + ~Expression(); + + Expression operator>(const Expression& other); + Expression operator<(const Expression& other); + Expression operator+(const Expression& other); + Expression operator-(const Expression& other); + + std::string toString() const; +private: + int m_value; + Operation m_operation; + Expression* m_operand1; + Expression* m_operand2; + + Expression(); +}; + +#endif // EXPRESSION_H diff --git a/sources/shiboken2/tests/libsample/filter.cpp b/sources/shiboken2/tests/libsample/filter.cpp new file mode 100644 index 000000000..dac5d5e96 --- /dev/null +++ b/sources/shiboken2/tests/libsample/filter.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <string> +#include "filter.h" +Data::Data(Field field, std::string value) + : m_field(field), m_value(value) +{ +} + +Union::Union(const Data& filter) +{ + m_filters.push_back(filter); +} + +Union::Union(const Intersection& filter) +{ + m_filters.push_back(filter); +} + +Union::Union(const Union& filter) +{ + m_filters = filter.filters(); +} + +Intersection::Intersection(const Data& filter) +{ + m_filters.push_back(filter); +} + +Intersection::Intersection(const Union& filter) +{ + m_filters.push_back(filter); +} + +Intersection::Intersection(const Intersection& filter) +{ + m_filters = filter.filters(); +} + +Intersection operator&(const Intersection& a, const Intersection& b) +{ + Intersection filter; + filter.addFilter(a); + filter.addFilter(b); + + return filter; +} diff --git a/sources/shiboken2/tests/libsample/filter.h b/sources/shiboken2/tests/libsample/filter.h new file mode 100644 index 000000000..6ee2e6123 --- /dev/null +++ b/sources/shiboken2/tests/libsample/filter.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef FILTER_H +#define FILTER_H + +#include <string> +#include <list> + +#include "libsamplemacros.h" + +class Intersection; + +class LIBSAMPLE_API Filter +{ +}; + +class LIBSAMPLE_API Data : public Filter +{ + +public: + enum Field { + Name, + Album, + Year + }; + + Data(Field field, std::string value); + + Field field() const { return m_field; } + std::string value() const { return m_value; } + +private: + Field m_field; + std::string m_value; +}; + +class LIBSAMPLE_API Union : public Filter +{ +public: + + Union(const Data&); + Union(const Intersection&); + Union() {}; + Union(const Union&); + + std::list<Filter> filters() const { return m_filters; } + void addFilter(const Filter& data) { m_filters.push_back(data); } + +private: + std::list<Filter> m_filters; +}; + +class LIBSAMPLE_API Intersection : public Filter +{ +public: + + Intersection(const Data&); + Intersection(const Union&); + Intersection() {}; + Intersection(const Intersection&); + + std::list<Filter> filters() const { return m_filters; } + void addFilter(const Filter& data) { m_filters.push_back(data); } + +private: + std::list<Filter> m_filters; +}; + +LIBSAMPLE_API Intersection operator&(const Intersection& a, const Intersection& b); + +#endif // FILTER_H + + diff --git a/sources/shiboken2/tests/libsample/functions.cpp b/sources/shiboken2/tests/libsample/functions.cpp new file mode 100644 index 000000000..4a15cdae8 --- /dev/null +++ b/sources/shiboken2/tests/libsample/functions.cpp @@ -0,0 +1,214 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "functions.h" +#include <string.h> +#include <iostream> + +using namespace std; + +void +printSomething() +{ + cout << __FUNCTION__ << endl; +} + +int +gimmeInt() +{ + static int val = 2; + val = val * 1.3; + return val; +} + +double +gimmeDouble() +{ + static double val = 7.77; + val = val * 1.3; + return val; +} + +std::list<Complex> +gimmeComplexList() +{ + std::list<Complex> lst; + lst.push_back(Complex()); + lst.push_back(Complex(1.1, 2.2)); + lst.push_back(Complex(1.3, 2.4)); + return lst; +} + +Complex +sumComplexPair(std::pair<Complex, Complex> cpx_pair) +{ + return cpx_pair.first + cpx_pair.second; +} + +double +multiplyPair(std::pair<double, double> pair) +{ + return pair.first * pair.second; +} + +int +countCharacters(const char* text) +{ + if (!text) + return -1; + int count; + for(count = 0; text[count] != '\0'; count++) + ; + return count; +} + +char* +makeCString() +{ + char* string = new char[strlen(__FUNCTION__) + 1]; + strcpy(string, __FUNCTION__); + return string; +} + +const char* +returnCString() +{ + return __FUNCTION__; +} + +GlobalOverloadFuncEnum +overloadedFunc(int val) +{ + return GlobalOverloadFunc_i; +} + +GlobalOverloadFuncEnum +overloadedFunc(double val) +{ + return GlobalOverloadFunc_d; +} + +char* +returnNullPrimitivePointer() +{ + return 0; +} + +ObjectType* +returnNullObjectTypePointer() +{ + return 0; +} + +Event* +returnNullValueTypePointer() +{ + return 0; +} + +unsigned int +doubleUnsignedInt(unsigned int value) +{ + return value * 2; +} + +long long +doubleLongLong(long long value) +{ + return value * 2; +} + +unsigned long long +doubleUnsignedLongLong(unsigned long long value) +{ + return value * 2; +} + +short +doubleShort(short value) +{ + return value * 2; +} + +int +acceptInt(int x) +{ + return x; +} + +unsigned int +acceptUInt(unsigned int x) +{ + return x; +} + +long +acceptLong(long x) +{ + return x; +} + +unsigned long +acceptULong(unsigned long x) +{ + return x; +} + +double +acceptDouble(double x) +{ + return x; +} + +int +acceptIntReference(int& x) +{ + return x; +} + +OddBool +acceptOddBoolReference(OddBool& x) +{ + return x; +} + +ClassWithFunctionPointer::ClassWithFunctionPointer() +{ + callFunctionPointer(0, &ClassWithFunctionPointer::doNothing); +} + +void ClassWithFunctionPointer::callFunctionPointer(int dummy, void (*fp)(void *)) +{ + size_t a = dummy; + fp(reinterpret_cast<void *>(a)); +} + +void ClassWithFunctionPointer::doNothing(void *operand) +{ + (void) operand; +} diff --git a/sources/shiboken2/tests/libsample/functions.h b/sources/shiboken2/tests/libsample/functions.h new file mode 100644 index 000000000..89a175bc4 --- /dev/null +++ b/sources/shiboken2/tests/libsample/functions.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +#include "libsamplemacros.h" +#include <list> +#include <utility> +#include "oddbool.h" +#include "complex.h" +#include "objecttype.h" + +enum GlobalEnum { + NoThing, + FirstThing, + SecondThing, + ThirdThing +}; + +enum GlobalOverloadFuncEnum { + GlobalOverloadFunc_i, + GlobalOverloadFunc_d +}; + +LIBSAMPLE_API void printSomething(); +LIBSAMPLE_API int gimmeInt(); +LIBSAMPLE_API double gimmeDouble(); +LIBSAMPLE_API double multiplyPair(std::pair<double, double> pair); +LIBSAMPLE_API std::list<Complex> gimmeComplexList(); +LIBSAMPLE_API Complex sumComplexPair(std::pair<Complex, Complex> cpx_pair); + +LIBSAMPLE_API int countCharacters(const char* text); +LIBSAMPLE_API char* makeCString(); +LIBSAMPLE_API const char* returnCString(); + +LIBSAMPLE_API char* returnNullPrimitivePointer(); +LIBSAMPLE_API ObjectType* returnNullObjectTypePointer(); +LIBSAMPLE_API Event* returnNullValueTypePointer(); + +// Tests overloading on functions (!methods) +LIBSAMPLE_API GlobalOverloadFuncEnum overloadedFunc(int val); +LIBSAMPLE_API GlobalOverloadFuncEnum overloadedFunc(double val); + +LIBSAMPLE_API unsigned int doubleUnsignedInt(unsigned int value); +LIBSAMPLE_API long long doubleLongLong(long long value); +LIBSAMPLE_API unsigned long long doubleUnsignedLongLong(unsigned long long value); +LIBSAMPLE_API short doubleShort(short value); + +LIBSAMPLE_API int acceptInt(int x); +LIBSAMPLE_API unsigned int acceptUInt(unsigned int x); +LIBSAMPLE_API long acceptLong(long x); +LIBSAMPLE_API unsigned long acceptULong(unsigned long x); +LIBSAMPLE_API double acceptDouble(double x); + +LIBSAMPLE_API int acceptIntReference(int& x); +LIBSAMPLE_API OddBool acceptOddBoolReference(OddBool& x); + + +class LIBSAMPLE_API ClassWithFunctionPointer +{ +public: + explicit ClassWithFunctionPointer(); + void callFunctionPointer(int dummy, void (*fp)(void *)); + static void doNothing(void *operand); +}; + +#endif // FUNCTIONS_H diff --git a/sources/shiboken2/tests/libsample/handle.cpp b/sources/shiboken2/tests/libsample/handle.cpp new file mode 100644 index 000000000..e0b1d8d18 --- /dev/null +++ b/sources/shiboken2/tests/libsample/handle.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "handle.h" + +HANDLE HandleHolder::createHandle() +{ + return (HANDLE) new OBJ; +} + +bool HandleHolder::compare(HandleHolder* other) +{ + return other->m_handle == m_handle; +} + +bool HandleHolder::compare2(HandleHolder* other) +{ + return other->m_handle2 == m_handle2; +} diff --git a/sources/shiboken2/tests/libsample/handle.h b/sources/shiboken2/tests/libsample/handle.h new file mode 100644 index 000000000..d656f946b --- /dev/null +++ b/sources/shiboken2/tests/libsample/handle.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef HANDLE_H +#define HANDLE_H + +#include "libsamplemacros.h" + +/* See http://bugs.pyside.org/show_bug.cgi?id=1105. */ +namespace Foo { + typedef unsigned long HANDLE; +} + +class LIBSAMPLE_API OBJ +{ +}; + +typedef OBJ* HANDLE; + +class LIBSAMPLE_API HandleHolder +{ +public: + explicit HandleHolder(HANDLE ptr = 0) : m_handle(ptr) {} + explicit HandleHolder(Foo::HANDLE val): m_handle2(val) {} + + inline void set(HANDLE ptr) { HANDLE tmp; tmp = m_handle; m_handle = tmp; } + inline void set(const Foo::HANDLE& val) { m_handle2 = val; } + inline HANDLE handle() { return m_handle; } + inline Foo::HANDLE handle2() { return m_handle2; } + + static HANDLE createHandle(); + bool compare(HandleHolder* other); + bool compare2(HandleHolder* other); + +private: + HANDLE m_handle; + Foo::HANDLE m_handle2; +}; + +struct LIBSAMPLE_API PrimitiveStruct {}; +typedef struct PrimitiveStruct* PrimitiveStructPtr; +struct LIBSAMPLE_API PrimitiveStructPointerHolder +{ + PrimitiveStructPtr primitiveStructPtr; +}; + +#endif // HANDLE_H diff --git a/sources/shiboken2/tests/libsample/implicitconv.cpp b/sources/shiboken2/tests/libsample/implicitconv.cpp new file mode 100644 index 000000000..df4af2537 --- /dev/null +++ b/sources/shiboken2/tests/libsample/implicitconv.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "implicitconv.h" + +ImplicitConv +ImplicitConv::implicitConvCommon(ImplicitConv implicit) +{ + return implicit; +} + +ImplicitConv +ImplicitConv::implicitConvDefault(ImplicitConv implicit) +{ + return implicit; +} + +ImplicitConv::ICOverloadedFuncEnum +ImplicitConv::implicitConvOverloading(ImplicitConv implicit, int dummyArg) +{ + return ImplicitConv::OverFunc_Ii; +} + +ImplicitConv::ICOverloadedFuncEnum +ImplicitConv::implicitConvOverloading(ImplicitConv implicit, bool dummyArg) +{ + return ImplicitConv::OverFunc_Ib; +} + +ImplicitConv::ICOverloadedFuncEnum +ImplicitConv::implicitConvOverloading(int dummyArg) +{ + return ImplicitConv::OverFunc_i; +} + +ImplicitConv::ICOverloadedFuncEnum +ImplicitConv::implicitConvOverloading(CtorEnum dummyArg) +{ + return ImplicitConv::OverFunc_C; +} + diff --git a/sources/shiboken2/tests/libsample/implicitconv.h b/sources/shiboken2/tests/libsample/implicitconv.h new file mode 100644 index 000000000..2a8ac66c2 --- /dev/null +++ b/sources/shiboken2/tests/libsample/implicitconv.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IMPLICITCONV_H +#define IMPLICITCONV_H + +#include "libsamplemacros.h" +#include "null.h" + +class ObjectType; + +class LIBSAMPLE_API ImplicitConv +{ +public: + enum CtorEnum { + CtorNone, + CtorOne, + CtorTwo, + CtorThree, + CtorObjectTypeReference, + CtorPrimitiveType + }; + + enum ICOverloadedFuncEnum { + OverFunc_Ii, + OverFunc_Ib, + OverFunc_i, + OverFunc_C + }; + + ImplicitConv() : m_ctorEnum(CtorNone), m_objId(-1), m_value(-1.0) {} + ImplicitConv(int objId) : m_ctorEnum(CtorOne), m_objId(objId), m_value(-1.0) {} + ImplicitConv(CtorEnum ctorEnum) : m_ctorEnum(ctorEnum), m_objId(-1), m_value(-1.0) {} + ImplicitConv(ObjectType&) : m_ctorEnum(CtorObjectTypeReference), m_objId(-1), m_value(-1.0) {} + ImplicitConv(double value, bool=true) : m_ctorEnum(CtorNone), m_value(value) {} + ImplicitConv(const Null& null) : m_ctorEnum(CtorPrimitiveType) {} + ~ImplicitConv() {} + + inline CtorEnum ctorEnum() { return m_ctorEnum; } + inline int objId() { return m_objId; } + inline double value() { return m_value; } + + static ImplicitConv implicitConvCommon(ImplicitConv implicit); + + static ImplicitConv implicitConvDefault(ImplicitConv implicit = CtorTwo); + + static ICOverloadedFuncEnum implicitConvOverloading(ImplicitConv implicit, int dummyArg); + static ICOverloadedFuncEnum implicitConvOverloading(ImplicitConv implicit, bool dummyArg); + static ICOverloadedFuncEnum implicitConvOverloading(int dummyArg); + static ICOverloadedFuncEnum implicitConvOverloading(CtorEnum dummyArg); + +private: + CtorEnum m_ctorEnum; + int m_objId; + double m_value; +}; + +#endif // IMPLICITCONV_H diff --git a/sources/shiboken2/tests/libsample/injectcode.cpp b/sources/shiboken2/tests/libsample/injectcode.cpp new file mode 100644 index 000000000..4406cdc5c --- /dev/null +++ b/sources/shiboken2/tests/libsample/injectcode.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "injectcode.h" +#include <sstream> + +using namespace std; + +InjectCode::InjectCode() +{ +} + +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) +{ + toStr(arg0); + m_valueHolder += arg1 ? "true" : "false"; + return m_valueHolder.c_str(); +} + +const char* InjectCode::overloadedMethod(int arg0, double arg1) +{ + return toStr(arg0 + arg1); +} + +const char* InjectCode::overloadedMethod(int argc, char** argv) +{ + return simpleMethod3(argc, argv); +} + +const char* InjectCode::virtualMethod(int arg) +{ + return toStr(arg); +} + +int InjectCode::arrayMethod(int count, int *values) const +{ + int ret = 0; + for (int i=0; i < count; i++) + ret += values[i]; + return ret; +} + +int InjectCode::sumArrayAndLength(int* values) const +{ + int sum = 0; + + while(*values) { + sum = sum + *values + 1; + values++; + } + + return sum; +} diff --git a/sources/shiboken2/tests/libsample/injectcode.h b/sources/shiboken2/tests/libsample/injectcode.h new file mode 100644 index 000000000..ba1bedb17 --- /dev/null +++ b/sources/shiboken2/tests/libsample/injectcode.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INJECTCODE_H +#define INJECTCODE_H + +#include "libsamplemacros.h" +#include <utility> +#include <string> + +class LIBSAMPLE_API InjectCode +{ +public: + InjectCode(); + virtual ~InjectCode(); + + const char* simpleMethod1(int arg0, int arg1); + const char* simpleMethod2(); + const char* simpleMethod3(int argc, char** argv); + + const char* overloadedMethod(int argc, char** argv); + const char* overloadedMethod(int arg0, double arg1); + const char* overloadedMethod(int arg0, bool arg1); + + virtual int arrayMethod(int count, int* values) const; + inline int callArrayMethod(int count, int *values) const { return arrayMethod(count, values); } + virtual const char* virtualMethod(int arg); + int sumArrayAndLength(int* values) const; +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/sources/shiboken2/tests/libsample/libsamplemacros.h b/sources/shiboken2/tests/libsample/libsamplemacros.h new file mode 100644 index 000000000..6a1bba88d --- /dev/null +++ b/sources/shiboken2/tests/libsample/libsamplemacros.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIBSAMPLEMACROS_H +#define LIBSAMPLEMACROS_H + +#if defined _WIN32 || defined __CYGWIN__ + #if LIBSAMPLE_BUILD + #define LIBSAMPLE_API __declspec(dllexport) + #else + #define LIBSAMPLE_API __declspec(dllimport) + #endif +#else +#if __GNUC__ >= 4 + #define LIBSAMPLE_API __attribute__ ((visibility("default"))) +#else + #define LIBSAMPLE_API +#endif +#endif + +#endif diff --git a/sources/shiboken2/tests/libsample/list.h b/sources/shiboken2/tests/libsample/list.h new file mode 100644 index 000000000..f1183440c --- /dev/null +++ b/sources/shiboken2/tests/libsample/list.h @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIST_H +#define LIST_H + +#include <list> +#include "libsamplemacros.h" +#include "point.h" + +class ObjectType; + +template<class T> +class List : public std::list<T> +{ +}; + +class IntList : public List<int> +{ +public: + enum CtorEnum { + NoParamsCtor, + IntCtor, + CopyCtor, + ListOfIntCtor + }; + + inline IntList() : m_ctorUsed(NoParamsCtor) {} + inline explicit IntList(int val) : m_ctorUsed(IntCtor) { push_back(val); } + inline IntList(const IntList& lst) : List<int>(lst), m_ctorUsed(CopyCtor) {} + inline IntList(const List<int>& lst) : List<int>(lst), m_ctorUsed(ListOfIntCtor) {} + + inline void append(int v) { insert(end(), v); } + CtorEnum constructorUsed() { return m_ctorUsed; } +private: + CtorEnum m_ctorUsed; +}; + +class PointValueList : public List<Point> +{ +public: + enum CtorEnum { + NoParamsCtor, + PointCtor, + CopyCtor, + ListOfPointValuesCtor + }; + + inline PointValueList() : m_ctorUsed(NoParamsCtor) {} + inline explicit PointValueList(Point val) : m_ctorUsed(PointCtor) { push_back(val); } + inline PointValueList(const PointValueList& lst) : List<Point>(lst), m_ctorUsed(CopyCtor) {} + inline PointValueList(const List<Point>& lst) : List<Point>(lst), m_ctorUsed(ListOfPointValuesCtor) {} + + inline void append(Point v) { insert(end(), v); } + CtorEnum constructorUsed() { return m_ctorUsed; } +private: + CtorEnum m_ctorUsed; +}; + +class ObjectTypePtrList : public List<ObjectType*> +{ +public: + enum CtorEnum { + NoParamsCtor, + ObjectTypeCtor, + CopyCtor, + ListOfObjectTypePtrCtor + }; + + inline ObjectTypePtrList() : m_ctorUsed(NoParamsCtor) {} + inline explicit ObjectTypePtrList(ObjectType* val) : m_ctorUsed(ObjectTypeCtor) { push_back(val); } + inline ObjectTypePtrList(const ObjectTypePtrList& lst) : List<ObjectType*>(lst), m_ctorUsed(CopyCtor) {} + inline ObjectTypePtrList(const List<ObjectType*>& lst) : List<ObjectType*>(lst), m_ctorUsed(ListOfObjectTypePtrCtor) {} + + inline void append(ObjectType* v) { insert(end(), v); } + CtorEnum constructorUsed() { return m_ctorUsed; } +private: + CtorEnum m_ctorUsed; +}; + +#endif // LIST_H diff --git a/sources/shiboken2/tests/libsample/listuser.cpp b/sources/shiboken2/tests/libsample/listuser.cpp new file mode 100644 index 000000000..24389766e --- /dev/null +++ b/sources/shiboken2/tests/libsample/listuser.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <numeric> +#include <cstdlib> +#include "listuser.h" + +using namespace std; + +std::list<int> +ListUser::callCreateList() +{ + return createList(); +} + +std::list<int> +ListUser::createList() +{ + std::list<int> retval; + for (int i = 0; i < 4; i++) + retval.push_front(rand()); + return retval; +} + +std::list<Complex> +ListUser::createComplexList(Complex cpx0, Complex cpx1) +{ + std::list<Complex> retval; + retval.push_back(cpx0); + retval.push_back(cpx1); + return retval; +} + +double +ListUser::sumList(std::list<int> vallist) +{ + return std::accumulate(vallist.begin(), vallist.end(), 0.0); +} + +double +ListUser::sumList(std::list<double> vallist) +{ + return std::accumulate(vallist.begin(), vallist.end(), 0.0); +} + +ListUser::ListOfSomething +ListUser::listOfPoints(const std::list<Point>& pointlist) +{ + return ListOfPoint; +} + +ListUser::ListOfSomething +ListUser::listOfPoints(const std::list<PointF>& pointlist) +{ + return ListOfPointF; +} + +void +ListUser::multiplyPointList(PointList& points, double multiplier) +{ + for(PointList::iterator piter = points.begin(); piter != points.end(); piter++) { + (*piter)->setX((*piter)->x() * multiplier); + (*piter)->setY((*piter)->y() * multiplier); + } +} + diff --git a/sources/shiboken2/tests/libsample/listuser.h b/sources/shiboken2/tests/libsample/listuser.h new file mode 100644 index 000000000..8f49c80d3 --- /dev/null +++ b/sources/shiboken2/tests/libsample/listuser.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LISTUSER_H +#define LISTUSER_H + +#include <list> +#include "complex.h" +#include "point.h" +#include "pointf.h" + +#include "libsamplemacros.h" + +class LIBSAMPLE_API ListUser +{ +public: + typedef std::list<Point*> PointList; + + enum ListOfSomething { + ListOfPoint, + ListOfPointF + }; + + ListUser() {} + ListUser(const ListUser& other) : m_lst(other.m_lst) {} + virtual ~ListUser() {} + + virtual std::list<int> createList(); + std::list<int> callCreateList(); + + static std::list<Complex> createComplexList(Complex cpx0, Complex cpx1); + + double sumList(std::list<int> vallist); + double sumList(std::list<double> vallist); + + static ListOfSomething listOfPoints(const std::list<Point>& pointlist); + static ListOfSomething listOfPoints(const std::list<PointF>& pointlist); + + static void multiplyPointList(PointList& points, double multiplier); + + inline void setList(std::list<int> lst) { m_lst = lst; } + inline std::list<int> getList() { return m_lst; } + +private: + std::list<int> m_lst; +}; + +#endif // LISTUSER_H + diff --git a/sources/shiboken2/tests/libsample/main.cpp b/sources/shiboken2/tests/libsample/main.cpp new file mode 100644 index 000000000..b22d05a36 --- /dev/null +++ b/sources/shiboken2/tests/libsample/main.cpp @@ -0,0 +1,244 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include <list> +#include "abstract.h" +#include "derived.h" +#include "kindergarten.h" +#include "complex.h" +#include "point.h" +#include "size.h" +#include "listuser.h" +#include "samplenamespace.h" + +using namespace std; + +int +main(int argv, char **argc) +{ + cout << endl; + + Derived derived; + + cout << endl; + + derived.unpureVirtual(); + derived.pureVirtual(); + derived.callPureVirtual(); + + cout << endl; + Abstract* abs; + abs = Abstract::createObject(); + cout << "Abstract::createObject(): " << abs << endl << endl; + delete abs; + + abs = Derived::createObject(); + cout << "Derived::createObject() : "; + abs->show(); + cout << endl; + delete abs; + cout << endl; + + abs = Derived::createObject(); + cout << "Derived::createObject() : "; + abs->show(); + cout << endl; + delete abs; + cout << endl; + + cout << endl << "-----------------------------------------" << endl; + + KinderGarten kg; + Derived* d[] = { 0, 0, 0 }; + + for (int i = 0; i < 3; i++) { + d[i] = new Derived(i); + d[i]->show(); + cout << endl; + kg.addChild(d[i]); + } + + kg.show(); + cout << endl; + + cout << endl << "* kill child "; + d[2]->show(); + cout << " ----------------" << endl; + kg.killChild(d[2]); + kg.show(); + cout << endl; + + cout << endl << "* release child "; + d[1]->show(); + cout << " -------------" << endl; + Abstract* released = kg.releaseChild(d[1]); + cout << "released: "; + released->show(); + cout << endl; + kg.show(); + cout << endl; + + cout << endl << "* kill children ------------------------------------" << endl; + kg.killChildren(); + kg.show(); + cout << endl << endl; + + cout << "-----------------------------------------" << endl; + ListUser lu; + cout << "ListUser::createList()" << endl; + std::list<int> intlist = lu.createList(); + for (std::list<int>::iterator it = intlist.begin(); it != intlist.end(); it++) { + cout << "* " << *it << endl; + } + + cout << "ListUser::createComplexList" << endl; + std::list<Complex> cpxlist = ListUser::createComplexList(Complex(1.1, 2.2), Complex(3.3, 4.4)); + for (std::list<Complex>::iterator it = cpxlist.begin(); it != cpxlist.end(); it++) { + cout << "* "; + (*it).show(); + cout << endl; + } + cout << endl; + + cout << "-----------------------------------------" << endl; + cout << "SampleNamespace" << endl; + + cout << "SampleNamespace::RandomNumber: "; + cout << SampleNamespace::getNumber(SampleNamespace::RandomNumber); + cout << endl; + cout << "SampleNamespace::UnixTime: "; + cout << SampleNamespace::getNumber(SampleNamespace::UnixTime); + cout << endl; + double val_d = 1.3; + cout << "SampleNamespace::powerOfTwo(" << val_d << "): "; + cout << SampleNamespace::powerOfTwo(val_d) << endl; + int val_i = 7; + cout << "SampleNamespace::powerOfTwo(" << val_i << "): "; + cout << SampleNamespace::powerOfTwo(val_i) << endl; + cout << endl; + + cout << "-----------------------------------------" << endl; + cout << "Point" << endl; + + Point p1(1.1, 2.2); + cout << "p1: "; + p1.show(); + cout << endl; + + Point p2(3.4, 5.6); + cout << "p2: "; + p2.show(); + cout << endl; + + cout << "p1 + p2 == "; + (p1 + p2).show(); + cout << endl; + + cout << "p1 * 2.0 == "; + (p1 * 2.0).show(); + cout << endl; + + cout << "1.5 * p2 == "; + (1.5 * p2).show(); + cout << endl; + + cout << "p1: "; + p1.show(); + cout << endl << "p2: "; + p2.show(); + cout << endl << "p1 += p2" << endl; + p1 += p2; + cout << "p1: "; + p1.show(); + cout << endl; + + cout << "p1 == p2 ? " << ((p1 == p2) ? "true" : "false") << endl; + cout << "p1 == p1 ? " << ((p1 == p1) ? "true" : "false") << endl; + cout << "p2 == p2 ? " << ((p2 == p2) ? "true" : "false") << endl; + + cout << "-----------------------------------------" << endl; + cout << "Size" << endl; + + Size s1(2, 2); + cout << "s1: "; + s1.show(); + cout << ", area: " << s1.calculateArea(); + cout << endl; + + Size s2(3, 5); + cout << "s2: "; + s2.show(); + cout << ", area: " << s2.calculateArea(); + cout << endl; + + cout << endl; + + cout << "s1 == s2 ? " << ((s1 == s2) ? "true" : "false") << endl; + cout << "s1 != s2 ? " << ((s1 != s2) ? "true" : "false") << endl; + + cout << "s1 < s2 ? " << ((s1 < s2) ? "true" : "false") << endl; + cout << "s1 <= s2 ? " << ((s1 <= s2) ? "true" : "false") << endl; + cout << "s1 > s2 ? " << ((s1 > s2) ? "true" : "false") << endl; + cout << "s1 >= s2 ? " << ((s1 >= s2) ? "true" : "false") << endl; + + cout << "s1 < 10 ? " << ((s1 < 10) ? "true" : "false") << endl; + cout << "s1 <= 10 ? " << ((s1 <= 10) ? "true" : "false") << endl; + cout << "s1 > 10 ? " << ((s1 > 10) ? "true" : "false") << endl; + cout << "s1 >= 10 ? " << ((s1 >= 10) ? "true" : "false") << endl; + cout << "s2 < 10 ? " << ((s2 < 10) ? "true" : "false") << endl; + cout << "s2 <= 10 ? " << ((s2 <= 10) ? "true" : "false") << endl; + cout << "s2 > 10 ? " << ((s2 > 10) ? "true" : "false") << endl; + cout << "s2 >= 10 ? " << ((s2 >= 10) ? "true" : "false") << endl; + cout << endl; + + cout << "s1: "; + s1.show(); + cout << endl << "s2: "; + s2.show(); + cout << endl << "s1 += s2" << endl; + s1 += s2; + cout << "s1: "; + s1.show(); + cout << endl; + + cout << endl; + + cout << "s1: "; + s1.show(); + cout << endl << "s1 *= 2.0" << endl; + s1 *= 2.0; + cout << "s1: "; + s1.show(); + cout << endl; + + cout << endl; + + return 0; +} + diff --git a/sources/shiboken2/tests/libsample/mapuser.cpp b/sources/shiboken2/tests/libsample/mapuser.cpp new file mode 100644 index 000000000..666188512 --- /dev/null +++ b/sources/shiboken2/tests/libsample/mapuser.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "mapuser.h" + +using namespace std; + +std::map<std::string, std::pair<Complex, int> > +MapUser::callCreateMap() +{ + return createMap(); +} + + +std::map<std::string, std::pair<Complex, int> > +MapUser::createMap() +{ + std::map<std::string, std::pair<Complex, int> > retval; + + std::pair<std::string, std::pair<Complex, int> > + item0("zero", std::pair<Complex, int>(Complex(1.2, 3.4), 2)); + retval.insert(item0); + + std::pair<std::string, std::pair<Complex, int> > + item1("one", std::pair<Complex, int>(Complex(5.6, 7.8), 3)); + retval.insert(item1); + + std::pair<std::string, std::pair<Complex, int> > + item2("two", std::pair<Complex, int>(Complex(9.1, 2.3), 5)); + retval.insert(item2); + + return retval; +} + +void +MapUser::showMap(std::map<std::string, int> mapping) +{ + std::map<std::string, int>::iterator it; + cout << __FUNCTION__ << endl; + for (it = mapping.begin() ; it != mapping.end(); it++) + cout << (*it).first << " => " << (*it).second << endl; +} + diff --git a/sources/shiboken2/tests/libsample/mapuser.h b/sources/shiboken2/tests/libsample/mapuser.h new file mode 100644 index 000000000..90d036945 --- /dev/null +++ b/sources/shiboken2/tests/libsample/mapuser.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAPUSER_H +#define MAPUSER_H + +#include <map> +#include <list> +#include <utility> +#include <string> +#include "complex.h" +#include "bytearray.h" + +#include "libsamplemacros.h" + +class LIBSAMPLE_API MapUser +{ +public: + MapUser() {} + virtual ~MapUser() {} + + virtual std::map<std::string, std::pair<Complex, int> > createMap(); + std::map<std::string, std::pair<Complex, int> > callCreateMap(); + + void showMap(std::map<std::string, int> mapping); + + inline void setMap(std::map<std::string, std::list<int> > map) { m_map = map; } + inline std::map<std::string, std::list<int> > getMap() { return m_map; } + + // Compile test + static void pointerToMap(std::map<std::string, std::string>* arg) {} + static void referenceToMap(std::map<std::string, std::string>& arg) {} + + inline const std::map<int, ByteArray>& passMapIntValueType(const std::map<int, ByteArray>& arg) { return arg; } + +private: + std::map<std::string, std::list<int> > m_map; +}; + +#endif // MAPUSER_H diff --git a/sources/shiboken2/tests/libsample/modelindex.h b/sources/shiboken2/tests/libsample/modelindex.h new file mode 100644 index 000000000..12cc97ce4 --- /dev/null +++ b/sources/shiboken2/tests/libsample/modelindex.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MODELINDEX_H +#define MODELINDEX_H + +#include "libsamplemacros.h" + +class ModelIndex +{ +public: + ModelIndex() : m_value(0) {} + ModelIndex(const ModelIndex& other) { m_value = other.m_value; } + inline void setValue(int value) { m_value = value; } + inline int value() const { return m_value; } + static int getValue(const ModelIndex& index) { return index.value(); } +private: + int m_value; +}; + +class ReferentModelIndex +{ +public: + ReferentModelIndex() {} + ReferentModelIndex(const ModelIndex& index) : m_index(index) {} + ReferentModelIndex(const ReferentModelIndex& other) { m_index = other.m_index; } + inline void setValue(int value) { m_index.setValue(value); } + inline int value() const { return m_index.value(); } + operator const ModelIndex&() const { return m_index; } +private: + ModelIndex m_index; +}; + +class PersistentModelIndex +{ +public: + PersistentModelIndex() {} + PersistentModelIndex(const ModelIndex& index) : m_index(index) {} + PersistentModelIndex(const PersistentModelIndex& other) { m_index = other.m_index; } + inline void setValue(int value) { m_index.setValue(value); } + inline int value() const { return m_index.value(); } + operator ModelIndex() const { return m_index; } +private: + ModelIndex m_index; +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/modifications.cpp b/sources/shiboken2/tests/libsample/modifications.cpp new file mode 100644 index 000000000..4a60a8071 --- /dev/null +++ b/sources/shiboken2/tests/libsample/modifications.cpp @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "modifications.h" +#include "objecttype.h" + +using namespace std; + +Modifications::Modifications() +{ + m_object = new ObjectType(); + m_object->setObjectName("MyObject"); +} + +Modifications::~Modifications() +{ + delete m_object; +} + +std::pair<double, double> +Modifications::pointToPair(Point pt, bool* ok) +{ + std::pair<double, double> retval(pt.x(), pt.y()); + *ok = true; + return retval; +} + +double +Modifications::multiplyPointCoordsPlusValue(bool* ok, Point pt, double value) +{ + double retval = (pt.x() * pt.y()) + value; + *ok = true; + return retval; +} + +int +Modifications::doublePlus(int value, int plus) +{ + return (2 * value) + plus; +} + +int +Modifications::power(int base, int exponent) +{ + if (exponent == 0) + return 1; + int retval = base; + for (int i = 1; i < exponent; i++) + retval = retval * base; + return retval; +} + +int +Modifications::timesTen(int number) +{ + return number * 10; +} + +int +Modifications::increment(int number) +{ + return ++number; +} + +void +Modifications::exclusiveCppStuff() +{ + cout << __FUNCTION__ << endl; +} + +int +Modifications::cppMultiply(int a, int b) +{ + return a * b; +} + +const char* +Modifications::className() +{ + return "Modifications"; +} + +Point +Modifications::sumPointArray(int arraySize, const Point pointArray[]) +{ + Point point; + for (int i = 0; i < arraySize; ++i) + point = point + pointArray[i]; + return point; +} + +int +Modifications::getSize(const void* data, int size) +{ + (void)data; + return size; +} + +int +Modifications::sumPointCoordinates(const Point* point) +{ + return point->x() + point->y(); +} + +double +Modifications::differenceOfPointCoordinates(const Point* pt, bool* ok) +{ + if (!pt) { + *ok = false; + return 0.0; + } + *ok = true; + double result = pt->x() - pt->y(); + if (result < 0) + result = result * -1.0; + return result; +} + +bool +Modifications::nonConversionRuleForArgumentWithDefaultValue(ObjectType** object) +{ + if (object) + *object = m_object; + return true; +} diff --git a/sources/shiboken2/tests/libsample/modifications.h b/sources/shiboken2/tests/libsample/modifications.h new file mode 100644 index 000000000..2978270b0 --- /dev/null +++ b/sources/shiboken2/tests/libsample/modifications.h @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MODIFICATIONS_H +#define MODIFICATIONS_H + +#include "libsamplemacros.h" +#include <utility> +#include "point.h" +#include "oddbool.h" + +class ObjectType; + +class LIBSAMPLE_API Modifications +{ +public: + Modifications(); + virtual ~Modifications(); + + enum OverloadedModFunc { + OverloadedNone, + Overloaded_ibid, + Overloaded_ibib, + Overloaded_ibiP, + Overloaded_ibii, + Overloaded_ibPP + }; + + // those overloaded methods should be heavily modified + // to push the overload decisor to its limits + inline OverloadedModFunc overloaded(int a0, bool b0, int c0, double d0) { return Overloaded_ibid; } + inline OverloadedModFunc overloaded(int a1, bool b1, int c1, bool d1) { return Overloaded_ibib; } + inline OverloadedModFunc overloaded(int a2, bool b2, int c2, Point d2) { return Overloaded_ibiP; } + inline OverloadedModFunc overloaded(int a3, bool b3, int c3 = 123, int d3 = 456) { return Overloaded_ibii; } + inline OverloadedModFunc overloaded(int a4, bool b4, Point c4, Point d4) { return Overloaded_ibPP; } + + inline void argRemoval0(int a0, bool a1, int a2 = 123, int a3 = 456) {} + inline void argRemoval0(int a0, bool a1, int a2, bool a3) {} + + inline void argRemoval1(int a0, bool a1, Point a2 = Point(1, 2), Point a3 = Point(3, 4), int a4 = 333) {} + inline void argRemoval1(int a0, bool a1, int a2, bool a3) {} + + inline void argRemoval2(int a0, bool a1, Point a2 = Point(1, 2), Point a3 = Point(3, 4), int a4 = 333) {} + + inline void argRemoval3(int a0, Point a1 = Point(1, 2), bool a2 = true, Point a3 = Point(3, 4), int a4 = 333) {} + + inline void argRemoval4(int a0, Point a1, bool a2, Point a3 = Point(3, 4), int a4 = 333) {} + + inline void argRemoval5(int a0, bool a1, Point a2 = Point(1, 2), Point a3 = Point(3, 4), int a4 = 333) {} + inline void argRemoval5(int a0, bool a1, int a2, bool a3) {} + + // 'ok' must be removed and the return value will be changed + // to a tuple (PyObject*) containing the expected result plus + // the 'ok' value as a Python boolean + std::pair<double, double> pointToPair(Point pt, bool* ok); + + // same as 'pointToPair' except that this time 'ok' is the first argument + double multiplyPointCoordsPlusValue(bool* ok, Point pt, double value); + + // completely remove 'plus' from the Python side + int doublePlus(int value, int plus = 0); + + // the default value for both arguments must be changed in Python + int power(int base = 1, int exponent = 0); + + // in Python set argument default value to 10 + int timesTen(int number); + + // in Python remove the argument default value + int increment(int number = 0); + + // don't export this method to Python + void exclusiveCppStuff(); + + // change the name of this regular method + int cppMultiply(int a, int b); + + // change the name of this virtual method + virtual const char* className(); + + Point sumPointArray(int arraySize, const Point pointArray[]); + + // Replace 'const void*' by 'ByteArray&'. + int getSize(const void* data, int size); + + // Mark the argument with a <no-null-pointer/> tag; + // the test implementation must expect point never to be null. + int sumPointCoordinates(const Point* point); + + // Modify the return value of a virtual method. + virtual double differenceOfPointCoordinates(const Point* pt, bool* ok); + double callDifferenceOfPointCoordinates(const Point* pt, bool* ok) { return differenceOfPointCoordinates(pt, ok); } + + // Sets an ObjectType in the argument and returns true. + bool nonConversionRuleForArgumentWithDefaultValue(ObjectType** object = 0); + ObjectType* getObject() const { return m_object; } + + // Inject code with a %CONVERTTOPYTHON that receives an user's primitive type. + static inline OddBool passOddBool(OddBool ob) { return ob; } + +private: + ObjectType* m_object; +}; + +class LIBSAMPLE_API AbstractModifications : public Modifications +{ +public: + AbstractModifications() {} + virtual ~AbstractModifications() {} + + inline bool invert(bool value) { return !value; } + + // completely remove this method in Python + virtual void pointlessPureVirtualMethod() = 0; +}; + +#endif // MODIFICATIONS_H diff --git a/sources/shiboken2/tests/libsample/modified_constructor.cpp b/sources/shiboken2/tests/libsample/modified_constructor.cpp new file mode 100644 index 000000000..48224358f --- /dev/null +++ b/sources/shiboken2/tests/libsample/modified_constructor.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "modified_constructor.h" + +ModifiedConstructor::ModifiedConstructor(int first_arg) +{ + m_stored_value = first_arg; +} + +int +ModifiedConstructor::retrieveValue() +{ + return m_stored_value; +} + + diff --git a/sources/shiboken2/tests/libsample/modified_constructor.h b/sources/shiboken2/tests/libsample/modified_constructor.h new file mode 100644 index 000000000..9ac5497d7 --- /dev/null +++ b/sources/shiboken2/tests/libsample/modified_constructor.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MODIFIEDCONSTRUCTOR_H +#define MODIFIEDCONSTRUCTOR_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API ModifiedConstructor +{ +public: + + ModifiedConstructor(int first_arg); + int retrieveValue(); + +private: + int m_stored_value; +}; + +#endif // MODIFIEDCONSTRUCTOR_H + diff --git a/sources/shiboken2/tests/libsample/multiple_derived.cpp b/sources/shiboken2/tests/libsample/multiple_derived.cpp new file mode 100644 index 000000000..1d7a33cbe --- /dev/null +++ b/sources/shiboken2/tests/libsample/multiple_derived.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "multiple_derived.h" + +MDerived1::MDerived1() : m_value(100) +{ +} + +MDerived2::MDerived2() : m_value(200) +{ +} + +MDerived3::MDerived3() : m_value(3000) +{ +} + +MDerived4::MDerived4() +{ +} + +MDerived5::MDerived5() +{ +} + +MDerived1* +MDerived1::transformFromBase1(Base1* self) +{ + MDerived1* ptr = dynamic_cast<MDerived1*>(self); + return ptr; +} + +MDerived1* +MDerived1::transformFromBase2(Base2* self) +{ + MDerived1* ptr = dynamic_cast<MDerived1*>(self); + return ptr; +} + diff --git a/sources/shiboken2/tests/libsample/multiple_derived.h b/sources/shiboken2/tests/libsample/multiple_derived.h new file mode 100644 index 000000000..5825e59b4 --- /dev/null +++ b/sources/shiboken2/tests/libsample/multiple_derived.h @@ -0,0 +1,195 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MDERIVED_H +#define MDERIVED_H + +#include "libsamplemacros.h" +#include <string> + +class Base1 +{ +public: + Base1() : m_value(1) {} + virtual ~Base1() {} + virtual int base1Method() { return m_value; } + + virtual void publicMethod() {}; +private: + int m_value; +}; + +class Base2 +{ +public: + Base2() : m_value(2) {} + virtual ~Base2() {} + virtual int base2Method() { return m_value; } +private: + int m_value; +}; + +class LIBSAMPLE_API MDerived1 : public Base1, public Base2 +{ +public: + MDerived1(); + virtual ~MDerived1() {} + + virtual int mderived1Method() { return m_value; } + virtual int base1Method() { return Base1::base1Method() * 10; } + virtual int base2Method() { return Base2::base2Method() * 10; } + + inline Base1* castToBase1() { return (Base1*) this; } + inline Base2* castToBase2() { return (Base2*) this; } + + static MDerived1* transformFromBase1(Base1 *self); + static MDerived1* transformFromBase2(Base2 *self); + +private: + virtual void publicMethod() {} + int m_value; +}; + +class SonOfMDerived1 : public MDerived1 +{ +public: + SonOfMDerived1() : m_value(0) {} + ~SonOfMDerived1() {} + + inline MDerived1* castToMDerived1() { return (MDerived1*) this; } + + int sonOfMDerived1Method() { return m_value; } +private: + int m_value; +}; + +class Base3 +{ +public: + explicit Base3(int val = 3) : m_value(val) {} + virtual ~Base3() {} + int base3Method() { return m_value; } +private: + int m_value; +}; + +class Base4 +{ +public: + Base4() : m_value(4) {} + virtual ~Base4() {} + int base4Method() { return m_value; } +private: + int m_value; +}; + +class Base5 +{ +public: + Base5() : m_value(5) {} + virtual ~Base5() {} + virtual int base5Method() { return m_value; } +private: + int m_value; +}; + +class Base6 +{ +public: + Base6() : m_value(6) {} + virtual ~Base6() {} + virtual int base6Method() { return m_value; } +private: + int m_value; +}; + + +class LIBSAMPLE_API MDerived2 : public Base3, public Base4, public Base5, public Base6 +{ +public: + MDerived2(); + virtual ~MDerived2() {} + + inline int base4Method() { return Base3::base3Method() * 10; } + inline int mderived2Method() { return m_value; } + + inline Base3* castToBase3() { return (Base3*) this; } + inline Base4* castToBase4() { return (Base4*) this; } + inline Base5* castToBase5() { return (Base5*) this; } + inline Base6* castToBase6() { return (Base6*) this; } + +private: + int m_value; +}; + +class LIBSAMPLE_API MDerived3 : public MDerived1, public MDerived2 +{ +public: + MDerived3(); + virtual ~MDerived3() {} + + inline virtual int mderived3Method() { return m_value; } + + inline MDerived1* castToMDerived1() { return (MDerived1*) this; } + inline MDerived2* castToMDerived2() { return (MDerived2*) this; } + + inline Base3* castToBase3() { return (Base3*) this; } + +private: + int m_value; +}; + +class LIBSAMPLE_API MDerived4 : public Base3, public Base4 +{ +public: + MDerived4(); + ~MDerived4() {} + + inline int mderived4Method() { return 0; } + inline int justDummyMethod() { return m_value; } + + inline Base3* castToBase3() { return (Base3*) this; } + inline Base4* castToBase4() { return (Base4*) this; } +private: + int m_value; +}; + +class LIBSAMPLE_API MDerived5 : public Base3, public Base4 +{ +public: + MDerived5(); + virtual ~MDerived5() {} + + virtual int mderived5Method() { return 0; } + + inline Base3* castToBase3() { return (Base3*) this; } + inline Base4* castToBase4() { return (Base4*) this; } +}; + +#endif // MDERIVED_H + diff --git a/sources/shiboken2/tests/libsample/noimplicitconversion.h b/sources/shiboken2/tests/libsample/noimplicitconversion.h new file mode 100644 index 000000000..0a8882d3a --- /dev/null +++ b/sources/shiboken2/tests/libsample/noimplicitconversion.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef NOIMPLICITCONVERSION_H +#define NOIMPLICITCONVERSION_H + +#include "libsamplemacros.h" + +// This class must not have implicit conversions AND +// no conversion operators should be defined in its own module. +class NoImplicitConversion +{ +public: + explicit NoImplicitConversion(int objId) : m_objId(objId) {} + inline int objId() const { return m_objId; } + inline static int receivesNoImplicitConversionByValue(NoImplicitConversion arg) { return arg.m_objId; } + inline static int receivesNoImplicitConversionByPointer(NoImplicitConversion* arg) { return arg->m_objId; } + inline static int receivesNoImplicitConversionByReference(NoImplicitConversion& arg) { return arg.m_objId; } +private: + int m_objId; +}; + +#endif // NOIMPLICITCONVERSION_H + diff --git a/sources/shiboken2/tests/libsample/nondefaultctor.h b/sources/shiboken2/tests/libsample/nondefaultctor.h new file mode 100644 index 000000000..d2b186bd8 --- /dev/null +++ b/sources/shiboken2/tests/libsample/nondefaultctor.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef NONDEFAULTCTOR_H +#define NONDEFAULTCTOR_H + +#include "libsamplemacros.h" + +class NonDefaultCtor +{ + int m_value; +public: + NonDefaultCtor(int value) : m_value(value) + { + } + + inline int value() + { + return m_value; + } + + inline NonDefaultCtor returnMyself() + { + return *this; + } + + inline NonDefaultCtor returnMyself(int) + { + return *this; + } + + inline NonDefaultCtor returnMyself(int, NonDefaultCtor) + { + return *this; + } + + virtual NonDefaultCtor returnMyselfVirtual() + { + return *this; + } + + inline NonDefaultCtor callReturnMyselfVirtual() + { + return returnMyselfVirtual(); + } + + virtual ~NonDefaultCtor() {} +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/null.h b/sources/shiboken2/tests/libsample/null.h new file mode 100644 index 000000000..634262a99 --- /dev/null +++ b/sources/shiboken2/tests/libsample/null.h @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef NULL_H +#define NULL_H + +class Null +{ +public: + Null(bool value) : m_isNull(value) {} + Null() : m_isNull(false) {} + void setIsNull(bool flag) { m_isNull = flag; } + +private: + bool m_isNull; +}; + +#endif // STR_H + diff --git a/sources/shiboken2/tests/libsample/objectmodel.cpp b/sources/shiboken2/tests/libsample/objectmodel.cpp new file mode 100644 index 000000000..370b58aa1 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objectmodel.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "objectmodel.h" + +void +ObjectModel::setData(ObjectType* data) +{ + m_data = data; +} + +ObjectType* +ObjectModel::data() const +{ + return m_data; +} + diff --git a/sources/shiboken2/tests/libsample/objectmodel.h b/sources/shiboken2/tests/libsample/objectmodel.h new file mode 100644 index 000000000..aa4a64433 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objectmodel.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTMODEL_H +#define OBJECTMODEL_H + +#include "objecttype.h" +#include "libsamplemacros.h" + +class LIBSAMPLE_API ObjectModel : public ObjectType +{ +public: + explicit ObjectModel(ObjectType* parent = 0) + : ObjectType(parent), m_data(0) + {} + + void setData(ObjectType* data); + virtual ObjectType* data() const; + + // The MethodCalled enum and related static methods were created to + // test bug #630 [http://bugs.openbossa.org/show_bug.cgi?id=630] + enum MethodCalled { ObjectTypeCalled, ObjectModelCalled }; + static MethodCalled receivesObjectTypeFamily(const ObjectType& object) { return ObjectModel::ObjectTypeCalled; } + static MethodCalled receivesObjectTypeFamily(const ObjectModel& object) { return ObjectModel::ObjectModelCalled; } + +private: + // The model holds only one piece of data. + // (This is just a test after all.) + ObjectType* m_data; +}; + +#endif // OBJECTMODEL_H + diff --git a/sources/shiboken2/tests/libsample/objecttype.cpp b/sources/shiboken2/tests/libsample/objecttype.cpp new file mode 100644 index 000000000..4f10b01df --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttype.cpp @@ -0,0 +1,311 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "objecttype.h" +#include "objecttypelayout.h" +#include <algorithm> +#include <iostream> +#include <string> +#include <assert.h> + +using namespace std; + +ObjectType::ObjectType(ObjectType* parent) : m_parent(0), m_layout(0), m_call_id(-1) +{ + setParent(parent); +} + +ObjectType::~ObjectType() +{ + for (ObjectTypeList::iterator child_iter = m_children.begin(); + child_iter != m_children.end(); ++child_iter) + delete *child_iter; +} + +ObjectType* +ObjectType::createWithChild() +{ + ObjectType* parent = create(); + ObjectType* child = create(); + child->setObjectName("child"); + child->setParent(parent); + return parent; +} + +void +ObjectType::removeChild(ObjectType* child) +{ + if (!child) + return; + + ObjectTypeList::iterator child_iter = std::find(m_children.begin(), m_children.end(), child); + if (child_iter != m_children.end()) { + m_children.erase(child_iter); + child->m_parent = 0; + } +} + +ObjectType* +ObjectType::takeChild(ObjectType* child) +{ + if (!child) + return 0; + + ObjectTypeList::iterator child_iter = std::find(m_children.begin(), m_children.end(), child); + if (child_iter != m_children.end()) { + m_children.erase(child_iter); + child->m_parent = 0; + return child; + } + return 0; +} + +ObjectType* +ObjectType::takeChild(const Str& name) +{ + return takeChild(findChild(name)); + +} + +ObjectType* +ObjectType::findChild(const Str& name) +{ + for (ObjectTypeList::iterator child_iter = m_children.begin(); + child_iter != m_children.end(); ++child_iter) { + + if ((*child_iter)->objectName() == name) + return *child_iter; + } + return 0; +} + +void +ObjectType::killChild(const Str& name) +{ + for (ObjectTypeList::iterator child_iter = m_children.begin(); + child_iter != m_children.end(); ++child_iter) { + + if ((*child_iter)->objectName() == name) { + ObjectType* child = *child_iter; + removeChild(child); + delete child; + break; + } + } +} + +void +ObjectType::setParent(ObjectType* parent) +{ + if (m_parent == parent) + return; + + if (m_parent) + m_parent->removeChild(this); + + m_parent = parent; + if (m_parent) + m_parent->m_children.push_back(this); +} + +void +ObjectType::setObjectName(const Str& name) +{ + m_objectName = name; +} + +Str +ObjectType::objectName() const +{ + return m_objectName; +} + +bool +ObjectType::causeEvent(Event::EventType eventType) +{ + Event e(eventType); + return event(&e); +} + +bool +ObjectType::event(Event* event) +{ + return true; +} + +int +ObjectType::processEvent(ObjectTypeList objects, Event *event) +{ + int evaluated = 0; + + for (ObjectTypeList::iterator obj_iter = objects.begin(); + obj_iter != objects.end(); ++obj_iter) { + if((*obj_iter)->event(event)) + evaluated++; + } + + return evaluated; + +} + +void +ObjectType::callInvalidateEvent(Event* event) +{ + invalidateEvent(event); +} + +void +ObjectType::setLayout(ObjectTypeLayout* l) +{ + if (!l) { + cerr << "[WARNING] ObjectType::setLayout: Cannot set layout to 0." << endl; + return; + } + + if (layout()) { + if (layout() != l) { + cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring(); + cerr << "' on ObjectType '" << objectName().cstring() << "', which already has a layout." << endl; + } + return; + } + + ObjectType* oldParent = l->parent(); + if (oldParent && oldParent != this) { + if (oldParent->isLayoutType()) { + cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring(); + cerr << "' on ObjectType '" << objectName().cstring() << "', when the ObjectTypeLayout already has a parent layout." << endl; + return; + } else { + // Steal the layout from an ObjectType parent. + oldParent->takeLayout(); + } + } + + m_layout = l; + if (oldParent != this) { + l->setParent(this); + l->reparentChildren(this); + } +} + +ObjectTypeLayout* ObjectType::takeLayout() +{ + ObjectTypeLayout* l = layout(); + if (!l) + return 0; + m_layout = 0; + l->setParent(0); + return l; +} + +unsigned int +objectTypeHash(const ObjectType* objectType) +{ + return reinterpret_cast<std::size_t>(objectType); +} + +unsigned char +ObjectType::callWithEnum(const Str& prefix, Event::EventType type, unsigned char value){ + return value*value; +} + +unsigned char +ObjectType::callWithEnum(const Str& prefix, unsigned char value) { + return value; +} + +void +ObjectType::setObjectSplittedName(const char*, const Str& prefix, const Str& suffix) +{ + std::string result(prefix.cstring()); + result += suffix.cstring(); + m_objectName = result.c_str(); +} + +void +ObjectType::setObjectNameWithSize(const char*, int size, const Str& name) +{ + std::string result(name.cstring(), size); + m_objectName = result.c_str(); +} + +void +ObjectType::setObjectNameWithSize(const Str& name, int size) +{ + setObjectNameWithSize("", size, name); +} + +void ObjectType::setObject(ObjectType *) +{ + m_call_id = 0; +} + +void ObjectType::setObject(const Null&) +{ + m_call_id = 1; +} + +int ObjectType::callId() const +{ + return m_call_id; +} + + +void ObjectType::callVirtualCreateChild() +{ + ObjectType* fake_parent = new ObjectType(); + ObjectType* fake_child = createChild(fake_parent); + assert(fake_child->isPython()); + delete fake_parent; +} + +ObjectType* ObjectType::createChild(ObjectType* parent) +{ + return new ObjectType(parent); +} + +std::size_t ObjectType::createObjectType() +{ + void* addr = new ObjectType(); + return (std::size_t) addr; +} + +OtherBase::~OtherBase() +{ +} + +ObjectTypeDerived::~ObjectTypeDerived() +{ +} + +bool +ObjectTypeDerived::event(Event* event) +{ + return true; +} diff --git a/sources/shiboken2/tests/libsample/objecttype.h b/sources/shiboken2/tests/libsample/objecttype.h new file mode 100644 index 000000000..eae95b3b1 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttype.h @@ -0,0 +1,176 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTTYPE_H +#define OBJECTTYPE_H + +#include <list> +#include "str.h" +#include "null.h" + +#include "libsamplemacros.h" + +#include <stddef.h> + +struct Event +{ + enum EventType { + NO_EVENT, + BASIC_EVENT, + SOME_EVENT, + ANY_EVENT + }; + Event(EventType eventType) : m_eventType(eventType) {} + EventType eventType() { return m_eventType; } +private: + EventType m_eventType; +}; + +class ObjectTypeLayout; +class ObjectType; +typedef std::list<ObjectType*> ObjectTypeList; + +class LIBSAMPLE_API ObjectType +{ +public: + // ### Fixme: Use uintptr_t in C++ 11 + typedef size_t Identifier; + + explicit ObjectType(ObjectType* parent = 0); + virtual ~ObjectType(); + + // factory method + inline static ObjectType* create() { return new ObjectType(); } + static ObjectType* createWithChild(); + + void setParent(ObjectType* parent); + inline ObjectType* parent() const { return m_parent; } + inline const ObjectTypeList& children() const { return m_children; } + void killChild(const Str& name); + void removeChild(ObjectType* child); + ObjectType* takeChild(ObjectType* child); + virtual ObjectType* takeChild(const Str& name); + ObjectType* findChild(const Str& name); + + Str objectName() const; + void setObjectName(const Str& name); + + inline Identifier identifier() const { return reinterpret_cast<Identifier>(this); } + + bool causeEvent(Event::EventType eventType); + + // Returns true if the event is processed. + virtual bool event(Event* event); + static int processEvent(ObjectTypeList objects, Event *event); + + void callInvalidateEvent(Event* event); + virtual void invalidateEvent(Event* event) {} + + // This nonsense method emulate QWidget.setLayout method + // All layout objects will became children of this object. + void setLayout(ObjectTypeLayout* layout); + inline ObjectTypeLayout* layout() const { return m_layout; } + + // This method should be reimplemented by ObjectTypeLayout. + virtual bool isLayoutType() { return false; } + + unsigned char callWithEnum(const Str& prefix, Event::EventType type, unsigned char value=80); + unsigned char callWithEnum(const Str& prefix, unsigned char value=0); + + //Functions used in test with named arguments + void setObjectSplittedName(const char*, const Str& prefix = Str("<unk"), const Str& suffix = Str("nown>")); + void setObjectNameWithSize(const char*, int size=9, const Str& name = Str("<unknown>")); + void setObjectNameWithSize(const Str& name = Str("<unknown>"), int size=9); + + //Function used to confuse the generator when two values accept Null as arg + void setObject(ObjectType *); + void setObject(const Null&); + int callId() const; + + //Function used to create a parent from C++ + virtual bool isPython() { return false; } + void callVirtualCreateChild(); + virtual ObjectType* createChild(ObjectType* parent); + static std::size_t createObjectType(); + + //return a parent from C++ + ObjectType* getCppParent() { + if (!m_parent) { + ObjectType* parent = new ObjectType(); + setParent(parent); + } + return m_parent; + } + + void destroyCppParent() { + delete m_parent; + m_parent = 0; + } + + //Deprecated test + bool deprecatedFunction() { return true; } + + // nextInFocusChain simply returns the parent to test object cycles; the parent + // may be returned by the QWidget's implementation but isn't always returned + ObjectType* nextInFocusChain() { return m_parent; } + +private: + ObjectType(const ObjectType&); + ObjectType& operator=(const ObjectType&); + + ObjectTypeLayout* takeLayout(); + + Str m_objectName; + ObjectType* m_parent; + ObjectTypeList m_children; + + ObjectTypeLayout* m_layout; + + + //used on overload null test + int m_call_id; +}; + +LIBSAMPLE_API unsigned int objectTypeHash(const ObjectType* objectType); + +class LIBSAMPLE_API OtherBase { +public: + OtherBase() {}; + virtual ~OtherBase(); +}; + +class LIBSAMPLE_API ObjectTypeDerived: public ObjectType, public OtherBase { +public: + ObjectTypeDerived(): ObjectType(), OtherBase() {}; + + virtual bool event(Event* event); + virtual ~ObjectTypeDerived(); +}; + +#endif // OBJECTTYPE_H + diff --git a/sources/shiboken2/tests/libsample/objecttypebyvalue.h b/sources/shiboken2/tests/libsample/objecttypebyvalue.h new file mode 100644 index 000000000..1b61ec4dc --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypebyvalue.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTTYPEBYVALUE_H +#define OBJECTTYPEBYVALUE_H +#include <list> +#include "protected.h" + +class ObjectTypeByValue +{ +public: + ObjectTypeByValue returnSomeKindOfMe() { return ObjectTypeByValue(); } + void acceptKindOfMeAsValue(ObjectTypeByValue kindOfMe) {} + + void acceptListOfObjectTypeByValue(std::list<ObjectTypeByValue> listOfMe) {} + + // prop used to check for segfaults + ProtectedProperty prop; +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/objecttypeholder.cpp b/sources/shiboken2/tests/libsample/objecttypeholder.cpp new file mode 100644 index 000000000..dabeb2f86 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypeholder.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "objecttypeholder.h" + +ObjectTypeHolder::ObjectTypeHolder(const char* objectName) +{ + m_objectType = new ObjectType(); + m_objectType->setObjectName(objectName); +} + +ObjectTypeHolder::~ObjectTypeHolder() +{ + delete m_objectType; +} + +Str +ObjectTypeHolder::passObjectTypeAsReference(const ObjectType& objectType) +{ + return objectType.objectName(); +} + +Str +ObjectTypeHolder::callPassObjectTypeAsReference() +{ + return passObjectTypeAsReference(*m_objectType); +} diff --git a/sources/shiboken2/tests/libsample/objecttypeholder.h b/sources/shiboken2/tests/libsample/objecttypeholder.h new file mode 100644 index 000000000..c2e7275d4 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypeholder.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTTYPEHOLDER_H +#define OBJECTTYPEHOLDER_H + +#include "libsamplemacros.h" +#include "objecttype.h" +#include "str.h" + +class LIBSAMPLE_API ObjectTypeHolder +{ +public: + explicit ObjectTypeHolder(const char* objectName); + virtual ~ObjectTypeHolder(); + + ObjectType* getObjecType() { return m_objectType; } + + virtual Str passObjectTypeAsReference(const ObjectType& objectType); + Str callPassObjectTypeAsReference(); + +private: + ObjectType* m_objectType; +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/objecttypelayout.cpp b/sources/shiboken2/tests/libsample/objecttypelayout.cpp new file mode 100644 index 000000000..d2a7d1bba --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypelayout.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "objecttypelayout.h" +#include <iostream> + +using namespace std; + +void ObjectTypeLayout::addObject(ObjectType* obj) +{ + if (obj->isLayoutType()) { + ObjectTypeLayout* l = reinterpret_cast<ObjectTypeLayout*>(obj); + if (l->parent()) { + cerr << "[WARNING] ObjectTypeLayout::addObject: layout '" << l->objectName().cstring(); + cerr << "' already has a parent." << endl; + return; + } + + l->setParent(this); + + if (parent() && !parent()->isLayoutType()) + l->reparentChildren(parent()); + } + + m_objects.push_back(obj); +} + +std::list< ObjectType* > ObjectTypeLayout::objects() const +{ + return m_objects; +} + +void ObjectTypeLayout::reparentChildren(ObjectType* parent) +{ + std::list<ObjectType*>::const_iterator it = m_objects.begin(); + for (; it != m_objects.end(); ++it) { + if ((*it)->isLayoutType()) + reinterpret_cast<ObjectTypeLayout*>(*it)->reparentChildren(parent); + else + (*it)->setParent(parent); + } +} + diff --git a/sources/shiboken2/tests/libsample/objecttypelayout.h b/sources/shiboken2/tests/libsample/objecttypelayout.h new file mode 100644 index 000000000..3fa8b9dbf --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypelayout.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTTYPELAYOUT_H +#define OBJECTTYPELAYOUT_H + +#include "libsamplemacros.h" +#include "objecttype.h" +#include <list> + +class ObjectType; + +class LIBSAMPLE_API ObjectTypeLayout : public ObjectType +{ +public: + void addObject(ObjectType* obj); + std::list<ObjectType*> objects() const; + + virtual bool isLayoutType() { return true; } + inline static ObjectTypeLayout* create() { return new ObjectTypeLayout(); } + + virtual ObjectType* takeChild(const Str& name) { return ObjectType::takeChild(name); } +private: + std::list<ObjectType*> m_objects; + + void reparentChildren(ObjectType* parent); + friend LIBSAMPLE_API void ObjectType::setLayout(ObjectTypeLayout* l); +}; + +#endif // OBJECTTYPELAYOUT_H + diff --git a/sources/shiboken2/tests/libsample/objecttypeoperators.cpp b/sources/shiboken2/tests/libsample/objecttypeoperators.cpp new file mode 100644 index 000000000..1bdaded30 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypeoperators.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "objecttypeoperators.h" + +ObjectTypeOperators::ObjectTypeOperators(const std::string key) : m_key(key) +{ +} + +bool ObjectTypeOperators::operator==(const ObjectTypeOperators& other) const +{ + return m_key == other.m_key; +} + +const ObjectTypeOperators& ObjectTypeOperators::operator<(const ObjectTypeOperators& other) const +{ + return m_key < other.m_key ? *this : other; +} + +bool operator==(const ObjectTypeOperators* obj, const std::string& str) +{ + return obj->key() == str; +} + +bool operator==(const std::string& str, const ObjectTypeOperators* obj) +{ + return str == obj->key(); +} + +std::string operator+(const ObjectTypeOperators* obj, const std::string& str) +{ + return obj->key() + str; +} + +std::string operator+(const std::string& str, const ObjectTypeOperators* obj) +{ + return str + obj->key(); +} diff --git a/sources/shiboken2/tests/libsample/objecttypeoperators.h b/sources/shiboken2/tests/libsample/objecttypeoperators.h new file mode 100644 index 000000000..5795ac456 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objecttypeoperators.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTTYPEOPERATORS_H +#define OBJECTTYPEOPERATORS_H + +#include "libsamplemacros.h" +#include <string> + +class LIBSAMPLE_API ObjectTypeOperators +{ +public: + explicit ObjectTypeOperators(const std::string key); + virtual ~ObjectTypeOperators() {} + + bool operator==(const ObjectTypeOperators& other) const; + const ObjectTypeOperators& operator<(const ObjectTypeOperators& other) const; + + // chaos! + virtual void operator>(const ObjectTypeOperators&) { m_key.append("operator>"); } + + std::string key() const { return m_key; } + +private: + std::string m_key; + + ObjectTypeOperators(ObjectTypeOperators&); + ObjectTypeOperators& operator=(ObjectTypeOperators&); +}; + +LIBSAMPLE_API bool operator==(const ObjectTypeOperators* obj, const std::string& str); +LIBSAMPLE_API bool operator==(const std::string& str, const ObjectTypeOperators* obj); +LIBSAMPLE_API std::string operator+(const ObjectTypeOperators* obj, const std::string& str); +LIBSAMPLE_API std::string operator+(const std::string& str, const ObjectTypeOperators* obj); + +#endif // OBJECTTYPEOPERATORS_H diff --git a/sources/shiboken2/tests/libsample/objectview.cpp b/sources/shiboken2/tests/libsample/objectview.cpp new file mode 100644 index 000000000..08c5f45f6 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objectview.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "objectview.h" +#include "objectmodel.h" +#include "str.h" + +Str +ObjectView::displayModelData() +{ + if (!m_model) + return Str("(NULL)"); + return Str("Name: %VAR").arg(m_model->objectName()); +} + +void +ObjectView::modifyModelData(Str& data) +{ + if (m_model) + m_model->setObjectName(data); +} + + +ObjectType* +ObjectView::getRawModelData() +{ + return m_model->data(); +} + diff --git a/sources/shiboken2/tests/libsample/objectview.h b/sources/shiboken2/tests/libsample/objectview.h new file mode 100644 index 000000000..0bcc09a30 --- /dev/null +++ b/sources/shiboken2/tests/libsample/objectview.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTVIEW_H +#define OBJECTVIEW_H + +#include "objecttype.h" +#include "libsamplemacros.h" + +class Str; +class ObjectModel; + +class LIBSAMPLE_API ObjectView : public ObjectType +{ +public: + ObjectView(ObjectModel* model = 0, ObjectType* parent = 0) + : ObjectType(parent), m_model(model) + {} + + inline void setModel(ObjectModel* model) { m_model = model; } + inline ObjectModel* model() const { return m_model; } + + Str displayModelData(); + void modifyModelData(Str& data); + + ObjectType* getRawModelData(); + +private: + ObjectModel* m_model; +}; + +#endif // OBJECTVIEW_H + diff --git a/sources/shiboken2/tests/libsample/oddbool.h b/sources/shiboken2/tests/libsample/oddbool.h new file mode 100644 index 000000000..ee0f4fa03 --- /dev/null +++ b/sources/shiboken2/tests/libsample/oddbool.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ODDBOOL_H +#define ODDBOOL_H + +#include "libsamplemacros.h" + +class OddBool +{ + +public: + inline explicit OddBool(bool b) : m_value(b) {} + bool value() const { return m_value; } + + inline OddBool operator!() const { return OddBool(!m_value); } + +private: + bool m_value; +}; + +inline bool operator==(OddBool b1, bool b2) { return (!b1).value() == !b2; } +inline bool operator==(bool b1, OddBool b2) { return (!b1) == (!b2).value(); } +inline bool operator==(OddBool b1, OddBool b2) { return (!b1).value() == (!b2).value(); } +inline bool operator!=(OddBool b1, bool b2) { return (!b1).value() != !b2; } +inline bool operator!=(bool b1, OddBool b2) { return (!b1) != (!b2).value(); } +inline bool operator!=(OddBool b1, OddBool b2) { return (!b1).value() != (!b2).value(); } + +class OddBoolUser +{ +public: + OddBoolUser() : m_oddbool(OddBool(false)) {} + OddBoolUser(const OddBool& oddBool) : m_oddbool(oddBool) {} + virtual ~OddBoolUser() {} + + inline OddBool oddBool() { return m_oddbool; } + inline void setOddBool(OddBool oddBool) { m_oddbool = oddBool; } + + virtual OddBool invertedOddBool() + { + return !m_oddbool; + } + + inline OddBool callInvertedOddBool() + { + return invertedOddBool(); + } + + static inline OddBool getOddBool(const OddBoolUser& oddBoolUser) + { + return oddBoolUser.m_oddbool; + } + +private: + OddBool m_oddbool; +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/onlycopy.cpp b/sources/shiboken2/tests/libsample/onlycopy.cpp new file mode 100644 index 000000000..5407f9d33 --- /dev/null +++ b/sources/shiboken2/tests/libsample/onlycopy.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "onlycopy.h" + +OnlyCopy::OnlyCopy(const OnlyCopy& other) +{ + m_value = other.m_value; +} + +OnlyCopy& +OnlyCopy::operator=(const OnlyCopy& other) +{ + m_value = other.m_value; + return *this; +} + +OnlyCopy +FriendOfOnlyCopy::createOnlyCopy(int value) +{ + + return OnlyCopy(value); +} + +std::list<OnlyCopy> +FriendOfOnlyCopy::createListOfOnlyCopy(int quantity) +{ + std::list<OnlyCopy> list; + for (int i = 0; i < quantity; ++i) + list.push_back(createOnlyCopy(i)); + return list; +} diff --git a/sources/shiboken2/tests/libsample/onlycopy.h b/sources/shiboken2/tests/libsample/onlycopy.h new file mode 100644 index 000000000..6abad7382 --- /dev/null +++ b/sources/shiboken2/tests/libsample/onlycopy.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ONLYCOPYCLASS_H +#define ONLYCOPYCLASS_H + +#include "libsamplemacros.h" +#include <list> + +// These classes simulate a situation found in +// QtWebKit's QWebDatabase and QWebSecurityOrigin. + +class LIBSAMPLE_API OnlyCopy +{ +public: + OnlyCopy(const OnlyCopy& other); + OnlyCopy& operator=(const OnlyCopy& other); + int value() const { return m_value; } + static int getValue(OnlyCopy onlyCopy) { return onlyCopy.m_value; } + static int getValueFromReference(const OnlyCopy& onlyCopy) { return onlyCopy.m_value; } +private: + int m_value; + OnlyCopy(int value) : m_value(value) {}; + friend class FriendOfOnlyCopy; +}; + +class LIBSAMPLE_API FriendOfOnlyCopy +{ +public: + static OnlyCopy createOnlyCopy(int value); + static std::list<OnlyCopy> createListOfOnlyCopy(int quantity); +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/overload.cpp b/sources/shiboken2/tests/libsample/overload.cpp new file mode 100644 index 000000000..753407e35 --- /dev/null +++ b/sources/shiboken2/tests/libsample/overload.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "overload.h" + +Overload::FunctionEnum Overload::overloaded() +{ + return Function0; +} + +Overload::FunctionEnum Overload::overloaded(Size* size) +{ + return Function1; +} + +Overload::FunctionEnum Overload::overloaded(Point* point, ParamEnum param) +{ + return Function2; +} + +Overload::FunctionEnum Overload::overloaded(const Point& point) +{ + return Function3; +} + diff --git a/sources/shiboken2/tests/libsample/overload.h b/sources/shiboken2/tests/libsample/overload.h new file mode 100644 index 000000000..5005b4f8c --- /dev/null +++ b/sources/shiboken2/tests/libsample/overload.h @@ -0,0 +1,143 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OVERLOAD_H +#define OVERLOAD_H + +#include "echo.h" +#include "str.h" +#include "size.h" +#include "point.h" +#include "pointf.h" +#include "polygon.h" +#include "rect.h" + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Overload +{ +public: + enum FunctionEnum { + Function0, + Function1, + Function2, + Function3, + Function4, + Function5, + Function6 + }; + + enum ParamEnum { + Param0, + Param1 + }; + + Overload() {} + virtual ~Overload() {} + + FunctionEnum overloaded(); + FunctionEnum overloaded(Size* size); + FunctionEnum overloaded(Point* point, ParamEnum param); + FunctionEnum overloaded(const Point& point); + + inline void differentReturnTypes(ParamEnum param = Param0) {} + inline int differentReturnTypes(ParamEnum param, int val) { return val; } + + inline int intOverloads(const Point& p, double d) { return 1; } + inline int intOverloads(int i, int i2) { return 2; } + inline int intOverloads(int i, int removedArg, double d) { return 3; } + + inline FunctionEnum intDoubleOverloads(int a0, int a1) const { return Function0; } + inline FunctionEnum intDoubleOverloads(double a0, double a1) const { return Function1; } + + void singleOverload(Point* x) {} + Point* singleOverload() {return new Point();} + + // Similar to QImage::trueMatrix(QMatrix,int,int) and QImage::trueMatrix(QTransform,int,int) + FunctionEnum wrapperIntIntOverloads(const Point& arg0, int arg1, int arg2) { return Function0; } + FunctionEnum wrapperIntIntOverloads(const Polygon& arg0, int arg1, int arg2) { return Function1; } + + // Similar to QImage constructor + FunctionEnum strBufferOverloads(const Str& arg0, const char* arg1 = 0, bool arg2 = true) { return Function0; } + FunctionEnum strBufferOverloads(unsigned char* arg0, int arg1) { return Function1; } + FunctionEnum strBufferOverloads() { return Function2; } + + // Similar to QPainter::drawText(...) + FunctionEnum drawText(const Point& a0, const Str& a1) { return Function0; } + FunctionEnum drawText(const PointF& a0, const Str& a1) { return Function1; } + FunctionEnum drawText(const Rect& a0, int a1, const Str& a2) { return Function2; } + FunctionEnum drawText(const RectF& a0, int a1, const Str& a2) { return Function3; } + FunctionEnum drawText(const RectF& a0, const Str& a1, const Echo& a2 = Echo()) { return Function4; } + FunctionEnum drawText(int a0, int a1, const Str& a2) { return Function5; } + FunctionEnum drawText(int a0, int a1, int a2, int a3, int a4, const Str& a5) { return Function6; } + + // A variant of the one similar to QPainter::drawText(...) + FunctionEnum drawText2(const Point& a0, const Str& a1) { return Function0; } + FunctionEnum drawText2(const PointF& a0, const Str& a1) { return Function1; } + FunctionEnum drawText2(const Rect& a0, int a1, const Str& a2) { return Function2; } + FunctionEnum drawText2(const RectF& a0, int a1, const Str& a2) { return Function3; } + FunctionEnum drawText2(const RectF& a0, const Str& a1, const Echo& a2 = Echo()) { return Function4; } + FunctionEnum drawText2(int a0, int a1, const Str& a2) { return Function5; } + FunctionEnum drawText2(int a0, int a1, int a2, int a3 = 0, int a4 = 0, const Str& a5 = Str()) { return Function6; } + + // A simpler variant of the one similar to QPainter::drawText(...) + FunctionEnum drawText3(const Str& a0, const Str& a1, const Str& a2) { return Function0; } + FunctionEnum drawText3(int a0, int a1, int a2, int a3, int a4) { return Function1; } + + // Another simpler variant of the one similar to QPainter::drawText(...) + FunctionEnum drawText4(int a0, int a1, int a2) { return Function0; } + FunctionEnum drawText4(int a0, int a1, int a2, int a3, int a4) { return Function1; } + + FunctionEnum acceptSequence() { return Function0; } + FunctionEnum acceptSequence(int a0, int a1) { return Function1; } + FunctionEnum acceptSequence(const Str& a0, ParamEnum a1 = Param0) { return Function2; } + FunctionEnum acceptSequence(const Size& a0) { return Function3; } + // The type must be changed to PySequence. + FunctionEnum acceptSequence(const char* const a0[]) { return Function4; } + FunctionEnum acceptSequence(void* a0) { return Function5; } +}; + +class LIBSAMPLE_API Overload2 : public Overload +{ +public: + // test bug#616, public and private method differ only by const + void doNothingInPublic() const {} + void doNothingInPublic(int) {} + virtual void doNothingInPublic3() const {} + void doNothingInPublic3(int) const {} +protected: + void doNothingInPublic2() const {} + void doNothingInPublic2(int) {} +private: + void doNothingInPublic() {} + void doNothingInPublic2() {} + void doNothingInPublic3() {} +}; + +#endif // OVERLOAD_H + diff --git a/sources/shiboken2/tests/libsample/overloadsort.cpp b/sources/shiboken2/tests/libsample/overloadsort.cpp new file mode 100644 index 000000000..f1bc9665b --- /dev/null +++ b/sources/shiboken2/tests/libsample/overloadsort.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "overloadsort.h" + diff --git a/sources/shiboken2/tests/libsample/overloadsort.h b/sources/shiboken2/tests/libsample/overloadsort.h new file mode 100644 index 000000000..9145504ae --- /dev/null +++ b/sources/shiboken2/tests/libsample/overloadsort.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OVERLOADSORT_H +#define OVERLOADSORT_H + +#include "libsamplemacros.h" + +#include <list> + +class ImplicitTarget +{ +public: + ImplicitTarget(){} +}; + +class ImplicitBase +{ +public: + ImplicitBase(){} + ImplicitBase(const ImplicitTarget &b){} +}; + +class SortedOverload +{ +public: + + inline const char *overload(int x) { + return "int"; + } + + inline const char *overload(double x) { + return "double"; + } + + inline const char *overload(ImplicitBase x) { + return "ImplicitBase"; + } + + inline const char *overload(ImplicitTarget x) { + return "ImplicitTarget"; + } + + inline const char *overload(const std::list<ImplicitBase> &x) { + return "list(ImplicitBase)"; + } + + inline int implicit_overload(const ImplicitBase &x) { + return 1; + } + + inline const char *overloadDeep(int x, ImplicitBase &y) { + return "ImplicitBase"; + } + + + inline const char* pyObjOverload(int, int) { return "int,int"; } + inline const char* pyObjOverload(unsigned char*, int) { return "PyObject,int"; } + +}; + +#endif // OVERLOADSORT_H + diff --git a/sources/shiboken2/tests/libsample/pairuser.cpp b/sources/shiboken2/tests/libsample/pairuser.cpp new file mode 100644 index 000000000..b4b51b8cf --- /dev/null +++ b/sources/shiboken2/tests/libsample/pairuser.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "pairuser.h" + +using namespace std; + +std::pair<int, int> +PairUser::callCreatePair() +{ + return createPair(); +} + +std::pair<int, int> +PairUser::createPair() +{ + return std::pair<int, int>(10, 20); +} + +std::pair<Complex, Complex> +PairUser::createComplexPair(Complex cpx0, Complex cpx1) +{ + return std::pair<Complex, Complex>(cpx0, cpx1); +} + +double +PairUser::sumPair(std::pair<int, double> pair) +{ + return ((double) pair.first) + pair.second; +} + diff --git a/sources/shiboken2/tests/libsample/pairuser.h b/sources/shiboken2/tests/libsample/pairuser.h new file mode 100644 index 000000000..7b87ba65b --- /dev/null +++ b/sources/shiboken2/tests/libsample/pairuser.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PAIRUSER_H +#define PAIRUSER_H + +#include <utility> +#include "complex.h" + +#include "libsamplemacros.h" + +class LIBSAMPLE_API PairUser +{ +public: + PairUser() {} + virtual ~PairUser() {} + + virtual std::pair<int, int> createPair(); + std::pair<int, int> callCreatePair(); + static std::pair<Complex, Complex> createComplexPair(Complex cpx0, Complex cpx1); + double sumPair(std::pair<int, double> pair); + + inline void setPair(std::pair<int, int> pair) { m_pair = pair; } + inline std::pair<int, int> getPair() { return m_pair; } + +private: + std::pair<int, int> m_pair; +}; +#endif // PAIRUSER_H + diff --git a/sources/shiboken2/tests/libsample/pen.cpp b/sources/shiboken2/tests/libsample/pen.cpp new file mode 100644 index 000000000..509b47c76 --- /dev/null +++ b/sources/shiboken2/tests/libsample/pen.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "pen.h" + +Color::Color() : m_null(true) +{ +} + +Color::Color(SampleNamespace::InValue arg) : m_null(false) +{ +} + +Color::Color(unsigned int arg) : m_null(false) +{ +} + +bool Color::isNull() const +{ + return m_null; +} + +Pen::Pen() : m_ctor(EmptyCtor) +{ +} + +Pen::Pen(SampleNamespace::Option option) : m_ctor(EnumCtor) +{ +} + +Pen::Pen(const Color& color) : m_ctor(ColorCtor) +{ +} + +Pen::Pen(const Pen& pen) : m_ctor(CopyCtor) +{ +} + +int Pen::ctorType() +{ + return m_ctor; +} diff --git a/sources/shiboken2/tests/libsample/pen.h b/sources/shiboken2/tests/libsample/pen.h new file mode 100644 index 000000000..e1fb641d3 --- /dev/null +++ b/sources/shiboken2/tests/libsample/pen.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PEN_H +#define PEN_H + +#include "libsamplemacros.h" +#include "samplenamespace.h" + +class LIBSAMPLE_API Color +{ +public: + Color(); + Color(SampleNamespace::InValue arg); + Color(unsigned int arg); + + bool isNull() const; +private: + bool m_null; +}; + +class LIBSAMPLE_API Pen +{ +public: + enum { EmptyCtor, EnumCtor, ColorCtor, CopyCtor }; + + Pen(); + Pen(SampleNamespace::Option option); + Pen(const Color& color); + Pen(const Pen& pen); + + int ctorType(); +private: + int m_ctor; +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/photon.cpp b/sources/shiboken2/tests/libsample/photon.cpp new file mode 100644 index 000000000..debc9f116 --- /dev/null +++ b/sources/shiboken2/tests/libsample/photon.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "photon.h" + +namespace Photon +{ +const ClassType Base::staticType; +int callCalculateForValueDuplicatorPointer(ValueDuplicator* value) +{ + return value->calculate(); +} +int callCalculateForValueDuplicatorReference(ValueDuplicator& value) +{ + return value.calculate(); +} +int countValueIdentities(const std::list<ValueIdentity>& values) +{ + return values.size(); +} +int countValueDuplicators(const std::list<TemplateBase<DuplicatorType> >& values) +{ + return values.size(); +} +} // namespace Photon diff --git a/sources/shiboken2/tests/libsample/photon.h b/sources/shiboken2/tests/libsample/photon.h new file mode 100644 index 000000000..d8b1be423 --- /dev/null +++ b/sources/shiboken2/tests/libsample/photon.h @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PHOTON_H +#define PHOTON_H + +#include <list> +#include "libsamplemacros.h" + +// This namespace and classes simulate +// situations found in Qt's phonon module. + +namespace Photon +{ + +enum ClassType { + BaseType = 0, + IdentityType = 1, + DuplicatorType = 2 +}; + +class LIBSAMPLE_API Base +{ +public: + explicit Base(int value) : m_value(value) {} + virtual ~Base() {} + inline void setValue(int value) { m_value = value; } + inline int value() const { return m_value; } + + template <class T> bool isType() { return type() == T::staticType; } + bool isType(ClassType t) { return type() == t; } + + virtual ClassType type() const { return BaseType; }; + static const ClassType staticType = BaseType; + +protected: + int m_value; +}; + +template<ClassType CLASS_TYPE> +class LIBSAMPLE_API TemplateBase : public Base +{ +public: + explicit TemplateBase(int value) : Base(value) {} + inline int multiplicator() const { return (int)CLASS_TYPE; } + inline int calculate() const { return m_value * ((int)CLASS_TYPE); } + static inline ClassType classType() { return CLASS_TYPE; } + + inline int sumValueUsingPointer(TemplateBase<CLASS_TYPE>* other) const { return m_value + other->m_value; } + inline int sumValueUsingReference(TemplateBase<CLASS_TYPE>& other) const { return m_value + other.m_value; } + + inline std::list<TemplateBase<CLASS_TYPE> > getListOfThisTemplateBase() + { + std::list<TemplateBase<CLASS_TYPE> > objs; + objs.push_back(*this); + objs.push_back(*this); + return objs; + } + + static inline TemplateBase<CLASS_TYPE>* passPointerThrough(TemplateBase<CLASS_TYPE>* obj) { return obj; } + + virtual ClassType type() const { return CLASS_TYPE; } + static const ClassType staticType = CLASS_TYPE; +}; + +#if defined _WIN32 || defined __CYGWIN__ +template class LIBSAMPLE_API TemplateBase<IdentityType>; +template class LIBSAMPLE_API TemplateBase<DuplicatorType>; +#endif + +typedef TemplateBase<IdentityType> ValueIdentity; +typedef TemplateBase<DuplicatorType> ValueDuplicator; + +LIBSAMPLE_API int callCalculateForValueDuplicatorPointer(ValueDuplicator* value); +LIBSAMPLE_API int callCalculateForValueDuplicatorReference(ValueDuplicator& value); +LIBSAMPLE_API int countValueIdentities(const std::list<ValueIdentity>& values); +LIBSAMPLE_API int countValueDuplicators(const std::list<TemplateBase<DuplicatorType> >& values); + +// This simulates an internal error (SEGV) caused by 'noexcept' in +// boost::intrusive_ptr before support for 'noexcept' was added. The ENTIRE +// code below is needed to trigger the exception; it isn't seen with just a +// 'noexcept' following a declaration. +// +// NOTE: For reasons that should be fairly obvious, this test unfortunately can +// only be "run" when building in C++11 mode. +#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900) +# define PHOTON_NOEXCEPT noexcept +#else +# define PHOTON_NOEXCEPT +#endif +class Pointer +{ +public: + Pointer() PHOTON_NOEXCEPT : px(0) {} + Pointer(int* p) : px(p) {} + + void reset() PHOTON_NOEXCEPT { Pointer().swap(*this); } + + int* get() const PHOTON_NOEXCEPT { return px; } + int& operator*() const { return *px; } + + void swap(Pointer& rhs) PHOTON_NOEXCEPT + { + int* tmp = px; + px = rhs.px; + rhs.px = tmp; + } + +private: + int* px; +}; + +} // namespace Photon + +#endif // PHOTON_H diff --git a/sources/shiboken2/tests/libsample/point.cpp b/sources/shiboken2/tests/libsample/point.cpp new file mode 100644 index 000000000..30275393b --- /dev/null +++ b/sources/shiboken2/tests/libsample/point.cpp @@ -0,0 +1,155 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "point.h" + +using namespace std; + +Point::Point(int x, int y) : m_x(x), m_y(y) +{ +} + +Point::Point(double x, double y) : m_x(x), m_y(y) +{ +} + +void +Point::midpoint(const Point& other, Point* midpoint) const +{ + if (!midpoint) + return; + midpoint->setX((m_x + other.m_x) / 2.0); + midpoint->setY((m_y + other.m_y) / 2.0); +} + +Point* +Point::copy() const +{ + Point* pt = new Point(); + pt->m_x = m_x; + pt->m_y = m_y; + return pt; +} + +void +Point::show() +{ + cout << "(x: " << m_x << ", y: " << m_y << ")"; +} + +bool +Point::operator==(const Point& other) +{ + return m_x == other.m_x && m_y == other.m_y; +} + +Point +Point::operator+(const Point& other) +{ + return Point(m_x + other.m_x, m_y + other.m_y); +} + +Point +Point::operator-(const Point& other) +{ + return Point(m_x - other.m_x, m_y - other.m_y); +} + +Point& +Point::operator+=(Point &other) +{ + m_x += other.m_x; + m_y += other.m_y; + return *this; +} + +Point& +Point::operator-=(Point &other) +{ + m_x -= other.m_x; + m_y -= other.m_y; + return *this; +} + +Point +operator*(const Point& pt, double mult) +{ + return Point(pt.m_x * mult, pt.m_y * mult); +} + +Point +operator*(const Point& pt, int mult) +{ + return Point(((int) pt.m_x) * mult, ((int) pt.m_y) * mult); +} + +Point +operator*(double mult, const Point& pt) +{ + return Point(pt.m_x * mult, pt.m_y * mult); +} + +Point +operator*(int mult, const Point& pt) +{ + return Point(((int) pt.m_x) * mult, ((int) pt.m_y) * mult); +} + +Point +operator-(const Point& pt) +{ + return Point(-pt.m_x, -pt.m_y); +} + +bool +operator!(const Point& pt) +{ + return (pt.m_x == 0.0 && pt.m_y == 0.0); +} + +Point +Point::operator/(int operand) +{ + return Point(m_x/operand, m_y/operand); +} + +Complex +transmutePointIntoComplex(const Point& point) +{ + Complex cpx(point.x(), point.y()); + return cpx; +} + +Point +transmuteComplexIntoPoint(const Complex& cpx) +{ + Point pt(cpx.real(), cpx.imag()); + return pt; +} + diff --git a/sources/shiboken2/tests/libsample/point.h b/sources/shiboken2/tests/libsample/point.h new file mode 100644 index 000000000..9416d6ddf --- /dev/null +++ b/sources/shiboken2/tests/libsample/point.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef POINT_H +#define POINT_H + +#include "complex.h" +#include <utility> + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Point +{ +public: + Point(int x = 0, int y = 0); + Point(double x, double y); + ~Point() {} + + inline double x() const { return m_x; } + inline double y() const { return m_y; } + + inline void setX(double x) { m_x = x; } + inline void setY(double y) { m_y = y; } + inline void setXAsUint(unsigned int x) { m_x = x; } + inline void setYAsUint(unsigned int y) { m_y = y; } + + // This method could simply return the midpoint, + // but the interesting part of the test is to set the + // result in the pointer argument. + void midpoint(const Point& other, Point* midpoint) const; + + Point* copy() const; + + inline const Point& getConstReferenceToSelf() const { return *this; } + inline const Point* getSelf() const { return this; } + + // The != operator is not implemented for the purpose of testing + // for the absense of the __ne__ method in the Python binding. + bool operator==(const Point& other); + + Point operator+(const Point& other); + Point operator-(const Point& other); + Point operator/(int operand); + + friend LIBSAMPLE_API Point operator*(const Point& pt, double mult); + friend LIBSAMPLE_API Point operator*(const Point& pt, int mult); + friend LIBSAMPLE_API Point operator*(double mult, const Point& pt); + friend LIBSAMPLE_API Point operator*(int mult, const Point& pt); + friend LIBSAMPLE_API Point operator-(const Point& pt); + friend LIBSAMPLE_API bool operator!(const Point& pt); + + Point& operator+=(Point &other); + Point& operator-=(Point &other); + + void show(); + +private: + double m_x; + double m_y; +}; + +LIBSAMPLE_API Point operator*(const Point& pt, double mult); +LIBSAMPLE_API Point operator*(const Point& pt, int mult); +LIBSAMPLE_API Point operator*(double mult, const Point& pt); +LIBSAMPLE_API Point operator*(int mult, const Point& pt); +LIBSAMPLE_API Point operator-(const Point& pt); +LIBSAMPLE_API bool operator!(const Point& pt); + +LIBSAMPLE_API Complex transmutePointIntoComplex(const Point& point); +LIBSAMPLE_API Point transmuteComplexIntoPoint(const Complex& cpx); + +LIBSAMPLE_API Point operator*(const Point& pt, double multiplier); + +#endif // POINT_H diff --git a/sources/shiboken2/tests/libsample/pointerholder.h b/sources/shiboken2/tests/libsample/pointerholder.h new file mode 100644 index 000000000..84e709d38 --- /dev/null +++ b/sources/shiboken2/tests/libsample/pointerholder.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef POINTERHOLDER_H +#define POINTERHOLDER_H + +#include "libsamplemacros.h" + +class PointerHolder +{ +public: + explicit PointerHolder(void* ptr) : m_pointer(ptr) {} + ~PointerHolder() {} + inline void* pointer() const { return m_pointer; } +private: + void* m_pointer; +}; + +#endif // POINTERHOLDER_H + diff --git a/sources/shiboken2/tests/libsample/pointf.cpp b/sources/shiboken2/tests/libsample/pointf.cpp new file mode 100644 index 000000000..0719bdb87 --- /dev/null +++ b/sources/shiboken2/tests/libsample/pointf.cpp @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "pointf.h" + +using namespace std; + +PointF::PointF(const Point& point) : m_x(point.x()), m_y(point.y()) +{ +} + +PointF::PointF(double x, double y) : m_x(x), m_y(y) +{ +} + +void +PointF::midpoint(const PointF& other, PointF* midpoint) const +{ + if (!midpoint) + return; + midpoint->setX((m_x + other.m_x) / 2.0); + midpoint->setY((m_y + other.m_y) / 2.0); +} + +void +PointF::show() +{ + cout << "(x: " << m_x << ", y: " << m_y << ")"; +} + +bool +PointF::operator==(const PointF& other) +{ + return m_x == other.m_x && m_y == other.m_y; +} + +PointF +PointF::operator+(const PointF& other) +{ + return PointF(m_x + other.m_x, m_y + other.m_y); +} + +PointF +PointF::operator-(const PointF& other) +{ + return PointF(m_x - other.m_x, m_y - other.m_y); +} + +PointF& +PointF::operator+=(PointF &other) +{ + m_x += other.m_x; + m_y += other.m_y; + return *this; +} + +PointF& +PointF::operator-=(PointF &other) +{ + m_x -= other.m_x; + m_y -= other.m_y; + return *this; +} + +PointF +operator*(const PointF& pt, double mult) +{ + return PointF(pt.m_x * mult, pt.m_y * mult); +} + +PointF +operator*(const PointF& pt, int mult) +{ + return PointF(((int) pt.m_x) * mult, ((int) pt.m_y) * mult); +} + +PointF +operator*(double mult, const PointF& pt) +{ + return PointF(pt.m_x * mult, pt.m_y * mult); +} + +PointF +operator*(int mult, const PointF& pt) +{ + return PointF(((int) pt.m_x) * mult, ((int) pt.m_y) * mult); +} + +PointF +operator-(const PointF& pt) +{ + return PointF(-pt.m_x, -pt.m_y); +} + +bool +operator!(const PointF& pt) +{ + return (pt.m_x == 0.0 && pt.m_y == 0.0); +} + diff --git a/sources/shiboken2/tests/libsample/pointf.h b/sources/shiboken2/tests/libsample/pointf.h new file mode 100644 index 000000000..c80739fea --- /dev/null +++ b/sources/shiboken2/tests/libsample/pointf.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef POINTF_H +#define POINTF_H + +#include "point.h" +#include <utility> + +#include "libsamplemacros.h" + +class LIBSAMPLE_API PointF +{ +public: + PointF(const Point& point); + PointF(double x = 0.0, double y = 0.0); + ~PointF() {} + + inline double x() const { return m_x; } + inline double y() const { return m_y; } + + inline void setX(double x) { m_x = x; } + inline void setY(double y) { m_y = y; } + + // This method could simply return the midpoint, + // but the interesting part of the test is to set the + // result in the pointer argument. + void midpoint(const PointF& other, PointF* midpoint) const; + + // The != operator is not implemented for the purpose of testing + // for the absence of the __ne__ method in the Python binding. + bool operator==(const PointF& other); + + PointF operator+(const PointF& other); + PointF operator-(const PointF& other); + + friend LIBSAMPLE_API PointF operator*(const PointF& pt, double mult); + friend LIBSAMPLE_API PointF operator*(const PointF& pt, int mult); + friend LIBSAMPLE_API PointF operator*(double mult, const PointF& pt); + friend LIBSAMPLE_API PointF operator*(int mult, const PointF& pt); + friend LIBSAMPLE_API PointF operator-(const PointF& pt); + friend LIBSAMPLE_API bool operator!(const PointF& pt); + + PointF& operator+=(PointF &other); + PointF& operator-=(PointF &other); + + void show(); + +private: + double m_x; + double m_y; +}; + +LIBSAMPLE_API PointF operator*(const PointF& pt, double mult); +LIBSAMPLE_API PointF operator*(const PointF& pt, int mult); +LIBSAMPLE_API PointF operator*(double mult, const PointF& pt); +LIBSAMPLE_API PointF operator*(int mult, const PointF& pt); +LIBSAMPLE_API PointF operator-(const PointF& pt); +LIBSAMPLE_API bool operator!(const PointF& pt); + +LIBSAMPLE_API PointF operator*(const PointF& pt, double multiplier); + +#endif // POINTF_H diff --git a/sources/shiboken2/tests/libsample/polygon.cpp b/sources/shiboken2/tests/libsample/polygon.cpp new file mode 100644 index 000000000..cb553a75c --- /dev/null +++ b/sources/shiboken2/tests/libsample/polygon.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "polygon.h" + +using namespace std; + +Polygon::Polygon(double x, double y) +{ + m_points.push_back(Point(x, y)); +} + +Polygon::Polygon(Point point) +{ + m_points.push_back(point); +} + +Polygon::Polygon(PointList points) +{ + m_points = points; +} + +void +Polygon::addPoint(Point point) +{ + m_points.push_back(point); +} + +Polygon +Polygon::doublePolygonScale(Polygon polygon) +{ + Polygon result; + for(PointList::const_iterator piter = result.points().begin(); piter != result.points().end(); piter++) + result.addPoint((*piter) * 2.0); + return result; +} + +void +Polygon::stealOwnershipFromPython(Point* point) +{ + delete point; +} + +void +Polygon::stealOwnershipFromPython(Polygon* polygon) +{ + delete polygon; +} + diff --git a/sources/shiboken2/tests/libsample/polygon.h b/sources/shiboken2/tests/libsample/polygon.h new file mode 100644 index 000000000..5e0769e0f --- /dev/null +++ b/sources/shiboken2/tests/libsample/polygon.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef POLYGON_H +#define POLYGON_H + +#include <list> +#include "point.h" + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Polygon +{ +public: + typedef std::list<Point> PointList; + + Polygon() {} + Polygon(double x, double y); + Polygon(Point point); + Polygon(PointList points); + ~Polygon() {} + + void addPoint(Point point); + + inline const PointList& points() const { return m_points; } + + // This method intentionally receives and returns copies of a Polygon object. + static Polygon doublePolygonScale(Polygon polygon); + + // This method invalidates the argument to be used for Polygon(Point) implicit conversion. + static void stealOwnershipFromPython(Point* point); + + // This method invalidates the argument to be used in a call to doublePolygonScale(Polygon). + static void stealOwnershipFromPython(Polygon* polygon); + +private: + PointList m_points; +}; + +#endif // POLYGON_H + diff --git a/sources/shiboken2/tests/libsample/privatector.h b/sources/shiboken2/tests/libsample/privatector.h new file mode 100644 index 000000000..4f4699761 --- /dev/null +++ b/sources/shiboken2/tests/libsample/privatector.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PRIVATECTOR_H +#define PRIVATECTOR_H + +#include "libsamplemacros.h" + +class PrivateCtor +{ +public: + inline static PrivateCtor* instance() + { + static PrivateCtor self; + self.m_instanciations++; + return &self; + } + + inline int instanceCalls() + { + return m_instanciations; + } + +private: + int m_instanciations; + + PrivateCtor() : m_instanciations(0) {} +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/privatedtor.h b/sources/shiboken2/tests/libsample/privatedtor.h new file mode 100644 index 000000000..a0c4d4769 --- /dev/null +++ b/sources/shiboken2/tests/libsample/privatedtor.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PRIVATEDTOR_H +#define PRIVATEDTOR_H + +#include "libsamplemacros.h" + +class PrivateDtor +{ +public: + inline static PrivateDtor* instance() + { + static PrivateDtor self; + self.m_instanciations++; + return &self; + } + + inline int instanceCalls() + { + return m_instanciations; + } + +protected: + inline int protectedInstanceCalls() { return m_instanciations; } + +private: + int m_instanciations; + + PrivateDtor() : m_instanciations(0) {} + PrivateDtor(const PrivateDtor&) {} + ~PrivateDtor() {} +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/protected.cpp b/sources/shiboken2/tests/libsample/protected.cpp new file mode 100644 index 000000000..08c9fd065 --- /dev/null +++ b/sources/shiboken2/tests/libsample/protected.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "protected.h" + +int ProtectedVirtualDestructor::dtor_called = 0; + diff --git a/sources/shiboken2/tests/libsample/protected.h b/sources/shiboken2/tests/libsample/protected.h new file mode 100644 index 000000000..c33bdf4b2 --- /dev/null +++ b/sources/shiboken2/tests/libsample/protected.h @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PROTECTED_H +#define PROTECTED_H + +#include "libsamplemacros.h" +#include "objecttype.h" +#include "point.h" +#include <string> +#include <list> + +class LIBSAMPLE_API ProtectedNonPolymorphic +{ +public: + explicit ProtectedNonPolymorphic(const char *name) : m_name(name) {} + ~ProtectedNonPolymorphic() {} + + inline const char* publicName() { return m_name.c_str(); } + + inline static ProtectedNonPolymorphic* create() { return new ProtectedNonPolymorphic("created"); } + +protected: + inline const char* protectedName() { return m_name.c_str(); } + inline int protectedSum(int a0, int a1) { return a0 + a1; } + inline int modifiedProtectedSum(int a0, int a1) { return a0 + a1; } + inline static const char* protectedStatic() { return "protectedStatic"; } + inline const char* dataTypeName(void *data = 0) const { return "pointer"; } + inline const char* dataTypeName(int data) const { return "integer"; } + +private: + std::string m_name; +}; + +class LIBSAMPLE_API ProtectedPolymorphic +{ +public: + explicit ProtectedPolymorphic(const char *name) : m_name(name) {} + virtual ~ProtectedPolymorphic() {} + + inline static ProtectedPolymorphic* create() { return new ProtectedPolymorphic("created"); } + inline const char* publicName() { return m_name.c_str(); } + inline const char* callProtectedName() { return protectedName(); } + +protected: + virtual const char* protectedName() { return m_name.c_str(); } + +private: + std::string m_name; +}; + +class LIBSAMPLE_API ProtectedPolymorphicDaughter : public ProtectedPolymorphic +{ +public: + explicit ProtectedPolymorphicDaughter(const char *name) : ProtectedPolymorphic(name) {} + inline static ProtectedPolymorphicDaughter* create() { return new ProtectedPolymorphicDaughter("created"); } +}; + +class LIBSAMPLE_API ProtectedPolymorphicGrandDaughter: public ProtectedPolymorphicDaughter +{ +public: + explicit ProtectedPolymorphicGrandDaughter(const char *name) : ProtectedPolymorphicDaughter(name) {} + inline static ProtectedPolymorphicGrandDaughter* create() { return new ProtectedPolymorphicGrandDaughter("created"); } +}; + +class LIBSAMPLE_API ProtectedVirtualDestructor +{ +public: + ProtectedVirtualDestructor() {} + inline static ProtectedVirtualDestructor* create() { return new ProtectedVirtualDestructor(); } + inline static int dtorCalled() { return dtor_called; } + inline static void resetDtorCounter() { dtor_called = 0; } +protected: + virtual ~ProtectedVirtualDestructor() { dtor_called++; } +private: + static int dtor_called; +}; + +class LIBSAMPLE_API ProtectedEnumClass +{ +public: + ProtectedEnumClass() {} + virtual ~ProtectedEnumClass() {} + enum PublicEnum { + PublicItem0, + PublicItem1 + }; +protected: + enum ProtectedEnum { + ProtectedItem0, + ProtectedItem1 + }; + ProtectedEnum callProtectedEnumMethod(ProtectedEnum in) { return protectedEnumMethod(in); } + inline PublicEnum callPublicEnumMethod(PublicEnum in) { return publicEnumMethod(in); } + + virtual ProtectedEnum protectedEnumMethod(ProtectedEnum in) { return in; } + virtual PublicEnum publicEnumMethod(PublicEnum in) { return in; } +}; + + +class LIBSAMPLE_API ProtectedProperty +{ +public: + ProtectedProperty() + : protectedValueTypeProperty(Point(0, 0)), + protectedProperty(0), + protectedEnumProperty(Event::NO_EVENT), + protectedValueTypePointerProperty(0), + protectedObjectTypeProperty(0) + {} +protected: + // This is deliberately the first member to test wrapper registration + // for value type members sharing the same memory address. + Point protectedValueTypeProperty; + int protectedProperty; + std::list<int> protectedContainerProperty; + Event::EventType protectedEnumProperty; + Point* protectedValueTypePointerProperty; + ObjectType* protectedObjectTypeProperty; +}; + +LIBSAMPLE_API inline ProtectedProperty* createProtectedProperty() { + return new ProtectedProperty; +} + +#endif // PROTECTED_H diff --git a/sources/shiboken2/tests/libsample/rect.h b/sources/shiboken2/tests/libsample/rect.h new file mode 100644 index 000000000..b7ce6e890 --- /dev/null +++ b/sources/shiboken2/tests/libsample/rect.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef RECT_H +#define RECT_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Rect +{ +public: + Rect() + { + m_left = m_top = 0; + m_right = m_bottom = -1; + } + Rect(int left, int top, int right, int bottom) + : m_left(left), m_top(top), m_right(right), m_bottom(bottom) { } + ~Rect() {} + inline int left() const { return m_left; } + inline int top() const { return m_top; } + inline int right() const { return m_right; } + inline int bottom() const { return m_bottom; } +private: + int m_left; + int m_top; + int m_right; + int m_bottom; +}; + +class LIBSAMPLE_API RectF +{ +public: + RectF() + { + m_left = m_top = 0; + m_right = m_bottom = -1; + } + RectF(int left, int top, int right, int bottom) + : m_left(left), m_top(top), m_right(right), m_bottom(bottom) { } + RectF(const Rect& other) + { + m_left = other.left(); + m_top = other.top(); + m_right = other.right(); + m_bottom = other.bottom(); + } + ~RectF() {} + inline double left() const { return m_left; } + inline double top() const { return m_top; } + inline double right() const { return m_right; } + inline double bottom() const { return m_bottom; } +private: + double m_left; + double m_top; + double m_right; + double m_bottom; +}; + +#endif // RECT_H + diff --git a/sources/shiboken2/tests/libsample/reference.cpp b/sources/shiboken2/tests/libsample/reference.cpp new file mode 100644 index 000000000..31a11a8ae --- /dev/null +++ b/sources/shiboken2/tests/libsample/reference.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "reference.h" + +using namespace std; + +void +Reference::show() const +{ + cout << "Reference.objId: " << m_objId << ", address: " << this; +} + +int +Reference::usesReferenceVirtual(Reference& r, int inc) +{ + return r.m_objId + inc; +} + +int +Reference::usesConstReferenceVirtual(const Reference& r, int inc) +{ + return r.m_objId + inc; +} + +int +Reference::callUsesReferenceVirtual(Reference& r, int inc) +{ + return usesReferenceVirtual(r, inc); +} + +int +Reference::callUsesConstReferenceVirtual(const Reference& r, int inc) +{ + return usesConstReferenceVirtual(r, inc); +} + +void +Reference::alterReferenceIdVirtual(Reference& r) +{ + r.setObjId(r.objId() * Reference::multiplier()); +} + +void +Reference::callAlterReferenceIdVirtual(Reference& r) +{ + alterReferenceIdVirtual(r); +} + +ObjTypeReference::~ObjTypeReference() +{ +} diff --git a/sources/shiboken2/tests/libsample/reference.h b/sources/shiboken2/tests/libsample/reference.h new file mode 100644 index 000000000..4ec8ee7fd --- /dev/null +++ b/sources/shiboken2/tests/libsample/reference.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REFERENCE_H +#define REFERENCE_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Reference +{ +public: + explicit Reference(int objId = -1) + : m_objId(objId) {} + virtual ~Reference() {} + + inline int objId() { return m_objId; } + inline void setObjId(int objId) { m_objId = objId; } + + inline static int usesReference(Reference& r) { return r.m_objId; } + inline static int usesConstReference(const Reference& r) { return r.m_objId; } + + virtual int usesReferenceVirtual(Reference& r, int inc); + virtual int usesConstReferenceVirtual(const Reference& r, int inc); + + int callUsesReferenceVirtual(Reference& r, int inc); + int callUsesConstReferenceVirtual(const Reference& r, int inc); + + virtual void alterReferenceIdVirtual(Reference& r); + void callAlterReferenceIdVirtual(Reference& r); + + void show() const; + + inline static int multiplier() { return 10; } + + virtual Reference& returnMyFirstArg(Reference& ref) { return ref; } + virtual Reference& returnMySecondArg(int a, Reference& ref) { return ref; } + + // nonsense operator to test if Shiboken is ignoring dereference operators. + int operator*() { return m_objId; } +private: + int m_objId; +}; + +class LIBSAMPLE_API ObjTypeReference +{ +public: + ObjTypeReference() {} + ObjTypeReference(const ObjTypeReference&) {} + virtual ~ObjTypeReference(); + virtual ObjTypeReference& returnMyFirstArg(ObjTypeReference& ref) { return ref; } + virtual ObjTypeReference& returnMySecondArg(int a, ObjTypeReference& ref) { return ref; } + virtual ObjTypeReference& justAPureVirtualFunc(ObjTypeReference& ref) = 0; +}; + +#endif // REFERENCE_H + diff --git a/sources/shiboken2/tests/libsample/removednamespaces.h b/sources/shiboken2/tests/libsample/removednamespaces.h new file mode 100644 index 000000000..4520b6a27 --- /dev/null +++ b/sources/shiboken2/tests/libsample/removednamespaces.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REMOVEDNAMESPACE_H +#define REMOVEDNAMESPACE_H + +#include "libsamplemacros.h" + +namespace RemovedNamespace1 +{ + +enum RemovedNamespace1_Enum { + RemovedNamespace1_Enum_Value0 +}; + +enum { + RemovedNamespace1_AnonymousEnum_Value0 +}; + +namespace RemovedNamespace2 { + enum RemovedNamespace2_Enum { + RemovedNamespace2_Enum_Value0 + }; +} + +} + +namespace UnremovedNamespace +{ +namespace RemovedNamespace3 +{ + enum RemovedNamespace3_Enum { + RemovedNamespace3_Enum_Value0 + }; + + enum { + RemovedNamespace3_AnonymousEnum_Value0 + }; +} +} + +#endif // REMOVEDNAMESPACE_H + diff --git a/sources/shiboken2/tests/libsample/sample.cpp b/sources/shiboken2/tests/libsample/sample.cpp new file mode 100644 index 000000000..fa1e1fda3 --- /dev/null +++ b/sources/shiboken2/tests/libsample/sample.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "sample.h" + +sample::sample::sample(int value) : m_value(value) +{ +} + +int sample::sample::value() const +{ + return m_value; +} + diff --git a/sources/shiboken2/tests/libsample/sample.h b/sources/shiboken2/tests/libsample/sample.h new file mode 100644 index 000000000..46e3d0d1a --- /dev/null +++ b/sources/shiboken2/tests/libsample/sample.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SAMPLE_H +#define SAMPLE_H + +#include "libsamplemacros.h" + +// namespace with the same name of the current package to try to mess up with the generator +namespace sample +{ + // to increase the mess we add a class with the same name of the package/namespace + class LIBSAMPLE_API sample + { + public: + sample(int value = 0); + int value() const; + private: + int m_value; + }; +} + +#endif diff --git a/sources/shiboken2/tests/libsample/samplenamespace.cpp b/sources/shiboken2/tests/libsample/samplenamespace.cpp new file mode 100644 index 000000000..1fc6dff40 --- /dev/null +++ b/sources/shiboken2/tests/libsample/samplenamespace.cpp @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include <cstdlib> +#include <time.h> +#include "samplenamespace.h" + +using namespace std; + +namespace SampleNamespace +{ + +OutValue +enumInEnumOut(InValue in) +{ + OutValue retval; + switch(in) { + case ZeroIn: + retval = ZeroOut; + break; + case OneIn: + retval = OneOut; + break; + case TwoIn: + retval = TwoOut; + break; + default: + retval = (OutValue) -1; + } + return retval; +} + +Option +enumArgumentWithDefaultValue(Option opt) +{ + return opt; +} + +int +getNumber(Option opt) +{ + int retval; + switch(opt) { + case RandomNumber: + retval = rand() % 100; + break; + case UnixTime: + retval = (int) time(0); + break; + default: + retval = 0; + } + return retval; +} + +void +doSomethingWithArray(const unsigned char* data, unsigned int size, const char* format) +{ + // This function does nothing in fact. + // It is here as a dummy copy of QPixmap.loadFromData method + // to check compilation issues, i.e. if it compiles, it's ok. +} + +int +enumItemAsDefaultValueToIntArgument(int value) +{ + return value; +} + +void +forceDecisorSideA(ObjectType* object) +{ +} + +void +forceDecisorSideA(const Point& pt, const Str& text, ObjectType* object) +{ +} + +void +forceDecisorSideB(int a, ObjectType* object) +{ +} + +void +forceDecisorSideB(int a, const Point& pt, const Str& text, ObjectType* object) +{ +} + +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/sources/shiboken2/tests/libsample/samplenamespace.h b/sources/shiboken2/tests/libsample/samplenamespace.h new file mode 100644 index 000000000..3ce410941 --- /dev/null +++ b/sources/shiboken2/tests/libsample/samplenamespace.h @@ -0,0 +1,162 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SAMPLENAMESPACE_H +#define SAMPLENAMESPACE_H + +#include <list> +#include "libsamplemacros.h" +#include "str.h" +#include "point.h" +#include "objecttype.h" + +// Anonymous global enum +enum { + AnonymousGlobalEnum_Value0, + AnonymousGlobalEnum_Value1 +}; + +// Invisible namespace +namespace Invisible +{ + +enum EnumOnNamespace { + Option1 = 1, + Option2 = 2, + Option3 = 3 +}; + +}; + +namespace SampleNamespace +{ + +enum Option { + None_, + RandomNumber, + UnixTime +}; + +enum InValue { + ZeroIn, + OneIn, + TwoIn +}; + +enum OutValue { + ZeroOut, + OneOut, + TwoOut +}; + +// Anonymous non-global enum. +// This counts as a class enum, since C++ namespaces +// are represented as classes in Python. +enum { + AnonymousClassEnum_Value0, + AnonymousClassEnum_Value1 +}; + +LIBSAMPLE_API OutValue enumInEnumOut(InValue in); + +LIBSAMPLE_API Option enumArgumentWithDefaultValue(Option opt = UnixTime); + +LIBSAMPLE_API int getNumber(Option opt); + +inline double powerOfTwo(double num) { + return num * num; +} + +LIBSAMPLE_API void doSomethingWithArray(const unsigned char* data, unsigned int size, const char* format = 0); + +LIBSAMPLE_API int enumItemAsDefaultValueToIntArgument(int value = ZeroIn); + +class SomeClass +{ +public: + class SomeInnerClass + { + public: + class OkThisIsRecursiveEnough + { + public: + virtual ~OkThisIsRecursiveEnough() {} + enum NiceEnum { + NiceValue1, NiceValue2 + }; + + inline int someMethod(SomeInnerClass*) { return 0; } + virtual OkThisIsRecursiveEnough* someVirtualMethod(OkThisIsRecursiveEnough* arg) { return arg; } + }; + protected: + enum ProtectedEnum { + ProtectedItem0, + ProtectedItem1 + }; + }; + struct SomeOtherInnerClass { + std::list<SomeInnerClass> someInnerClasses; + }; +protected: + enum ProtectedEnum { + ProtectedItem0, + ProtectedItem1 + }; +}; + +class DerivedFromNamespace : public SomeClass::SomeInnerClass::OkThisIsRecursiveEnough +{ +public: + // FIXME Uncomment this when the fix for MSVC is available + // only to cause namespace confusion +// enum SampleNamespace { +// }; + virtual OkThisIsRecursiveEnough* someVirtualMethod(OkThisIsRecursiveEnough* arg) { return arg; } + inline OkThisIsRecursiveEnough* methodReturningTypeFromParentScope() { return 0; } +}; + +// The combination of the following two overloaded methods could trigger a +// problematic behaviour on the overload decisor, if it isn't working properly. +LIBSAMPLE_API void forceDecisorSideA(ObjectType* object = 0); +LIBSAMPLE_API void forceDecisorSideA(const Point& pt, const Str& text, ObjectType* object = 0); + +// The combination of the following two overloaded methods could trigger a +// problematic behaviour on the overload decisor, if it isn't working properly. +// This is a variation of forceDecisorSideB. +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/sources/shiboken2/tests/libsample/sbkdate.cpp b/sources/shiboken2/tests/libsample/sbkdate.cpp new file mode 100644 index 000000000..e823521ec --- /dev/null +++ b/sources/shiboken2/tests/libsample/sbkdate.cpp @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "sbkdate.h" + +SbkDate::SbkDate(int d, int m, int y) : m_d(d), m_m(m), m_y(y) +{ +} + +int SbkDate::day() const +{ + return m_d; +} + +int SbkDate::month() const +{ + return m_m; +} + +int SbkDate::year() const +{ + return m_y; +} diff --git a/sources/shiboken2/tests/libsample/sbkdate.h b/sources/shiboken2/tests/libsample/sbkdate.h new file mode 100644 index 000000000..7d0893cbb --- /dev/null +++ b/sources/shiboken2/tests/libsample/sbkdate.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SBKDATE_H +#define SBKDATE_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API SbkDate +{ +public: + SbkDate(int d, int m, int y); + + int day() const; + int month() const; + int year() const; + +private: + int m_d; + int m_m; + int m_y; +}; + +#endif // SBKDATE_H + diff --git a/sources/shiboken2/tests/libsample/simplefile.cpp b/sources/shiboken2/tests/libsample/simplefile.cpp new file mode 100644 index 000000000..bdbd3d1f5 --- /dev/null +++ b/sources/shiboken2/tests/libsample/simplefile.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <stdlib.h> +#include <string.h> +#include <fstream> +#include "simplefile.h" + +class SimpleFile_p +{ +public: + SimpleFile_p(const char* filename) : m_descriptor(0), m_size(0) + { + m_filename = strdup(filename); + } + + ~SimpleFile_p() + { + free(m_filename); + } + + char* m_filename; + FILE* m_descriptor; + long m_size; +}; + +SimpleFile::SimpleFile(const char* filename) +{ + p = new SimpleFile_p(filename); +} + +SimpleFile::~SimpleFile() +{ + close(); + delete p; +} + +const char* SimpleFile::filename() +{ + return p->m_filename; +} + +long SimpleFile::size() +{ + return p->m_size; +} + +bool +SimpleFile::open() +{ + if ((p->m_descriptor = fopen(p->m_filename, "rb")) == 0) + return false; + + fseek(p->m_descriptor, 0, SEEK_END); + p->m_size = ftell(p->m_descriptor); + rewind(p->m_descriptor); + + return true; +} + +void +SimpleFile::close() +{ + if (p->m_descriptor) { + fclose(p->m_descriptor); + p->m_descriptor = 0; + } +} + +bool +SimpleFile::exists() const +{ + std::ifstream ifile(p->m_filename); + return !ifile.fail(); +} + +bool +SimpleFile::exists(const char* filename) +{ + std::ifstream ifile(filename); + return !ifile.fail(); +} + diff --git a/sources/shiboken2/tests/libsample/simplefile.h b/sources/shiboken2/tests/libsample/simplefile.h new file mode 100644 index 000000000..bae3b9998 --- /dev/null +++ b/sources/shiboken2/tests/libsample/simplefile.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SIMPLEFILE_H +#define SIMPLEFILE_H + +#include "libsamplemacros.h" +#include <stdio.h> + +class SimpleFile_p; + +class LIBSAMPLE_API SimpleFile +{ +public: + explicit SimpleFile(const char* filename); + ~SimpleFile(); + + const char* filename(); + long size(); + bool open(); + void close(); + + bool exists() const; + static bool exists(const char* filename); + +private: + SimpleFile_p *p; +}; + +#endif // SIMPLEFILE_H + diff --git a/sources/shiboken2/tests/libsample/size.cpp b/sources/shiboken2/tests/libsample/size.cpp new file mode 100644 index 000000000..1faf5dfb2 --- /dev/null +++ b/sources/shiboken2/tests/libsample/size.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "size.h" + +using namespace std; + +void +Size::show() const +{ + cout << "(width: " << m_width << ", height: " << m_height << ")"; +} + diff --git a/sources/shiboken2/tests/libsample/size.h b/sources/shiboken2/tests/libsample/size.h new file mode 100644 index 000000000..433aba770 --- /dev/null +++ b/sources/shiboken2/tests/libsample/size.h @@ -0,0 +1,206 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SIZE_H +#define SIZE_H + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Size +{ +public: + Size(double width = 0.0, double height = 0.0) : m_width(width), m_height(height) {} + ~Size() {} + + inline double width() { return m_width; } + inline void setWidth(double width) { m_width = width; } + inline double height() { return m_height; } + inline void setHeight(double height) { m_height = height; } + + inline double calculateArea() const { return m_width * m_height; } + + // Comparison Operators + inline bool operator==(const Size& other) + { + return m_width == other.m_width && m_height == other.m_height; + } + + inline bool operator<(const Size& other) + { + return calculateArea() < other.calculateArea(); + } + + inline bool operator>(const Size& other) + { + // On some x86 hardware and compiler combinations, floating point + // comparisons may fail due to a hardware bug. One workaround is to + // simplify comparison expressions by putting partial results in + // variables. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323#c109 + // for details. + double a = calculateArea(); + double b = other.calculateArea(); + return a > b; + } + + inline bool operator<=(const Size& other) + { + // See comments for operator>() + double a = calculateArea(); + double b = other.calculateArea(); + return a <= b; + } + + inline bool operator>=(const Size& other) + { + return calculateArea() >= other.calculateArea(); + } + + inline bool operator<(double area) { return calculateArea() < area; } + inline bool operator>(double area) { return calculateArea() > area; } + inline bool operator<=(double area) { return calculateArea() <= area; } + inline bool operator>=(double area) { return calculateArea() >= area; } + + // Arithmetic Operators + inline Size& operator+=(const Size& s) + { + m_width += s.m_width; + m_height += s.m_height; + return *this; + } + + inline Size& operator-=(const Size& s) + { + m_width -= s.m_width; + m_height -= s.m_height; + return *this; + } + + inline Size& operator*=(double mult) + { + m_width *= mult; + m_height *= mult; + return *this; + } + + inline Size& operator/=(double div) + { + m_width /= div; + m_height /= div; + return *this; + } + + // TODO: add ++size, size++, --size, size-- + + // External operators + friend inline bool operator!=(const Size&, const Size&); + friend inline const Size operator+(const Size&, const Size&); + friend inline const Size operator-(const Size&, const Size&); + friend inline const Size operator*(const Size&, double); + friend inline const Size operator*(double, const Size&); + friend inline const Size operator/(const Size&, double); + + friend inline bool operator<(double, const Size&); + friend inline bool operator>(double, const Size&); + friend inline bool operator<=(double, const Size&); + friend inline bool operator>=(double, const Size&); + + void show() const; + +private: + double m_width; + double m_height; +}; + +// Comparison Operators +inline bool operator!=(const Size& s1, const Size& s2) +{ + return s1.m_width != s2.m_width || s1.m_height != s2.m_height; +} + +inline bool operator<(double area, const Size& s) +{ + return area < s.calculateArea(); +} + +inline bool operator>(double area, const Size& s) +{ + return area > s.calculateArea(); +} + +inline bool operator<=(double area, const Size& s) +{ + return area <= s.calculateArea(); +} + +inline bool operator>=(double area, const Size& s) +{ + return area >= s.calculateArea(); +} + +// Arithmetic Operators +inline const Size operator+(const Size& s1, const Size& s2) +{ + return Size(s1.m_width + s2.m_width, s1.m_height + s2.m_height); +} + +inline const Size operator-(const Size& s1, const Size& s2) +{ + return Size(s1.m_width - s2.m_width, s1.m_height - s2.m_height); +} + +inline const Size operator*(const Size& s, double mult) +{ + return Size(s.m_width * mult, s.m_height * mult); +} + +inline const Size operator*(double mult, const Size& s) +{ + return Size(s.m_width * mult, s.m_height * mult); +} + +inline const Size operator/(const Size& s, double div) +{ + return Size(s.m_width / div, s.m_height / div); +} + +typedef double real; +typedef unsigned short ushort; +class LIBSAMPLE_API SizeF +{ +public: + SizeF(real width, real height) : m_width(width), m_height(height) {} + real width() { return m_width; } + real height() { return m_height; } + static inline ushort passTypedefOfUnsignedShort(ushort value) { return value; } +private: + real m_width; + real m_height; +}; + +#endif // SIZE_H + diff --git a/sources/shiboken2/tests/libsample/sometime.cpp b/sources/shiboken2/tests/libsample/sometime.cpp new file mode 100644 index 000000000..3038e26ca --- /dev/null +++ b/sources/shiboken2/tests/libsample/sometime.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "sometime.h" +#include <stdio.h> + +void +Time::setTime() +{ + m_hour = 0; + m_minute = 0; + m_second = 0; + m_msec = 0; + m_is_null = true; +} + +void +Time::setTime(int h, int m, int s, int ms) +{ + m_hour = h; + m_minute = m; + m_second = s; + m_msec = ms; + m_is_null = false; +} + + +Time::NumArgs +Time::somethingCompletelyDifferent() +{ + return ZeroArgs; +} + +Time::NumArgs +Time::somethingCompletelyDifferent(int h, int m, ImplicitConv ic, ObjectType* type) +{ + if (type) + return FourArgs; + if (ic.ctorEnum() == ImplicitConv::CtorThree && ic.objId() == -1) + return TwoArgs; + return ThreeArgs; +} + +Str +Time::toString() const +{ + if (m_is_null) + return Str(); + char buffer[13]; + sprintf(buffer, "%02d:%02d:%02d.%03d", m_hour, m_minute, m_second, m_msec); + return Str(buffer); +} + +bool +Time::operator==(const Time& other) const +{ + return m_hour == other.m_hour + && m_minute == other.m_minute + && m_second == other.m_second + && m_msec == other.m_msec + && m_is_null == other.m_is_null; +} + +bool +Time::operator!=(const Time& other) const +{ + return !operator==(other); +} + +Time::operator Str() const +{ + return Time::toString(); +} + diff --git a/sources/shiboken2/tests/libsample/sometime.h b/sources/shiboken2/tests/libsample/sometime.h new file mode 100644 index 000000000..5f2ccf88a --- /dev/null +++ b/sources/shiboken2/tests/libsample/sometime.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SOMETIME_H +#define SOMETIME_H + +#include "libsamplemacros.h" +#include "str.h" +#include "implicitconv.h" +#include "objecttype.h" + +class LIBSAMPLE_API Time +{ +public: + enum NumArgs { + ZeroArgs, + TwoArgs, + ThreeArgs, + FourArgs + }; + + Time() + : m_hour(0), m_minute(0), m_second(0), m_msec(0), m_is_null(true) + {} + Time(int h, int m, int s = 0, int ms = 0) + : m_hour(h), m_minute(m), m_second(s), m_msec(ms), m_is_null(false) + {} + + ~Time() {} + + inline bool isNull() const { return m_is_null; } + + inline int hour() const { return m_hour; } + inline int minute() const { return m_minute; } + inline int second() const { return m_second; } + inline int msec() const { return m_msec; } + + void setTime(); + void setTime(int h, int m, int s = 0, int ms = 0); + + // This one is completely different from the other methods in this class, + // it was added to give the overload decisor a really hard time with + // an value-type with implicit conversion and a default argument, and also + // an object-type, just because I feel like it. + NumArgs somethingCompletelyDifferent(); + NumArgs somethingCompletelyDifferent(int h, int m, + ImplicitConv ic = ImplicitConv::CtorThree, + ObjectType* type = 0); + + Str toString() const; + bool operator==(const Time& other) const; + bool operator!=(const Time& other) const; + + // This cast operator must become an implicit conversion of Str. + operator Str() const; + +private: + int m_hour; + int m_minute; + int m_second; + int m_msec; + + bool m_is_null; +}; + +#endif // SOMETIME_H + diff --git a/sources/shiboken2/tests/libsample/str.cpp b/sources/shiboken2/tests/libsample/str.cpp new file mode 100644 index 000000000..f3be2591e --- /dev/null +++ b/sources/shiboken2/tests/libsample/str.cpp @@ -0,0 +1,185 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "str.h" +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <sstream> + +using namespace std; + +Str::Str(const Str& s) +{ + init(s.cstring()); +} + +Str::Str(char c) +{ + char str[2] = { c, 0 }; + init(str); +} + +Str::Str(const char* cstr) +{ + init(cstr); +} + +void +Str::init(const char* cstr) +{ + if (cstr) + m_str = cstr; +} + +Str::~Str() +{ +} + +Str +Str::arg(const Str& s) const +{ + size_t idx = m_str.find_first_of("%VAR"); + if (idx == std::string::npos) { + return *this; + } else { + std::string result = m_str; + result.replace(idx, 4, s.m_str); + return result.c_str(); + } +} + +Str& +Str::append(const Str& s) +{ + m_str += s.m_str; + return *this; +} + +Str& +Str::prepend(const Str& s) +{ + m_str = s.m_str + m_str; + return *this; +} + +const char* +Str::cstring() const +{ + return m_str.c_str(); +} + +int +Str::toInt(bool* ok, int base) const +{ + bool my_ok; + int result = 0; + istringstream conv(m_str); + switch (base) { + case 8: + conv >> std::oct >> result; + break; + case 10: + conv >> std::dec >> result; + break; + case 16: + conv >> std::hex >> result; + break; + } + my_ok = istringstream::eofbit & conv.rdstate(); + if (!my_ok) + result = 0; + if (ok) + *ok = my_ok; + return result; +} + +void +Str::show() const +{ + printf("%s", cstring()); +} + +char +Str::get_char(int pos) const +{ + return m_str[pos]; +} + +bool +Str::set_char(int pos, char ch) +{ + m_str[pos] = ch; + return true; +} + +Str Str::operator+(int number) const +{ + ostringstream in; + in << m_str << number; + return in.str().c_str(); +} + +bool Str::operator==(const Str& other) const +{ + return m_str == other.m_str; +} + +Str operator+(int number, const Str& str) +{ + ostringstream in; + in << number << str.m_str; + return in.str().c_str(); +} + +bool Str::operator<(const Str& other) const +{ + return m_str < other.m_str; +} + +unsigned int strHash(const Str& str) +{ + unsigned int result = 0; + const std::string& cppStr = str.m_str; + std::string::const_iterator it = cppStr.begin(); + for (; it != cppStr.end(); ++it) + result = 5 * result + *it; + return result; +} + +void changePStr(PStr* pstr, const char* suffix) +{ + pstr->append(suffix); +} + +void duplicatePStr(PStr* pstr) +{ + if (!pstr) + return; + pstr->append(*pstr); +} diff --git a/sources/shiboken2/tests/libsample/str.h b/sources/shiboken2/tests/libsample/str.h new file mode 100644 index 000000000..9c1e8c0a8 --- /dev/null +++ b/sources/shiboken2/tests/libsample/str.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef STR_H +#define STR_H +#include <string> + +#include "libsamplemacros.h" + +class LIBSAMPLE_API Str +{ +public: + Str(const Str& s); + Str(char c); + Str(const char* cstr = ""); + ~Str(); + + Str arg(const Str& s) const; + + Str& append(const Str& s); + Str& prepend(const Str& s); + + const char* cstring() const; + char get_char(int pos) const; + bool set_char(int pos, char ch); + + int toInt(bool* ok = 0, int base = 10) const; + + void show() const; + + inline int size() const { return m_str.size(); } + + // nonsense operator just to test reverse operators + Str operator+(int number) const; + bool operator==(const Str& other) const; + bool operator<(const Str& other) const; + +private: + void init(const char* cstr); + std::string m_str; + + friend LIBSAMPLE_API Str operator+(int number, const Str& str); + friend LIBSAMPLE_API unsigned int strHash(const Str& str); +}; + +LIBSAMPLE_API Str operator+(int number, const Str& str); +LIBSAMPLE_API unsigned int strHash(const Str& str); + +typedef Str PStr; +LIBSAMPLE_API void changePStr(PStr* pstr, const char* suffix); +LIBSAMPLE_API void duplicatePStr(PStr* pstr = 0); + +#endif // STR_H diff --git a/sources/shiboken2/tests/libsample/strlist.cpp b/sources/shiboken2/tests/libsample/strlist.cpp new file mode 100644 index 000000000..8956110d8 --- /dev/null +++ b/sources/shiboken2/tests/libsample/strlist.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "strlist.h" + +bool +StrList::operator==(const std::list<Str>& other) const +{ + if (size() != other.size()) + return false; + StrList::const_iterator this_it = begin(); + StrList::const_iterator other_it = begin(); + while (this_it != end()) { + if (!((*this_it) == (*other_it))) + return false; + ++this_it; + ++other_it; + } + return true; +} + +Str +StrList::join(const Str& sep) const +{ + Str result; + for (StrList::const_iterator it = begin(); it != end(); ++it) { + if (it != begin()) + result.append(sep); + result.append(*it); + } + return result; +} diff --git a/sources/shiboken2/tests/libsample/strlist.h b/sources/shiboken2/tests/libsample/strlist.h new file mode 100644 index 000000000..be908d5d5 --- /dev/null +++ b/sources/shiboken2/tests/libsample/strlist.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef STRLIST_H +#define STRLIST_H + +#include <list> +#include "str.h" + +#include "libsamplemacros.h" + +class LIBSAMPLE_API StrList : public std::list<Str> +{ +public: + enum CtorEnum { + NoParamsCtor, + StrCtor, + CopyCtor, + ListOfStrCtor + }; + + inline StrList() : m_ctorUsed(NoParamsCtor) {} + inline explicit StrList(const Str& str) : m_ctorUsed(StrCtor) { push_back(str); } + inline StrList(const StrList& lst) : std::list<Str>(lst), m_ctorUsed(CopyCtor) {} + inline StrList(const std::list<Str>& lst) : std::list<Str>(lst), m_ctorUsed(ListOfStrCtor) {} + + inline void append(Str str) { push_back(str); } + Str join(const Str& sep) const; + + bool operator==(const std::list<Str>& other) const; + inline bool operator!=(const std::list<Str>& other) const { return !(*this == other); } + + CtorEnum constructorUsed() { return m_ctorUsed; } +private: + CtorEnum m_ctorUsed; +}; + +typedef StrList PStrList; + +#endif // STRLIST_H diff --git a/sources/shiboken2/tests/libsample/templateptr.cpp b/sources/shiboken2/tests/libsample/templateptr.cpp new file mode 100644 index 000000000..571184776 --- /dev/null +++ b/sources/shiboken2/tests/libsample/templateptr.cpp @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "templateptr.h" + +void TemplatePtr::dummy(std::list<std::pair<BlackBox *, BlackBox *> > & items) +{ +}
\ No newline at end of file diff --git a/sources/shiboken2/tests/libsample/templateptr.h b/sources/shiboken2/tests/libsample/templateptr.h new file mode 100644 index 000000000..67e154b1c --- /dev/null +++ b/sources/shiboken2/tests/libsample/templateptr.h @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEMPLATEPTR_H +#define TEMPLATEPTR_H + +#include <utility> +#include <list> +#include "libsamplemacros.h" +#include "blackbox.h" + +class LIBSAMPLE_API TemplatePtr +{ +public: + void dummy(std::list<std::pair<BlackBox *, BlackBox *> > & items); +}; + +#endif diff --git a/sources/shiboken2/tests/libsample/transform.cpp b/sources/shiboken2/tests/libsample/transform.cpp new file mode 100644 index 000000000..951e2ca5b --- /dev/null +++ b/sources/shiboken2/tests/libsample/transform.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2013 Kitware, Inc. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "transform.h" + +#ifdef _WIN32 +#include <math.h> +#include <float.h> +static inline bool isfinite(double a) { return _finite(a); } +#else +#include <cmath> +#endif + +using namespace std; + +Point applyHomogeneousTransform( + const Point& in, + double m11, double m12, double m13, + double m21, double m22, double m23, + double m31, double m32, double m33, + bool* okay) +{ + double x = m11 * in.x() + m12 * in.y() + m13; + double y = m21 * in.x() + m22 * in.y() + m23; + double w = m31 * in.x() + m32 * in.y() + m33; + + if (isfinite(w) && fabs(w) > 1e-10) + { + if (okay) + *okay = true; + return Point(x / w, y / w); + } + else + { + if (okay) + *okay = false; + return Point(); + } +} diff --git a/sources/shiboken2/tests/libsample/transform.h b/sources/shiboken2/tests/libsample/transform.h new file mode 100644 index 000000000..fda5b33c5 --- /dev/null +++ b/sources/shiboken2/tests/libsample/transform.h @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2013 Kitware, Inc. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TRANSFORM_H +#define TRANSFORM_H + +#include "point.h" + +#include "libsamplemacros.h" + +LIBSAMPLE_API Point +applyHomogeneousTransform( + const Point& in, + double m11, double m12, double m13, + double m21, double m22, double m23, + double m31, double m32, double m33, + bool* okay); + +#endif // TRANSFORM_H diff --git a/sources/shiboken2/tests/libsample/valueandvirtual.h b/sources/shiboken2/tests/libsample/valueandvirtual.h new file mode 100644 index 000000000..54e8114b5 --- /dev/null +++ b/sources/shiboken2/tests/libsample/valueandvirtual.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VALUEANDVIRTUAL_H +#define VALUEANDVIRTUAL_H + +class ValueAndVirtual +{ +public: + ValueAndVirtual(int id) : m_id(id) {} + ValueAndVirtual(const ValueAndVirtual &other) { m_id = other.m_id; } + + bool operator()(int id, int id2) { return id == id2; } + + inline int id() { return m_id; } + virtual ~ValueAndVirtual() {}; +private: + int m_id; +}; + +#endif // VALUEANDVIRTUAL_H + diff --git a/sources/shiboken2/tests/libsample/virtualmethods.cpp b/sources/shiboken2/tests/libsample/virtualmethods.cpp new file mode 100644 index 000000000..05b854e08 --- /dev/null +++ b/sources/shiboken2/tests/libsample/virtualmethods.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "virtualmethods.h" + +int VirtualDtor::dtor_called = 0; + +double +VirtualMethods::virtualMethod0(Point pt, int val, Complex cpx, bool b) +{ + return (pt.x() * pt.y() * val) + cpx.imag() + ((int) b); +} + +bool +VirtualMethods::createStr(const char* text, Str*& ret) +{ + if (!text) { + ret = 0; + return false; + } + + ret = new Str(text); + return true; +} + +void +VirtualMethods::getMargins(int* left, int* top, int* right, int* bottom) const +{ + *left = m_left; + *top = m_top; + *right = m_right; + *bottom = m_bottom; +} + diff --git a/sources/shiboken2/tests/libsample/virtualmethods.h b/sources/shiboken2/tests/libsample/virtualmethods.h new file mode 100644 index 000000000..5754d0d5d --- /dev/null +++ b/sources/shiboken2/tests/libsample/virtualmethods.h @@ -0,0 +1,139 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VIRTUALMETHODS_H +#define VIRTUALMETHODS_H + +#include "point.h" +#include "complex.h" +#include "str.h" + +#include "libsamplemacros.h" +#include "strlist.h" + +class LIBSAMPLE_API VirtualMethods +{ +public: + VirtualMethods(Str name = "VirtualMethods") : m_name(name) + { + m_left = m_top = m_right = m_bottom = 0; + } + virtual ~VirtualMethods() {} + + virtual double virtualMethod0(Point pt, int val, Complex cpx, bool b); + double callVirtualMethod0(Point pt, int val, Complex cpx, bool b) + { + return virtualMethod0(pt, val, cpx, b); + } + + // Binding modification: rename. + virtual int sum0(int a0, int a1, int a2) { return a0 + a1 + a2; } + int callSum0(int a0, int a1, int a2) { return sum0(a0, a1, a2); } + + // Binding modification: set default value for the last argument. + virtual int sum1(int a0, int a1, int a2) { return a0 + a1 + a2; } + int callSum1(int a0, int a1, int a2) { return sum1(a0, a1, a2); } + + // Binding modification: remove the last argument and set a default value for it. + virtual int sum2(int a0, int a1, int a2) { return a0 + a1 + a2; } + int callSum2(int a0, int a1, int a2) { return sum2(a0, a1, a2); } + + // Binding modification: remove the second argument. + virtual int sum3(int a0, int a1, int a2) { return a0 + a1 + a2; } + int callSum3(int a0, int a1, int a2) { return sum3(a0, a1, a2); } + + // Binding modification: remove the second argument and set its default + // value, then inject code on the binding reimplementation of the virtual + // (with a native inject-code) to sum the value of the removed + // argument to the first argument before the method is called. + virtual int sum4(int a0, int a1, int a2) { return a0 + a1 + a2; } + int callSum4(int a0, int a1, int a2) { return sum4(a0, a1, a2); } + + // Binding modification: prepend a string to the results of a Python override. + virtual Str name() { return m_name; } + Str callName() { return name(); } + + // Binding modification: code injection that calls the Python override by itself. + virtual void callMe() {} + void callCallMe() { callMe(); } + + // Passing reference to pointers. + virtual bool createStr(const char* text, Str*& ret); + bool callCreateStr(const char* text, Str*& ret) { return createStr(text, ret); } + + // Return a non-binded method + std::list<Str> callStrListToStdList(const StrList& strList) { return strListToStdList(strList); } + virtual std::list<Str> strListToStdList(const StrList& strList ) { return strList; } + + void setMargins(int left, int top, int right, int bottom) + { + m_left = left; + m_top = top; + m_right = right; + m_bottom = bottom; + } + virtual void getMargins(int* left, int* top, int* right, int* bottom) const; + void callGetMargins(int* left, int* top, int* right, int* bottom) const + { + getMargins(left, top, right, bottom); + } + + virtual int recursionOnModifiedVirtual(Str arg) const { return 0; } + int callRecursionOnModifiedVirtual(Str arg) const { return recursionOnModifiedVirtual(arg); } + +private: + Str m_name; + int m_left; + int m_top; + int m_right; + int m_bottom; +}; + +class LIBSAMPLE_API VirtualDaughter : public VirtualMethods +{ +public: + VirtualDaughter() : VirtualMethods() {} + VirtualDaughter(Str name) : VirtualMethods(name) {} +}; + +class LIBSAMPLE_API VirtualDtor +{ +public: + VirtualDtor() {} + virtual ~VirtualDtor() { dtor_called++; } + + static VirtualDtor* create() { return new VirtualDtor(); } + static int dtorCalled() { return dtor_called; } + static void resetDtorCounter() { dtor_called = 0; } + +private: + static int dtor_called; +}; + +#endif // VIRTUALMETHODS_H + diff --git a/sources/shiboken2/tests/libsample/voidholder.h b/sources/shiboken2/tests/libsample/voidholder.h new file mode 100644 index 000000000..228bbae3f --- /dev/null +++ b/sources/shiboken2/tests/libsample/voidholder.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VOIDHOLDER_H +#define VOIDHOLDER_H + +#include "libsamplemacros.h" + +class VoidHolder +{ +public: + explicit VoidHolder(void* ptr = 0) : m_ptr(ptr) {} + ~VoidHolder() {} + inline void* voidPointer() { return m_ptr; } + inline static void* gimmeMeSomeVoidPointer() + { + static void* pointerToSomething = new VoidHolder(); + return pointerToSomething; + } +private: + void* m_ptr; +}; + +#endif // VOIDHOLDER_H + diff --git a/sources/shiboken2/tests/libsmart/CMakeLists.txt b/sources/shiboken2/tests/libsmart/CMakeLists.txt new file mode 100644 index 000000000..66c27cdae --- /dev/null +++ b/sources/shiboken2/tests/libsmart/CMakeLists.txt @@ -0,0 +1,11 @@ +project(libsmart) + +set(libsmart_SRC +smart.cpp +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_definitions("-DLIBSMART_BUILD") +add_library(libsmart SHARED ${libsmart_SRC}) +set_property(TARGET libsmart PROPERTY PREFIX "") + diff --git a/sources/shiboken2/tests/libsmart/libsmartmacros.h b/sources/shiboken2/tests/libsmart/libsmartmacros.h new file mode 100644 index 000000000..e4166caff --- /dev/null +++ b/sources/shiboken2/tests/libsmart/libsmartmacros.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LIB_SMART_MACROS_H +#define LIB_SMART_MACROS_H + +#if defined _WIN32 || defined __CYGWIN__ + #if LIBSMART_BUILD + #define LIB_SMART_API __declspec(dllexport) + #else + #define LIB_SMART_API __declspec(dllimport) + #endif +#else +#if __GNUC__ >= 4 + #define LIB_SMART_API __attribute__ ((visibility("default"))) +#else + #define LIB_SMART_API +#endif +#endif + +#endif diff --git a/sources/shiboken2/tests/libsmart/smart.cpp b/sources/shiboken2/tests/libsmart/smart.cpp new file mode 100644 index 000000000..28c9cf055 --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart.cpp @@ -0,0 +1,174 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "smart.h" + +bool shouldPrint() { + return Registry::getInstance()->shouldPrint(); +} + +Obj::Obj() : m_integer(123), m_internalInteger(new Integer) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Object constructor " << this << '\n'; +} + +Obj::~Obj() +{ + Registry::getInstance()->remove(this); + delete m_internalInteger; + if (shouldPrint()) + std::cout << "Object destructor " << this << '\n'; +} + + +void Obj::printObj() { + if (shouldPrint()) { + std::cout << "integer value: " << m_integer + << " internal integer value: " << m_internalInteger->m_int << '\n'; + } +} + + +SharedPtr<Obj> Obj::giveSharedPtrToObj() +{ + SharedPtr<Obj> o(new Obj); + return o; +} + +SharedPtr<Integer> Obj::giveSharedPtrToInteger() +{ + SharedPtr<Integer> o(new Integer); + return o; +} + +int Obj::takeSharedPtrToObj(SharedPtr<Obj> pObj) +{ + pObj->printObj(); + return pObj->m_integer; +} + +int Obj::takeSharedPtrToInteger(SharedPtr<Integer> pInt) +{ + pInt->printInteger(); + return pInt->m_int; +} + +Integer Obj::takeInteger(Integer val) +{ + return val; +} + +Integer::Integer() : m_int(456) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Integer constructor " << this << '\n'; +} + +Integer::Integer(const Integer &other) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Integer copy constructor " << this << '\n'; + m_int = other.m_int; +} + +Integer &Integer::operator=(const Integer &other) +{ + Registry::getInstance()->add(this); + if (shouldPrint()) + std::cout << "Integer operator= " << this << '\n'; + m_int = other.m_int; + return *this; +} + +Integer::~Integer() +{ + Registry::getInstance()->remove(this); + if (shouldPrint()) + std::cout << "Integer destructor " << this << '\n'; +} + +void Integer::printInteger() +{ + if (shouldPrint()) + std::cout << "Integer value for object " << this << " is " << m_int << '\n'; +} + +Registry *Registry::getInstance() +{ + static Registry registry; + return ®istry; +} + +Registry::Registry() : m_printStuff(false) +{ + +} + +void Registry::add(Obj *p) +{ + m_objects.push_back(p); +} + +void Registry::add(Integer *p) +{ + m_integers.push_back(p); +} + +void Registry::remove(Obj *p) +{ + m_objects.erase(std::remove(m_objects.begin(), m_objects.end(), p), m_objects.end()); +} + +void Registry::remove(Integer *p) +{ + m_integers.erase(std::remove(m_integers.begin(), m_integers.end(), p), m_integers.end()); +} + +int Registry::countObjects() const +{ + return static_cast<int>(m_objects.size()); +} + +int Registry::countIntegers() const +{ + return static_cast<int>(m_integers.size()); +} + +bool Registry::shouldPrint() const +{ + return m_printStuff; +} + +void Registry::setShouldPrint(bool flag) +{ + m_printStuff = flag; +} diff --git a/sources/shiboken2/tests/libsmart/smart.h b/sources/shiboken2/tests/libsmart/smart.h new file mode 100644 index 000000000..c6b0dbbca --- /dev/null +++ b/sources/shiboken2/tests/libsmart/smart.h @@ -0,0 +1,210 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SMART_H +#define SMART_H + +#include <algorithm> +#include <iostream> +#include <vector> + +#include "libsmartmacros.h" + +// Forward declarations. +template <class T> +class SharedPtr; +class Integer; +class Obj; + +LIB_SMART_API bool shouldPrint(); + +// Used to track which C++ objects are alive. +class LIB_SMART_API Registry { +public: + static Registry *getInstance(); + + void add(Obj *p); + void add(Integer *p); + void remove(Obj *p); + void remove(Integer *p); + int countObjects() const; + int countIntegers() const; + bool shouldPrint() const; + void setShouldPrint(bool flag); + +protected: + Registry(); + +private: + bool m_printStuff; + std::vector<Obj *> m_objects; + std::vector<Integer *> m_integers; +}; + +template <class T> +class RefData { +public: + RefData(T *ptr) : m_refCount(1), m_heldPtr(ptr) {} + ~RefData() { delete m_heldPtr; } + int inc() { return ++m_refCount; } + int dec() { return --m_refCount; } + int useCount() { return m_refCount; } + int m_refCount; + T *m_heldPtr; +}; + +template <class T> +class SharedPtr { +public: + SharedPtr() : m_refData(0) { + if (shouldPrint()) + std::cout << "shared_ptr default constructor " << this << "\n"; + } + + SharedPtr(T *v) + { + if (shouldPrint()) + std::cout << "shared_ptr constructor " << this << " with pointer " << v << "\n"; + if (v) + m_refData = new RefData<T>(v); + } + + SharedPtr(const SharedPtr<T> &other) : m_refData(other.m_refData) + { + if (shouldPrint()) + std::cout << "shared_ptr copy constructor " << this << " with pointer " + << other.m_refData << "\n"; + if (m_refData) + m_refData->inc(); + } + + SharedPtr<T> &operator=(const SharedPtr<T>& other) + { + if (this != &other) { + if (shouldPrint()) + std::cout << "shared_ptr assignment operator " << this << " with pointer " + << other.m_refData << "\n"; + if (m_refData && m_refData->dec() == 0) + delete m_refData; + m_refData = other.m_refData; + if (m_refData) + m_refData->inc(); + } + return *this; + } + + T *data() const + { + if (m_refData) + return m_refData->m_heldPtr; + return 0; + } + + int useCount() const + { + if (m_refData) + return m_refData->useCount(); + return 0; + } + + void dummyMethod1() + { + + } + + T& operator*() const + { + // Crashes if smart pointer is empty (just like std::shared_ptr). + return *(m_refData->m_heldPtr); + } + + T *operator->() const + { + if (m_refData) + return m_refData->m_heldPtr; + return 0; + } + + bool operator!() const + { + return !m_refData || !m_refData->m_heldPtr; + } + + bool isNull() const + { + return !m_refData || !m_refData->m_heldPtr; + } + + operator bool() const + { + return m_refData && m_refData->m_heldPtr; + } + + ~SharedPtr() + { + if (m_refData) { + if (shouldPrint()) + std::cout << "shared_ptr destructor " << this << " remaining refcount " + << m_refData->useCount() - 1 << "\n"; + } + if (m_refData && m_refData->dec() == 0) + delete m_refData; + } + + RefData<T> *m_refData; +}; + +class LIB_SMART_API Integer { +public: + Integer(); + Integer(const Integer &other); + Integer &operator=(const Integer &other); + ~Integer(); + void printInteger(); + int m_int; +}; + +// Couldn't name it Object because it caused some namespace clashes. +class LIB_SMART_API Obj { +public: + Obj(); + virtual ~Obj(); + + void printObj(); + Integer takeInteger(Integer val); + SharedPtr<Obj> giveSharedPtrToObj(); + SharedPtr<Integer> giveSharedPtrToInteger(); + int takeSharedPtrToObj(SharedPtr<Obj> pObj); + int takeSharedPtrToInteger(SharedPtr<Integer> pInt); + + int m_integer; + Integer *m_internalInteger; +}; + +#endif // SMART_H + diff --git a/sources/shiboken2/tests/minimalbinding/CMakeLists.txt b/sources/shiboken2/tests/minimalbinding/CMakeLists.txt new file mode 100644 index 000000000..fb0b2cc74 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/CMakeLists.txt @@ -0,0 +1,38 @@ +project(minimal) + +set(minimal_TYPESYSTEM +${CMAKE_CURRENT_SOURCE_DIR}/typesystem_minimal.xml +) + +set(minimal_SRC +${CMAKE_CURRENT_BINARY_DIR}/minimal/minimal_module_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/minimal/obj_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/minimal/val_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/minimal/listuser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/minimal/minbooluser_wrapper.cpp +) + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/minimal-binding.txt.in" + "${CMAKE_CURRENT_BINARY_DIR}/minimal-binding.txt" @ONLY) + +add_custom_command(OUTPUT ${minimal_SRC} +COMMAND shiboken2 --project-file=${CMAKE_CURRENT_BINARY_DIR}/minimal-binding.txt ${GENERATOR_EXTRA_FLAGS} +DEPENDS ${minimal_TYPESYSTEM} ${CMAKE_CURRENT_SOURCE_DIR}/global.h shiboken2 +WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +COMMENT "Running generator for 'minimal' test binding..." +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR} + ${SBK_PYTHON_INCLUDE_DIR} + ${libminimal_SOURCE_DIR} + ${libshiboken_SOURCE_DIR}) +add_library(minimal MODULE ${minimal_SRC}) +set_property(TARGET minimal PROPERTY PREFIX "") +if(WIN32) + set_property(TARGET minimal PROPERTY SUFFIX ".pyd") +endif() +target_link_libraries(minimal + libminimal + ${SBK_PYTHON_LIBRARIES} + libshiboken) diff --git a/sources/shiboken2/tests/minimalbinding/global.h b/sources/shiboken2/tests/minimalbinding/global.h new file mode 100644 index 000000000..93eb38ed5 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/global.h @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "obj.h" +#include "val.h" +#include "minbool.h" +#include "listuser.h" +#include "typedef.h" diff --git a/sources/shiboken2/tests/minimalbinding/listuser_test.py b/sources/shiboken2/tests/minimalbinding/listuser_test.py new file mode 100644 index 000000000..d3b7f01e7 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/listuser_test.py @@ -0,0 +1,323 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from minimal import ListUser, Val, Obj +from py3kcompat import IS_PY3K + +if IS_PY3K: + import functools + reduce = functools.reduce + + +class ExtListUser(ListUser): + def __init__(self): + ListUser.__init__(self) + + def createIntList(self, num): + return list(range(0, num * 2, 2)) + + def sumIntList(self, intList): + return sum(intList) * 2 + + def createMinBoolList(self, mb1, mb2): + return [not mb1, not mb2] + + def oredMinBoolList(self, minBoolList): + return not reduce(lambda a, b: a|b, minBoolList) + + def createValList(self, num): + return [Val(i) for i in range(0, num * 2, 2)] + + def sumValList(self, valList): + return sum([val.valId() for val in valList]) * 2 + + def createObjList(self, o1, o2): + o1.setObjId(o1.objId() * 2) + o2.setObjId(o2.objId() * 2) + return [o1, o2] + + def sumObjList(self, objList): + return sum([obj.objId() for obj in objList]) * 2 + + def createListOfIntLists(self, num): + return [self.createIntList(num)] * 4 + + def sumListOfIntLists(self, intListList): + return sum([sum(line) for line in intListList]) * 2 + + +class IntListConversionTest(unittest.TestCase): + + def testCreateIntList(self): + num = 4 + lu = ListUser() + lst = lu.createIntList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), int) + self.assertEqual(lst, list(range(num))) + lst = lu.callCreateIntList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), int) + self.assertEqual(lst, list(range(num))) + + def testCreateIntListFromExtendedClass(self): + lu = ExtListUser() + num = 4 + lst = lu.createIntList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), int) + self.assertEqual(lst, list(range(0, num * 2, 2))) + lst = lu.callCreateIntList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), int) + self.assertEqual(lst, list(range(0, num * 2, 2))) + + def testSumIntList(self): + lu = ListUser() + lst = range(4) + self.assertEqual(lu.sumIntList(lst), sum(lst)) + self.assertEqual(lu.callSumIntList(lst), sum(lst)) + + def testSumIntListFromExtendedClass(self): + lu = ExtListUser() + lst = range(4) + self.assertEqual(lu.sumIntList(lst), sum(lst) * 2) + self.assertEqual(lu.callSumIntList(lst), sum(lst) * 2) + + +class MinBoolListConversionTest(unittest.TestCase): + + def testCreateMinBoolList(self): + lu = ListUser() + lst = lu.createMinBoolList(True, False) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), bool) + self.assertEqual(lst, [True, False]) + + lst = lu.callCreateMinBoolList(False, True) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), bool) + self.assertEqual(lst, [False, True]) + + def testCreateMinBoolListFromExtendedClass(self): + lu = ExtListUser() + lst = lu.createMinBoolList(True, False) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), bool) + self.assertEqual(lst, [False, True]) + + lst = lu.callCreateMinBoolList(False, True) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), bool) + self.assertEqual(lst, [True, False]) + + def testOredMinBoolList(self): + lu = ListUser() + lst = [False, False, True] + self.assertTrue(lu.oredMinBoolList(lst)) + self.assertTrue(lu.callOredMinBoolList(lst)) + lst = [False, False, False] + self.assertFalse(lu.oredMinBoolList(lst)) + self.assertFalse(lu.callOredMinBoolList(lst)) + + def testOredMinBoolListFromExtendedClass(self): + lu = ExtListUser() + lst = [False, False, True] + self.assertFalse(lu.oredMinBoolList(lst)) + self.assertFalse(lu.callOredMinBoolList(lst)) + lst = [False, False, False] + self.assertTrue(lu.oredMinBoolList(lst)) + self.assertTrue(lu.callOredMinBoolList(lst)) + + +class ValListConversionTest(unittest.TestCase): + + def testCreateValList(self): + num = 4 + lu = ListUser() + lst = lu.createValList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), Val) + self.assertEqual([val.valId() for val in lst], list(range(num))) + lst = lu.callCreateValList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), Val) + self.assertEqual([val.valId() for val in lst], list(range(num))) + + def testCreateValListFromExtendedClass(self): + lu = ExtListUser() + num = 4 + lst = lu.createValList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), Val) + self.assertEqual([val.valId() for val in lst], list(range(0, num * 2, 2))) + lst = lu.callCreateValList(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), Val) + self.assertEqual([val.valId() for val in lst], list(range(0, num * 2, 2))) + + def testSumValList(self): + lu = ListUser() + lst = [Val(i) for i in range(4)] + self.assertEqual(lu.sumValList(lst), sum([val.valId() for val in lst])) + self.assertEqual(lu.callSumValList(lst), sum([val.valId() for val in lst])) + + def testSumValListFromExtendedClass(self): + lu = ExtListUser() + lst = [Val(i) for i in range(4)] + self.assertEqual(lu.sumValList(lst), sum([val.valId() for val in lst]) * 2) + self.assertEqual(lu.callSumValList(lst), sum([val.valId() for val in lst]) * 2) + + +class ObjListConversionTest(unittest.TestCase): + + def testCreateObjList(self): + o1 = Obj(1) + o2 = Obj(2) + lu = ListUser() + lst = lu.createObjList(o1, o2) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), Obj) + self.assertEqual(lst, [o1, o2]) + self.assertEqual([obj.objId() for obj in lst], [1, 2]) + + lst = lu.callCreateObjList(o1, o2) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), Obj) + self.assertEqual(lst, [o1, o2]) + self.assertEqual([obj.objId() for obj in lst], [1, 2]) + + def testCreateObjListFromExtendedClass(self): + o1 = Obj(1) + o2 = Obj(2) + lu = ExtListUser() + lst = lu.createObjList(o1, o2) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), Obj) + self.assertEqual(lst, [o1, o2]) + self.assertEqual([obj.objId() for obj in lst], [2, 4]) + + lst = lu.callCreateObjList(o1, o2) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), 2) + for i in lst: + self.assertEqual(type(i), Obj) + self.assertEqual(lst, [o1, o2]) + self.assertEqual([obj.objId() for obj in lst], [4, 8]) + + def testSumObjList(self): + lu = ListUser() + lst = [Obj(i) for i in list(range(4))] + self.assertEqual(lu.sumObjList(lst), sum([obj.objId() for obj in lst])) + self.assertEqual(lu.callSumObjList(lst), sum([obj.objId() for obj in lst])) + + def testSumObjListFromExtendedClass(self): + lu = ExtListUser() + lst = [Obj(i) for i in list(range(4))] + self.assertEqual(lu.sumObjList(lst), sum([obj.objId() for obj in lst]) * 2) + self.assertEqual(lu.callSumObjList(lst), sum([obj.objId() for obj in lst]) * 2) + + +class ListOfIntListConversionTest(unittest.TestCase): + + def testCreateListOfIntLists(self): + num = 4 + lu = ListUser() + lst = lu.createListOfIntLists(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), list) + self.assertEqual(i, list(range(num))) + for j in i: + self.assertEqual(type(j), int) + self.assertEqual(lst, [list(range(num))] * 4) + + def testCreateListOfIntListsFromExtendedClass(self): + num = 4 + lu = ExtListUser() + lst = lu.createListOfIntLists(num) + self.assertEqual(type(lst), list) + self.assertEqual(len(lst), num) + for i in lst: + self.assertEqual(type(i), list) + self.assertEqual(i, list(range(0, num * 2, 2))) + for j in i: + self.assertEqual(type(j), int) + self.assertEqual(lst, [list(range(0, num * 2, 2))] * 4) + + def testSumListIntLists(self): + lu = ListUser() + lst = [range(4)] * 4 + self.assertEqual(lu.sumListOfIntLists(lst), sum([sum(line) for line in [range(4)] * 4])) + self.assertEqual(lu.callSumListOfIntLists(lst), sum([sum(line) for line in [range(4)] * 4])) + + def testSumListOfIntListsFromExtendedClass(self): + lu = ExtListUser() + lst = [range(4)] * 4 + self.assertEqual(lu.sumListOfIntLists(lst), sum([sum(line) for line in [range(4)] * 4]) * 2) + self.assertEqual(lu.callSumListOfIntLists(lst), sum([sum(line) for line in [range(4)] * 4]) * 2) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/minimalbinding/minbool_test.py b/sources/shiboken2/tests/minimalbinding/minbool_test.py new file mode 100644 index 000000000..b7830a79b --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/minbool_test.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from minimal import MinBoolUser + +class DerivedMinBoolUser (MinBoolUser): + def returnMyselfVirtual(self): + return MinBoolUser() + +class MinBoolTest(unittest.TestCase): + + def testMinBoolUser(self): + mbuTrue = MinBoolUser() + mbuFalse = MinBoolUser() + mbuTrue.setMinBool(True) + self.assertEqual(mbuFalse.minBool(), False) + self.assertEqual(mbuTrue.minBool(), True) + self.assertEqual(mbuTrue.callInvertedMinBool(), False) + + self.assertEqual(mbuTrue.minBool() == True, True) + self.assertEqual(False == mbuFalse.minBool(), True) + self.assertEqual(mbuTrue.minBool() == mbuFalse.minBool(), False) + + self.assertEqual(mbuFalse.minBool() != True, True) + self.assertEqual(True != mbuFalse.minBool(), True) + self.assertEqual(mbuTrue.minBool() != mbuFalse.minBool(), True) + + def testVirtuals(self): + dmbu = DerivedMinBoolUser() + self.assertEqual(dmbu.invertedMinBool(), True) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/minimalbinding/minimal-binding.txt.in b/sources/shiboken2/tests/minimalbinding/minimal-binding.txt.in new file mode 100644 index 000000000..85b139676 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/minimal-binding.txt.in @@ -0,0 +1,15 @@ +[generator-project] + +generator-set = shiboken + +header-file = @CMAKE_CURRENT_SOURCE_DIR@/global.h +typesystem-file = @minimal_TYPESYSTEM@ + +output-directory = @CMAKE_CURRENT_BINARY_DIR@ + +include-path = @libminimal_SOURCE_DIR@ + +typesystem-path = @CMAKE_CURRENT_SOURCE_DIR@ + +enable-parent-ctor-heuristic +use-isnull-as-nb_nonzero diff --git a/sources/shiboken2/tests/minimalbinding/obj_test.py b/sources/shiboken2/tests/minimalbinding/obj_test.py new file mode 100644 index 000000000..bf00f1c92 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/obj_test.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from minimal import Obj + +class ExtObj(Obj): + def __init__(self, objId): + Obj.__init__(self, objId) + self.virtual_method_called = False + + def virtualMethod(self, val): + self.virtual_method_called = True + return not Obj.virtualMethod(self, val) + + def passObjectType(self, obj): + obj.setObjId(obj.objId() + 1) + return obj + + def passObjectTypeReference(self, obj): + obj.setObjId(obj.objId() + 1) + return obj + + +class ObjTest(unittest.TestCase): + + def testNormalMethod(self): + objId = 123 + obj = Obj(objId) + self.assertEqual(obj.objId(), objId) + + def testNormalMethodFromExtendedClass(self): + objId = 123 + obj = ExtObj(objId) + self.assertEqual(obj.objId(), objId) + + def testVirtualMethod(self): + obj = Obj(0) + even_number = 8 + self.assertEqual(obj.virtualMethod(even_number), obj.callVirtualMethod(even_number)) + + def testVirtualMethodFromExtendedClass(self): + obj = ExtObj(0) + even_number = 8 + self.assertEqual(obj.virtualMethod(even_number), obj.callVirtualMethod(even_number)) + self.assertTrue(obj.virtual_method_called) + + def testPassObjectType(self): + obj = Obj(0) + self.assertEqual(obj, obj.passObjectType(obj)) + self.assertEqual(obj, obj.callPassObjectType(obj)) + + def testPassObjectTypeNone(self): + obj = Obj(0) + self.assertEqual(None, obj.passObjectType(None)) + self.assertEqual(None, obj.callPassObjectType(None)) + + def testPassObjectTypeReference(self): + obj = Obj(0) + self.assertEqual(obj, obj.passObjectTypeReference(obj)) + self.assertEqual(obj, obj.callPassObjectTypeReference(obj)) + + def testPassObjectTypeFromExtendedClass(self): + obj = ExtObj(0) + self.assertEqual(obj.objId(), 0) + sameObj = obj.passObjectType(obj) + self.assertEqual(obj, sameObj) + self.assertEqual(sameObj.objId(), 1) + sameObj = obj.callPassObjectType(obj) + self.assertEqual(obj, sameObj) + self.assertEqual(sameObj.objId(), 2) + + def testPassObjectTypeReferenceFromExtendedClass(self): + obj = ExtObj(0) + self.assertEqual(obj.objId(), 0) + sameObj = obj.passObjectTypeReference(obj) + self.assertEqual(obj, sameObj) + self.assertEqual(sameObj.objId(), 1) + sameObj = obj.callPassObjectTypeReference(obj) + self.assertEqual(obj, sameObj) + self.assertEqual(sameObj.objId(), 2) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/minimalbinding/typedef_test.py b/sources/shiboken2/tests/minimalbinding/typedef_test.py new file mode 100644 index 000000000..62df9cbea --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/typedef_test.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from minimal import * +from py3kcompat import IS_PY3K + +try: + import numpy as np +except ImportError as e: + print(e) + np = None + + +if IS_PY3K: + import functools + reduce = functools.reduce + + +class TypedefTest(unittest.TestCase): + + def setUp(self): + self.the_size = 8 + + def test_arrayFuncInt(self): + none = () + full = range(self.the_size) + self.assertTrue(arrayFuncInt(none), "None is empty, arrayFuncInt should return true") + self.assertFalse(arrayFuncInt(full), "Full is NOT empty, arrayFuncInt should return false") + + self.assertTrue(arrayFuncInt(np.array(none)), "None is empty, arrayFuncInt should return true") + self.assertFalse(arrayFuncInt(np.array(full)), "Full is NOT empty, arrayFuncInt should return false") + + def test_arrayFuncIntTypedef(self): + none = () + full = (1, 2, 3) + self.assertTrue(arrayFuncIntTypedef(none), "None is empty, arrayFuncIntTypedef should return true") + self.assertFalse(arrayFuncIntTypedef(full), "Full is NOT empty, arrayFuncIntTypedef should return false") + + self.assertTrue(arrayFuncIntTypedef(np.array(none)), "None is empty, arrayFuncIntTypedef should return true") + self.assertFalse(arrayFuncIntTypedef(np.array(full)), "Full is NOT empty, arrayFuncIntTypedef should return false") + + def test_arrayFuncIntReturn(self): + none = arrayFuncIntReturn(0) + full = arrayFuncIntReturn(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + def test_arrayFuncIntReturnTypedef(self): + none = arrayFuncIntReturnTypedef(0) + full = arrayFuncIntReturnTypedef(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + def test_arrayFunc(self): + none = () + full = range(self.the_size) + self.assertTrue(arrayFunc(none), "None is empty, arrayFunc should return true") + self.assertFalse(arrayFunc(full), "Full is NOT empty, arrayFunc should return false") + + self.assertTrue(arrayFunc(np.array(none)), "None is empty, arrayFunc should return true") + self.assertFalse(arrayFunc(np.array(full)), "Full is NOT empty, arrayFunc should return false") + + def test_arrayFuncTypedef(self): + none = () + full = (1, 2, 3) + self.assertTrue(arrayFuncTypedef(none), "None is empty, arrayFuncTypedef should return true") + self.assertFalse(arrayFuncTypedef(full), "Full is NOT empty, arrayFuncTypedef should return false") + + self.assertTrue(arrayFuncTypedef(np.array(none)), "None is empty, arrayFuncTypedef should return true") + self.assertFalse(arrayFuncTypedef(np.array(full)), "Full is NOT empty, arrayFuncTypedef should return false") + + def test_arrayFuncReturn(self): + none = arrayFuncReturn(0) + full = arrayFuncReturn(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + def test_arrayFuncReturnTypedef(self): + none = arrayFuncReturnTypedef(0) + full = arrayFuncReturnTypedef(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + +if __name__ == '__main__': + if np != None: + unittest.main() diff --git a/sources/shiboken2/tests/minimalbinding/typesystem_minimal.xml b/sources/shiboken2/tests/minimalbinding/typesystem_minimal.xml new file mode 100644 index 000000000..968b27c53 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/typesystem_minimal.xml @@ -0,0 +1,93 @@ +<?xml version="1.0"?> +<typesystem package="minimal"> + <primitive-type name="bool"/> + <primitive-type name="int"/> + + <primitive-type name="MinBool" target-lang-api-name="PyBool" default-constructor="MinBool(false)"> + <include file-name="minbool.h" location="global"/> + <conversion-rule> + <native-to-target> + return PyBool_FromLong(%in.value()); + </native-to-target> + <target-to-native> + <add-conversion type="PyBool" check="PyBool_Check(%in)"> + %out = %OUTTYPE(%in == Py_True); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <container-type name="std::list" type="list"> + <include file-name="list" location="global"/> + <conversion-rule> + <native-to-target> + PyObject* %out = PyList_New((int) %in.size()); + %INTYPE::const_iterator it = %in.begin(); + for (int idx = 0; it != %in.end(); ++it, ++idx) { + %INTYPE_0 cppItem(*it); + PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem)); + } + return %out; + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0)); + for (int i = 0; i < PySequence_Fast_GET_SIZE(seq.object()); i++) { + PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), i); + %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem); + %out.push_back(cppItem); + } + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + + <object-type name="Obj"/> + <value-type name="Val"> + <enum-type name="ValEnum"/> + </value-type> + <value-type name="ListUser"/> + <value-type name="MinBoolUser"/> + + <container-type name="std::vector" type="vector"> + <include file-name="vector" location="global"/> + <conversion-rule> + <native-to-target> + %INTYPE::size_type vectorSize = %in.size(); + PyObject* %out = PyList_New((int) vectorSize); + for (%INTYPE::size_type idx = 0; idx < vectorSize; ++idx) { + %INTYPE_0 cppItem(%in[idx]); + PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem)); + } + return %out; + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0)); + int vectorSize = PySequence_Fast_GET_SIZE(seq.object()); + %out.reserve(vectorSize); + for (int idx = 0; idx < vectorSize; ++idx ) { + PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), idx); + %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem); + %out.push_back(cppItem); + } + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + <!-- Test wrapping of a typedef --> + <function signature="arrayFuncInt(std::vector<int>)" /> + <!-- Note manual expansion of the typedef --> + <function signature="arrayFuncIntTypedef(std::vector<int>)" /> + + <function signature="arrayFuncIntReturn(int)" /> + <function signature="arrayFuncIntReturnTypedef(int)" /> + + <!-- Test wrapping of a typedef of a typedef --> + <function signature="arrayFunc(std::vector<int>)" /> + <!-- Note manual expansion of the typedef --> + <function signature="arrayFuncTypedef(std::vector<int>)" /> + + <function signature="arrayFuncReturn(int)" /> + <function signature="arrayFuncReturnTypedef(int)" /> +</typesystem> diff --git a/sources/shiboken2/tests/minimalbinding/val_test.py b/sources/shiboken2/tests/minimalbinding/val_test.py new file mode 100644 index 000000000..97e4e4778 --- /dev/null +++ b/sources/shiboken2/tests/minimalbinding/val_test.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from minimal import Val + + +class ExtVal(Val): + def __init__(self, valId): + Val.__init__(self, valId) + + def passValueType(self, val): + return ExtVal(val.valId() + 1) + + def passValueTypePointer(self, val): + val.setValId(val.valId() + 1) + return val + + def passValueTypeReference(self, val): + val.setValId(val.valId() + 1) + return val + + +class ValTest(unittest.TestCase): + + def testNormalMethod(self): + valId = 123 + val = Val(valId) + self.assertEqual(val.valId(), valId) + + def testPassValueType(self): + val = Val(123) + val1 = val.passValueType(val) + self.assertNotEqual(val, val1) + self.assertEqual(val1.valId(), 123) + val2 = val.callPassValueType(val) + self.assertNotEqual(val, val2) + self.assertEqual(val2.valId(), 123) + + def testPassValueTypePointer(self): + val = Val(0) + self.assertEqual(val, val.passValueTypePointer(val)) + self.assertEqual(val, val.callPassValueTypePointer(val)) + + def testPassValueTypeReference(self): + val = Val(0) + self.assertEqual(val, val.passValueTypeReference(val)) + self.assertEqual(val, val.callPassValueTypeReference(val)) + + def testPassAndReceiveEnumValue(self): + val = Val(0) + self.assertEqual(val.oneOrTheOtherEnumValue(Val.One), Val.Other) + self.assertEqual(val.oneOrTheOtherEnumValue(Val.Other), Val.One) + + def testPassValueTypeFromExtendedClass(self): + val = ExtVal(0) + val1 = val.passValueType(val) + self.assertNotEqual(val, val1) + self.assertEqual(val1.valId(), val.valId() + 1) + val2 = val.callPassValueType(val) + self.assertNotEqual(val, val2) + self.assertEqual(val2.valId(), val.valId() + 1) + + def testPassValueTypePointerFromExtendedClass(self): + val = ExtVal(0) + self.assertEqual(val.valId(), 0) + sameVal = val.passValueTypePointer(val) + self.assertEqual(val, sameVal) + self.assertEqual(sameVal.valId(), 1) + sameVal = val.callPassValueTypePointer(val) + self.assertEqual(val, sameVal) + self.assertEqual(sameVal.valId(), 2) + + def testPassValueTypeReferenceFromExtendedClass(self): + val = ExtVal(0) + self.assertEqual(val.valId(), 0) + sameVal = val.passValueTypeReference(val) + self.assertEqual(val, sameVal) + self.assertEqual(sameVal.valId(), 1) + sameVal = val.callPassValueTypeReference(val) + self.assertEqual(val, sameVal) + self.assertEqual(sameVal.valId(), 2) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/otherbinding/CMakeLists.txt b/sources/shiboken2/tests/otherbinding/CMakeLists.txt new file mode 100644 index 000000000..ac7178e5d --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/CMakeLists.txt @@ -0,0 +1,48 @@ +project(other) + +set(other_TYPESYSTEM +${CMAKE_CURRENT_SOURCE_DIR}/typesystem_other.xml +) + +set(other_SRC +${CMAKE_CURRENT_BINARY_DIR}/other/extendsnoimplicitconversion_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/other/number_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/other/otherderived_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/other/othermultiplederived_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/other/otherobjecttype_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/other/other_module_wrapper.cpp +) + + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/other-binding.txt.in" + "${CMAKE_CURRENT_BINARY_DIR}/other-binding.txt" @ONLY) + +add_custom_command(OUTPUT ${other_SRC} +COMMAND shiboken2 --project-file=${CMAKE_CURRENT_BINARY_DIR}/other-binding.txt ${GENERATOR_EXTRA_FLAGS} +DEPENDS ${other_TYPESYSTEM} ${CMAKE_CURRENT_SOURCE_DIR}/global.h shiboken2 +WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +COMMENT "Running generator for 'other' test binding..." +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR} + ${SBK_PYTHON_INCLUDE_DIR} + ${libother_SOURCE_DIR} + ${libsample_SOURCE_DIR} + ${libsample_SOURCE_DIR}/.. + ${sample_BINARY_DIR} + ${sample_BINARY_DIR}/sample + ${libshiboken_SOURCE_DIR}) +add_library(other MODULE ${other_SRC}) +set_property(TARGET other PROPERTY PREFIX "") +if(WIN32) + set_property(TARGET other PROPERTY SUFFIX ".pyd") +endif() +target_link_libraries(other + libother + libsample + ${SBK_PYTHON_LIBRARIES} + libshiboken) + +add_dependencies(other sample) + diff --git a/sources/shiboken2/tests/otherbinding/collector_external_operator_test.py b/sources/shiboken2/tests/otherbinding/collector_external_operator_test.py new file mode 100644 index 000000000..328aec837 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/collector_external_operator_test.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Collector shift operators defined in other modules.''' + +import unittest + +from sample import Collector, ObjectType +from other import OtherObjectType + +class CollectorOtherObjectType(unittest.TestCase): + '''Test cases for Collector << OtherObjectType''' + + def testLShiftWithExpectedType(self): + '''Collector << ObjectType # libsample << operator''' + collector = Collector() + obj = ObjectType() + collector << obj + self.assertEqual(collector.items()[0], obj.identifier()) + + def testOtherReversal(self): + '''Collector << OtherObjectType # libother << operator''' + collector = Collector() + obj = OtherObjectType() + collector << obj + self.assertEqual(collector.items()[0], obj.identifier() * 2) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/otherbinding/conversion_operator_for_class_without_implicit_conversions_test.py b/sources/shiboken2/tests/otherbinding/conversion_operator_for_class_without_implicit_conversions_test.py new file mode 100755 index 000000000..5c3733dc2 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/conversion_operator_for_class_without_implicit_conversions_test.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests calling NoImplicitConversion using a ExtendsNoImplicitConversion parameter, + being that the latter defines a new conversion operator for the former, and this one + has no implicit conversions.''' + +import unittest + +from sample import NoImplicitConversion +from other import ExtendsNoImplicitConversion + +class ConversionOperatorForClassWithoutImplicitConversionsTest(unittest.TestCase): + '''Tests calling NoImplicitConversion constructor using a ExtendsNoImplicitConversion parameter.''' + + def testNoImplicitConversion(self): + '''Basic test to see if the NoImplicitConversion is Ok.''' + obj = NoImplicitConversion(123) + # NoImplicitConversion.receivesNoImplicitConversionByValue(NoImplicitConversion) + self.assertEqual(obj.objId(), NoImplicitConversion.receivesNoImplicitConversionByValue(obj)) + # NoImplicitConversion.receivesNoImplicitConversionByPointer(NoImplicitConversion*) + self.assertEqual(obj.objId(), NoImplicitConversion.receivesNoImplicitConversionByPointer(obj)) + # NoImplicitConversion.receivesNoImplicitConversionByReference(NoImplicitConversion&) + self.assertEqual(obj.objId(), NoImplicitConversion.receivesNoImplicitConversionByReference(obj)) + + def testPassingExtendsNoImplicitConversionAsNoImplicitConversionByValue(self): + '''Gives an ExtendsNoImplicitConversion object to a function expecting a NoImplicitConversion, passing by value.''' + obj = ExtendsNoImplicitConversion(123) + self.assertEqual(obj.objId(), NoImplicitConversion.receivesNoImplicitConversionByValue(obj)) + + def testPassingExtendsNoImplicitConversionAsNoImplicitConversionByReference(self): + '''Gives an ExtendsNoImplicitConversion object to a function expecting a NoImplicitConversion, passing by reference.''' + obj = ExtendsNoImplicitConversion(123) + self.assertEqual(obj.objId(), NoImplicitConversion.receivesNoImplicitConversionByReference(obj)) + + def testPassingExtendsNoImplicitConversionAsNoImplicitConversionByPointer(self): + '''Gives an ExtendsNoImplicitConversion object to a function expecting a NoImplicitConversion, passing by pointer. + This should not be accepted, since pointers should not be converted.''' + obj = ExtendsNoImplicitConversion(123) + self.assertRaises(TypeError, NoImplicitConversion.receivesNoImplicitConversionByPointer, obj) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/otherbinding/extended_multiply_operator_test.py b/sources/shiboken2/tests/otherbinding/extended_multiply_operator_test.py new file mode 100755 index 000000000..4acc53024 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/extended_multiply_operator_test.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for libsample's Point multiply operator defined in libother module.''' + +import unittest + +from sample import Point +from other import Number + +class PointOperationsWithNumber(unittest.TestCase): + '''Test cases for libsample's Point multiply operator defined in libother module.''' + + def testPointTimesInt(self): + '''sample.Point * int''' + pt1 = Point(2, 7) + num = 3 + pt2 = Point(pt1.x() * num, pt1.y() * num) + self.assertEqual(pt1 * num, pt2) + + def testIntTimesPoint(self): + '''int * sample.Point''' + pt1 = Point(2, 7) + num = 3 + pt2 = Point(pt1.x() * num, pt1.y() * num) + self.assertEqual(num * pt1, pt2) + + def testPointTimesNumber(self): + '''sample.Point * other.Number''' + pt = Point(2, 7) + num = Number(11) + self.assertEqual(pt * num, pt * 11) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/otherbinding/global.h b/sources/shiboken2/tests/otherbinding/global.h new file mode 100644 index 000000000..cc6634ab4 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/global.h @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "../samplebinding/global.h" +#include "extendsnoimplicitconversion.h" +#include "number.h" +#include "otherderived.h" +#include "otherobjecttype.h" +#include "othermultiplederived.h" + diff --git a/sources/shiboken2/tests/otherbinding/module_reload_test.py b/sources/shiboken2/tests/otherbinding/module_reload_test.py new file mode 100644 index 000000000..0f601d1a3 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/module_reload_test.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os +import sys +import shutil +import unittest + +from py3kcompat import IS_PY3K + +if IS_PY3K: + from imp import reload + +orig_path = os.path.join(os.path.dirname(__file__)) +workdir = os.getcwd() +src = os.path.join(orig_path, 'test_module_template.py') +dst = os.path.join(workdir, 'test_module.py') +shutil.copyfile(src, dst) +sys.path.append(workdir) + +class TestModuleReloading(unittest.TestCase): + + def testModuleReloading(self): + '''Test module reloading with on-the-fly modifications.''' + import test_module + for i in range(3): + oldObject = test_module.obj + self.assertTrue(oldObject is test_module.obj) + reload(test_module) + self.assertFalse(oldObject is test_module.obj) + +if __name__ == "__main__": + unittest.main() diff --git a/sources/shiboken2/tests/otherbinding/new_ctor_operator_test.py b/sources/shiboken2/tests/otherbinding/new_ctor_operator_test.py new file mode 100755 index 000000000..634106cea --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/new_ctor_operator_test.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests calling Str constructor using a Number parameter, being that number defines a cast operator to Str.''' + +import unittest + +from sample import Str +from other import Number + +class NewCtorOperatorTest(unittest.TestCase): + '''Tests calling Str constructor using a Number parameter, being that number defines a cast operator to Str.''' + + def testNumber(self): + '''Basic test to see if the Number class is Ok.''' + value = 123 + num = Number(value) + self.assertEqual(num.value(), value) + + def testStrCtorWithNumberArgument(self): + '''Try to build a Str from 'sample' module with a Number argument from 'other' module.''' + value = 123 + num = Number(value) + string = Str(num) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/otherbinding/objtypehashes_test.py b/sources/shiboken2/tests/otherbinding/objtypehashes_test.py new file mode 100644 index 000000000..753aec1a1 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/objtypehashes_test.py @@ -0,0 +1,53 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import * +from other import * +import shiboken2 as shiboken + +class TestHashFuncs (unittest.TestCase): + + def testIt(self): + obj1 = HandleHolder() + obj2 = HandleHolder() + + hash1 = hash(obj1) + hash2 = hash(obj2) + self.assertNotEqual(hash1, hash2) + + # Now invalidate the object and test its hash. It shouldn't segfault. + shiboken.invalidate(obj1) + + hash1_2 = hash(obj1) + self.assertEqual(hash1_2, hash1) + + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/otherbinding/other-binding.txt.in b/sources/shiboken2/tests/otherbinding/other-binding.txt.in new file mode 100644 index 000000000..a17b70fc1 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/other-binding.txt.in @@ -0,0 +1,18 @@ +[generator-project] + +generator-set = shiboken + +header-file = @CMAKE_CURRENT_SOURCE_DIR@/global.h +typesystem-file = @other_TYPESYSTEM@ + +output-directory = @CMAKE_CURRENT_BINARY_DIR@ + +include-path = @libother_SOURCE_DIR@ +include-path = @libsample_SOURCE_DIR@ +include-path = @libsample_SOURCE_DIR@/.. + +typesystem-path = @CMAKE_CURRENT_SOURCE_DIR@ +typesystem-path = @sample_SOURCE_DIR@ + +enable-parent-ctor-heuristic + diff --git a/sources/shiboken2/tests/otherbinding/otherderived_test.py b/sources/shiboken2/tests/otherbinding/otherderived_test.py new file mode 100644 index 000000000..f6906c88a --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/otherderived_test.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for OtherDerived class''' + +import sys +import unittest + +from sample import Abstract, Derived +from other import OtherDerived, Number + +class Multiple(Derived, Number): + def __init__(self): + Derived.__init__(self, 42) + Number.__init__(self, 42) + + def testCall(self): + return True + +class OtherDeviant(OtherDerived): + def __init__(self): + OtherDerived.__init__(self) + self.pure_virtual_called = False + self.unpure_virtual_called = False + + def pureVirtual(self): + self.pure_virtual_called = True + + def unpureVirtual(self): + self.unpure_virtual_called = True + + def className(self): + return 'OtherDeviant' + +class MultipleTest(unittest.TestCase): + '''Test case for Multiple derived class''' + + def testConstructor(self): + o = Multiple() + self.assertTrue(isinstance(o, Multiple)) + self.assertTrue(isinstance(o, Number)) + self.assertTrue(isinstance(o, Derived)) + del o + + def testMethodCall(self): + o = Multiple() + self.assertTrue(o.id_(), 42) + self.assertTrue(o.value(), 42) + self.assertTrue(o.testCall()) + +class OtherDerivedTest(unittest.TestCase): + '''Test case for OtherDerived class''' + + def testParentClassMethodsAvailability(self): + '''Test if OtherDerived class really inherits its methods from parent.''' + inherited_methods = set(['callPureVirtual', 'callUnpureVirtual', + 'id_', 'pureVirtual', 'unpureVirtual']) + self.assertTrue(inherited_methods.issubset(dir(OtherDerived))) + + def testReimplementedPureVirtualMethodCall(self): + '''Test if a Python override of a implemented pure virtual method is correctly called from C++.''' + d = OtherDeviant() + d.callPureVirtual() + self.assertTrue(d.pure_virtual_called) + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a reimplemented virtual method is correctly called from C++.''' + d = OtherDeviant() + d.callUnpureVirtual() + self.assertTrue(d.unpure_virtual_called) + + def testVirtualMethodCallString(self): + '''Test virtual method call returning string.''' + d = OtherDerived() + self.assertEqual(d.className(), 'OtherDerived') + self.assertEqual(d.getClassName(), 'OtherDerived') + + def testReimplementedVirtualMethodCallReturningString(self): + '''Test if a Python override of a reimplemented virtual method is correctly called from C++.''' + d = OtherDeviant() + self.assertEqual(d.className(), 'OtherDeviant') + self.assertEqual(d.getClassName(), 'OtherDeviant') + + def testCallToMethodWithAbstractArgument(self): + '''Call to method that expects an Abstract argument.''' + objId = 123 + d = OtherDerived(objId) + self.assertEqual(Abstract.getObjectId(d), objId) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/otherbinding/test_module_template.py b/sources/shiboken2/tests/otherbinding/test_module_template.py new file mode 100644 index 000000000..ddde85c2e --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/test_module_template.py @@ -0,0 +1,40 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from other import * +from sample import * + + +class MyObjectType(ObjectType): + pass + +class MyOtherObjectType(OtherObjectType): + value = 10 + + +obj = MyObjectType() diff --git a/sources/shiboken2/tests/otherbinding/typediscovery_test.py b/sources/shiboken2/tests/otherbinding/typediscovery_test.py new file mode 100644 index 000000000..507c5be94 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/typediscovery_test.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for type discovery''' + +import unittest + +from sample import Abstract, Base1, Derived, MDerived1, MDerived3, SonOfMDerived1 +from other import OtherMultipleDerived + +class TypeDiscoveryTest(unittest.TestCase): + + def testPureVirtualsOfImpossibleTypeDiscovery(self): + a = Derived.triggerImpossibleTypeDiscovery() + self.assertEqual(type(a), Abstract) + # call some pure virtual method + a.pureVirtual() + + def testAnotherImpossibleTypeDiscovery(self): + a = Derived.triggerAnotherImpossibleTypeDiscovery() + self.assertEqual(type(a), Derived) + + def testMultipleInheritance(self): + obj = OtherMultipleDerived.createObject("Base1"); + self.assertEqual(type(obj), Base1) + obj = OtherMultipleDerived.createObject("MDerived1"); + self.assertEqual(type(obj), MDerived1) + obj = OtherMultipleDerived.createObject("SonOfMDerived1"); + self.assertEqual(type(obj), SonOfMDerived1) + obj = OtherMultipleDerived.createObject("MDerived3"); + self.assertEqual(type(obj), MDerived3) + obj = OtherMultipleDerived.createObject("OtherMultipleDerived"); + self.assertEqual(type(obj), OtherMultipleDerived) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/otherbinding/typesystem_other.xml b/sources/shiboken2/tests/otherbinding/typesystem_other.xml new file mode 100644 index 000000000..63ccdd518 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/typesystem_other.xml @@ -0,0 +1,15 @@ +<?xml version="1.0"?> +<typesystem package="other"> + <load-typesystem name="typesystem_sample.xml" generate="no" /> + + <object-type name="OtherObjectType" /> + <object-type name="OtherDerived" /> + <object-type name="OtherMultipleDerived" /> + + <value-type name="ExtendsNoImplicitConversion" /> + <value-type name="Number" /> + + <suppress-warning text="signature 'operator!=(ByteArray,const char*)' for function modification in 'ByteArray' not found." /> + <suppress-warning text="signature 'operator+(ByteArray,const char*)' for function modification in 'ByteArray' not found." /> + <suppress-warning text="signature 'operator==(ByteArray,const char*)' for function modification in 'ByteArray' not found." /> +</typesystem> diff --git a/sources/shiboken2/tests/otherbinding/usersprimitivefromothermodule_test.py b/sources/shiboken2/tests/otherbinding/usersprimitivefromothermodule_test.py new file mode 100755 index 000000000..06cfce371 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/usersprimitivefromothermodule_test.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests user defined primitive type from a required module.''' + +import unittest +from other import Number + +class UserDefinedPrimitiveTypeFromRequiredModuleTest(unittest.TestCase): + + def testUsersPrimitiveFromRequiredModuleAsArgument(self): + '''static Number Number::fromComplex(Complex)''' + cpx = complex(3.0, 1.2) + number = Number.fromComplex(cpx) + self.assertEqual(number.value(), int(cpx.real)) + + def testUsersPrimitiveFromRequiredModuleAsReturnValue(self): + '''Complex Number::toComplex()''' + number = Number(12) + cpx = number.toComplex() + self.assertEqual(number.value(), int(cpx.real)) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/otherbinding/wrongctor_test.py b/sources/shiboken2/tests/otherbinding/wrongctor_test.py new file mode 100644 index 000000000..61317fe66 --- /dev/null +++ b/sources/shiboken2/tests/otherbinding/wrongctor_test.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import * +from other import * + +class Foo(OtherDerived): + def __init__(self): + Abstract.__init__(self, 2) # this should raise an exception + +class Foo2(ObjectType, OtherDerived): + def __init__(self): + ObjectType.__init__(self) + Abstract.__init__(self, 2) # this should raise an exception + + +class WrongCtorTest(unittest.TestCase): + def testIt(self): + self.assertRaises(TypeError, Foo) + self.assertRaises(TypeError, Foo2) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/py3k.py b/sources/shiboken2/tests/py3k.py new file mode 100644 index 000000000..5f2961a6a --- /dev/null +++ b/sources/shiboken2/tests/py3k.py @@ -0,0 +1,2 @@ +def printToFile(f, str): + print(str, file=f) diff --git a/sources/shiboken2/tests/py3kcompat.py b/sources/shiboken2/tests/py3kcompat.py new file mode 100644 index 000000000..45550efb3 --- /dev/null +++ b/sources/shiboken2/tests/py3kcompat.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import sys + +IS_PY3K = sys.version_info[0] == 3 + +if IS_PY3K: + def unicode(s): + return s + + def b(s): + return bytes(s, "UTF8") + + def l(n): + return n + + long = int +else: + def b(s): + return s + + def l(n): + return long(n) + + unicode = unicode + long = long diff --git a/sources/shiboken2/tests/samplebinding/CMakeLists.txt b/sources/shiboken2/tests/samplebinding/CMakeLists.txt new file mode 100644 index 000000000..f897712f4 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/CMakeLists.txt @@ -0,0 +1,145 @@ +project(sample) + +set(sample_TYPESYSTEM +${CMAKE_CURRENT_SOURCE_DIR}/typesystem_sample.xml +) + +set(sample_SRC +${CMAKE_CURRENT_BINARY_DIR}/sample/abstractmodifications_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/abstract_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base1_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base2_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base3_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base4_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base5_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base6_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/blackbox_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/bytearray_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/bucket_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/classwithfunctionpointer_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/collector_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/color_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/ctorconvrule_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/cvlistuser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/cvvaluetype_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sbkdate_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/derived_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/derived_someinnerclass_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/echo_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/event_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/expression_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/friendofonlycopy_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/handleholder_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/implicitconv_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/implicitbase_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/implicittarget_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/intlist_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sortedoverload_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/intwrapper_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/injectcode_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/listuser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mapuser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived1_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived2_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived3_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived4_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived5_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/modelindex_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/modifications_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/modifiedconstructor_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/noimplicitconversion_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/nondefaultctor_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objectmodel_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttype_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypebyvalue_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypeholder_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypederived_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypelayout_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypeptrlist_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objecttypeoperators_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objectview_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/objtypereference_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/oddbooluser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/onlycopy_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/otherbase_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/overload_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/overload2_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/pen_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/persistentmodelindex_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/photon_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/photon_base_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/photon_valueidentity_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/photon_valueduplicator_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/point_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/pointerholder_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/pointf_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/pointvaluelist_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/polygon_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/primitivestructpointerholder_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/privatector_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/privatedtor_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedenumclass_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectednonpolymorphic_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphic_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphicdaughter_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphicgranddaughter_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedproperty_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/protectedvirtualdestructor_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/rect_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/rectf_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/reference_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/referentmodelindex_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sample_module_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sample_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sample_sample_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_someclass_someinnerclass_okthisisrecursiveenough_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_someclass_someinnerclass_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_someclass_someotherinnerclass_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_someclass_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_derivedfromnamespace_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/simplefile_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/size_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sizef_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sonofmderived1_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/str_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/strlist_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/time_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/templateptr_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/unremovednamespace_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/virtualdaughter_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/virtualdtor_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/virtualmethods_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/voidholder_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/valueandvirtual_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/filter_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/data_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/intersection_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/union_wrapper.cpp +) + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/sample-binding.txt.in" + "${CMAKE_CURRENT_BINARY_DIR}/sample-binding.txt" @ONLY) + +add_custom_command(OUTPUT ${sample_SRC} +COMMAND shiboken2 --project-file=${CMAKE_CURRENT_BINARY_DIR}/sample-binding.txt ${GENERATOR_EXTRA_FLAGS} +DEPENDS ${sample_TYPESYSTEM} ${CMAKE_CURRENT_SOURCE_DIR}/global.h shiboken2 +WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +COMMENT "Running generator for 'sample' test binding..." +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR} + ${SBK_PYTHON_INCLUDE_DIR} + ${libsample_SOURCE_DIR} + ${libshiboken_SOURCE_DIR}) +add_library(sample MODULE ${sample_SRC}) +set_property(TARGET sample PROPERTY PREFIX "") +if(WIN32) + set_property(TARGET sample PROPERTY SUFFIX ".pyd") +endif() +target_link_libraries(sample + libsample + ${SBK_PYTHON_LIBRARIES} + libshiboken) diff --git a/sources/shiboken2/tests/samplebinding/__del___test.py b/sources/shiboken2/tests/samplebinding/__del___test.py new file mode 100644 index 000000000..fd787b5f0 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/__del___test.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +import sample + +delCalled = False + +class MyObject(sample.ObjectType): + def __del__(self): + global delCalled + delCalled = True + +class TestDel(unittest.TestCase): + def testIt(self): + a = MyObject() + del a + self.assertTrue(delCalled) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/abstract_test.py b/sources/shiboken2/tests/samplebinding/abstract_test.py new file mode 100644 index 000000000..46aad3260 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/abstract_test.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Abstract class''' + +import sys +import unittest + +from sample import Abstract + +class Incomplete(Abstract): + def __init__(self): + Abstract.__init__(self) + +class Concrete(Abstract): + def __init__(self): + Abstract.__init__(self) + self.pure_virtual_called = False + self.unpure_virtual_called = False + + def pureVirtual(self): + self.pure_virtual_called = True + + def pureVirtualReturningVoidPtr(self): + return 42 + + def unpureVirtual(self): + self.unpure_virtual_called = True + + def virtualGettingAEnum(self, enum): + self.virtual_getting_enum = True + + +class AbstractTest(unittest.TestCase): + '''Test case for Abstract class''' + + def testAbstractPureVirtualMethodAvailability(self): + '''Test if Abstract class pure virtual method was properly wrapped.''' + self.assertTrue('pureVirtual' in dir(Abstract)) + + def testAbstractInstanciation(self): + '''Test if instanciation of an abstract class raises the correct exception.''' + self.assertRaises(NotImplementedError, Abstract) + + def testUnimplementedPureVirtualMethodCall(self): + '''Test if calling a pure virtual method raises the correct exception.''' + i = Incomplete() + self.assertRaises(NotImplementedError, i.pureVirtual) + + def testPureVirtualReturningVoidPtrReturnValue(self): + '''Test if a pure virtual method returning void ptr can be properly reimplemented''' + # Note that the semantics of reimplementing the pure virtual method in + # Python and calling it from C++ is undefined until it's decided how to + # cast the Python data types to void pointers + c = Concrete() + self.assertEqual(c.pureVirtualReturningVoidPtr(),42) + + def testReimplementedVirtualMethodCall(self): + '''Test if instanciation of an abstract class raises the correct exception.''' + i = Concrete() + self.assertRaises(NotImplementedError, i.callPureVirtual) + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a virtual method is correctly called from C++.''' + c = Concrete() + c.callUnpureVirtual() + self.assertTrue(c.unpure_virtual_called) + + def testImplementedPureVirtualMethodCall(self): + '''Test if a Python override of a pure virtual method is correctly called from C++.''' + c = Concrete() + c.callPureVirtual() + self.assertTrue(c.pure_virtual_called) + + def testEnumParameterOnVirtualMethodCall(self): + '''testEnumParameterOnVirtualMethodCall''' + c = Concrete() + c.callVirtualGettingEnum(Abstract.Short) + self.assertTrue(c.virtual_getting_enum) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/addedfunction_test.py b/sources/shiboken2/tests/samplebinding/addedfunction_test.py new file mode 100644 index 000000000..1620a5114 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/addedfunction_test.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''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/sources/shiboken2/tests/samplebinding/addedfunction_with_container_args_test.py b/sources/shiboken2/tests/samplebinding/addedfunction_with_container_args_test.py new file mode 100644 index 000000000..da2406698 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/addedfunction_with_container_args_test.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for added functions with nested and multi-argument container types.''' + +import unittest +from sample import sum2d, sumproduct + +class TestAddedFunctionsWithContainerArgs(unittest.TestCase): + '''Tests added functions with nested and multi-argument container types.''' + + def testNestedContainerType(self): + '''Test added function with single-argument containers.''' + values = [[1,2],[3,4,5],[6]] + self.assertEqual(sum2d(values), 21) + + def testMultiArgContainerType(self): + '''Test added function with a two-argument container.''' + values = [(1,2),(3,4),(5,6)] + self.assertEqual(sumproduct(values), 44) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/argumentmodifications_test.py b/sources/shiboken2/tests/samplebinding/argumentmodifications_test.py new file mode 100644 index 000000000..90db99f91 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/argumentmodifications_test.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for method arguments modifications performed as described on typesystem.''' + +import sys +import unittest + +from sample import Modifications, Point + +class ArgumentModificationsTest(unittest.TestCase): + '''Test cases for method arguments modifications performed as described on typesystem.''' + + def setUp(self): + self.mods = Modifications() + + def tearDown(self): + del self.mods + + def testArgRemoval0(self): + '''Tests argument removal modifications on Modifications.argRemoval0.''' + # void [-> PyObject*] argRemoval0(int, bool, int = 123 [removed, new val = 321], int = 456) + # code-injection: returns tuple with received parameters plus removed ones + a0, a1, a2 = 1, True, 2 + self.assertEqual(self.mods.argRemoval0(a0, a1), (a0, a1, 321, 456)) + self.assertEqual(self.mods.argRemoval0(a0, a1, a2), (a0, a1, 321, a2)) + # the other wasn't modified + # void argRemoval0(int, bool, int, bool) + self.assertEqual(self.mods.argRemoval0(0, False, 0, False), None) + + def testArgRemoval1(self): + '''Tests argument removal modifications on Modifications.argRemoval1.''' + # void [-> PyObject*] argRemoval1(int, bool, Point = Point(1, 2) [removed], Point = Point(3, 4) [removed], int = 333) + # code-injection: returns tuple with received parameters plus removed ones + a0, a1, a2 = 1, True, 2 + self.assertEqual(self.mods.argRemoval1(a0, a1), (a0, a1, Point(1, 2), Point(3, 4), 333)) + self.assertEqual(self.mods.argRemoval1(a0, a1, a2), (a0, a1, Point(1, 2), Point(3, 4), a2)) + # the other wasn't modified + # void argRemoval1(int, bool, int, bool) + self.assertEqual(self.mods.argRemoval1(0, False, 0, False), None) + + def testArgRemoval2(self): + '''Tests argument removal modifications on Modifications.argRemoval2.''' + # void [-> PyObject*] argRemoval2(int, bool, Point = Point(1, 2) [removed], Point = Point(3, 4) [removed], int = 333) + # code-injection: returns tuple with received parameters plus removed ones + a0, a1, a2 = 1, True, 2 + self.assertEqual(self.mods.argRemoval2(a0, a1), (a0, a1, Point(1, 2), Point(3, 4), 333)) + self.assertEqual(self.mods.argRemoval2(a0, a1, a2), (a0, a1, Point(1, 2), Point(3, 4), a2)) + + def testArgRemoval3(self): + '''Tests argument removal modifications on Modifications.argRemoval3.''' + # void [-> PyObject*] argRemoval3(int, Point = Point(1, 2) [removed], bool = true, Point = Point(3, 4) [removed], int = 333) + # code-injection: returns tuple with received parameters plus removed ones + a0, a1, a2 = 1, True, 2 + self.assertEqual(self.mods.argRemoval3(a0), (a0, Point(1, 2), True, Point(3, 4), 333)) + self.assertEqual(self.mods.argRemoval3(a0, a1), (a0, Point(1, 2), a1, Point(3, 4), 333)) + self.assertEqual(self.mods.argRemoval3(a0, a1, a2), (a0, Point(1, 2), a1, Point(3, 4), a2)) + + def testArgRemoval4(self): + '''Tests argument removal modifications on Modifications.argRemoval4.''' + # void [-> PyObject*] argRemoval4(int, Point [removed, new val = Point(6, 9)], bool, Point = Point(3, 4) [removed], int = 333) + # code-injection: returns tuple with received parameters plus removed ones + a0, a1, a2 = 1, True, 2 + self.assertRaises(TypeError, self.mods.argRemoval4, a0) + self.assertEqual(self.mods.argRemoval4(a0, a1), (a0, Point(6, 9), a1, Point(3, 4), 333)) + self.assertEqual(self.mods.argRemoval4(a0, a1, a2), (a0, Point(6, 9), a1, Point(3, 4), a2)) + + def testArgRemoval5(self): + '''Tests argument removal modifications on Modifications.argRemoval5.''' + # void [-> PyObject*] argRemoval5(int [removed, new val = 100], bool, + # Point = Point(1, 2) [removed], + # Point = Point(3, 4) [removed], int = 333) + # code-injection: returns tuple with received parameters plus removed ones + a0, a1, a2 = True, 2, True + self.assertEqual(self.mods.argRemoval5(a0), (100, a0, Point(1, 2), Point(3, 4), 333)) + self.assertEqual(self.mods.argRemoval5(a0, a1), (100, a0, Point(1, 2), Point(3, 4), a1)) + # void [-> PyObject*] argRemoval5(int [removed, new val = 200], bool, int, bool) + # code-injection: returns tuple with received parameters plus removed ones + self.assertEqual(self.mods.argRemoval5(a0, a1, a2), (200, a0, a1, a2)) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/bug_554_test.py b/sources/shiboken2/tests/samplebinding/bug_554_test.py new file mode 100644 index 000000000..0bcce92cf --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/bug_554_test.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Unit test for bug#554''' + +from sample import * + +class Bug554: + def crash(self): + class Crasher(ObjectType): + pass + +if __name__ == '__main__': + bug = Bug554() + bug.crash() + + diff --git a/sources/shiboken2/tests/samplebinding/bug_704_test.py b/sources/shiboken2/tests/samplebinding/bug_704_test.py new file mode 100644 index 000000000..6b393f3b1 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/bug_704_test.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from py3kcompat import IS_PY3K + +from sample import ObjectType + + +class NewStyle(object): + def name(self): + return "NewStyle" + +class OldStyle: + def name(self): + return "OldStyle" + +def defineNewStyle(): + class MyObjectNew(ObjectType, NewStyle): + pass + +def defineOldStyle(): + class MyObjectOld(ObjectType, OldStyle): + pass + +class ObjectTypeTest(unittest.TestCase): + '''Test cases to avoid declaring Shiboken classes with multiple inheritance from old style classes.''' + + def testObjectTypeNewStype(self): + defineNewStyle() + + def testObjectTypeOldStype(self): + # Py 3k doesn't have old style classes + if not IS_PY3K: + self.assertRaises(TypeError, defineOldStyle) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/bytearray_bufferprotocol.cpp b/sources/shiboken2/tests/samplebinding/bytearray_bufferprotocol.cpp new file mode 100644 index 000000000..39fb8d353 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/bytearray_bufferprotocol.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#if PY_VERSION_HEX < 0x03000000 +// ByteArray buffer protocol functions +// See: http://www.python.org/dev/peps/pep-3118/ +extern "C" { +static Py_ssize_t SbkByteArray_segcountproc(PyObject* self, Py_ssize_t* lenp) +{ + if (lenp) + *lenp = self->ob_type->tp_as_sequence->sq_length(self); + return 1; +} +static Py_ssize_t SbkByteArray_readbufferproc(PyObject* self, Py_ssize_t segment, void** ptrptr) +{ + if (segment || !Shiboken::Object::isValid(self)) + return -1; + + ByteArray* cppSelf = %CONVERTTOCPP[ByteArray*](self); + *ptrptr = reinterpret_cast<void*>(const_cast<char*>(cppSelf->data())); + return cppSelf->size(); +} +PyBufferProcs SbkByteArrayBufferProc = { + /*bf_getreadbuffer*/ &SbkByteArray_readbufferproc, + /*bf_getwritebuffer*/ (writebufferproc)&SbkByteArray_readbufferproc, + /*bf_getsegcount*/ &SbkByteArray_segcountproc, + /*bf_getcharbuffer*/ (charbufferproc)&SbkByteArray_readbufferproc +}; +} +#endif diff --git a/sources/shiboken2/tests/samplebinding/bytearray_test.py b/sources/shiboken2/tests/samplebinding/bytearray_test.py new file mode 100644 index 000000000..0e8039469 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/bytearray_test.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from os.path import isdir +from sample import ByteArray +from py3kcompat import b + + +class ByteArrayBufferProtocolTest(unittest.TestCase): + '''Tests ByteArray implementation of Python buffer protocol.''' + + def testByteArrayBufferProtocol(self): + # Tests ByteArray implementation of Python buffer protocol using the os.path.isdir + # function which an unicode object or other object implementing the Python buffer protocol. + isdir(str(ByteArray('/tmp'))) + + +class ByteArrayConcatenationOperatorTest(unittest.TestCase): + '''Test cases for ByteArray concatenation with '+' operator.''' + + def testConcatByteArrayAndPythonString(self): + # Test concatenation of a ByteArray with a Python string, in this order. + ba = ByteArray('foo') + result = ba + '\x00bar' + self.assertEqual(type(result), ByteArray) + self.assertEqual(result, 'foo\x00bar') + + def testConcatPythonStringAndByteArray(self): + # Test concatenation of a Python string with a ByteArray, in this order. + concat_python_string_add_qbytearray_worked = True + ba = ByteArray('foo') + result = 'bar\x00' + ba + self.assertEqual(type(result), ByteArray) + self.assertEqual(result, 'bar\x00foo') + + +class ByteArrayOperatorEqual(unittest.TestCase): + '''TestCase for operator ByteArray == ByteArray.''' + + def testDefault(self): + # ByteArray() == ByteArray() + obj1 = ByteArray() + obj2 = ByteArray() + self.assertEqual(obj1, obj2) + + def testSimple(self): + # ByteArray(some_string) == ByteArray(some_string) + string = 'egg snakes' + self.assertEqual(ByteArray(string), ByteArray(string)) + + def testPyString(self): + # ByteArray(string) == string + string = 'my test string' + self.assertEqual(ByteArray(string), string) + + def testQString(self): + # ByteArray(string) == string + string = 'another test string' + self.assertEqual(ByteArray(string), string) + + +class ByteArrayOperatorAt(unittest.TestCase): + '''TestCase for operator ByteArray[]''' + + def testInRange(self): + # ByteArray[x] where x is a valid index. + string = 'abcdefgh' + obj = ByteArray(string) + for i in range(len(string)): + self.assertEqual(obj[i], b(string[i])) + + def testInRangeReverse(self): + # ByteArray[x] where x is a valid index (reverse order). + string = 'abcdefgh' + obj = ByteArray(string) + for i in range(len(string)-1, 0, -1): + self.assertEqual(obj[i], b(string[i])) + + def testOutOfRange(self): + # ByteArray[x] where x is out of index. + string = '1234567' + obj = ByteArray(string) + self.assertRaises(IndexError, lambda :obj[len(string)]) + + def testNullStrings(self): + ba = ByteArray('\x00') + self.assertEqual(ba.at(0), '\x00') + self.assertEqual(ba[0], b('\x00')) + + +class ByteArrayOperatorLen(unittest.TestCase): + '''Test case for __len__ operator of ByteArray''' + + def testBasic(self): + '''ByteArray __len__''' + self.assertEqual(len(ByteArray()), 0) + self.assertEqual(len(ByteArray('')), 0) + self.assertEqual(len(ByteArray(' ')), 1) + self.assertEqual(len(ByteArray('yabadaba')), 8) + + +class ByteArrayAndPythonStr(unittest.TestCase): + '''Test case for __str__ operator of ByteArray''' + + def testStrOperator(self): + '''ByteArray __str__''' + self.assertEqual(ByteArray().__str__(), '') + self.assertEqual(ByteArray('').__str__(), '') + self.assertEqual(ByteArray('aaa').__str__(), 'aaa') + + def testPythonStrAndNull(self): + s1 = b('123\000321') + ba = ByteArray(s1) + s2 = ba.data() + self.assertEqual(s1, s2) + self.assertEqual(type(s1), type(s2)) + self.assertEqual(s1, ba) + self.assertNotEqual(type(s1), type(ba)) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/child_return_test.py b/sources/shiboken2/tests/samplebinding/child_return_test.py new file mode 100644 index 000000000..30859dc5a --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/child_return_test.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''The BlackBox class has cases of ownership transference between C++ and Python.''' + +import sys +import unittest + +from sample import * + +class ReturnOfChildTest(unittest.TestCase): + '''The BlackBox class has cases of ownership transference between C++ and Python.''' + + def testKillParentKeepingChild(self): + '''Ownership transference from Python to C++ and back again.''' + o1 = ObjectType.createWithChild() + child = o1.children()[0] + del o1 + self.assertRaises(RuntimeError, child.objectName) + + def testKillParentKeepingChild2(self): + '''Ownership transference from Python to C++ and back again.''' + o1 = ObjectType.createWithChild() + child = o1.findChild("child") + del o1 + self.assertRaises(RuntimeError, child.objectName) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/class_fields_test.py b/sources/shiboken2/tests/samplebinding/class_fields_test.py new file mode 100644 index 000000000..0a2491cf2 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/class_fields_test.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Simple test case for accessing the exposed C++ class fields.''' + +from sys import getrefcount +import unittest + +from sample import Derived, Point, ObjectType + +class TestAccessingCppFields(unittest.TestCase): + '''Simple test case for accessing the exposed C++ class fields.''' + + def testAccessingPrimitiveTypeField(self): + '''Reads and writes a primitive type (in this case an 'int') field.''' + d = Derived() + self.assertEqual(type(d.primitiveField), int) + + # attribution + old_value = d.primitiveField + new_value = 2255 + d.primitiveField = new_value + self.assertEqual(d.primitiveField, new_value) + self.assertNotEqual(d.primitiveField, old_value) + + # attribution with a convertible type + value = 1.2 + d.primitiveField = value + self.assertEqual(d.primitiveField, int(value)) + + # attribution with invalid type + self.assertRaises(TypeError, lambda : setattr(d, 'primitiveField', None)) + + def testAccessingUsersPrimitiveTypeField(self): + '''Reads and writes an user's primitive type (in this case an 'Complex') field.''' + d = Derived() + self.assertEqual(type(d.userPrimitiveField), complex) + + # attribution + old_value = d.userPrimitiveField + new_value = complex(1.1, 2.2) + d.userPrimitiveField = new_value + self.assertEqual(d.userPrimitiveField, new_value) + self.assertNotEqual(d.userPrimitiveField, old_value) + + # attribution with invalid type + self.assertRaises(TypeError, lambda : setattr(d, 'userPrimitiveField', None)) + + def testAccessingValueTypeField(self): + '''Reads and writes a value type (in this case a 'Point') field.''' + d = Derived() + self.assertEqual(type(d.valueTypeField), Point) + + # attribution + old_value = d.valueTypeField + new_value = Point(-10, 537) + d.valueTypeField = new_value + self.assertEqual(d.valueTypeField, new_value) + + #object modify + d.valueTypeField.setX(10) + d.valueTypeField.setY(20) + self.assertEqual(d.valueTypeField.x(), 10) + self.assertEqual(d.valueTypeField.y(), 20) + + # attribution with invalid type + self.assertRaises(TypeError, lambda : setattr(d, 'valueTypeField', 123)) + + def testAccessingObjectTypeField(self): + '''Reads and writes a object type (in this case an 'ObjectType') field.''' + d = Derived() + + # attribution + old_value = d.objectTypeField + new_value = ObjectType() + d.objectTypeField = new_value + self.assertEqual(d.objectTypeField, new_value) + self.assertNotEqual(d.objectTypeField, old_value) + + # attribution with a convertible type + value = None + d.objectTypeField = value + self.assertEqual(d.objectTypeField, value) + + # attribution with invalid type + self.assertRaises(TypeError, lambda : setattr(d, 'objectTypeField', 123)) + + def testRefCountingAccessingObjectTypeField(self): + '''Accessing a object type field should respect the reference counting rules.''' + d = Derived() + + # attributing object to instance's field should increase its reference count + o1 = ObjectType() + refcount1 = getrefcount(o1) + d.objectTypeField = o1 + self.assertEqual(d.objectTypeField, o1) + self.assertEqual(getrefcount(d.objectTypeField), refcount1 + 1) + + # attributing a new object to instance's field should decrease the previous object's reference count + o2 = ObjectType() + refcount2 = getrefcount(o2) + d.objectTypeField = o2 + self.assertEqual(d.objectTypeField, o2) + self.assertEqual(getrefcount(o1), refcount1) + self.assertEqual(getrefcount(d.objectTypeField), refcount2 + 1) + + def testRefCountingOfReferredObjectAfterDeletingReferrer(self): + '''Deleting the object referring to other object should decrease the reference count of the referee.''' + d = Derived() + o = ObjectType() + refcount = getrefcount(o) + d.objectTypeField = o + self.assertEqual(getrefcount(o), refcount + 1) + del d + self.assertEqual(getrefcount(o), refcount) + + def testStaticField(self): + self.assertEqual(Derived.staticPrimitiveField, 0) + + def testAccessingUnsignedIntBitField(self): + d = Derived() + + # attribution + old_value = d.bitField + new_value = 1 + d.bitField= new_value + self.assertEqual(d.bitField, new_value) + self.assertNotEqual(d.bitField, old_value) + + # attribution with a convertible type + value = 1.2 + d.bitField = value + self.assertEqual(d.bitField, int(value)) + + # attribution with invalid type + self.assertRaises(TypeError, lambda : setattr(d, 'bitField', None)) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/collector_test.py b/sources/shiboken2/tests/samplebinding/collector_test.py new file mode 100644 index 000000000..cbd6326e6 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/collector_test.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Collector class' shift operators.''' + +import sys +import unittest + +from sample import Collector, IntWrapper, ObjectType + + +class CollectorTest(unittest.TestCase): + '''Test cases for Collector class' shift operators.''' + + def testLShiftOperatorSingleUse(self): + '''Test case for using the Collector.__lshift__ operator just one time.''' + collector = Collector() + collector << 13 + self.assertEqual(collector.size(), 1) + self.assertEqual(collector.items(), [13]) + + def testLShiftOperatorMultipleUses(self): + '''Test case for using the Collector.__lshift__ operator many times in the same line.''' + collector = Collector() + collector << 2 << 3 << 5 << 7 << 11 + self.assertEqual(collector.size(), 5) + self.assertEqual(collector.items(), [2, 3, 5, 7, 11]) + +class CollectorExternalOperator(unittest.TestCase): + '''Test cases for external operators of Collector''' + + def testLShiftExternal(self): + '''Collector external operator''' + collector = Collector() + collector << IntWrapper(5) + self.assertEqual(collector.size(), 1) + self.assertEqual(collector.items(), [5]) + + +class CollectorObjectType(unittest.TestCase): + '''Test cases for Collector ObjectType''' + + def testBasic(self): + '''Collector << ObjectType # greedy collector''' + collector = Collector() + obj = ObjectType() + collector << obj + self.assertEqual(collector.items()[0], obj.identifier()) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/complex_test.py b/sources/shiboken2/tests/samplebinding/complex_test.py new file mode 100644 index 000000000..3a14f4d15 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/complex_test.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Complex class''' + +import sys +import unittest + +import sample +from sample import Point + +class ComplexTest(unittest.TestCase): + '''Test case for conversions between C++ Complex class to Python complex class''' + + def testFunctionReturningComplexObject(self): + '''Test function returning a C++ Complex object.''' + cpx = sample.transmutePointIntoComplex(Point(5.0, 2.3)) + self.assertEqual(cpx, complex(5.0, 2.3)) + + def testFunctionReceivingComplexObjectAsArgument(self): + '''Test function returning a C++ Complex object.''' + pt = sample.transmuteComplexIntoPoint(complex(1.2, 3.4)) + # these assertions intentionally avoids to test the == operator, + # it should have its own test cases. + self.assertEqual(pt.x(), 1.2) + self.assertEqual(pt.y(), 3.4) + + def testComplexList(self): + '''Test list of C++ Complex objects conversion to a list of Python complex objects.''' + # the global function gimmeComplexList() is expected to return a list + # containing the following Complex values: [0j, 1.1+2.2j, 1.3+2.4j] + cpxlist = sample.gimmeComplexList() + self.assertEqual(cpxlist, [complex(), complex(1.1, 2.2), complex(1.3, 2.4)]) + + def testSumComplexPair(self): + '''Test sum of a tuple containing two complex objects.''' + cpx1 = complex(1.2, 3.4) + cpx2 = complex(5.6, 7.8) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), cpx1 + cpx2) + + def testUsingTuples(self): + cpx1, cpx2 = (1.2, 3.4), (5.6, 7.8) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), sample.sumComplexPair((complex(*cpx1), complex(*cpx2)))) + cpx1, cpx2 = (1, 3), (5, 7) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), sample.sumComplexPair((complex(*cpx1), complex(*cpx2)))) + cpx1, cpx2 = (1.2, 3), (5.6, 7) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), sample.sumComplexPair((complex(*cpx1), complex(*cpx2)))) + cpx1, cpx2 = (1, 2, 3), (4, 5, 7) + self.assertRaises(TypeError, sample.sumComplexPair, (cpx1, cpx2)) + cpx1, cpx2 = ('1', '2'), ('4', '5') + self.assertRaises(TypeError, sample.sumComplexPair, (cpx1, cpx2)) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/conversion_operator_test.py b/sources/shiboken2/tests/samplebinding/conversion_operator_test.py new file mode 100644 index 000000000..01ac102e8 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/conversion_operator_test.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for implicit conversion generated by conversion operator.''' + +import unittest + +from sample import Time, StrList + +class ConversionOperatorTest(unittest.TestCase): + '''Test cases for implicit conversion generated by conversion operator.''' + + def testConversionOperator(self): + '''Time defined an conversion operator for Str, so passing a Time object to a method expecting a Str should work.''' + t = Time(1, 2, 3) + t_str = t.toString() + sl = StrList() + + # StrList.append expects a Str object. + sl.append(t) + + self.assertEqual(len(sl), 1) + self.assertEqual(sl[0], t_str) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/copy_test.py b/sources/shiboken2/tests/samplebinding/copy_test.py new file mode 100644 index 000000000..09c8e4cbc --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/copy_test.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for deep copy of objects''' + +import copy +import unittest + +try: + import cPickle as pickle +except ImportError: + import pickle + + +from sample import Point + + +class SimpleCopy(unittest.TestCase): + '''Simple copy of objects''' + + def testCopy(self): + point = Point(0.1, 2.4) + new_point = copy.copy(point) + + self.assertTrue(point is not new_point) + self.assertEqual(point, new_point) + + +class DeepCopy(unittest.TestCase): + '''Deep copy with shiboken objects''' + + def testDeepCopy(self): + '''Deep copy of value types''' + point = Point(3.1, 4.2) + new_point = copy.deepcopy([point])[0] + + self.assertTrue(point is not new_point) + self.assertEqual(point, new_point) + + +class PicklingTest(unittest.TestCase): + '''Support pickling''' + + def testSimple(self): + '''Simple pickling and unpickling''' + + point = Point(10.2, 43.5) + + data = pickle.dumps(point) + new_point = pickle.loads(data) + + self.assertEqual(point, new_point) + self.assertTrue(point is not new_point) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ctorconvrule_test.py b/sources/shiboken2/tests/samplebinding/ctorconvrule_test.py new file mode 100644 index 000000000..d1ebdc9d3 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ctorconvrule_test.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for proper generation of constructor altered by conversion-rule tag.''' + +import unittest + +from sample import CtorConvRule + +class TestCtorConvRule(unittest.TestCase): + '''Simple test case for CtorConvRule''' + + def testCtorConvRule(self): + '''Test CtorConvRule argument modification through conversion-rule tag.''' + value = 123 + obj = CtorConvRule(value) + self.assertEqual(obj.value(), value + 1) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/cyclic_test.py b/sources/shiboken2/tests/samplebinding/cyclic_test.py new file mode 100644 index 000000000..e92b3605f --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/cyclic_test.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import ObjectType +from sample import ObjectView +from sample import ObjectModel + + + +class ObjTest(unittest.TestCase): + + def test_cyclic_dependency_withParent(self): + """Create 2 objects with a cyclic dependency, so that they can + only be removed by the garbage collector, and then invoke the + garbage collector in a different thread. + """ + import gc + + class CyclicChildObject(ObjectType): + def __init__(self, parent): + super(CyclicChildObject, self).__init__(parent) + self._parent = parent + + class CyclicObject(ObjectType): + def __init__(self): + super(CyclicObject, self).__init__() + CyclicChildObject(self) + + # turn off automatic garbage collection, to be able to trigger it + # at the 'right' time + gc.disable() + alive = lambda :sum(isinstance(o, CyclicObject) for o in gc.get_objects() ) + + # + # first proof that the wizard is only destructed by the garbage + # collector + # + cycle = CyclicObject() + self.assertTrue(alive()) + del cycle + self.assertTrue(alive()) + gc.collect() + self.assertFalse(alive()) + + def test_cyclic_dependency_withKeepRef(self): + """Create 2 objects with a cyclic dependency, so that they can + only be removed by the garbage collector, and then invoke the + garbage collector in a different thread. + """ + import gc + + class CyclicChildObject(ObjectView): + def __init__(self, model): + super(CyclicChildObject, self).__init__(None) + self.setModel(model) + + class CyclicObject(ObjectModel): + def __init__(self): + super(CyclicObject, self).__init__() + self._view = CyclicChildObject(self) + + # turn off automatic garbage collection, to be able to trigger it + # at the 'right' time + gc.disable() + alive = lambda :sum(isinstance(o, CyclicObject) for o in gc.get_objects() ) + + # + # first proof that the wizard is only destructed by the garbage + # collector + # + cycle = CyclicObject() + self.assertTrue(alive()) + del cycle + self.assertTrue(alive()) + gc.collect() + self.assertFalse(alive()) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/date_test.py b/sources/shiboken2/tests/samplebinding/date_test.py new file mode 100644 index 000000000..800774a57 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/date_test.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for python conversions types ''' + +import sys +import unittest +from datetime import date + +from sample import SbkDate + +class DateConversionTest(unittest.TestCase): + + def testConstructorWithDateObject(self): + pyDate = date(2010, 12, 12) + cDate = SbkDate(pyDate) + self.assertTrue(cDate.day(), pyDate.day) + self.assertTrue(cDate.month(), pyDate.month) + self.assertTrue(cDate.year(), pyDate.year) + + def testToPythonFunction(self): + cDate = SbkDate(2010, 12, 12) + pyDate = cDate.toPython() + self.assertTrue(cDate.day(), pyDate.day) + self.assertTrue(cDate.month(), pyDate.month) + self.assertTrue(cDate.year(), pyDate.year) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/decisor_test.py b/sources/shiboken2/tests/samplebinding/decisor_test.py new file mode 100644 index 000000000..33f41b9ff --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/decisor_test.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for the method overload decisor.''' + +import unittest + +from sample import SampleNamespace, Point, ObjectType, ObjectModel + +class DecisorTest(unittest.TestCase): + '''Test cases for the method overload decisor.''' + + def testCallWithInvalidParametersSideA(self): + '''Call a method missing with the last argument missing. + This can trigger the bug #262, which means using an argument + not provided by the user.''' + pt = Point() + self.assertRaises(TypeError, SampleNamespace.forceDecisorSideA, pt) + + def testCallWithInvalidParametersSideB(self): + '''Same as the previous test, but with an integer as first argument, + just to complicate things for the overload method decisor.''' + pt = Point() + self.assertRaises(TypeError, SampleNamespace.forceDecisorSideB, 1, pt) + + def testDecideCallWithInheritance(self): + '''Call methods overloads that receive parent and inheritor classes' instances.''' + objecttype = ObjectType() + objectmodel = ObjectModel() + self.assertEqual(ObjectModel.receivesObjectTypeFamily(objecttype), ObjectModel.ObjectTypeCalled) + self.assertNotEqual(ObjectModel.receivesObjectTypeFamily(objecttype), ObjectModel.ObjectModelCalled) + self.assertEqual(ObjectModel.receivesObjectTypeFamily(objectmodel), ObjectModel.ObjectModelCalled) + self.assertNotEqual(ObjectModel.receivesObjectTypeFamily(objectmodel), ObjectModel.ObjectTypeCalled) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/delete_test.py b/sources/shiboken2/tests/samplebinding/delete_test.py new file mode 100644 index 000000000..bc20ea234 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/delete_test.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +import sample +import shiboken2 as shiboken + +class DeleteTest(unittest.TestCase): + def testNonCppWrapperClassDelete(self): + """Would segfault when shiboken.delete called on obj not created from + Python """ + obj = sample.ObjectType() + child = obj.createChild(None) + shiboken.delete(child) + assert not shiboken.isValid(child) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/deprecated_test.py b/sources/shiboken2/tests/samplebinding/deprecated_test.py new file mode 100644 index 000000000..6c850cd10 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/deprecated_test.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import warnings +import unittest + +from sample import ObjectType + +class TestDeprecatedCall(unittest.TestCase): + def testCallWithError(self): + o = ObjectType() + warnings.simplefilter('error') + self.assertRaises(DeprecationWarning, o.deprecatedFunction) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/derived_test.py b/sources/shiboken2/tests/samplebinding/derived_test.py new file mode 100644 index 000000000..b19835fef --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/derived_test.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Derived class''' + +import sys +import unittest + +import sample +from sample import Abstract, Derived, OverloadedFuncEnum + +class Deviant(Derived): + def __init__(self): + Derived.__init__(self) + self.pure_virtual_called = False + self.unpure_virtual_called = False + + def pureVirtual(self): + self.pure_virtual_called = True + + def unpureVirtual(self): + self.unpure_virtual_called = True + + def className(self): + return 'Deviant' + +class DerivedTest(unittest.TestCase): + '''Test case for Derived class''' + + def testParentClassMethodsAvailability(self): + '''Test if Derived class really inherits its methods from parent.''' + inherited_methods = set(['callPureVirtual', 'callUnpureVirtual', + 'id_', 'pureVirtual', 'unpureVirtual']) + self.assertTrue(inherited_methods.issubset(dir(Derived))) + + def testOverloadedMethodCall(self): + '''Test if the correct overloaded method is being called.''' + derived = Derived() + + result = derived.overloaded(1, 2) + self.assertEqual(type(result), OverloadedFuncEnum) + self.assertEqual(result, sample.OverloadedFunc_ii) + + result = derived.overloaded(3) + self.assertEqual(type(result), OverloadedFuncEnum) + self.assertEqual(result, sample.OverloadedFunc_ii) + + result = derived.overloaded(4.4) + self.assertEqual(type(result), OverloadedFuncEnum) + self.assertEqual(result, sample.OverloadedFunc_d) + + def testOtherOverloadedMethodCall(self): + '''Another test to check overloaded method calling, just to double check.''' + derived = Derived() + + result = derived.otherOverloaded(1, 2, True, 3.3) + self.assertEqual(type(result), Derived.OtherOverloadedFuncEnum) + self.assertEqual(result, sample.Derived.OtherOverloadedFunc_iibd) + + result = derived.otherOverloaded(1, 2.2) + self.assertEqual(type(result), Derived.OtherOverloadedFuncEnum) + self.assertEqual(result, Derived.OtherOverloadedFunc_id) + + def testOverloadedMethodCallWithDifferentNumericTypes(self): + '''Test if the correct overloaded method accepts a different numeric type as argument.''' + derived = Derived() + result = derived.overloaded(1.1, 2.2) + self.assertEqual(type(result), OverloadedFuncEnum) + self.assertEqual(result, sample.OverloadedFunc_ii) + + def testOverloadedMethodCallWithWrongNumberOfArguments(self): + '''Test if a call to an overloaded method with the wrong number of arguments raises an exception.''' + derived = Derived() + self.assertRaises(TypeError, derived.otherOverloaded, 1, 2, True) + + def testReimplementedPureVirtualMethodCall(self): + '''Test if a Python override of a implemented pure virtual method is correctly called from C++.''' + d = Deviant() + d.callPureVirtual() + self.assertTrue(d.pure_virtual_called) + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a reimplemented virtual method is correctly called from C++.''' + d = Deviant() + d.callUnpureVirtual() + self.assertTrue(d.unpure_virtual_called) + + def testVirtualMethodCallString(self): + '''Test virtual method call returning string.''' + d = Derived() + self.assertEqual(d.className(), 'Derived') + self.assertEqual(d.getClassName(), 'Derived') + + def testReimplementedVirtualMethodCallReturningString(self): + '''Test if a Python override of a reimplemented virtual method is correctly called from C++.''' + d = Deviant() + self.assertEqual(d.className(), 'Deviant') + self.assertEqual(d.getClassName(), 'Deviant') + + def testSingleArgument(self): + '''Test singleArgument call.''' + d = Derived() + self.assertTrue(d.singleArgument(False)) + self.assertTrue(not d.singleArgument(True)) + + def testMethodCallWithDefaultValue(self): + '''Test method call with default value.''' + d = Derived() + self.assertEqual(d.defaultValue(3), 3.1) + self.assertEqual(d.defaultValue(), 0.1) + + def testCallToMethodWithAbstractArgument(self): + '''Call to method that expects an Abstract argument.''' + objId = 123 + d = Derived(objId) + self.assertEqual(Abstract.getObjectId(d), objId) + + def testObjectCreationWithParentType(self): + '''Derived class creates an instance of itself in C++ and returns it as a pointer to its ancestor Abstract.''' + obj = Derived.createObject() + self.assertEqual(type(obj), Derived) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/duck_punching_test.py b/sources/shiboken2/tests/samplebinding/duck_punching_test.py new file mode 100644 index 000000000..fd60a96af --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/duck_punching_test.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for virtual methods.''' + +import types +import unittest +from py3kcompat import IS_PY3K + +from sample import VirtualMethods, SimpleFile, Point + +def MethodTypeCompat(func, instance): + if IS_PY3K: + return types.MethodType(func, instance) + else: + return types.MethodType(func, instance, type(instance)) + +class Duck(VirtualMethods): + def __init__(self): + VirtualMethods.__init__(self) + +class Monkey(SimpleFile): + def __init__(self, filename): + SimpleFile.__init__(self, filename) + +class DuckPunchingTest(unittest.TestCase): + '''Test case for duck punching (aka "monkey patching").''' + + def setUp(self): + self.multiplier = 2.0 + self.duck_method_called = False + self.call_counter = 0 + + def testMonkeyPatchOnVirtualMethod(self): + '''Injects new 'virtualMethod0' on a VirtualMethods instance and makes C++ call it.''' + vm = VirtualMethods() + pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True + + result1 = vm.virtualMethod0(pt, val, cpx, b) + result2 = vm.callVirtualMethod0(pt, val, cpx, b) + self.assertEqual(result1, result2) + self.assertEqual(result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b)) + + def myVirtualMethod0(obj, pt, val, cpx, b): + self.duck_method_called = True + return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier + vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm) + + result1 = vm.callVirtualMethod0(pt, val, cpx, b) + self.assertTrue(self.duck_method_called) + + result2 = vm.virtualMethod0(pt, val, cpx, b) + self.assertEqual(result1, result2) + self.assertEqual(result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b) * self.multiplier) + + # This is done to decrease the refcount of the vm object + # allowing the object wrapper to be deleted before the + # BindingManager. This is useful when compiling Shiboken + # for debug, since the BindingManager destructor has an + # assert that checks if the wrapper mapper is empty. + vm.virtualMethod0 = None + + def testMonkeyPatchOnVirtualMethodWithInheritance(self): + '''Injects new 'virtualMethod0' on an object that inherits from VirtualMethods and makes C++ call it.''' + duck = Duck() + pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True + + result1 = duck.virtualMethod0(pt, val, cpx, b) + result2 = duck.callVirtualMethod0(pt, val, cpx, b) + self.assertEqual(result1, result2) + self.assertEqual(result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b)) + + def myVirtualMethod0(obj, pt, val, cpx, b): + self.duck_method_called = True + return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier + duck.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, duck) + + result1 = duck.callVirtualMethod0(pt, val, cpx, b) + self.assertTrue(self.duck_method_called) + + result2 = duck.virtualMethod0(pt, val, cpx, b) + self.assertEqual(result1, result2) + self.assertEqual(result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b) * self.multiplier) + + duck.virtualMethod0 = None + + def testMonkeyPatchOnMethodWithStaticAndNonStaticOverloads(self): + '''Injects new 'exists' on a SimpleFile instance and makes C++ call it.''' + simplefile = SimpleFile('foobar') + + # Static 'exists' + simplefile.exists('sbrubbles') + self.assertFalse(self.duck_method_called) + # Non-static 'exists' + simplefile.exists() + self.assertFalse(self.duck_method_called) + + def myExists(obj): + self.duck_method_called = True + return False + simplefile.exists = MethodTypeCompat(myExists, simplefile) + + # Static 'exists' was overridden by the monkey patch, which accepts 0 arguments + self.assertRaises(TypeError, simplefile.exists, 'sbrubbles') + # Monkey patched exists + simplefile.exists() + self.assertTrue(self.duck_method_called) + + simplefile.exists = None + + def testMonkeyPatchOnMethodWithStaticAndNonStaticOverloadsWithInheritance(self): + '''Injects new 'exists' on an object that inherits from SimpleFile and makes C++ call it.''' + monkey = Monkey('foobar') + + # Static 'exists' + monkey.exists('sbrubbles') + self.assertFalse(self.duck_method_called) + # Non-static 'exists' + monkey.exists() + self.assertFalse(self.duck_method_called) + + def myExists(obj): + self.duck_method_called = True + return False + monkey.exists = MethodTypeCompat(myExists, monkey) + + # Static 'exists' was overridden by the monkey patch, which accepts 0 arguments + self.assertRaises(TypeError, monkey.exists, 'sbrubbles') + # Monkey patched exists + monkey.exists() + self.assertTrue(self.duck_method_called) + + monkey.exists = None + + def testForInfiniteRecursion(self): + def myVirtualMethod0(obj, pt, val, cpx, b): + self.call_counter += 1 + return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) + vm = VirtualMethods() + vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm) + pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True + vm.virtualMethod0(pt, val, cpx, b) + self.assertEqual(self.call_counter, 1) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/echo_test.py b/sources/shiboken2/tests/samplebinding/echo_test.py new file mode 100644 index 000000000..1b0b6a2cc --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/echo_test.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for <add-function> with const char* as argument''' + +import unittest + +from sample import Echo + +class TestEcho(unittest.TestCase): + '''Simple test case for Echo.echo''' + + def testEcho(self): + '''Test function added with const char * as arg''' + x = 'Foobar' + y = Echo().echo(x) + self.assertEqual(x, y) + + def testCallOperator(self): + e = Echo() + self.assertEqual(e("Hello", 3), "Hello3"); +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/enum_test.py b/sources/shiboken2/tests/samplebinding/enum_test.py new file mode 100644 index 000000000..6468d3cc4 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/enum_test.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Python representation of C++ enums.''' + +import os +import sys +import unittest + +import sample +from sample import SampleNamespace, ObjectType, Event +from py3kcompat import IS_PY3K, b + +def createTempFile(): + if sys.version_info >= (2, 6): + import tempfile + return tempfile.SpooledTemporaryFile(mode='rw') + else: + return os.tmpfile() + +class EnumTest(unittest.TestCase): + '''Test case for Python representation of C++ enums.''' + + def testEnumRepr(self): + enum = SampleNamespace.Option(1) + self.assertEqual(eval(repr(enum)), enum) + + enum = SampleNamespace.Option(999) + self.assertEqual(eval(repr(enum)), enum) + + def testHashability(self): + self.assertEqual(hash(SampleNamespace.TwoIn), hash(SampleNamespace.TwoOut)) + self.assertNotEqual(hash(SampleNamespace.TwoIn), hash(SampleNamespace.OneIn)) + + def testEnumValuesInsideEnum(self): + '''Enum values should be accessible inside the enum as well as outside.''' + for value_name in SampleNamespace.Option.values: + enum_item1 = getattr(SampleNamespace.Option, value_name) + enum_item2 = getattr(SampleNamespace, value_name) + self.assertEqual(enum_item1, enum_item2) + + def testPassingIntegerOnEnumArgument(self): + '''Tries to use an integer in place of an enum argument.''' + self.assertRaises(TypeError, SampleNamespace.getNumber, 1) + + def testBuildingEnumFromIntegerValue(self): + '''Tries to build the proper enum using an integer.''' + SampleNamespace.getNumber(SampleNamespace.Option(1)) + + def testBuildingEnumWithDefaultValue(self): + '''Enum constructor with default value''' + enum = SampleNamespace.Option() + self.assertEqual(enum, SampleNamespace.None_) + + def testEnumConversionToAndFromPython(self): + '''Conversion of enum objects from Python to C++ back again.''' + enumout = SampleNamespace.enumInEnumOut(SampleNamespace.TwoIn) + self.assertTrue(enumout, SampleNamespace.TwoOut) + self.assertEqual(repr(enumout), repr(SampleNamespace.TwoOut)) + + def testEnumConstructorWithTooManyParameters(self): + '''Calling the constructor of non-extensible enum with the wrong number of parameters.''' + self.assertRaises(TypeError, SampleNamespace.InValue, 13, 14) + + def testEnumConstructorWithNonNumberParameter(self): + '''Calling the constructor of non-extensible enum with a string.''' + self.assertRaises(TypeError, SampleNamespace.InValue, '1') + + def testEnumItemAsDefaultValueToIntArgument(self): + '''Calls function with an enum item as default value to an int argument.''' + self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(), SampleNamespace.ZeroIn) + self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(SampleNamespace.ZeroOut), SampleNamespace.ZeroOut) + self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(123), 123) + + def testAnonymousGlobalEnums(self): + '''Checks availability of anonymous global enum items.''' + self.assertEqual(sample.AnonymousGlobalEnum_Value0, 0) + self.assertEqual(sample.AnonymousGlobalEnum_Value1, 1) + + def testAnonymousClassEnums(self): + '''Checks availability of anonymous class enum items.''' + self.assertEqual(SampleNamespace.AnonymousClassEnum_Value0, 0) + self.assertEqual(SampleNamespace.AnonymousClassEnum_Value1, 1) + + def testEnumTpPrintImplementation(self): + '''Without SbkEnum.tp_print 'print' returns the enum represented as an int.''' + tmpfile = createTempFile() + if IS_PY3K: + from py3k import printToFile + printToFile(tmpfile, Event.ANY_EVENT) + else: + sys.stdout = tmpfile + print(Event.ANY_EVENT) + sys.stdout = sys.__stdout__ + tmpfile.seek(0) + text = tmpfile.read().strip() + tmpfile.close() + self.assertEqual(text, str(Event.ANY_EVENT)) + self.assertEqual(text, repr(Event.ANY_EVENT)) + + def testEnumArgumentWithDefaultValue(self): + '''Option enumArgumentWithDefaultValue(Option opt = UnixTime);''' + self.assertEqual(SampleNamespace.enumArgumentWithDefaultValue(), SampleNamespace.UnixTime) + self.assertEqual(SampleNamespace.enumArgumentWithDefaultValue(SampleNamespace.RandomNumber), SampleNamespace.RandomNumber) + + +class MyEvent(Event): + def __init__(self): + Event.__init__(self, Event.EventType(999)) + +class OutOfBoundsTest(unittest.TestCase): + def testValue(self): + e = MyEvent() + self.assertEqual(repr(e.eventType()), 'sample.Event.EventType(999)') + + def testNoneName(self): + e = MyEvent() + t = e.eventType() + self.assertEqual(t.name, None) + +class EnumOverloadTest(unittest.TestCase): + '''Test case for overloads involving enums''' + + def testWithInt(self): + '''Overload with Enums and ints with default value''' + o = ObjectType() + + self.assertEqual(o.callWithEnum('', Event.ANY_EVENT, 9), 81) + self.assertEqual(o.callWithEnum('', 9), 9) + +class EnumOperators(unittest.TestCase): + '''Test case for operations on enums''' + + def testInequalitySameObject(self): + self.assertFalse(Event.ANY_EVENT != Event.ANY_EVENT) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py b/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py new file mode 100644 index 000000000..820e21f7c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +import sample + +class TestEnumFromRemovedNamespace(unittest.TestCase): + def testEnumPromotedToGlobal(self): + sample.RemovedNamespace1_Enum + sample.RemovedNamespace1_Enum_Value0 + sample.RemovedNamespace1_AnonymousEnum_Value0 + sample.RemovedNamespace2_Enum + sample.RemovedNamespace2_Enum_Value0 + + def testEnumPromotedToUpperNamespace(self): + sample.UnremovedNamespace + sample.UnremovedNamespace.RemovedNamespace3_Enum + sample.UnremovedNamespace.RemovedNamespace3_Enum_Value0 + sample.UnremovedNamespace.RemovedNamespace3_AnonymousEnum_Value0 + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/event_loop_call_virtual_test.py b/sources/shiboken2/tests/samplebinding/event_loop_call_virtual_test.py new file mode 100644 index 000000000..d8ed028c4 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/event_loop_call_virtual_test.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Simple event loop dispatcher test.''' + +import time +import unittest +from random import random + +from sample import ObjectType, Event + + +class NoOverride(ObjectType): + + pass + + +class Override(ObjectType): + + def __init__(self): + ObjectType.__init__(self) + self.called = False + + def event(self, event): + self.called = True + return True + + +class TestEventLoop(unittest.TestCase): + + def testEventLoop(self): + '''Calling virtuals in a event loop''' + objs = [ObjectType(), NoOverride(), Override()] + + evaluated = ObjectType.processEvent(objs, + Event(Event.BASIC_EVENT)) + + self.assertEqual(evaluated, 3) + self.assertTrue(objs[2].called) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/event_loop_thread_test.py b/sources/shiboken2/tests/samplebinding/event_loop_thread_test.py new file mode 100644 index 000000000..8211ecd15 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/event_loop_thread_test.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import time +import threading +import unittest +from random import random + +from sample import ObjectType, Event + + +class Producer(ObjectType): + + def __init__(self): + ObjectType.__init__(self) + self.data = None + self.read = False + + def event(self, event): + self.data = random() + + while not self.read: + time.sleep(0.01) + + return True + + +class Collector(threading.Thread): + + def __init__(self, objects): + threading.Thread.__init__(self) + self.max_runs = len(objects) + self.objects = objects + self.data = [] + + def run(self): + i = 0 + while i < self.max_runs: + if self.objects[i].data is not None: + self.data.append(self.objects[i].data) + self.objects[i].read = True + i += 1 + time.sleep(0.01) + + +class TestEventLoopWithThread(unittest.TestCase): + '''Communication between a python thread and an simple + event loop in C++''' + + def testBasic(self): + '''Allowing threads and calling virtuals from C++''' + number = 10 + objs = [Producer() for x in range(number)] + thread = Collector(objs) + + thread.start() + + evaluated = ObjectType.processEvent(objs, + Event(Event.BASIC_EVENT)) + + thread.join() + + producer_data = [x.data for x in objs] + self.assertEqual(evaluated, number) + self.assertEqual(producer_data, thread.data) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/filter_test.py b/sources/shiboken2/tests/samplebinding/filter_test.py new file mode 100644 index 000000000..eb84a932c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/filter_test.py @@ -0,0 +1,45 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import Data, Intersection, Union + +class TestFilters(unittest.TestCase): + + def testAnd(self): + + f1 = Data(Data.Name, "joe") + f2 = Union() + + inter = f1 & f2 + + self.assertEqual(type(inter), Intersection) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/global.h b/sources/shiboken2/tests/samplebinding/global.h new file mode 100644 index 000000000..d273de961 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/global.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "abstract.h" +#include "blackbox.h" +#include "bytearray.h" +#include "bucket.h" +#include "collector.h" +#include "complex.h" +#include "ctorconvrule.h" +#include "cvlist.h" +#include "sbkdate.h" +#include "derived.h" +#include "echo.h" +#include "functions.h" +#include "implicitconv.h" +#include "overloadsort.h" +#include "handle.h" +#include "injectcode.h" +#include "list.h" +#include "listuser.h" +#include "mapuser.h" +#include "modelindex.h" +#include "modifications.h" +#include "modified_constructor.h" +#include "multiple_derived.h" +#include "noimplicitconversion.h" +#include "nondefaultctor.h" +#include "objectmodel.h" +#include "objecttype.h" +#include "objecttypebyvalue.h" +#include "objecttypeholder.h" +#include "objecttypelayout.h" +#include "objecttypeoperators.h" +#include "objectview.h" +#include "oddbool.h" +#include "onlycopy.h" +#include "overload.h" +#include "pairuser.h" +#include "pen.h" +#include "photon.h" +#include "point.h" +#include "pointf.h" +#include "pointerholder.h" +#include "polygon.h" +#include "privatector.h" +#include "privatedtor.h" +#include "protected.h" +#include "rect.h" +#include "reference.h" +#include "removednamespaces.h" +#include "sample.h" +#include "samplenamespace.h" +#include "simplefile.h" +#include "size.h" +#include "str.h" +#include "strlist.h" +#include "sometime.h" +#include "templateptr.h" +#include "transform.h" +#include "virtualmethods.h" +#include "voidholder.h" +#include "valueandvirtual.h" +#include "expression.h" +#include "filter.h" diff --git a/sources/shiboken2/tests/samplebinding/handleholder_test.py b/sources/shiboken2/tests/samplebinding/handleholder_test.py new file mode 100644 index 000000000..6d945e499 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/handleholder_test.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +''' Test case for a class that holds a unknown handle object. + Test case for BUG #1105. +''' + +import unittest + +from sample import HandleHolder + +class HandleHolderTest(unittest.TestCase): + def testCreation(self): + holder = HandleHolder(HandleHolder.createHandle()) + holder2 = HandleHolder(HandleHolder.createHandle()) + self.assertEqual(holder.compare(holder2), False) + + def testTransfer(self): + holder = HandleHolder() + holder2 = HandleHolder(holder.handle()) + self.assertTrue(holder.compare(holder2)) + + def testUseDefinedType(self): + holder = HandleHolder(8) + holder2 = HandleHolder(holder.handle2()) + self.assertTrue(holder.compare2(holder2)) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/hashabletype_test.py b/sources/shiboken2/tests/samplebinding/hashabletype_test.py new file mode 100644 index 000000000..069824ca7 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/hashabletype_test.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for __hash__''' + +import unittest + +from sample import * + +class HashableTest(unittest.TestCase): + + def testStrHash(self): + h = {} + s = Str("Hi") + h[s] = 2 + self.assertTrue(h.get(s), 2) + + def testObjectTypeHash(self): + h = {} + o = ObjectType() + h[o] = 2 + self.assertTrue(h.get(o), 2) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ignorederefop_test.py b/sources/shiboken2/tests/samplebinding/ignorederefop_test.py new file mode 100644 index 000000000..dc3fe8b13 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ignorederefop_test.py @@ -0,0 +1,38 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import * + +class TestLackOfDereferenceOperators (unittest.TestCase): + def testIf(self): + r = Reference() + self.assertFalse(hasattr(r, "__mul__")) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/implicitconv_numerical_test.py b/sources/shiboken2/tests/samplebinding/implicitconv_numerical_test.py new file mode 100644 index 000000000..3fe66e30c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/implicitconv_numerical_test.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for inplicit converting C++ numeric types.''' + +import unittest +import sys +import sample +from py3kcompat import IS_PY3K, l, long + +# Hardcode the limits of the underlying C-types depending on architecture and memory +# model (taking MSVC using LLP64 into account). +cIntMin = -2147483648 +cIntMax = 2147483647 +cLongMin = cIntMin +cLongMax = cIntMax +maxRepresentableInt = sys.maxsize if IS_PY3K else sys.maxint +is64bitArchitecture = maxRepresentableInt > 2**32 +if is64bitArchitecture and sys.platform != 'win32': + cLongMin = -9223372036854775808 + cLongMax = 9223372036854775807 + +class NumericTester(unittest.TestCase): + '''Helper class for numeric comparison testing''' + + def check_value(self, source, expected, callback, desired_type=None): + result = callback(source) + self.assertEqual(result, expected) + + if desired_type: + self.assertEqual(type(result), desired_type) + + +class FloatImplicitConvert(NumericTester): + '''Test case for implicit converting C++ numeric types.''' + + def testFloatAsInt(self): + '''Float as Int''' + self.check_value(3.14, 3, sample.acceptInt, int) + self.assertRaises(OverflowError, sample.acceptInt, cIntMax + 400) + + def testFloatAsLong(self): + '''Float as Long''' + #C++ longs are python ints for us + self.check_value(3.14, 3, sample.acceptLong, int) + self.assertRaises(OverflowError, sample.acceptLong, cLongMax + 400) + + def testFloatAsUInt(self): + '''Float as unsigned Int''' + self.check_value(3.14, 3, sample.acceptUInt, long) + self.assertRaises(OverflowError, sample.acceptUInt, -3.14) + + def testFloatAsULong(self): + '''Float as unsigned Long''' + #FIXME Breaking with SystemError "bad argument to internal function" + self.check_value(3.14, 3, sample.acceptULong, long) + self.assertRaises(OverflowError, sample.acceptULong, -3.14) + + def testFloatAsDouble(self): + '''Float as double''' + self.check_value(3.14, 3.14, sample.acceptDouble, float) + + +class IntImplicitConvert(NumericTester): + '''Test case for implicit converting C++ numeric types.''' + + def testIntAsInt(self): + '''Int as Int''' + self.check_value(3, 3, sample.acceptInt, int) + + def testIntAsLong(self): + '''Int as Long''' + self.check_value(3, 3, sample.acceptLong, int) + + # cLongMax goes here as CPython implements int as a C long + self.check_value(cLongMax, cLongMax, sample.acceptLong, int) + self.check_value(cLongMin, cLongMin, sample.acceptLong, int) + + def testIntAsUInt(self): + '''Int as unsigned Int''' + self.check_value(3, 3, sample.acceptUInt, long) + self.assertRaises(OverflowError, sample.acceptUInt, -3) + + def testIntAsULong(self): + '''Int as unsigned Long''' + self.check_value(3, 3, sample.acceptULong, long) + self.assertRaises(OverflowError, sample.acceptULong, -3) + + def testFloatAsDouble(self): + '''Float as double''' + self.check_value(3.14, 3.14, sample.acceptDouble, float) + + +class LongImplicitConvert(NumericTester): + '''Test case for implicit converting C++ numeric types.''' + + def testLongAsInt(self): + '''Long as Int''' + self.check_value(l(24224), 24224, sample.acceptInt, int) + self.assertRaises(OverflowError, sample.acceptInt, cIntMax + 20) + + def testLongAsLong(self): + '''Long as Long''' + self.check_value(l(2405), 2405, sample.acceptLong, int) + self.assertRaises(OverflowError, sample.acceptLong, cLongMax + 20) + + def testLongAsUInt(self): + '''Long as unsigned Int''' + self.check_value(l(260), 260, sample.acceptUInt, long) + self.assertRaises(OverflowError, sample.acceptUInt, -42) + + def testLongAsULong(self): + '''Long as unsigned Long''' + self.check_value(l(128), 128, sample.acceptULong, long) + self.assertRaises(OverflowError, sample.acceptULong, l(-334)) + + def testLongAsDouble(self): + '''Float as double''' + self.check_value(l(42), 42, sample.acceptDouble, float) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/implicitconv_test.py b/sources/shiboken2/tests/samplebinding/implicitconv_test.py new file mode 100644 index 000000000..a94151d80 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/implicitconv_test.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for implicit conversions''' + +import unittest + +from sample import ImplicitConv, ObjectType + +class ImplicitConvTest(unittest.TestCase): + '''Test case for implicit conversions''' + + def testImplicitConversions(self): + '''Test if overloaded function call decisor takes implicit conversions into account.''' + ic = ImplicitConv.implicitConvCommon(ImplicitConv()) + self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorNone) + + ic = ImplicitConv.implicitConvCommon(3) + self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorOne) + self.assertEqual(ic.objId(), 3) + + ic = ImplicitConv.implicitConvCommon(ImplicitConv.CtorThree) + self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorThree) + + obj = ObjectType() + ic = ImplicitConv.implicitConvCommon(obj) + self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorObjectTypeReference) + + ic = ImplicitConv.implicitConvCommon(42.42) + self.assertEqual(ic.value(), 42.42) + + ic = ImplicitConv(None) + self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorPrimitiveType) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/inheritanceandscope_test.py b/sources/shiboken2/tests/samplebinding/inheritanceandscope_test.py new file mode 100755 index 000000000..a8ede0548 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/inheritanceandscope_test.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for finding scope in cases involving inheritance.''' + +import unittest + +from sample import SampleNamespace + +class ScopeAndInheritanceTest(unittest.TestCase): + '''Test cases for finding scope in cases involving inheritance.''' + + def testMethodCorrectlyWrapper(self): + '''A method returning a type declared in the scope of the method's + class parent must be found and the method correctly exported.''' + meth = getattr(SampleNamespace.DerivedFromNamespace, 'methodReturningTypeFromParentScope') + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/injectcode_test.py b/sources/shiboken2/tests/samplebinding/injectcode_test.py new file mode 100644 index 000000000..99847f891 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/injectcode_test.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for std::list container conversions''' + +import unittest +from sample import InjectCode + +class MyInjectCode(InjectCode): + def __init__(self): + InjectCode.__init__(self) + self.multiplier = 2 + + def arrayMethod(self, values): + return self.multiplier * sum(values) + +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") + + def testArgumentRemovalAndArgumentTypeModification(self): + '''A method has its first argument removed and the second modified.''' + ic = InjectCode() + values = (1, 2, 3, 4, 5) + result = ic.arrayMethod(values) + self.assertEqual(result, sum(values)) + + def testCallVirtualMethodWithArgumentRemovalAndArgumentTypeModification(self): + '''A virtual method has its first argument removed and the second modified.''' + ic = InjectCode() + values = (1, 2, 3, 4, 5) + result = ic.callArrayMethod(values) + self.assertEqual(result, sum(values)) + + def testCallReimplementedVirtualMethodWithArgumentRemovalAndArgumentTypeModification(self): + '''Calls a reimplemented virtual method that had its first argument removed and the second modified.''' + ic = MyInjectCode() + values = (1, 2, 3, 4, 5) + result = ic.callArrayMethod(values) + self.assertEqual(result, ic.multiplier * sum(values)) + + def testUsageOfTypeSystemCheckVariableOnPrimitiveType(self): + '''When the sequence item is convertible to an integer -1 is returned, or -2 if its not convertible.''' + ic = InjectCode() + values = (1, 2, 3, 4, '5', 6.7) + result = ic.arrayMethod(values) + + fixedValues = [v for v in values if isinstance(v, int)]\ + + [-1 for v in values if isinstance(v, float)]\ + + [-2 for v in values if not isinstance(v, int) and not isinstance(v, float)] + self.assertEqual(result, sum(fixedValues)) + + +class IntArrayTest(unittest.TestCase): + '''Test case for converting python sequence to int array''' + + def testBasic(self): + '''Shiboken::sequenceToIntArray - basic case''' + args = [1, 2, 3, 4] + ic = InjectCode() + self.assertEqual(sum(args) + len(args), ic.sumArrayAndLength(args)) + + def testEmpty(self): + '''Shiboken::sequenceToIntArray - empty sequence''' + args = [] + ic = InjectCode() + self.assertEqual(sum(args) + len(args), ic.sumArrayAndLength(args)) + + def testWithZero(self): + '''Shiboken::sequenceToIntArray - count only up to zero''' + args = [1, 2, 0, 3] + ic = InjectCode() + self.assertEqual(sum([1, 2]) + len([1, 2]), ic.sumArrayAndLength(args)) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/innerclass_test.py b/sources/shiboken2/tests/samplebinding/innerclass_test.py new file mode 100644 index 000000000..0df636857 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/innerclass_test.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import Derived + +class TestInnerClass(unittest.TestCase): + def testInstaciate(self): + d = Derived.SomeInnerClass() + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/intlist_test.py b/sources/shiboken2/tests/samplebinding/intlist_test.py new file mode 100644 index 000000000..6d21ef053 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/intlist_test.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import IntList + +class IntListTest(unittest.TestCase): + + def testAutoFunctionsToBaseList(self): + lst = IntList() + self.assertEqual(len(lst), 0) + lst.append(10) + self.assertEqual(lst[0], 10) + lst.append(20) + self.assertEqual(lst[1], 20) + lst.append(30) + self.assertEqual(lst[2], 30) + lst[1] = 25 + self.assertEqual(lst[0], 10) + self.assertEqual(lst[1], 25) + self.assertEqual(lst[2], 30) + self.assertEqual(len(lst), 3) + + def testIntListCtor_NoParams(self): + '''IntList constructor receives no parameter.''' + il = IntList() + self.assertEqual(len(il), 0) + self.assertEqual(il.constructorUsed(), IntList.NoParamsCtor) + + def testIntListCtor_int(self): + '''IntList constructor receives an integer.''' + value = 123 + il = IntList(value) + self.assertEqual(len(il), 1) + self.assertEqual(il[0], value) + self.assertEqual(il.constructorUsed(), IntList.IntCtor) + + def testIntListCtor_IntList(self): + '''IntList constructor receives an IntList object.''' + il1 = IntList(123) + il2 = IntList(il1) + self.assertEqual(len(il1), len(il2)) + for i in range(len(il1)): + self.assertEqual(il1[i], il2[i]) + self.assertEqual(il2.constructorUsed(), IntList.CopyCtor) + + def testIntListCtor_ListOfInts(self): + '''IntList constructor receives an integer list.''' + ints = [123, 456] + il = IntList(ints) + self.assertEqual(len(il), len(ints)) + for i in range(len(il)): + self.assertEqual(il[i], ints[i]) + self.assertEqual(il.constructorUsed(), IntList.ListOfIntCtor) + + def testIntListAttributeTypeCheck(self): + '''Attribute values to IntList.''' + il = IntList([0, 1, 2]) + self.assertEqual(len(il), 3) + il[0] = 123 + self.assertEqual(len(il), 3) + self.assertEqual(il[0], 123) + il[1] = 432.1 + self.assertEqual(len(il), 3) + self.assertEqual(il[1], int(432.1)) + self.assertRaises(TypeError, il.__setitem__, 2, '78') + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/invalid_virtual_return_test.py b/sources/shiboken2/tests/samplebinding/invalid_virtual_return_test.py new file mode 100644 index 000000000..f160f1e07 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/invalid_virtual_return_test.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for returning invalid types in a virtual function''' + +import unittest +from sample import ObjectModel, ObjectType, ObjectView + +import warnings + + +class MyObject(ObjectType): + pass + + +class ListModelWrong(ObjectModel): + + def __init__(self, parent=None): + ObjectModel.__init__(self, parent) + self.obj = 0 + + def data(self): + warnings.simplefilter('error') + # Shouldn't segfault. Must set TypeError + return self.obj + + +class ModelWrongReturnTest(unittest.TestCase): + + def testWrongTypeReturn(self): + model = ListModelWrong() + view = ObjectView(model) + self.assertRaises(RuntimeWarning, view.getRawModelData) # calls model.data() + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/keep_reference_test.py b/sources/shiboken2/tests/samplebinding/keep_reference_test.py new file mode 100644 index 000000000..a8f63f6df --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/keep_reference_test.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for objects that keep references to other object without owning them (e.g. model/view relationships).''' + +import unittest +from sys import getrefcount + +from sample import ObjectModel, ObjectView + +class TestKeepReference(unittest.TestCase): + '''Test case for objects that keep references to other object without owning them (e.g. model/view relationships).''' + + def testReferenceCounting(self): + '''Tests reference count of model-like object referred by view-like objects.''' + model1 = ObjectModel() + refcount1 = getrefcount(model1) + view1 = ObjectView() + view1.setModel(model1) + self.assertEqual(getrefcount(view1.model()), refcount1 + 1) + + view2 = ObjectView() + view2.setModel(model1) + self.assertEqual(getrefcount(view2.model()), refcount1 + 2) + + model2 = ObjectModel() + view2.setModel(model2) + self.assertEqual(getrefcount(view1.model()), refcount1 + 1) + + def testReferenceCountingWhenDeletingReferrer(self): + '''Tests reference count of model-like object referred by deceased view-like object.''' + model = ObjectModel() + refcount1 = getrefcount(model) + view = ObjectView() + view.setModel(model) + self.assertEqual(getrefcount(view.model()), refcount1 + 1) + + del view + self.assertEqual(getrefcount(model), refcount1) + + def testReferreedObjectSurvivalAfterContextEnd(self): + '''Model-like object assigned to a view-like object must survive after get out of context.''' + def createModelAndSetToView(view): + model = ObjectModel() + model.setObjectName('created model') + view.setModel(model) + view = ObjectView() + createModelAndSetToView(view) + model = view.model() + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/list_test.py b/sources/shiboken2/tests/samplebinding/list_test.py new file mode 100644 index 000000000..22cb4c04b --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/list_test.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for std::list container conversions''' + +import unittest + +from sample import ListUser, Point, PointF + +class ExtendedListUser(ListUser): + def __init__(self): + ListUser.__init__(self) + self.create_list_called = False + + def createList(self): + self.create_list_called = True + return [2, 3, 5, 7, 13] + +class ListConversionTest(unittest.TestCase): + '''Test case for std::list container conversions''' + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a virtual method is correctly called from C++.''' + lu = ExtendedListUser() + lst = lu.callCreateList() + self.assertTrue(lu.create_list_called) + self.assertEqual(type(lst), list) + for item in lst: + self.assertEqual(type(item), int) + + def testPrimitiveConversionInsideContainer(self): + '''Test primitive type conversion inside conversible std::list container.''' + cpx0 = complex(1.2, 3.4) + cpx1 = complex(5.6, 7.8) + lst = ListUser.createComplexList(cpx0, cpx1) + self.assertEqual(type(lst), list) + for item in lst: + self.assertEqual(type(item), complex) + self.assertEqual(lst, [cpx0, cpx1]) + + def testSumListIntegers(self): + '''Test method that sums a list of integer values.''' + lu = ListUser() + lst = [3, 5, 7] + result = lu.sumList(lst) + self.assertEqual(result, sum(lst)) + + def testSumListFloats(self): + '''Test method that sums a list of float values.''' + lu = ListUser() + lst = [3.3, 4.4, 5.5] + result = lu.sumList(lst) + self.assertEqual(result, sum(lst)) + + def testConversionInBothDirections(self): + '''Test converting a list from Python to C++ and back again.''' + lu = ListUser() + lst = [3, 5, 7] + lu.setList(lst) + result = lu.getList() + self.assertEqual(result, lst) + + def testConversionInBothDirectionsWithSimilarContainer(self): + '''Test converting a tuple, instead of the expected list, from Python to C++ and back again.''' + lu = ListUser() + lst = (3, 5, 7) + lu.setList(lst) + result = lu.getList() + self.assertNotEqual(result, lst) + self.assertEqual(result, list(lst)) + + def testConversionOfListOfObjectsPassedAsArgument(self): + '''Calls method with a Python list of wrapped objects to be converted to a C++ list.''' + mult = 3 + pts0 = (Point(1.0, 2.0), Point(3.3, 4.4), Point(5, 6)) + pts1 = (Point(1.0, 2.0), Point(3.3, 4.4), Point(5, 6)) + ListUser.multiplyPointList(pts1, mult) + for pt0, pt1 in zip(pts0, pts1): + self.assertEqual(pt0.x() * mult, pt1.x()) + self.assertEqual(pt0.y() * mult, pt1.y()) + + def testConversionOfInvalidLists(self): + mult = 3 + pts = (Point(1.0, 2.0), 3, Point(5, 6)) + self.assertRaises(TypeError, ListUser.multiplyPointList, pts, mult) + + def testOverloadMethodReceivingRelatedContainerTypes(self): + self.assertEqual(ListUser.ListOfPointF, ListUser.listOfPoints([PointF()])) + self.assertEqual(ListUser.ListOfPoint, ListUser.listOfPoints([Point()])) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/lock_test.py b/sources/shiboken2/tests/samplebinding/lock_test.py new file mode 100644 index 000000000..35bf5149d --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/lock_test.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python + +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Simple test with a blocking C++ method that should allow python + threads to run.''' + +import unittest +import threading + +from sample import Bucket + + +class Unlocker(threading.Thread): + + def __init__(self, bucket): + threading.Thread.__init__(self) + self.bucket = bucket + + def run(self): + while not self.bucket.locked(): + pass + + self.bucket.unlock() + + +class MyBucket(Bucket): + + def __init__(self): + Bucket.__init__(self) + + def virtualBlockerMethod(self): + self.lock() + return True + + +class TestLockUnlock(unittest.TestCase): + + def testBasic(self): + '''Locking in C++ and releasing in a python thread''' + bucket = Bucket() + unlocker = Unlocker(bucket) + + unlocker.start() + bucket.lock() + unlocker.join() + + def testVirtualBlocker(self): + '''Same as the basic case but blocker method is a C++ virtual called from C++.''' + bucket = Bucket() + unlocker = Unlocker(bucket) + + unlocker.start() + result = bucket.callVirtualBlockerMethodButYouDontKnowThis() + unlocker.join() + self.assertTrue(result) + + def testReimplementedVirtualBlocker(self): + '''Same as the basic case but blocker method is a C++ virtual reimplemented in Python and called from C++.''' + mybucket = MyBucket() + unlocker = Unlocker(mybucket) + + unlocker.start() + result = mybucket.callVirtualBlockerMethodButYouDontKnowThis() + unlocker.join() + self.assertTrue(result) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/map_test.py b/sources/shiboken2/tests/samplebinding/map_test.py new file mode 100644 index 000000000..18cefd857 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/map_test.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for std::map container conversions''' + +import unittest + +from sample import MapUser + +class ExtendedMapUser(MapUser): + def __init__(self): + MapUser.__init__(self) + self.create_map_called = False + + def createMap(self): + self.create_map_called = True + return {'two' : (complex(2.2, 2.2), 2), + 'three' : (complex(3.3, 3.3), 3), + 'five' : (complex(5.5, 5.5), 5), + 'seven' : (complex(7.7, 7.7), 7)} + +class MapConversionTest(unittest.TestCase): + '''Test case for std::map container conversions''' + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a virtual method is correctly called from C++.''' + mu = ExtendedMapUser() + map_ = mu.callCreateMap() + self.assertTrue(mu.create_map_called) + self.assertEqual(type(map_), dict) + for key, value in map_.items(): + self.assertEqual(type(key), str) + self.assertEqual(type(value[0]), complex) + self.assertEqual(type(value[1]), int) + + def testConversionInBothDirections(self): + '''Test converting a map from Python to C++ and back again.''' + mu = MapUser() + map_ = {'odds' : [2, 4, 6], 'evens' : [3, 5, 7], 'primes' : [3, 4, 6]} + mu.setMap(map_) + result = mu.getMap() + self.assertEqual(result, map_) + + def testConversionMapIntKeyValueTypeValue(self): + '''C++ signature: MapUser::passMapIntValueType(const std::map<int, const ByteArray>&)''' + mu = MapUser() + map_ = {0 : 'string'} + result = mu.passMapIntValueType(map_) + self.assertEqual(map_, result) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/metaclass_test.py b/sources/shiboken2/tests/samplebinding/metaclass_test.py new file mode 100644 index 000000000..972a2f17d --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/metaclass_test.py @@ -0,0 +1,61 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from sample import * +import unittest + +class MetaA(type): + pass + +class A(object): + __metaclass__ = MetaA + +MetaB = type(Point) +B = Point + +class MetaC(MetaA, MetaB): + pass +class C(A, B): + __metaclass__ = MetaC + +class D(C): + pass + +class TestMetaClass(unittest.TestCase): + def testIt(self): + w1 = C() # works + w1.setX(1) + w1.setY(2) + + w2 = D() # should work! + w2.setX(3) + w2.setY(4) + + w3 = w1 + w2 + self.assertEqual(w3.x(), 4) + self.assertEqual(w3.y(), 6) diff --git a/sources/shiboken2/tests/samplebinding/mi_virtual_methods_test.py b/sources/shiboken2/tests/samplebinding/mi_virtual_methods_test.py new file mode 100644 index 000000000..0905dead9 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/mi_virtual_methods_test.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for virtual methods in multiple inheritance scenarios''' + +import unittest + +from sample import VirtualMethods, ObjectType, Event + + +class ImplementsNone(ObjectType, VirtualMethods): + '''Implements no virtual methods''' + + def __init__(self): + ObjectType.__init__(self) + VirtualMethods.__init__(self) + + +class ImplementsBoth(ObjectType, VirtualMethods): + '''Implements ObjectType.event and VirtualMethods.sum1''' + + def __init__(self): + ObjectType.__init__(self) + VirtualMethods.__init__(self) + self.event_processed = False + + def event(self, event): + self.event_processed = True + return True + + def sum1(self, arg0, arg1, arg2): + return (arg0 + arg1 + arg2) * 2 + + +class CppVirtualTest(unittest.TestCase): + '''Virtual method defined in c++ called from C++''' + + def testCpp(self): + '''C++ calling C++ virtual method in multiple inheritance scenario''' + obj = ImplementsNone() + self.assertTrue(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT))) + self.assertRaises(AttributeError, getattr, obj, 'event_processed') + + self.assertEqual(obj.callSum0(1, 2, 3), 6) + + +class PyVirtualTest(unittest.TestCase): + '''Virtual method reimplemented in python called from C++''' + + def testEvent(self): + '''C++ calling Python reimplementation of virtual in multiple inheritance''' + obj = ImplementsBoth() + self.assertTrue(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT))) + self.assertTrue(obj.event_processed) + + self.assertEqual(obj.callSum1(1, 2, 3), 12) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/mixed_mi_test.py b/sources/shiboken2/tests/samplebinding/mixed_mi_test.py new file mode 100644 index 000000000..b6268cf2c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/mixed_mi_test.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for multiple inheritance in mixed Python/C++ scenarios''' + +import unittest + +from sample import ObjectType + + +class Base(object): + '''Base Python class''' + + def __init__(self): + self.name = '' + + def pythonName(self): + return self.name + + def setPythonName(self, name): + self.name = name + + +class Child(Base, ObjectType): + '''Dummy class with mixed parents''' + + def __init__(self): + Base.__init__(self) + ObjectType.__init__(self) + + +class MixedInheritanceTest(unittest.TestCase): + + def testMixed(self): + '''Mixed Python/C++ multiple inheritance''' + obj = Child() + + obj.setObjectName('aaa') + self.assertEqual(obj.objectName(), 'aaa') + + obj.setPythonName('python') + self.assertEqual(obj.pythonName(), 'python') + + +if __name__ == '__main__': + unittest.main() + + diff --git a/sources/shiboken2/tests/samplebinding/modelindex_test.py b/sources/shiboken2/tests/samplebinding/modelindex_test.py new file mode 100644 index 000000000..5dd7d3bc7 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/modelindex_test.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import ModelIndex, ReferentModelIndex, PersistentModelIndex + +class TestCastOperator(unittest.TestCase): + + def testCastOperatorReturningValue(self): + index = PersistentModelIndex() + index.setValue(123) + self.assertEqual(index.value(), 123) + self.assertEqual(index.value(), ModelIndex.getValue(index)) + + def testCastOperatorReturningReference(self): + index = ReferentModelIndex() + index.setValue(123) + self.assertEqual(index.value(), 123) + self.assertEqual(index.value(), ModelIndex.getValue(index)) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/modelview_test.py b/sources/shiboken2/tests/samplebinding/modelview_test.py new file mode 100644 index 000000000..081de303d --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/modelview_test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for objects that keep references to other object without owning them (e.g. model/view relationships).''' + +import unittest +from sample import ObjectModel, ObjectType, ObjectView + + +object_name = 'test object' + +class MyObject(ObjectType): + pass + +class ListModelKeepsReference(ObjectModel): + def __init__(self, parent=None): + ObjectModel.__init__(self, parent) + self.obj = MyObject() + self.obj.setObjectName(object_name) + + def data(self): + return self.obj + +class ListModelDoesntKeepsReference(ObjectModel): + def data(self): + obj = MyObject() + obj.setObjectName(object_name) + return obj + + +class ModelViewTest(unittest.TestCase): + + def testListModelDoesntKeepsReference(self): + model = ListModelDoesntKeepsReference() + view = ObjectView(model) + obj = view.getRawModelData() + self.assertEqual(type(obj), MyObject) + self.assertEqual(obj.objectName(), object_name) + + def testListModelKeepsReference(self): + model = ListModelKeepsReference() + view = ObjectView(model) + obj = view.getRawModelData() + self.assertEqual(type(obj), MyObject) + self.assertEqual(obj.objectName(), object_name) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/modifications_test.py b/sources/shiboken2/tests/samplebinding/modifications_test.py new file mode 100644 index 000000000..44748dc8c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/modifications_test.py @@ -0,0 +1,224 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for method modifications performed as described on type system. ''' + +import unittest + +from sample import Modifications, Point, ByteArray + +class ExtModifications(Modifications): + def __init__(self): + Modifications.__init__(self) + self.multiplier = 3.0 + self.increment = 10.0 + + def name(self): + return 'ExtModifications' + + def differenceOfPointCoordinates(self, point): + ok, res = Modifications.differenceOfPointCoordinates(self, point) + return ok, res * self.multiplier + self.increment + + +class ModificationsTest(unittest.TestCase): + '''Test cases for method modifications performed as described on type system. ''' + + def setUp(self): + self.mods = Modifications() + + def tearDown(self): + del self.mods + + def testClassMembersAvailability(self): + '''Test if Modified class really have the expected members.''' + expected_members = set(['OverloadedModFunc', 'OverloadedNone', + 'Overloaded_ibiP', 'Overloaded_ibib', + 'Overloaded_ibid', 'Overloaded_ibii', + 'calculateArea', 'doublePlus', 'increment', + 'multiplyPointCoordsPlusValue', 'name', + 'pointToPair', 'overloaded', 'power', + 'timesTen']) + self.assertTrue(expected_members.issubset(dir(Modifications))) + + def testRenamedMethodAvailability(self): + '''Test if Modification class really have renamed the 'className' virtual method to 'name'.''' + self.assertTrue('className' not in dir(Modifications)) + self.assertTrue('name' in dir(Modifications)) + + def testReimplementationOfRenamedVirtualMethod(self): + '''Test if class inheriting from Modification class have the reimplementation of renamed virtual method called.''' + em = ExtModifications() + self.assertEqual(self.mods.name(), 'Modifications') + self.assertEqual(em.name(), 'ExtModifications') + + def testRegularMethodRenaming(self): + '''Test if Modifications::cppMultiply was correctly renamed to calculateArea.''' + self.assertTrue('cppMultiply' not in dir(Modifications)) + self.assertTrue('calculateArea' in dir(Modifications)) + self.assertEqual(self.mods.calculateArea(3, 6), 3 * 6) + + def testRegularMethodRemoval(self): + '''Test if 'Modifications::exclusiveCppStuff' was removed from Python bindings.''' + self.assertTrue('exclusiveCppStuff' not in dir(Modifications)) + + def testArgumentRemoval(self): + '''Test if second argument of Modifications::doublePlus(int, int) was removed.''' + self.assertRaises(TypeError, self.mods.doublePlus, 3, 7) + self.assertEqual(self.mods.doublePlus(7), 14) + + def testDefaultValueRemoval(self): + '''Test if default value was removed from first argument of Modifications::increment(int).''' + self.assertRaises(TypeError, self.mods.increment) + self.assertEqual(self.mods.increment(7), 8) + + def testDefaultValueReplacement(self): + '''Test if default values for both arguments of Modifications::power(int, int) were modified.''' + # original default values: int power(int base = 1, int exponent = 0); + self.assertNotEqual(self.mods.power(4), 1) + # modified default values: int power(int base = 2, int exponent = 1); + self.assertEqual(self.mods.power(), 2) + self.assertEqual(self.mods.power(3), 3) + self.assertEqual(self.mods.power(5, 3), 5**3) + + def testSetNewDefaultValue(self): + '''Test if default value was correctly set to 10 for first argument of Modifications::timesTen(int).''' + self.assertEqual(self.mods.timesTen(7), 70) + self.assertEqual(self.mods.timesTen(), 100) + + def testArgumentRemovalAndReturnTypeModificationWithTypesystemTemplates1(self): + '''Test modifications to method signature and return value using type system templates (case 1).''' + result, ok = self.mods.pointToPair(Point(2, 5)) + self.assertEqual(type(ok), bool) + self.assertEqual(type(result), tuple) + self.assertEqual(len(result), 2) + self.assertEqual(type(result[0]), float) + self.assertEqual(type(result[1]), float) + self.assertEqual(result[0], 2.0) + self.assertEqual(result[1], 5.0) + + def testArgumentRemovalAndReturnTypeModificationWithTypesystemTemplates2(self): + '''Test modifications to method signature and return value using type system templates (case 2).''' + result, ok = self.mods.multiplyPointCoordsPlusValue(Point(2, 5), 4.1) + self.assertEqual(type(ok), bool) + self.assertEqual(type(result), float) + self.assertEqual(result, 14.1) + + def testOverloadedMethodModifications(self): + '''Tests modifications to an overloaded method''' + # overloaded(int, bool[removed], int, double) + self.assertEqual(self.mods.overloaded(1, 2, 3.1), Modifications.Overloaded_ibid) + # overloaded(int, bool, int[removed,default=321], int) + self.assertEqual(self.mods.overloaded(1, True, 2), Modifications.Overloaded_ibii) + # the others weren't modified + self.assertEqual(self.mods.overloaded(1, True, 2, False), Modifications.Overloaded_ibib) + self.assertEqual(self.mods.overloaded(1, False, 2, Point(3, 4)), Modifications.Overloaded_ibiP) + self.assertRaises(TypeError, self.mods.overloaded, 1, True, Point(2, 3), Point(4, 5)) + self.assertEqual(self.mods.over(1, True, Point(2, 3), Point(4, 5)), Modifications.Overloaded_ibPP) + + def testPointArrayModification(self): + points = (Point(1, 1), Point(2, 2)) + summedPoint = Point(1, 1) + Point(2, 2) + self.assertEqual(self.mods.sumPointArray(points), summedPoint) + + def testTypeSystemVariableReplacementInFunctionModification(self): + ba = ByteArray('12345') + self.assertEqual(self.mods.getSize(ba), len(ba)) + self.assertEqual(self.mods.getSize(ba, 20), 20) + + def testNoNulPointerTag(self): + point = Point(12, 34) + self.assertEqual(self.mods.sumPointCoordinates(point), 12 + 34) + self.assertRaises(TypeError, self.mods.sumPointCoordinates, None) + + def testNonConversionRuleForArgumentWithDefaultValue(self): + status, obj = self.mods.nonConversionRuleForArgumentWithDefaultValue() + self.assertTrue(status) + self.assertEqual(obj, self.mods.getObject()) + self.assertEqual(obj.objectName(), 'MyObject') + + def testInjectCodeWithConversionVariableForUserPrimitive(self): + self.assertTrue(Modifications.invertBoolean(False)) + self.assertFalse(Modifications.invertBoolean(True)) + + def testConversionRuleForReturnType(self): + x, y = 11, 2 + diff = float(abs(x - y)) + point = Point(x, y) + + ok, res = self.mods.differenceOfPointCoordinates(point) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, diff) + + ok, res = self.mods.callDifferenceOfPointCoordinates(point) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, diff) + + ok, res = self.mods.differenceOfPointCoordinates(None) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, 0.0) + + ok, res = self.mods.callDifferenceOfPointCoordinates(None) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, 0.0) + + def testConversionRuleForReturnTypeOnExtendedClass(self): + x, y = 11, 2 + diff = float(abs(x - y)) + point = Point(x, y) + em = ExtModifications() + + ok, res = em.differenceOfPointCoordinates(point) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, diff * em.multiplier + em.increment) + + ok, res = em.callDifferenceOfPointCoordinates(point) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, diff * em.multiplier + em.increment) + + ok, res = em.differenceOfPointCoordinates(None) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, em.increment) + + ok, res = em.callDifferenceOfPointCoordinates(None) + self.assertTrue(isinstance(ok, bool)) + self.assertTrue(isinstance(res, float)) + self.assertEqual(res, em.increment) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/modified_constructor_test.py b/sources/shiboken2/tests/samplebinding/modified_constructor_test.py new file mode 100644 index 000000000..859f89452 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/modified_constructor_test.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests cases for ConstructorWithModifiedArgument class.''' + +import sys +import unittest + +from sample import * + + +class ConstructorWithModifiedArgumentTest(unittest.TestCase): + '''Test cases for ConstructorWithModifiedArgument class.''' + + def testConstructorWithModifiedArgument(self): + sampleClass = ModifiedConstructor("10") + self.assertTrue(sampleClass.retrieveValue(), 10) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/modifiedvirtualmethods_test.py b/sources/shiboken2/tests/samplebinding/modifiedvirtualmethods_test.py new file mode 100644 index 000000000..3121a9cc2 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/modifiedvirtualmethods_test.py @@ -0,0 +1,248 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for modified virtual methods.''' + +import unittest + +from sample import VirtualMethods, Str + +class ExtendedVirtualMethods(VirtualMethods): + def __init__(self): + VirtualMethods.__init__(self) + self.name_called = False + self.sum0_called = False + self.sum1_called = False + self.sum2_called = False + self.sum3_called = False + self.sum4_called = False + self.sumThree_called = False + self.callMe_called = 0 + self.multiplier = 12345 + + def sum0(self, a0, a1, a2): + self.sum0_called = True + return VirtualMethods.sumThree(self, a0, a1, a2) * self.multiplier + + def sumThree(self, a0, a1, a2): + self.sumThree_called = True + return VirtualMethods.sumThree(self, a0, a1, a2) * self.multiplier + + def sum1(self, a0, a1, a2): + self.sum1_called = True + return VirtualMethods.sum1(self, a0, a1, a2) * self.multiplier + + def sum2(self, a0, a1): + self.sum2_called = True + return VirtualMethods.sum2(self, a0, a1) * self.multiplier + + def sum3(self, a0, a1): + self.sum3_called = True + return VirtualMethods.sum3(self, a0, a1) * self.multiplier + + def sum4(self, a0, a1): + self.sum4_called = True + return VirtualMethods.sum4(self, a0, a1) * self.multiplier + + def name(self): + self.name_called = True + return Str('ExtendedVirtualMethods') + + def callMe(self): + self.callMe_called += 1 + + def getMargins(self): + return tuple([m*2 for m in VirtualMethods.getMargins(self)]) + + +class VirtualMethodsTest(unittest.TestCase): + '''Test case for modified virtual methods.''' + + def setUp(self): + self.vm = VirtualMethods() + self.evm = ExtendedVirtualMethods() + + def tearDown(self): + del self.vm + del self.evm + + def testModifiedVirtualMethod0(self): + '''Renamed virtual method.''' + a0, a1, a2 = 2, 3, 5 + result0 = self.vm.callSum0(a0, a1, a2) + result1 = self.vm.sumThree(a0, a1, a2) + self.assertEqual(result0, a0 + a1 + a2) + self.assertEqual(result0, result1) + self.assertRaises(AttributeError, getattr, self.vm, 'sum0') + + def testReimplementedModifiedVirtualMethod0(self): + '''Override of a renamed virtual method.''' + a0, a1, a2 = 2, 3, 5 + result0 = self.vm.callSum0(a0, a1, a2) + result1 = self.vm.sumThree(a0, a1, a2) + result2 = self.evm.callSum0(a0, a1, a2) + self.assertEqual(result0, result1) + self.assertEqual(result0 * self.evm.multiplier, result2) + self.assertTrue(self.evm.sumThree_called) + self.assertFalse(self.evm.sum0_called) + + def testModifiedVirtualMethod1(self): + '''Virtual method with three arguments and the last one + changed to have the default value set to 1000.''' + a0, a1, a2 = 2, 3, 5 + result0 = self.vm.sum1(a0, a1) + self.assertEqual(result0, a0 + a1 + 1000) + result1 = self.vm.sum1(a0, a1, a2) + result2 = self.vm.callSum1(a0, a1, a2) + self.assertEqual(result1, result2) + + def testReimplementedModifiedVirtualMethod1(self): + '''Override of the virtual method with three arguments and + the last one changed to have the default value set to 1000.''' + a0, a1 = 2, 3 + result0 = self.vm.sum1(a0, a1) + result1 = self.evm.callSum1(a0, a1, 1000) + self.assertEqual(result0 * self.evm.multiplier, result1) + self.assertTrue(self.evm.sum1_called) + + def testModifiedVirtualMethod2(self): + '''Virtual method originally with three arguments, the last + one was removed and the default value set to 2000.''' + a0, a1 = 1, 2 + result0 = self.vm.sum2(a0, a1) + self.assertEqual(result0, a0 + a1 + 2000) + result1 = self.vm.sum2(a0, a1) + result2 = self.vm.callSum2(a0, a1, 2000) + self.assertEqual(result1, result2) + self.assertRaises(TypeError, self.vm.sum2, 1, 2, 3) + + def testReimplementedModifiedVirtualMethod2(self): + '''Override of the virtual method originally with three arguments, + the last one was removed and the default value set to 2000.''' + a0, a1 = 1, 2 + ignored = 54321 + result0 = self.vm.sum2(a0, a1) + result1 = self.evm.callSum2(a0, a1, ignored) + self.assertEqual(result0 * self.evm.multiplier, result1) + self.assertTrue(self.evm.sum2_called) + + def testModifiedVirtualMethod3(self): + '''Virtual method originally with three arguments have the second + one removed and replaced by custom code that replaces it by the sum + of the first and the last arguments.''' + a0, a1 = 1, 2 + result0 = self.vm.sum3(a0, a1) + self.assertEqual(result0, a0 + (a0 + a1) + a1) + result1 = self.vm.callSum3(a0, 10, a1) + self.assertNotEqual(result0, result1) + result2 = self.vm.callSum3(a0, a0 + a1, a1) + self.assertEqual(result0, result2) + self.assertRaises(TypeError, self.vm.sum3, 1, 2, 3) + + def testReimplementedModifiedVirtualMethod3(self): + '''Override of the virtual method originally with three arguments + have the second one removed and replaced by custom code that + replaces it by the sum of the first and the last arguments.''' + a0, a1 = 1, 2 + ignored = 54321 + result0 = self.vm.sum3(a0, a1) + result1 = self.evm.callSum3(a0, ignored, a1) + self.assertEqual(result0 * self.evm.multiplier, result1) + self.assertTrue(self.evm.sum3_called) + + def testModifiedVirtualMethod4(self): + '''Virtual method originally with three arguments, the + last one was removed and the default value set to 3000.''' + a0, a1 = 1, 2 + default_value = 3000 + result0 = self.vm.sum4(a0, a1) + self.assertEqual(result0, a0 + default_value + a1) + removed_arg_value = 100 + result1 = self.vm.callSum4(a0, removed_arg_value, a1) + self.assertEqual(result1, a0 + removed_arg_value + a1) + self.assertRaises(TypeError, self.vm.sum4, 1, 2, 3) + + def testReimplementedModifiedVirtualMethod4(self): + '''Override of the virtual method originally with three arguments, + the last one was removed and the default value set to 3000. + The method was modified with code injection on the binding override + (the one that receives calls from C++ with the original signature + and forwards it to Python overrides) that subtracts the value of the + second argument (removed in Python) from the value of the first + before sending them to Python.''' + a0, a1 = 1, 2 + removed_arg_value = 2011 + default_value = 3000 + result = self.evm.callSum4(a0, removed_arg_value, a1) + self.assertEqual(result, (a0 - removed_arg_value + a1 + default_value) * self.evm.multiplier) + self.assertTrue(self.evm.sum4_called) + + def testOverridenMethodResultModification(self): + '''Injected code modifies the result of a call to a virtual + method overridden in Python.''' + orig_name = self.vm.callName() + self.assertEqual(orig_name, 'VirtualMethods') + name = self.evm.callName() + self.assertEqual(name, 'PimpedExtendedVirtualMethods') + self.assertEqual(name, Str('PimpedExtendedVirtualMethods')) + self.assertTrue(self.evm.name_called) + + def testInjectCodeCallsPythonVirtualMethodOverride(self): + '''When injected code calls the Python override by itself + no code for the method call should be generated.''' + self.evm.callCallMe() + self.assertEqual(self.evm.callMe_called, 1) + + def testAllArgumentsRemoved(self): + values = (10, 20, 30, 40) + self.vm.setMargins(*values) + self.assertEqual(self.vm.getMargins(), values) + + def testAllArgumentsRemovedCallVirtual(self): + values = (10, 20, 30, 40) + self.vm.setMargins(*values) + self.assertEqual(self.vm.callGetMargins(), values) + + def testExtendedAllArgumentsRemoved(self): + values = (10, 20, 30, 40) + self.evm.setMargins(*values) + double = tuple([m*2 for m in values]) + self.assertEqual(self.evm.getMargins(), double) + + def testExtendedAllArgumentsRemovedCallVirtual(self): + values = (10, 20, 30, 40) + self.evm.setMargins(*values) + double = tuple([m*2 for m in values]) + self.assertEqual(self.evm.callGetMargins(), double) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/multi_cpp_inheritance_test.py b/sources/shiboken2/tests/samplebinding/multi_cpp_inheritance_test.py new file mode 100644 index 000000000..6546c3ac6 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/multi_cpp_inheritance_test.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for multiple inheritance''' + +import sys +import unittest + +from sample import * + +class SimpleUseCase(ObjectType, Str): + def __init__(self, name): + ObjectType.__init__(self) + Str.__init__(self, name) + +class SimpleUseCaseReverse(Str, ObjectType): + def __init__(self, name): + ObjectType.__init__(self) + Str.__init__(self, name) + +class SimpleUseCase2(SimpleUseCase): + def __init__(self, name): + SimpleUseCase.__init__(self, name) + +class ComplexUseCase(SimpleUseCase2, Point): + def __init__(self, name): + SimpleUseCase2.__init__(self, name) + Point.__init__(self) + +class ComplexUseCaseReverse(Point, SimpleUseCase2): + def __init__(self, name): + SimpleUseCase2.__init__(self, name) + Point.__init__(self) + +class MultipleCppDerivedTest(unittest.TestCase): + def testInstanciation(self): + s = SimpleUseCase("Hi") + self.assertEqual(s, "Hi") + s.setObjectName(s) + self.assertEqual(s.objectName(), "Hi") + + def testInstanciation2(self): + s = SimpleUseCase2("Hi") + self.assertEqual(s, "Hi") + s.setObjectName(s) + self.assertEqual(s.objectName(), "Hi") + + def testComplexInstanciation(self): + c = ComplexUseCase("Hi") + self.assertEqual(c, "Hi") + c.setObjectName(c) + self.assertEqual(c.objectName(), "Hi") + c.setX(2); + self.assertEqual(c.x(), 2) + +class MultipleCppDerivedReverseTest(unittest.TestCase): + def testInstanciation(self): + s = SimpleUseCaseReverse("Hi") + self.assertEqual(s, "Hi") + s.setObjectName(s) + self.assertEqual(s.objectName(), "Hi") + + def testInstanciation2(self): + s = SimpleUseCase2("Hi") + self.assertEqual(s, "Hi") + s.setObjectName(s) + self.assertEqual(s.objectName(), "Hi") + + def testComplexInstanciation(self): + c = ComplexUseCaseReverse("Hi") + c.setObjectName(c) + self.assertEqual(c.objectName(), "Hi") + c.setX(2); + self.assertEqual(c, Point(2, 0)) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/multiple_derived_test.py b/sources/shiboken2/tests/samplebinding/multiple_derived_test.py new file mode 100644 index 000000000..e84cc76fd --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/multiple_derived_test.py @@ -0,0 +1,221 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for multiple inheritance''' + +import sys +import unittest + +from sample import Base1, Base2, Base3, Base4, Base5, Base6 +from sample import MDerived1, MDerived2, MDerived3, MDerived4, MDerived5, SonOfMDerived1 + +class ExtMDerived1(MDerived1): + def __init__(self): + MDerived1.__init__(self) + self.multiplier = 20 + self.base2Method_called = False + def base2Method(self): + return Base2.base2Method(self) * self.multiplier + +class MultipleDerivedTest(unittest.TestCase): + '''Test cases for multiple inheritance''' + + def testIsInstance(self): + '''MDerived1 is instance of its parents Base1 and Base2.''' + a = MDerived1() + self.assertTrue(isinstance(a, MDerived1)) + self.assertTrue(isinstance(a, Base1)) + self.assertTrue(isinstance(a, Base2)) + + def testIsSubclass(self): + '''MDerived1 is subclass of its parents Base1 and Base2.''' + self.assertTrue(issubclass(MDerived1, Base1)) + self.assertTrue(issubclass(MDerived1, Base2)) + + def testCallToFunctionWithBase1ArgumentThatCastsBackToMDerived1(self): + '''MDerived1 is passed as an Base1 argument to a method that returns it casted back to MDerived1.''' + a = MDerived1() + b = MDerived1.transformFromBase1(a) + self.assertEqual(a, b) + + def testCallToFunctionWithBase2ArgumentThatCastsBackToMDerived1(self): + '''MDerived1 is passed as an Base2 argument to a method that returns it casted back to MDerived1.''' + a = MDerived1() + b = MDerived1.transformFromBase2(a) + self.assertEqual(a, b) + + def testPythonClassIsInstance(self): + '''Python defined class ExtMDerived1 is instance of its parents MDerived1, Base1 and Base2.''' + a = ExtMDerived1() + self.assertTrue(isinstance(a, ExtMDerived1)) + self.assertTrue(isinstance(a, MDerived1)) + self.assertTrue(isinstance(a, Base1)) + self.assertTrue(isinstance(a, Base2)) + + def testPythonClassIsSubclass(self): + '''Python defined class ExtMDerived1 is subclass of its parents MDerived1, Base1 and Base2.''' + self.assertTrue(issubclass(ExtMDerived1, MDerived1)) + self.assertTrue(issubclass(ExtMDerived1, Base1)) + self.assertTrue(issubclass(ExtMDerived1, Base2)) + + def testCastFromMDerived1ToBases(self): + '''MDerived1 is casted by C++ to its parents and the binding must return the MDerived1 wrapper.''' + a = MDerived1() + refcnt = sys.getrefcount(a) + b1 = a.castToBase1() + b2 = a.castToBase2() + self.assertTrue(isinstance(b1, MDerived1)) + self.assertTrue(isinstance(b2, MDerived1)) + self.assertEqual(a, b1) + self.assertEqual(a, b2) + self.assertEqual(sys.getrefcount(a), refcnt + 2) + + def testCastFromExtMDerived1ToMDerived1Bases(self): + '''Python defined class ExtMDerived1 is casted by C++ to MDerived1 parents and the binding must return the correct ExtMDerived1 instance.''' + a = ExtMDerived1() + refcnt = sys.getrefcount(a) + b1 = a.castToBase1() + self.assertTrue(isinstance(b1, MDerived1)) + self.assertTrue(isinstance(b1, ExtMDerived1)) + b2 = a.castToBase2() + self.assertTrue(isinstance(b2, MDerived1)) + self.assertTrue(isinstance(b2, ExtMDerived1)) + self.assertEqual(a, b1) + self.assertEqual(a, b2) + self.assertEqual(sys.getrefcount(a), refcnt + 2) + + def testCastFromSonOfMDerived1ToBases(self): + '''SonOfMDerived1 is casted by C++ to its parents and the binding must return the SonOfMDerived1 wrapper.''' + a = SonOfMDerived1() + refcnt = sys.getrefcount(a) + md1 = a.castToMDerived1() + b1 = a.castToBase1() + b2 = a.castToBase2() + self.assertTrue(isinstance(md1, SonOfMDerived1)) + self.assertTrue(isinstance(b2, SonOfMDerived1)) + self.assertTrue(isinstance(b2, SonOfMDerived1)) + self.assertEqual(a, md1) + self.assertEqual(a, b1) + self.assertEqual(a, b2) + self.assertEqual(sys.getrefcount(a), refcnt + 3) + + def testReimplementedBase2VirtualMethodOnClassInheritingFromMDerived1(self): + a = ExtMDerived1() + value = a.base2Method() + self.assertTrue(value, Base2.base2Method(a) * a.multiplier) + + def testCastFromMDerived2ToBases(self): + '''MDerived2 is casted by C++ to its parents and the binding must return the MDerived2 wrapper.''' + a = MDerived2() + refcnt = sys.getrefcount(a) + b3 = a.castToBase3() + b4 = a.castToBase4() + b5 = a.castToBase5() + b6 = a.castToBase6() + self.assertTrue(isinstance(b3, MDerived2)) + self.assertTrue(isinstance(b4, MDerived2)) + self.assertTrue(isinstance(b5, MDerived2)) + self.assertTrue(isinstance(b6, MDerived2)) + self.assertEqual(a, b3) + self.assertEqual(a, b4) + self.assertEqual(a, b5) + self.assertEqual(a, b6) + self.assertEqual(sys.getrefcount(a), refcnt + 4) + + def testCastFromMDerived3ToBases(self): + '''MDerived3 is casted by C++ to its parents and the binding must return the MDerived3 wrapper.''' + a = MDerived3() + refcnt = sys.getrefcount(a) + md1 = a.castToMDerived1() + md2 = a.castToMDerived2() + b1 = a.castToBase1() + b2 = a.castToBase2() + b3 = a.castToBase3() + b4 = a.castToBase4() + b5 = a.castToBase5() + b6 = a.castToBase6() + self.assertTrue(isinstance(md1, MDerived3)) + self.assertTrue(isinstance(md2, MDerived3)) + self.assertTrue(isinstance(b1, MDerived3)) + self.assertTrue(isinstance(b2, MDerived3)) + self.assertTrue(isinstance(b3, MDerived3)) + self.assertTrue(isinstance(b4, MDerived3)) + self.assertTrue(isinstance(b5, MDerived3)) + self.assertTrue(isinstance(b6, MDerived3)) + self.assertEqual(a, md1) + self.assertEqual(a, md2) + self.assertEqual(a, b1) + self.assertEqual(a, b2) + self.assertEqual(a, b3) + self.assertEqual(a, b4) + self.assertEqual(a, b5) + self.assertEqual(a, b6) + self.assertEqual(sys.getrefcount(a), refcnt + 8) + + def testCastFromMDerived4ToBases(self): + '''MDerived4 is casted by C++ to its parents and the binding must return the MDerived4 wrapper.''' + a = MDerived4() + refcnt = sys.getrefcount(a) + b3 = a.castToBase3() + b4 = a.castToBase4() + self.assertTrue(isinstance(b3, MDerived4)) + self.assertTrue(isinstance(b4, MDerived4)) + self.assertEqual(a, b3) + self.assertEqual(a, b4) + self.assertEqual(sys.getrefcount(a), refcnt + 2) + + def testCastFromMDerived5ToBases(self): + '''MDerived5 is casted by C++ to its parents and the binding must return the MDerived5 wrapper.''' + a = MDerived5() + refcnt = sys.getrefcount(a) + b3 = a.castToBase3() + b4 = a.castToBase4() + self.assertTrue(isinstance(b3, MDerived5)) + self.assertTrue(isinstance(b4, MDerived5)) + self.assertEqual(a, b3) + self.assertEqual(a, b4) + self.assertEqual(sys.getrefcount(a), refcnt + 2) + + def testCastFromMDerived3ToBase3(self): + '''MDerived3 is casted by C++ to Base3 grandparent using both the inherited and reimplement castToBase3 methods.''' + a = MDerived3() + refcnt = sys.getrefcount(a) + b3_reimplemented = a.castToBase3() + b3_inherited = MDerived2.castToBase3(a) + self.assertTrue(isinstance(b3_reimplemented, MDerived3)) + self.assertTrue(isinstance(b3_inherited, MDerived3)) + self.assertEqual(a, b3_reimplemented) + self.assertEqual(a, b3_inherited) + self.assertEqual(sys.getrefcount(a), refcnt + 2) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/namespace_test.py b/sources/shiboken2/tests/samplebinding/namespace_test.py new file mode 100644 index 000000000..653b9081b --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/namespace_test.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for std::map container conversions''' + +import unittest +from sample import * +from py3kcompat import IS_PY3K + +if IS_PY3K: + TYPE_STR = "class" +else: + TYPE_STR = "type" + +class TestEnumUnderNamespace(unittest.TestCase): + def testInvisibleNamespace(self): + o1 = EnumOnNamespace.Option1 + self.assertEqual(o1, 1) + +class TestClassesUnderNamespace(unittest.TestCase): + def testIt(self): + c1 = SampleNamespace.SomeClass() + e1 = SampleNamespace.SomeClass.ProtectedEnum() + c2 = SampleNamespace.SomeClass.SomeInnerClass() + e2 = SampleNamespace.SomeClass.SomeInnerClass.ProtectedEnum() + c3 = SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough() + e3 = SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum() + + def testFunctionAddedOnNamespace(self): + res = SampleNamespace.ImInsideANamespace(2, 2) + self.assertEqual(res, 4) + + def testTpNames(self): + self.assertEqual(str(SampleNamespace.SomeClass), "<%s 'sample.SampleNamespace.SomeClass'>"%TYPE_STR) + self.assertEqual(str(SampleNamespace.SomeClass.ProtectedEnum), "<%s 'sample.SampleNamespace.SomeClass.ProtectedEnum'>"%TYPE_STR) + self.assertEqual(str(SampleNamespace.SomeClass.SomeInnerClass.ProtectedEnum), "<%s 'sample.SampleNamespace.SomeClass.SomeInnerClass.ProtectedEnum'>"%TYPE_STR) + self.assertEqual(str(SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough), "<%s 'sample.SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough'>"%TYPE_STR) + self.assertEqual(str(SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum), "<%s 'sample.SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum'>"%TYPE_STR) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/newdivision_test.py b/sources/shiboken2/tests/samplebinding/newdivision_test.py new file mode 100644 index 000000000..d1a2eda75 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/newdivision_test.py @@ -0,0 +1,42 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from __future__ import division +from sample import * +import unittest + +class TestNewDivision(unittest.TestCase): + + def testIt(self): + p = Point(4, 4) + p2 = p/2 + self.assertEqual(p2, Point(2, 2)) + +if __name__ == "__main__": + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/nondefaultctor_test.py b/sources/shiboken2/tests/samplebinding/nondefaultctor_test.py new file mode 100644 index 000000000..e38dc9455 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/nondefaultctor_test.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for ...''' + +import sys +import unittest + +from sample import NonDefaultCtor + +class DerivedNonDefaultCtor (NonDefaultCtor): + def returnMyselfVirtual(self): + return NonDefaultCtor(self.value()+1) + +class AnotherDerivedNonDefaultCtor (NonDefaultCtor): + def __init__(self, some_string): + pass + +class NonDefaultCtorTest(unittest.TestCase): + + def testNonDefaultCtor(self): + c = NonDefaultCtor(2) + # these functions returns NonDefaultCtor by value, so a PyObjecy is created every time + self.assertNotEqual(c.returnMyself(), c) + self.assertEqual(c.returnMyself().value(), 2) + self.assertNotEqual(c.returnMyself(3), c) + self.assertEqual(c.returnMyself(3).value(), 2) + self.assertNotEqual(c.returnMyself(4, c), c) + self.assertEqual(c.returnMyself(4, c).value(), 2) + + def testVirtuals(self): + c = DerivedNonDefaultCtor(3) + # these functions returns NonDefaultCtor by value, so a PyObjecy is created every time + self.assertNotEqual(c.returnMyselfVirtual(), c) + self.assertEqual(c.returnMyselfVirtual().value(), 4) + self.assertEqual(c.callReturnMyselfVirtual().value(), 4) + + def testCtorOverload(self): + c = AnotherDerivedNonDefaultCtor("testing") + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/nonzero_test.py b/sources/shiboken2/tests/samplebinding/nonzero_test.py new file mode 100644 index 000000000..7020d30a8 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/nonzero_test.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import * + +class TestNonZeroOperator(unittest.TestCase): + def testIt(self): + c = Color() + self.assertFalse(c) + c = Color(2) + self.assertTrue(c) + +if __name__ == "__main__": + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/numericaltypedef_test.py b/sources/shiboken2/tests/samplebinding/numericaltypedef_test.py new file mode 100644 index 000000000..774b7f1d1 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/numericaltypedef_test.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import SizeF + +class NumericalTypedefTest(unittest.TestCase): + + def testNumericalTypedefExact(self): + width, height = (1.1, 2.2) + size = SizeF(width, height) + self.assertEqual(size.width(), width) + self.assertEqual(size.height(), height) + + def testNumericalTypedefConvertible(self): + width, height = (1, 2) + size = SizeF(width, height) + self.assertEqual(size.width(), float(width)) + self.assertEqual(size.height(), float(height)) + + def testNumericalTypedefOfUnsignedShort(self): + self.assertEqual(SizeF.passTypedefOfUnsignedShort(123), 123) + self.assertEqual(SizeF.passTypedefOfUnsignedShort(321), 321) + self.assertNotEqual(SizeF.passTypedefOfUnsignedShort(123), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/numpy_test.py b/sources/shiboken2/tests/samplebinding/numpy_test.py new file mode 100644 index 000000000..65861e864 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/numpy_test.py @@ -0,0 +1,58 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import sys + +try: + from distutils import sysconfig + if bool(sysconfig.get_config_var('Py_DEBUG')): + sys.exit(0) + import numpy +except: + sys.exit(0) + +import unittest +from sample import PointF + +class TestNumpyTypes(unittest.TestCase): + + def testNumpyConverted(self): + x, y = (0.1, 0.2) + p = PointF(float(numpy.float32(x)), float(numpy.float32(y))) + self.assertAlmostEqual(p.x(), x) + self.assertAlmostEqual(p.y(), y) + + def testNumpyAsIs(self): + x, y = (0.1, 0.2) + p = PointF(numpy.float32(x), numpy.float32(y)) + self.assertAlmostEqual(p.x(), x) + self.assertAlmostEqual(p.y(), y) + +if __name__ == "__main__": + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/objecttype_test.py b/sources/shiboken2/tests/samplebinding/objecttype_test.py new file mode 100644 index 000000000..1a06e7e08 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/objecttype_test.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests ObjectType class of object-type with privates copy constructor and = operator.''' + +import unittest +import sys + +from sample import ObjectType, Str +import shiboken2 as shiboken + + +class ObjectTypeTest(unittest.TestCase): + '''Test cases ObjectType class of object-type with privates copy constructor and = operator.''' + + def testObjectTypeSetObjectNameWithStrVariable(self): + '''ObjectType.setObjectName with Str variable as argument.''' + s = Str('object name') + o = ObjectType() + o.setObjectName(s) + self.assertEqual(str(o.objectName()), str(s)) + + def testObjectTypeSetObjectNameWithStrInstantiation(self): + '''ObjectType.setObjectName with Str instantiation as argument.''' + s = 'object name' + o = ObjectType() + o.setObjectName(Str(s)) + self.assertEqual(str(o.objectName()), s) + + def testObjectTypeSetObjectNameWithPythonString(self): + '''ObjectType.setObjectName with Python string as argument.''' + o = ObjectType() + o.setObjectName('object name') + self.assertEqual(str(o.objectName()), 'object name') + + def testNullOverload(self): + o = ObjectType() + o.setObject(None) + self.assertEqual(o.callId(), 0) + o.setNullObject(None) + self.assertEqual(o.callId(), 1) + + def testParentFromCpp(self): + o = ObjectType() + self.assertEqual(sys.getrefcount(o), 2) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.getCppParent().setObjectName('parent') + self.assertEqual(sys.getrefcount(o), 3) + o.destroyCppParent() + self.assertEqual(sys.getrefcount(o), 2) + + def testNextInFocusChainCycle(self): + parent = ObjectType() + child = ObjectType(parent) + next_focus = child.nextInFocusChain() + + shiboken.invalidate(parent) + + def testNextInFocusChainCycleList(self): + '''As above but in for a list of objects''' + parents = [] + children = [] + focus_chains = [] + for i in range(10): + parent = ObjectType() + child = ObjectType(parent) + next_focus = child.nextInFocusChain() + parents.append(parent) + children.append(child) + focus_chains.append(next_focus) + + shiboken.invalidate(parents) + + def testClassDecref(self): + # Bug was that class PyTypeObject wasn't decrefed when instance died + before = sys.getrefcount(ObjectType) + + for i in range(1000): + obj = ObjectType() + shiboken.delete(obj) + + after = sys.getrefcount(ObjectType) + + self.assertLess(abs(before - after), 5) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/objecttype_with_named_args_test.py b/sources/shiboken2/tests/samplebinding/objecttype_with_named_args_test.py new file mode 100755 index 000000000..533b5a5d1 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/objecttype_with_named_args_test.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import ObjectType + +class NamedArgsTest(unittest.TestCase): + + def testOneArgument(self): + p = ObjectType() + o = ObjectType(parent=p) + self.assertEqual(o.parent(), p) + + def testMoreArguments(self): + o = ObjectType() + + o.setObjectSplittedName("", prefix="pys", suffix="ide") + self.assertEqual(o.objectName(), "pyside") + + o.setObjectSplittedName("", suffix="ide", prefix="pys") + self.assertEqual(o.objectName(), "pyside") + + o.setObjectNameWithSize(name="pyside", size=6) + self.assertEqual(o.objectName(), "pyside") + + o.setObjectNameWithSize(size=6, name="pyside") + self.assertEqual(o.objectName(), "pyside") + + + def testUseDefaultValues(self): + o = ObjectType() + + o.setObjectNameWithSize(size=3) + self.assertEqual(o.objectName(), "<un") # use name='unknown' default argument + + o.setObjectSplittedName("") + self.assertEqual(o.objectName(), "<unknown>") # user prefix='<unk' and suffix='nown>' + + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/objecttypebyvalue_test.py b/sources/shiboken2/tests/samplebinding/objecttypebyvalue_test.py new file mode 100644 index 000000000..a9938b8af --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/objecttypebyvalue_test.py @@ -0,0 +1,42 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from sample import * +import unittest + +class ObjectTypeByValueTest (unittest.TestCase): + def testIt(self): + factory = ObjectTypeByValue() + obj = factory.returnSomeKindOfMe() + # This should crash! + obj.prop.protectedValueTypeProperty.setX(1.0) + # just to make sure it will segfault + obj.prop.protectedValueTypeProperty.setY(2.0) + +if __name__ == "__main__": + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/objecttypelayout_test.py b/sources/shiboken2/tests/samplebinding/objecttypelayout_test.py new file mode 100644 index 000000000..4d0e8cab7 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/objecttypelayout_test.py @@ -0,0 +1,302 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests cases for ObjectTypeLayout class.''' + +import sys +import unittest + +from sample import * + + +class ObjectTypeLayoutTest(unittest.TestCase): + '''Test cases for ObjectTypeLayout class.''' + + def testOwnershipOverride(self): + l = ObjectTypeLayout() + + o1 = ObjectType(l) + o1.setObjectName('o1') + + self.assertEqual(sys.getrefcount(o1), 3) + l.takeChild('o1') + self.assertEqual(sys.getrefcount(o1), 2) + + + def testSetNullLayout(self): + '''ObjectType.setLayout(0).''' + o2 = ObjectType() + o2.setLayout(None) + + def testSetNullLayoutToObjectTypeCreatedInCpp(self): + '''ObjectType.setLayout(0) to object created in C++.''' + o1 = ObjectType.create() + o1.setLayout(None) + + def testObjectTypeLayout(self): + '''ObjectType.setLayout.''' + p1 = ObjectType() + c1 = ObjectType() + c2 = ObjectType() + c3 = ObjectType() + layout = ObjectTypeLayout() + layout.addObject(c1) + layout.addObject(c2) + layout.addObject(c3) + self.assertEqual(c1.parent(), None) + self.assertEqual(c2.parent(), None) + self.assertEqual(c3.parent(), None) + + p1.setLayout(layout) + del p1 # This must kill c1, c2 and c3 + + self.assertRaises(RuntimeError, c1.objectName) + self.assertRaises(RuntimeError, c2.objectName) + self.assertRaises(RuntimeError, c3.objectName) + self.assertRaises(RuntimeError, layout.objectName) + + def testObjectTypeLayoutWithObjectsCreatedInCpp(self): + '''ObjectType.setLayout with objects created in C++.''' + p1 = ObjectType.create() + c1 = ObjectType.create() + c2 = ObjectType.create() + c3 = ObjectType.create() + layout = ObjectTypeLayout() + layout.addObject(c1) + layout.addObject(c2) + layout.addObject(c3) + self.assertEqual(c1.parent(), None) + self.assertEqual(c2.parent(), None) + self.assertEqual(c3.parent(), None) + + p1.setLayout(layout) + del p1 # This must kill c1, c2 and c3 + + self.assertRaises(RuntimeError, c1.objectName) + self.assertRaises(RuntimeError, c2.objectName) + self.assertRaises(RuntimeError, c3.objectName) + self.assertRaises(RuntimeError, layout.objectName) + + def testObjectTypeLayoutTransference(self): + '''Transfer a layout from one ObjectType to another, so that all the items in the layout get reparented.''' + p1 = ObjectType() + p2 = ObjectType() + c1 = ObjectType() + c2 = ObjectType() + + layout = ObjectTypeLayout() + layout.addObject(c1) + layout.addObject(c2) + + p1.setLayout(layout) + + self.assertEqual(len(p2.children()), 0) + self.assertEqual(c1.parent(), p1) + self.assertEqual(c2.parent(), p1) + self.assertEqual(set(p1.children()), set([c1, c2, layout])) + + p2.setLayout(layout) + + self.assertEqual(len(p1.children()), 0) + self.assertEqual(c1.parent(), p2) + self.assertEqual(c2.parent(), p2) + self.assertEqual(set(p2.children()), set([c1, c2, layout])) + + def testObjectTypeLayoutInsideAnotherLayout(self): + '''Adds one ObjectTypeLayout to another and sets the parent to an ObjectType.''' + p1 = ObjectType() + + l1 = ObjectTypeLayout() + c1 = ObjectType() + l1.addObject(c1) + c2 = ObjectType() + l1.addObject(c2) + + l2 = ObjectTypeLayout() + c3 = ObjectType() + l2.addObject(c3) + c4 = ObjectType() + l2.addObject(c4) + + l1.addObject(l2) + + p1.setLayout(l1) + + self.assertEqual(c1.parent(), p1) + self.assertEqual(c2.parent(), p1) + self.assertEqual(c3.parent(), p1) + self.assertEqual(c4.parent(), p1) + self.assertEqual(l1.parent(), p1) + self.assertEqual(l2.parent(), l1) + + del p1 + + self.assertRaises(RuntimeError, c1.objectName) + self.assertRaises(RuntimeError, c2.objectName) + self.assertRaises(RuntimeError, c3.objectName) + self.assertRaises(RuntimeError, c4.objectName) + self.assertRaises(RuntimeError, l1.objectName) + self.assertRaises(RuntimeError, l2.objectName) + + def testObjectTypeLayoutInsideAnotherLayoutAndEveryoneCreatedInCpp(self): + '''Adds one ObjectTypeLayout to another and sets the parent to an ObjectType. All the objects are created in C++.''' + p1 = ObjectType.create() + + l1 = ObjectTypeLayout.create() + c1 = ObjectType.create() + l1.addObject(c1) + c2 = ObjectType.create() + l1.addObject(c2) + + l2 = ObjectTypeLayout.create() + c3 = ObjectType.create() + l2.addObject(c3) + c4 = ObjectType.create() + l2.addObject(c4) + + l1.addObject(l2) + + p1.setLayout(l1) + + self.assertEqual(c1.parent(), p1) + self.assertEqual(c2.parent(), p1) + self.assertEqual(c3.parent(), p1) + self.assertEqual(c4.parent(), p1) + self.assertEqual(l1.parent(), p1) + self.assertEqual(l2.parent(), l1) + + del p1 + + self.assertRaises(RuntimeError, c1.objectName) + self.assertRaises(RuntimeError, c2.objectName) + self.assertRaises(RuntimeError, c3.objectName) + self.assertRaises(RuntimeError, c4.objectName) + self.assertRaises(RuntimeError, l1.objectName) + self.assertRaises(RuntimeError, l2.objectName) + + def testTransferNestedLayoutsBetweenObjects(self): + '''Adds one ObjectTypeLayout to another, sets the parent to an ObjectType and then transfer it to another object.''' + p1 = ObjectType() + p2 = ObjectType() + + l1 = ObjectTypeLayout() + c1 = ObjectType() + l1.addObject(c1) + c2 = ObjectType() + l1.addObject(c2) + + l2 = ObjectTypeLayout() + c3 = ObjectType() + l2.addObject(c3) + c4 = ObjectType() + l2.addObject(c4) + + l1.addObject(l2) + + p1.setLayout(l1) + + self.assertEqual(c1.parent(), p1) + self.assertEqual(c2.parent(), p1) + self.assertEqual(c3.parent(), p1) + self.assertEqual(c4.parent(), p1) + self.assertEqual(l1.parent(), p1) + self.assertEqual(l2.parent(), l1) + + p2.setLayout(l1) + del p1 + + self.assertEqual(c1.parent(), p2) + self.assertEqual(c2.parent(), p2) + self.assertEqual(c3.parent(), p2) + self.assertEqual(c4.parent(), p2) + self.assertEqual(l1.parent(), p2) + self.assertEqual(l2.parent(), l1) + + del p2 + + self.assertRaises(RuntimeError, c1.objectName) + self.assertRaises(RuntimeError, c2.objectName) + self.assertRaises(RuntimeError, c3.objectName) + self.assertRaises(RuntimeError, c4.objectName) + self.assertRaises(RuntimeError, l1.objectName) + self.assertRaises(RuntimeError, l2.objectName) + + def testTransferNestedLayoutsBetweenObjectsAndEveryoneCreatedInCpp(self): + '''Adds one ObjectTypeLayout to another, sets the parent to an ObjectType and then transfer it to another object. + All the objects are created in C++.''' + p1 = ObjectType.create() + p2 = ObjectType.create() + + l1 = ObjectTypeLayout.create() + c1 = ObjectType.create() + l1.addObject(c1) + c2 = ObjectType.create() + l1.addObject(c2) + + l2 = ObjectTypeLayout.create() + c3 = ObjectType.create() + l2.addObject(c3) + c4 = ObjectType.create() + l2.addObject(c4) + + l1.addObject(l2) + + p1.setLayout(l1) + + self.assertEqual(c1.parent(), p1) + self.assertEqual(c2.parent(), p1) + self.assertEqual(c3.parent(), p1) + self.assertEqual(c4.parent(), p1) + self.assertEqual(l1.parent(), p1) + self.assertEqual(l2.parent(), l1) + + p2.setLayout(l1) + del p1 + + self.assertEqual(c1.parent(), p2) + self.assertEqual(c2.parent(), p2) + self.assertEqual(c3.parent(), p2) + self.assertEqual(c4.parent(), p2) + self.assertEqual(l1.parent(), p2) + self.assertEqual(l2.parent(), l1) + + del p2 + + self.assertRaises(RuntimeError, c1.objectName) + self.assertRaises(RuntimeError, c2.objectName) + self.assertRaises(RuntimeError, c3.objectName) + self.assertRaises(RuntimeError, c4.objectName) + self.assertRaises(RuntimeError, l1.objectName) + self.assertRaises(RuntimeError, l2.objectName) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/objecttypeoperators_test.py b/sources/shiboken2/tests/samplebinding/objecttypeoperators_test.py new file mode 100644 index 000000000..a8bcf1aa0 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/objecttypeoperators_test.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import * + +class ObjectTypeOperatorsTest(unittest.TestCase): + + def testIt(self): + a = ObjectTypeOperators("a") + b = ObjectTypeOperators("b") + self.assertFalse(a == b) + self.assertEqual(a, a < b) + + # this should change a.key() and return nothing. + self.assertEqual(None, a > b) + self.assertEqual(a.key(), "aoperator>") + + def testPointerOpeators(self): + a = ObjectTypeOperators("a") + b = ObjectTypeOperators("b") + self.assertEqual(a + "bc", "abc") + self.assertEqual("bc" + a, "bca") + self.assertEqual("a", a) + self.assertEqual(a, "a") + + def testOperatorInjection(self): + a = ObjectTypeOperators("a") + self.assertNotEqual(a, "b") + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/objecttypereferenceasvirtualmethodargument_test.py b/sources/shiboken2/tests/samplebinding/objecttypereferenceasvirtualmethodargument_test.py new file mode 100644 index 000000000..3f09df200 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/objecttypereferenceasvirtualmethodargument_test.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import ObjectTypeHolder + +class TestObjectTypeReferenceAsVirtualMethodArgument(unittest.TestCase): + + def testBasic(self): + holder = ObjectTypeHolder('TheObjectFromC++') + self.assertEqual(holder.callPassObjectTypeAsReference(), 'TheObjectFromC++') + + def testExtended(self): + class Holder(ObjectTypeHolder): + def passObjectTypeAsReference(self, objectType): + return objectType.objectName().prepend(('ThisIs')) + holder = Holder('TheObjectFromC++') + self.assertEqual(holder.callPassObjectTypeAsReference(), 'ThisIsTheObjectFromC++') + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/oddbool_test.py b/sources/shiboken2/tests/samplebinding/oddbool_test.py new file mode 100644 index 000000000..3424d9dc0 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/oddbool_test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for OddBool user's primitive type conversion.''' + +import unittest + +from sample import OddBoolUser + +class DerivedOddBoolUser (OddBoolUser): + def returnMyselfVirtual(self): + return OddBoolUser() + pass + +class OddBoolTest(unittest.TestCase): + + def testOddBoolUser(self): + obuTrue = OddBoolUser() + obuFalse = OddBoolUser() + obuTrue.setOddBool(True) + self.assertEqual(obuFalse.oddBool(), False) + self.assertEqual(obuTrue.oddBool(), True) + self.assertEqual(obuTrue.callInvertedOddBool(), False) + + self.assertEqual(obuTrue.oddBool() == True, True) + self.assertEqual(False == obuFalse.oddBool(), True) + self.assertEqual(obuTrue.oddBool() == obuFalse.oddBool(), False) + + self.assertEqual(obuFalse.oddBool() != True, True) + self.assertEqual(True != obuFalse.oddBool(), True) + self.assertEqual(obuTrue.oddBool() != obuFalse.oddBool(), True) + + def testVirtuals(self): + dobu = DerivedOddBoolUser() + self.assertEqual(dobu.invertedOddBool(), True) + + def testImplicitConversionWithUsersPrimitiveType(self): + obu = OddBoolUser(True) + self.assertTrue(obu.oddBool()) + obu = OddBoolUser(False) + self.assertFalse(obu.oddBool()) + cpx = complex(1.0, 0.0) + obu = OddBoolUser(cpx) + self.assertTrue(obu.oddBool()) + cpx = complex(0.0, 0.0) + obu = OddBoolUser(cpx) + self.assertFalse(obu.oddBool()) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/oldstyleclass_as_number_test.py b/sources/shiboken2/tests/samplebinding/oldstyleclass_as_number_test.py new file mode 100644 index 000000000..a4f52e1fe --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/oldstyleclass_as_number_test.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +import sample +from py3kcompat import IS_PY3K + +class OldStyle: + pass + +class NewStyle(object): + pass + +class OldStyleNumber: + def __init__(self, value): + self.value = value + def __trunc__(self): + return self.value + +class NewStyleNumber(object): + def __init__(self, value): + self.value = value + def __int__(self): + return int(self.value) + def __trunc__(self): + return self.value + +class TestOldStyleClassAsNumber(unittest.TestCase): + + def testBasic(self): + '''For the sake of calibration...''' + self.assertEqual(sample.acceptInt(123), 123) + + def testOldStyleClassPassedAsInteger(self): + '''Old-style classes aren't numbers and shouldn't be accepted.''' + obj = OldStyle() + self.assertRaises(TypeError, sample.acceptInt, obj) + + def testNewStyleClassPassedAsInteger(self): + '''New-style classes aren't numbers and shouldn't be accepted.''' + obj = NewStyle() + self.assertRaises(TypeError, sample.acceptInt, obj) + + def testOldStyleClassWithNumberProtocol(self): + obj = OldStyleNumber(123) + self.assertEqual(sample.acceptInt(obj), obj.value) + + def testNewStyleClassWithNumberProtocol(self): + obj = NewStyleNumber(123) + self.assertEqual(sample.acceptInt(obj), obj.value) + +if __name__ == "__main__" and not IS_PY3K: + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/onlycopyclass_test.py b/sources/shiboken2/tests/samplebinding/onlycopyclass_test.py new file mode 100644 index 000000000..647d2cdd7 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/onlycopyclass_test.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import OnlyCopy, FriendOfOnlyCopy + +class ClassWithOnlyCopyCtorTest(unittest.TestCase): + def testGetOne(self): + obj = FriendOfOnlyCopy.createOnlyCopy(123) + self.assertEqual(type(obj), OnlyCopy) + self.assertEqual(obj.value(), 123) + + def testGetMany(self): + objs = FriendOfOnlyCopy.createListOfOnlyCopy(3) + self.assertEqual(type(objs), list) + self.assertEqual(len(objs), 3) + for value, obj in enumerate(objs): + self.assertEqual(obj.value(), value) + + def testPassAsValue(self): + obj = FriendOfOnlyCopy.createOnlyCopy(123) + self.assertEqual(obj.value(), OnlyCopy.getValue(obj)) + + def testPassAsReference(self): + obj = FriendOfOnlyCopy.createOnlyCopy(123) + self.assertEqual(obj.value(), OnlyCopy.getValueFromReference(obj)) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/overflow_test.py b/sources/shiboken2/tests/samplebinding/overflow_test.py new file mode 100644 index 000000000..8a0fff45d --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/overflow_test.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for overflowing C++ numeric types.''' + +import sys +import unittest + +from sample import * +from py3kcompat import IS_PY3K, long + +class OverflowTest(unittest.TestCase): + '''Test case for overflowing C++ numeric types.''' + + def testUnsignedInt(self): + '''C++ function receives an unsigned int argument and raise OverflowError if the value is negative.''' + val = 100 + self.assertEqual(doubleUnsignedInt(val), 2 * val) + val *= -1 + self.assertRaises(OverflowError, doubleUnsignedInt, val) + + def testLongLong(self): + '''C++ function receives an long long argument and raise OverflowError if the value is negative.''' + val = 100 + self.assertEqual(doubleLongLong(val), 2 * val) + val = long(100) + self.assertEqual(doubleLongLong(val), 2 * val) + val = (2 << 64) + 1 + self.assertRaises(OverflowError, doubleLongLong, val) + + def testUnsignedLongLong(self): + '''C++ function receives an unsigned long long argument and raise OverflowError if the value is negative.''' + val = 100 + self.assertEqual(doubleUnsignedLongLong(val), 2 * val) + val = long(100) + self.assertEqual(doubleUnsignedLongLong(val), 2 * val) + val = -100 + self.assertRaises(OverflowError, doubleUnsignedLongLong, val) + val = long(-200) + self.assertRaises(OverflowError, doubleUnsignedLongLong, val) + + def testOverflow(self): + '''Calls function with unsigned int parameter using an overflowing value.''' + self.assertRaises(OverflowError, doubleUnsignedInt, 42415335332353253) + doubleUnsignedInt(0xdeadbeef) + + def testShortOverflow(self): + '''Calls function with short parameter using an overflowing value.''' + doubleShort(-3) + self.assertRaises(OverflowError, doubleShort, 0xFFFF*-1) + self.assertRaises(OverflowError, doubleShort, 0xFFFF + 1) + + def testOverflowOnCtor(self): + '''Calls object ctor with int parameter using overflowing values.''' + self.assertRaises(OverflowError, Point, 42415335332353253, 42415335332353253) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/overload_sorting_test.py b/sources/shiboken2/tests/samplebinding/overload_sorting_test.py new file mode 100644 index 000000000..30e1ad955 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/overload_sorting_test.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for overload sorting''' + +import unittest + +from sample import * + +class Dummy(object): + pass + +class SimpleOverloadSorting(unittest.TestCase): + + def setUp(self): + self.obj = SortedOverload() + + def testIntDouble(self): + '''Overloads with int and double''' + self.assertEqual(self.obj.overload(3), "int") + self.assertEqual(self.obj.overload(3.14), "double") + + def testImplicitConvert(self): + '''Overloads with implicit convertible types''' + self.assertEqual(self.obj.overload(ImplicitTarget()), "ImplicitTarget") + self.assertEqual(self.obj.overload(ImplicitBase()), "ImplicitBase") + + def testContainer(self): + '''Overloads with containers arguments''' + self.assertEqual(self.obj.overload([ImplicitBase()]), "list(ImplicitBase)") + + def testPyObject(self): + '''Overloads with PyObject args''' + self.assertEqual(self.obj.overload(Dummy()), "PyObject") + + def testImplicitOnly(self): + '''Passing an implicit convertible object to an overload''' + self.assertTrue(self.obj.implicit_overload(ImplicitTarget())) + + def testPyObjectSort(self): + self.assertEqual(self.obj.pyObjOverload(1, 2), "int,int") + self.assertEqual(self.obj.pyObjOverload(object(), 2), "PyObject,int") + + +class DeepOverloadSorting(unittest.TestCase): + + def setUp(self): + self.obj = SortedOverload() + + def testPyObject(self): + '''Deep Overload - (int, PyObject *)''' + self.assertEqual(self.obj.overloadDeep(1, Dummy()), "PyObject") + + def testImplicit(self): + '''Deep Overload - (int, ImplicitBase *)''' + self.assertEqual(self.obj.overloadDeep(1, ImplicitBase()), "ImplicitBase") + +class EnumOverIntSorting(unittest.TestCase): + def testEnumOverInt(self): + ic = ImplicitConv(ImplicitConv.CtorTwo) + self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorTwo) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/overload_test.py b/sources/shiboken2/tests/samplebinding/overload_test.py new file mode 100644 index 000000000..efcd3bed8 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/overload_test.py @@ -0,0 +1,209 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Overload class''' + +import unittest +from sample import Echo, Overload, Point, PointF, Polygon, Rect, RectF, Size, Str + + +def raisesWithErrorMessage(func, arguments, errorType, errorMsg): + '''NOTE: Using 'try' because assertRaisesRegexp is not available + to check the error message.''' + try: + func(*arguments) + return False + except Exception as err: + if type(err) != TypeError: + return False + if not errorMsg in str(err): + return False + return True + + +class OverloadTest(unittest.TestCase): + '''Test case for Overload class''' + + def testOverloadMethod0(self): + '''Check overloaded method call for signature "overloaded()".''' + overload = Overload() + self.assertEqual(overload.overloaded(), Overload.Function0) + + def testOverloadMethod1(self): + '''Check overloaded method call for signature "overloaded(Size*)".''' + overload = Overload() + size = Size() + self.assertEqual(overload.overloaded(size), Overload.Function1) + + def testOverloadMethod2(self): + '''Check overloaded method call for signature "overloaded(Point*, ParamEnum)".''' + overload = Overload() + point = Point() + self.assertEqual(overload.overloaded(point, Overload.Param1), Overload.Function2) + + def testOverloadMethod3(self): + '''Check overloaded method call for signature "overloaded(const Point&)".''' + overload = Overload() + point = Point() + self.assertEqual(overload.overloaded(point), Overload.Function3) + + def testDifferentReturnTypes(self): + '''Check method calls for overloads with different return types.''' + overload = Overload() + self.assertEqual(overload.differentReturnTypes(), None) + self.assertEqual(overload.differentReturnTypes(Overload.Param1), None) + self.assertEqual(overload.differentReturnTypes(Overload.Param0, 13), 13) + + def testIntOverloads(self): + overload = Overload() + self.assertEqual(overload.intOverloads(2, 3), 2) + self.assertEqual(overload.intOverloads(2, 4.5), 3) + self.assertEqual(overload.intOverloads(Point(0, 0), 3), 1) + + def testIntDoubleOverloads(self): + overload = Overload() + self.assertEqual(overload.intDoubleOverloads(1, 2), Overload.Function0) + self.assertEqual(overload.intDoubleOverloads(1, 2.0), Overload.Function0) + self.assertEqual(overload.intDoubleOverloads(1.0, 2), Overload.Function1) + self.assertEqual(overload.intDoubleOverloads(1.0, 2.0), Overload.Function1) + + def testWrapperIntIntOverloads(self): + overload = Overload() + self.assertEqual(overload.wrapperIntIntOverloads(Point(), 1, 2), Overload.Function0) + self.assertEqual(overload.wrapperIntIntOverloads(Polygon(), 1, 2), Overload.Function1) + + def testDrawTextPointAndStr(self): + overload = Overload() + self.assertEqual(overload.drawText(Point(), Str()), Overload.Function0) + self.assertEqual(overload.drawText(Point(), ''), Overload.Function0) + self.assertEqual(overload.drawText(PointF(), Str()), Overload.Function1) + self.assertEqual(overload.drawText(PointF(), ''), Overload.Function1) + + def testDrawTextRectIntStr(self): + overload = Overload() + self.assertEqual(overload.drawText(Rect(), 1, Str()), Overload.Function2) + self.assertEqual(overload.drawText(Rect(), 1, ''), Overload.Function2) + self.assertEqual(overload.drawText(RectF(), 1, Str()), Overload.Function3) + self.assertEqual(overload.drawText(RectF(), 1, ''), Overload.Function3) + + def testDrawTextRectFStrEcho(self): + overload = Overload() + self.assertEqual(overload.drawText(RectF(), Str()), Overload.Function4) + self.assertEqual(overload.drawText(RectF(), ''), Overload.Function4) + self.assertEqual(overload.drawText(RectF(), Str(), Echo()), Overload.Function4) + self.assertEqual(overload.drawText(RectF(), '', Echo()), Overload.Function4) + self.assertEqual(overload.drawText(Rect(), Str()), Overload.Function4) + self.assertEqual(overload.drawText(Rect(), ''), Overload.Function4) + self.assertEqual(overload.drawText(Rect(), Str(), Echo()), Overload.Function4) + self.assertEqual(overload.drawText(Rect(), '', Echo()), Overload.Function4) + + def testDrawTextIntIntStr(self): + overload = Overload() + self.assertEqual(overload.drawText(1, 2, Str()), Overload.Function5) + self.assertEqual(overload.drawText(1, 2, ''), Overload.Function5) + + def testDrawTextIntIntIntIntStr(self): + overload = Overload() + self.assertEqual(overload.drawText(1, 2, 3, 4, 5, Str()), Overload.Function6) + self.assertEqual(overload.drawText(1, 2, 3, 4, 5, ''), Overload.Function6) + + def testDrawText2IntIntIntIntStr(self): + overload = Overload() + self.assertEqual(overload.drawText2(1, 2, 3, 4, 5, Str()), Overload.Function6) + self.assertEqual(overload.drawText2(1, 2, 3, 4, 5, ''), Overload.Function6) + self.assertEqual(overload.drawText2(1, 2, 3, 4, 5), Overload.Function6) + self.assertEqual(overload.drawText2(1, 2, 3, 4), Overload.Function6) + self.assertEqual(overload.drawText2(1, 2, 3), Overload.Function6) + + def testDrawText3(self): + overload = Overload() + self.assertEqual(overload.drawText3(Str(), Str(), Str()), Overload.Function0) + self.assertEqual(overload.drawText3('', '', ''), Overload.Function0) + self.assertEqual(overload.drawText3(1, 2, 3, 4, 5), Overload.Function1) + self.assertEqual(overload.drawText3(1, 2, 3, 4, 5), Overload.Function1) + + def testDrawText3Exception(self): + overload = Overload() + args = (Str(), Str(), Str(), 4, 5) + result = raisesWithErrorMessage(overload.drawText3, args, + TypeError, 'called with wrong argument types:') + self.assertTrue(result) + + def testDrawText4(self): + overload = Overload() + self.assertEqual(overload.drawText4(1, 2, 3), Overload.Function0) + self.assertEqual(overload.drawText4(1, 2, 3, 4, 5), Overload.Function1) + + def testAcceptSequence(self): + # Overload.acceptSequence() + overload = Overload() + self.assertEqual(overload.acceptSequence(), Overload.Function0) + + def testAcceptSequenceIntInt(self): + # Overload.acceptSequence(int,int) + overload = Overload() + self.assertEqual(overload.acceptSequence(1, 2), Overload.Function1) + + def testAcceptSequenceStrParamEnum(self): + # Overload.acceptSequence(Str,Overload::ParamEnum) + overload = Overload() + self.assertEqual(overload.acceptSequence(''), Overload.Function2) + self.assertEqual(overload.acceptSequence('', Overload.Param0), Overload.Function2) + self.assertEqual(overload.acceptSequence(Str('')), Overload.Function2) + self.assertEqual(overload.acceptSequence(Str(''), Overload.Param0), Overload.Function2) + + def testAcceptSequenceSize(self): + # Overload.acceptSequence(Size) + overload = Overload() + self.assertEqual(overload.acceptSequence(Size()), Overload.Function3) + + def testAcceptSequenceStringList(self): + # Overload.acceptSequence(const char**) + overload = Overload() + strings = ['line 1', 'line 2'] + self.assertEqual(overload.acceptSequence(strings), Overload.Function4) + args = (['line 1', 2], ) + result = raisesWithErrorMessage(overload.acceptSequence, args, + TypeError, 'The argument must be a sequence of strings.') + self.assertTrue(result) + + def testAcceptSequencePyObject(self): + # Overload.acceptSequence(void*) + overload = Overload() + class Foo(object): + pass + foo = Foo() + self.assertEqual(overload.acceptSequence(foo), Overload.Function5) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/overloadwithdefault_test.py b/sources/shiboken2/tests/samplebinding/overloadwithdefault_test.py new file mode 100644 index 000000000..9f243a573 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/overloadwithdefault_test.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import Overload, Str +from py3kcompat import b + +class OverloadTest(unittest.TestCase): + + def testNoArgument(self): + overload = Overload() + self.assertEqual(overload.strBufferOverloads(), Overload.Function2) + + def testStrArgument(self): + overload = Overload() + self.assertEqual(overload.strBufferOverloads(Str('')), Overload.Function0) + self.assertEqual(overload.strBufferOverloads(Str(''), ''), Overload.Function0) + self.assertEqual(overload.strBufferOverloads(Str(''), '', False), Overload.Function0) + + def testStringArgumentAsStr(self): + overload = Overload() + self.assertEqual(overload.strBufferOverloads('', ''), Overload.Function0) + self.assertEqual(overload.strBufferOverloads('', '', False), Overload.Function0) + + def testStringArgumentAsBuffer(self): + overload = Overload() + self.assertEqual(overload.strBufferOverloads(b'', 0), Overload.Function1) + + def testBufferArgument(self): + overload = Overload() + self.assertEqual(overload.strBufferOverloads(b(''), 0), Overload.Function1) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_argument_invalidation_test.py b/sources/shiboken2/tests/samplebinding/ownership_argument_invalidation_test.py new file mode 100644 index 000000000..5c6f31395 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_argument_invalidation_test.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Wrapper validity tests for arguments.''' + +import sys +import unittest + +from sample import Polygon, Point + +class WrapperValidityOfArgumentsTest(unittest.TestCase): + '''Wrapper validity tests for arguments.''' + + def testInvalidArgumentToMethod(self): + '''Call to method using invalidated Python wrapper as argument should raise RuntimeError.''' + poly = Polygon() + Polygon.stealOwnershipFromPython(poly) + self.assertRaises(RuntimeError, Polygon.doublePolygonScale, poly) + + def testInvalidArgumentToConstructor(self): + '''Call to constructor using invalidated Python wrapper as argument should raise RuntimeError.''' + pt = Point(1, 2) + Polygon.stealOwnershipFromPython(pt) + self.assertRaises(RuntimeError, Polygon, pt) + + def testInvalidArgumentWithImplicitConversion(self): + '''Call to method using invalidated Python wrapper to be implicitly converted should raise RuntimeError.''' + pt = Point(1, 2) + Polygon.stealOwnershipFromPython(pt) + self.assertRaises(RuntimeError, Polygon.doublePolygonScale, pt) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_delete_child_in_cpp_test.py b/sources/shiboken2/tests/samplebinding/ownership_delete_child_in_cpp_test.py new file mode 100644 index 000000000..829cabd81 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_delete_child_in_cpp_test.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests for destroy a child object in C++''' + +import unittest + +from sample import ObjectType + + +class DeleteChildInCpp(unittest.TestCase): + '''Test case for destroying a child in c++''' + + def testDeleteChild(self): + '''Delete child in C++ should invalidate child - using C++ wrapper''' + parent = ObjectType() + parent.setObjectName('parent') + child = ObjectType(parent) + child.setObjectName('child') + + parent.killChild('child') + self.assertRaises(RuntimeError, child.objectName) + self.assertEqual(parent.objectName(), 'parent') + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/ownership_delete_child_in_python_test.py b/sources/shiboken2/tests/samplebinding/ownership_delete_child_in_python_test.py new file mode 100644 index 000000000..32312af7f --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_delete_child_in_python_test.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests for deleting a child object in python''' + +import unittest +import random +import string + +from sample import ObjectType +from py3kcompat import IS_PY3K + +if IS_PY3K: + string.letters = string.ascii_letters + + +class DeleteChildInPython(unittest.TestCase): + '''Test case for deleting (unref) a child in python''' + + def testDeleteChild(self): + '''Delete child in python should not invalidate child''' + parent = ObjectType() + child = ObjectType(parent) + name = ''.join(random.sample(string.letters, 5)) + child.setObjectName(name) + + del child + new_child = parent.children()[0] + self.assertEqual(new_child.objectName(), name) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/ownership_delete_parent_test.py b/sources/shiboken2/tests/samplebinding/ownership_delete_parent_test.py new file mode 100644 index 000000000..1c135a4aa --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_delete_parent_test.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests for destroying the parent''' + +import sys +import unittest + +from sample import ObjectType + + +class DeleteParentTest(unittest.TestCase): + '''Test case for deleting a parent object''' + + def testParentDestructor(self): + '''Delete parent object should invalidate child''' + parent = ObjectType() + child = ObjectType() + child.setParent(parent) + + refcount_before = sys.getrefcount(child) + + del parent + self.assertRaises(RuntimeError, child.objectName) + self.assertEqual(sys.getrefcount(child), refcount_before-1) + + def testParentDestructorMultipleChildren(self): + '''Delete parent object should invalidate all children''' + parent = ObjectType() + children = [ObjectType() for _ in range(10)] + + for child in children: + child.setParent(parent) + + del parent + for i, child in enumerate(children): + self.assertRaises(RuntimeError, child.objectName) + self.assertEqual(sys.getrefcount(child), 4) + + def testRecursiveParentDelete(self): + '''Delete parent should invalidate grandchildren''' + parent = ObjectType() + child = ObjectType(parent) + grandchild = ObjectType(child) + + del parent + self.assertRaises(RuntimeError, child.objectName) + self.assertEqual(sys.getrefcount(child), 2) + self.assertRaises(RuntimeError, grandchild.objectName) + self.assertEqual(sys.getrefcount(grandchild), 2) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/ownership_invalidate_after_use_test.py b/sources/shiboken2/tests/samplebinding/ownership_invalidate_after_use_test.py new file mode 100644 index 000000000..8b3e84738 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_invalidate_after_use_test.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Ownership tests for cases of invalidation of Python wrapper after use.''' + +import sys +import unittest + +from sample import ObjectType, ObjectTypeDerived, Event + + +class ExtObjectType(ObjectType): + def __init__(self): + ObjectType.__init__(self) + self.type_of_last_event = None + self.last_event = None + def event(self, event): + self.last_event = event + self.type_of_last_event = event.eventType() + return True + +class MyObjectType (ObjectType): + def __init__(self): + super(MyObjectType, self).__init__() + self.fail = False + + def event(self, ev): + self.callInvalidateEvent(ev) + try: + ev.eventType() + except: + self.fail = True + raise + return True + + def invalidateEvent(self, ev): + pass + +class ExtObjectTypeDerived(ObjectTypeDerived): + def __init__(self): + ObjectTypeDerived.__init__(self) + self.type_of_last_event = None + self.last_event = None + def event(self, event): + self.last_event = event + self.type_of_last_event = event.eventType() + return True + +class OwnershipInvalidateAfterUseTest(unittest.TestCase): + '''Ownership tests for cases of invalidation of Python wrapper after use.''' + + def testInvalidateAfterUse(self): + '''In ObjectType.event(Event*) the wrapper object created for Event must me marked as invalid after the method is called.''' + eot = ExtObjectType() + eot.causeEvent(Event.SOME_EVENT) + self.assertEqual(eot.type_of_last_event, Event.SOME_EVENT) + self.assertRaises(RuntimeError, eot.last_event.eventType) + + def testObjectInvalidatedAfterUseAsParameter(self): + '''Tries to use wrapper invalidated after use as a parameter to another method.''' + eot = ExtObjectType() + ot = ObjectType() + eot.causeEvent(Event.ANY_EVENT) + self.assertEqual(eot.type_of_last_event, Event.ANY_EVENT) + self.assertRaises(RuntimeError, ot.event, eot.last_event) + + def testit(self): + obj = MyObjectType() + obj.causeEvent(Event.BASIC_EVENT) + self.assertFalse(obj.fail) + + def testInvalidateAfterUseInDerived(self): + '''Invalidate was failing in a derived C++ class that also inherited + other base classes''' + eot = ExtObjectTypeDerived() + eot.causeEvent(Event.SOME_EVENT) + self.assertEqual(eot.type_of_last_event, Event.SOME_EVENT) + self.assertRaises(RuntimeError, eot.last_event.eventType) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_invalidate_child_test.py b/sources/shiboken2/tests/samplebinding/ownership_invalidate_child_test.py new file mode 100644 index 000000000..e92fa39f5 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_invalidate_child_test.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests for invalidating a C++ created child that was already on the care of a parent.''' + +import unittest + +from sample import ObjectType, BlackBox + + +class InvalidateChildTest(unittest.TestCase): + '''Tests for invalidating a C++ created child that was already on the care of a parent.''' + + def testInvalidateChild(self): + '''Invalidating method call should remove child from the care of a parent if it has one.''' + parent = ObjectType() + child1 = ObjectType(parent) + child1.setObjectName('child1') + child2 = ObjectType.create() + child2.setParent(parent) + child2.setObjectName('child2') + + self.assertEqual(parent.children(), [child1, child2]) + + bbox = BlackBox() + + # This method steals ownership from Python to C++. + bbox.keepObjectType(child1) + self.assertEqual(parent.children(), [child2]) + + bbox.keepObjectType(child2) + self.assertEqual(parent.children(), []) + + del parent + + self.assertEqual(child1.objectName(), 'child1') + self.assertRaises(RuntimeError, child2.objectName) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_invalidate_nonpolymorphic_test.py b/sources/shiboken2/tests/samplebinding/ownership_invalidate_nonpolymorphic_test.py new file mode 100644 index 000000000..69527974c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_invalidate_nonpolymorphic_test.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''The BlackBox class has cases of ownership transference between Python and C++.''' + +import sys +import unittest + +from sample import Point, BlackBox + +class OwnershipInvalidateNonPolymorphicTest(unittest.TestCase): + '''The BlackBox class has cases of ownership transference between Python and C++.''' + + def testOwnershipTransference(self): + '''Ownership transference from Python to C++ and back again.''' + p1 = Point(10, 20) + bb = BlackBox() + p1_ticket = bb.keepPoint(p1) + self.assertRaises(RuntimeError, p1.x) + p1_ret = bb.retrievePoint(p1_ticket) + self.assertEqual(p1_ret, Point(10, 20)) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_invalidate_parent_test.py b/sources/shiboken2/tests/samplebinding/ownership_invalidate_parent_test.py new file mode 100644 index 000000000..848c982ce --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_invalidate_parent_test.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests for invalidating a parent of other objects.''' + +import unittest + +from sample import ObjectType, BlackBox + + +class InvalidateParentTest(unittest.TestCase): + '''Tests for invalidating a parent of other objects.''' + + def testInvalidateParent(self): + '''Invalidate parent should invalidate children''' + parent = ObjectType.create() + child1 = ObjectType(parent) + child1.setObjectName("child1") + child2 = ObjectType.create() + child2.setObjectName("child2") + child2.setParent(parent) + grandchild1 = ObjectType(child1) + grandchild1.setObjectName("grandchild1") + grandchild2 = ObjectType.create() + grandchild2.setObjectName("grandchild2") + grandchild2.setParent(child2) + bbox = BlackBox() + + bbox.keepObjectType(parent) # Should invalidate the parent + + self.assertRaises(RuntimeError, parent.objectName) + # some children still valid they are wrapper classes + self.assertEqual(child1.objectName(), "child1") + self.assertRaises(RuntimeError, child2.objectName) + self.assertEqual(grandchild1.objectName(), "grandchild1") + self.assertRaises(RuntimeError, grandchild2.objectName) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_reparenting_test.py b/sources/shiboken2/tests/samplebinding/ownership_reparenting_test.py new file mode 100644 index 000000000..20e6aca03 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_reparenting_test.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests for object reparenting.''' + +import unittest +import sys + +from sample import ObjectType + +class ExtObjectType(ObjectType): + def __init__(self): + ObjectType.__init__(self) + + +class ReparentingTest(unittest.TestCase): + '''Tests for object reparenting.''' + + def testReparentedObjectTypeIdentity(self): + '''Reparent children from one parent to another.''' + object_list = [] + old_parent = ObjectType() + new_parent = ObjectType() + for i in range(3): + obj = ObjectType() + object_list.append(obj) + obj.setParent(old_parent) + for obj in object_list: + obj.setParent(new_parent) + for child in new_parent.children(): + self.assertTrue(child in object_list) + + def testReparentWithTheSameParent(self): + '''Set the same parent twice to check if the ref continue the same''' + obj = ObjectType() + parent = ObjectType() + self.assertEqual(sys.getrefcount(obj), 2) + obj.setParent(parent) + self.assertEqual(sys.getrefcount(obj), 3) + obj.setParent(parent) + self.assertEqual(sys.getrefcount(obj), 3) + + def testReparentedExtObjectType(self): + '''Reparent children from one extended parent to another.''' + object_list = [] + old_parent = ExtObjectType() + new_parent = ExtObjectType() + for i in range(3): + obj = ExtObjectType() + object_list.append(obj) + obj.setParent(old_parent) + for obj in object_list: + obj.setParent(new_parent) + for orig, child in zip(object_list, new_parent.children()): + self.assertEqual(type(orig), type(child)) + + def testReparentedObjectTypeIdentityWithParentsCreatedInCpp(self): + '''Reparent children from one parent to another, both created in C++.''' + object_list = [] + old_parent = ObjectType.create() + new_parent = ObjectType.create() + for i in range(3): + obj = ObjectType() + object_list.append(obj) + obj.setParent(old_parent) + for obj in object_list: + obj.setParent(new_parent) + for child in new_parent.children(): + self.assertTrue(child in object_list) + + def testReparentedObjectTypeIdentityWithChildrenCreatedInCpp(self): + '''Reparent children created in C++ from one parent to another.''' + object_list = [] + old_parent = ObjectType() + new_parent = ObjectType() + for i in range(3): + obj = ObjectType.create() + object_list.append(obj) + obj.setParent(old_parent) + for obj in object_list: + obj.setParent(new_parent) + for child in new_parent.children(): + self.assertTrue(child in object_list) + + def testReparentedObjectTypeIdentityWithParentsAndChildrenCreatedInCpp(self): + '''Reparent children from one parent to another. Parents and children are created in C++.''' + object_list = [] + old_parent = ObjectType.create() + new_parent = ObjectType.create() + for i in range(3): + obj = ObjectType.create() + object_list.append(obj) + obj.setParent(old_parent) + for obj in object_list: + obj.setParent(new_parent) + for child in new_parent.children(): + self.assertTrue(child in object_list) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/ownership_transference_test.py b/sources/shiboken2/tests/samplebinding/ownership_transference_test.py new file mode 100644 index 000000000..0df2a09ba --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/ownership_transference_test.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''The BlackBox class has cases of ownership transference between C++ and Python.''' + +import sys +import unittest + +from sample import ObjectType, BlackBox + +class BlackBoxTest(unittest.TestCase): + '''The BlackBox class has cases of ownership transference between C++ and Python.''' + + def testOwnershipTransference(self): + '''Ownership transference from Python to C++ and back again.''' + o1 = ObjectType() + o1.setObjectName('object1') + o1_refcnt = sys.getrefcount(o1) + o2 = ObjectType() + o2.setObjectName('object2') + o2_refcnt = sys.getrefcount(o2) + bb = BlackBox() + o1_ticket = bb.keepObjectType(o1) + o2_ticket = bb.keepObjectType(o2) + self.assertEqual(set(bb.objects()), set([o1, o2])) + self.assertEqual(str(o1.objectName()), 'object1') + self.assertEqual(str(o2.objectName()), 'object2') + self.assertEqual(sys.getrefcount(o1), o1_refcnt + 1) # PySide give +1 ref to object with c++ ownership + self.assertEqual(sys.getrefcount(o2), o2_refcnt + 1) + o2 = bb.retrieveObjectType(o2_ticket) + self.assertEqual(sys.getrefcount(o2), o2_refcnt) + del bb + self.assertRaises(RuntimeError, o1.objectName) + self.assertEqual(str(o2.objectName()), 'object2') + self.assertEqual(sys.getrefcount(o2), o2_refcnt) + + def testBlackBoxReleasingUnknownObjectType(self): + '''Asks BlackBox to release an unknown ObjectType.''' + o1 = ObjectType() + o2 = ObjectType() + bb = BlackBox() + o1_ticket = bb.keepObjectType(o1) + o3 = bb.retrieveObjectType(-5) + self.assertEqual(o3, None) + + def testOwnershipTransferenceCppCreated(self): + '''Ownership transference using a C++ created object.''' + o1 = ObjectType.create() + o1.setObjectName('object1') + o1_refcnt = sys.getrefcount(o1) + bb = BlackBox() + o1_ticket = bb.keepObjectType(o1) + self.assertRaises(RuntimeError, o1.objectName) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/pair_test.py b/sources/shiboken2/tests/samplebinding/pair_test.py new file mode 100644 index 000000000..4786213c5 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/pair_test.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for std::pair container conversions''' + +import sys +import unittest + +from sample import PairUser + +class ExtendedPairUser(PairUser): + def __init__(self): + PairUser.__init__(self) + self.create_pair_called = False + + def createPair(self): + self.create_pair_called = True + return (7, 13) + +class PairConversionTest(unittest.TestCase): + '''Test case for std::pair container conversions''' + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a virtual method is correctly called from C++.''' + pu = ExtendedPairUser() + pair = pu.callCreatePair() + self.assertTrue(pu.create_pair_called) + self.assertEqual(type(pair), tuple) + self.assertEqual(type(pair[0]), int) + self.assertEqual(type(pair[1]), int) + self.assertEqual(pair, (7, 13)) + + def testPrimitiveConversionInsideContainer(self): + '''Test primitive type conversion inside conversible std::pair container.''' + cpx0 = complex(1.2, 3.4) + cpx1 = complex(5.6, 7.8) + cp = PairUser.createComplexPair(cpx0, cpx1) + self.assertEqual(type(cp), tuple) + self.assertEqual(type(cp[0]), complex) + self.assertEqual(type(cp[1]), complex) + self.assertEqual(cp, (cpx0, cpx1)) + + def testSumPair(self): + '''Test method that sums the items of a pair using values of the types expected by C++ (int and double)''' + pu = PairUser() + pair = (3, 7.13) + result = pu.sumPair(pair) + self.assertEqual(result, sum(pair)) + + def testSumPairDifferentTypes(self): + '''Test method that sums the items of a pair using values of types different from the ones expected by C++ (int and double)''' + pu = PairUser() + pair = (3.3, 7) + result = pu.sumPair(pair) + self.assertNotEqual(result, sum(pair)) + self.assertEqual(result, int(pair[0]) + pair[1]) + + def testConversionInBothDirections(self): + '''Test converting a pair from Python to C++ and the other way around.''' + pu = PairUser() + pair = (3, 5) + pu.setPair(pair) + result = pu.getPair() + self.assertEqual(result, pair) + + def testConversionInBothDirectionsWithSimilarContainer(self): + '''Test converting a list, instead of the expected tuple, from Python to C++ and the other way around.''' + pu = PairUser() + pair = [3, 5] + pu.setPair(pair) + result = pu.getPair() + self.assertNotEqual(result, pair) + self.assertEqual(result, tuple(pair)) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/pen_test.py b/sources/shiboken2/tests/samplebinding/pen_test.py new file mode 100644 index 000000000..54c96b9c8 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/pen_test.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for <add-function> with const char* as argument''' + +import unittest + +from sample import Color, Pen, SampleNamespace + +class TestPen(unittest.TestCase): + '''Simple test case for Pen.''' + + def testPenWithEmptyConstructor(self): + pen = Pen() + self.assertEqual(pen.ctorType(), Pen.EmptyCtor) + + def testPenWithEnumConstructor(self): + pen = Pen(SampleNamespace.RandomNumber) + self.assertEqual(pen.ctorType(), Pen.EnumCtor) + + def testPenWithColorConstructor(self): + pen = Pen(Color()) + self.assertEqual(pen.ctorType(), Pen.ColorCtor) + + def testPenWithCopyConstructor(self): + pen = Pen(Pen()) + self.assertEqual(pen.ctorType(), Pen.CopyCtor) + + def testPenWithIntConvertedToColor(self): + pen = Pen(1) + self.assertEqual(pen.ctorType(), Pen.ColorCtor) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/point_test.py b/sources/shiboken2/tests/samplebinding/point_test.py new file mode 100644 index 000000000..6f84ac999 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/point_test.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for Point class''' + +import sys +import unittest + +from sample import Point +from py3kcompat import unicode + +class PointTest(unittest.TestCase): + '''Test case for Point class, including operator overloads.''' + + def testConstructor(self): + '''Test Point class constructor.''' + pt = Point(5.0, 2.3) + self.assertEqual(pt.x(), 5.0) + self.assertEqual(pt.y(), 2.3) + + def testPlusOperator(self): + '''Test Point class + operator.''' + pt1 = Point(5.0, 2.3) + pt2 = Point(0.5, 3.2) + self.assertEqual(pt1 + pt2, Point(5.0 + 0.5, 2.3 + 3.2)) + + def testEqualOperator(self): + '''Test Point class == operator.''' + pt1 = Point(5.0, 2.3) + pt2 = Point(5.0, 2.3) + pt3 = Point(0.5, 3.2) + self.assertTrue(pt1 == pt1) + self.assertTrue(pt1 == pt2) + self.assertFalse(pt1 == pt3) + self.assertFalse(pt1 == object()) + + def testNotEqualOperator(self): + '''Test Point class != operator.''' + pt1 = Point(5.0, 2.3) + pt2 = Point(5.0, 2.3) + self.assertRaises(NotImplementedError, pt1.__ne__, pt2) + + def testReturnNewCopy(self): + '''Point returns a copy of itself.''' + pt1 = Point(1.1, 2.3) + pt2 = pt1.copy() + self.assertEqual(pt1, pt2) + pt2 += pt1 + self.assertFalse(pt1 == pt2) + + def testReturnConstPointer(self): + '''Point returns a const pointer for itself.''' + pt1 = Point(5.0, 2.3) + refcount1 = sys.getrefcount(pt1) + pt2 = pt1.getSelf() + self.assertEqual(pt1, pt2) + self.assertEqual(sys.getrefcount(pt1), refcount1 + 1) + self.assertEqual(sys.getrefcount(pt1), sys.getrefcount(pt2)) + + def testUintOverflow(self): + pt1 = Point(0.0, 0.0) + self.assertRaises(OverflowError, pt1.setXAsUint, 840835495615213080) + self.assertEqual(pt1.x(), 0.0) + + def testAddedOperator(self): + p = Point(0.0, 0.0) + r = p - unicode('Hi') + self.assertEqual(r, unicode('Hi')) + + # now the reverse op. + r = unicode('Hi') - p + self.assertEqual(r, unicode('Hi')) + + def testModifiedMethod(self): + pt1 = Point(0.0, 0.0) + pt2 = Point(10.0, 10.0) + expected = Point((pt1.x() + pt2.x()) / 2.0, (pt1.y() + pt2.y()) / 2.0) + self.assertEqual(pt1.midpoint(pt2), expected) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/pointerholder_test.py b/sources/shiboken2/tests/samplebinding/pointerholder_test.py new file mode 100644 index 000000000..32ef99d5c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/pointerholder_test.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for a class that holds an arbitraty pointer and is modified to hold an PyObject.''' + +import sys +import unittest + +from sample import PointerHolder + +class TestPointerHolder(unittest.TestCase): + '''Test cases for a class that holds an arbitraty pointer and is modified to hold an PyObject.''' + + def testStoringAndRetrievingPointer(self): + ph = PointerHolder('Hello') + self.assertEqual(ph.pointer(), 'Hello') + a = (1, 2, 3) + ph = PointerHolder(a) + self.assertEqual(ph.pointer(), a) + + def testReferenceCounting(self): + '''Test reference counting when retrieving data with PointerHolder.pointer().''' + a = (1, 2, 3) + refcnt = sys.getrefcount(a) + ph = PointerHolder(a) + ptr = ph.pointer() + self.assertEqual(sys.getrefcount(a), refcnt + 1) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/pointf_test.py b/sources/shiboken2/tests/samplebinding/pointf_test.py new file mode 100644 index 000000000..9318dca24 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/pointf_test.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for PointF class''' + +import unittest + +from sample import PointF + +class PointFTest(unittest.TestCase): + '''Test case for PointF class, including operator overloads.''' + + def testConstructor(self): + '''Test PointF class constructor.''' + pt = PointF(5.0, 2.3) + self.assertEqual(pt.x(), 5.0) + self.assertEqual(pt.y(), 2.3) + + def testPlusOperator(self): + '''Test PointF class + operator.''' + pt1 = PointF(5.0, 2.3) + pt2 = PointF(0.5, 3.2) + self.assertEqual(pt1 + pt2, PointF(5.0 + 0.5, 2.3 + 3.2)) + + def testEqualOperator(self): + '''Test PointF class == operator.''' + pt1 = PointF(5.0, 2.3) + pt2 = PointF(5.0, 2.3) + pt3 = PointF(0.5, 3.2) + self.assertTrue(pt1 == pt1) + self.assertTrue(pt1 == pt2) + self.assertFalse(pt1 == pt3) + + def testModifiedMethod(self): + pt1 = PointF(0.0, 0.0) + pt2 = PointF(10.0, 10.0) + expected = PointF((pt1.x() + pt2.x()) / 2.0, (pt1.y() + pt2.y()) / 2.0) + self.assertEqual(pt1.midpoint(pt2), expected) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/primitivereferenceargument_test.py b/sources/shiboken2/tests/samplebinding/primitivereferenceargument_test.py new file mode 100644 index 000000000..e973c0d78 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/primitivereferenceargument_test.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +import sample + +class PrimitiveReferenceArgumentTest(unittest.TestCase): + + def testIntReferenceArgument(self): + '''C++ signature: int acceptIntReference(int&)''' + self.assertEqual(sample.acceptIntReference(123), 123) + + def testOddBoolReferenceArgument(self): + '''C++ signature: OddBool acceptOddBoolReference(OddBool&)''' + self.assertEqual(sample.acceptOddBoolReference(True), True) + self.assertEqual(sample.acceptOddBoolReference(False), False) + self.assertNotEqual(sample.acceptOddBoolReference(True), False) + self.assertNotEqual(sample.acceptOddBoolReference(False), True) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/privatector_test.py b/sources/shiboken2/tests/samplebinding/privatector_test.py new file mode 100644 index 000000000..471f735cb --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/privatector_test.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for a class with only a private constructor.''' + +import gc +import sys +import unittest + +from sample import PrivateCtor + + +class PrivateCtorTest(unittest.TestCase): + '''Test case for PrivateCtor class''' + + def testPrivateCtorInstanciation(self): + '''Test if instanciation of class with a private constructor raises an exception.''' + self.assertRaises(TypeError, PrivateCtor) + + def testPrivateCtorInheritance(self): + '''Test if inheriting from PrivateCtor raises an exception.''' + def inherit(): + class Foo(PrivateCtor): + pass + self.assertRaises(TypeError, inherit) + + def testPrivateCtorInstanceMethod(self): + '''Test if PrivateCtor.instance() method return the proper singleton.''' + pd1 = PrivateCtor.instance() + calls = pd1.instanceCalls() + self.assertEqual(type(pd1), PrivateCtor) + pd2 = PrivateCtor.instance() + self.assertEqual(pd2, pd1) + self.assertEqual(pd2.instanceCalls(), calls + 1) + + def testPrivateCtorRefCounting(self): + '''Test refcounting of the singleton returned by PrivateCtor.instance().''' + pd1 = PrivateCtor.instance() + calls = pd1.instanceCalls() + refcnt = sys.getrefcount(pd1) + pd2 = PrivateCtor.instance() + self.assertEqual(pd2.instanceCalls(), calls + 1) + self.assertEqual(sys.getrefcount(pd2), sys.getrefcount(pd1)) + self.assertEqual(sys.getrefcount(pd2), refcnt + 1) + del pd1 + self.assertEqual(sys.getrefcount(pd2), refcnt) + del pd2 + gc.collect() + pd3 = PrivateCtor.instance() + self.assertEqual(type(pd3), PrivateCtor) + self.assertEqual(pd3.instanceCalls(), calls + 2) + self.assertEqual(sys.getrefcount(pd3), refcnt) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/privatedtor_test.py b/sources/shiboken2/tests/samplebinding/privatedtor_test.py new file mode 100644 index 000000000..3048a9848 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/privatedtor_test.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for a class with a private destructor.''' + +import gc +import sys +import unittest + +import shiboken2 as shiboken +from sample import PrivateDtor + + +class PrivateDtorTest(unittest.TestCase): + '''Test case for PrivateDtor class''' + + def testPrivateDtorInstanciation(self): + '''Test if instanciation of class with a private destructor raises an exception.''' + self.assertRaises(TypeError, PrivateDtor) + + def testPrivateDtorInheritance(self): + '''Test if inheriting from PrivateDtor raises an exception.''' + def inherit(): + class Foo(PrivateDtor): + pass + self.assertRaises(TypeError, inherit) + + def testPrivateDtorInstanceMethod(self): + '''Test if PrivateDtor.instance() method return the proper singleton.''' + pd1 = PrivateDtor.instance() + calls = pd1.instanceCalls() + self.assertEqual(type(pd1), PrivateDtor) + pd2 = PrivateDtor.instance() + self.assertEqual(pd2, pd1) + self.assertEqual(pd2.instanceCalls(), calls + 1) + + def testPrivateDtorRefCounting(self): + '''Test refcounting of the singleton returned by PrivateDtor.instance().''' + pd1 = PrivateDtor.instance() + calls = pd1.instanceCalls() + refcnt = sys.getrefcount(pd1) + pd2 = PrivateDtor.instance() + self.assertEqual(pd2.instanceCalls(), calls + 1) + self.assertEqual(sys.getrefcount(pd2), sys.getrefcount(pd1)) + self.assertEqual(sys.getrefcount(pd2), refcnt + 1) + del pd1 + self.assertEqual(sys.getrefcount(pd2), refcnt) + del pd2 + gc.collect() + pd3 = PrivateDtor.instance() + self.assertEqual(type(pd3), PrivateDtor) + self.assertEqual(pd3.instanceCalls(), calls + 2) + self.assertEqual(sys.getrefcount(pd3), refcnt) + + def testClassDecref(self): + # Bug was that class PyTypeObject wasn't decrefed when instance + # was invalidated + + before = sys.getrefcount(PrivateDtor) + + for i in range(1000): + obj = PrivateDtor.instance() + shiboken.invalidate(obj) + + after = sys.getrefcount(PrivateDtor) + + self.assertLess(abs(before - after), 5) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/protected_test.py b/sources/shiboken2/tests/samplebinding/protected_test.py new file mode 100644 index 000000000..9201a63cf --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/protected_test.py @@ -0,0 +1,357 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for protected methods.''' + +import unittest + +from sample import cacheSize +from sample import ProtectedNonPolymorphic, ProtectedVirtualDestructor +from sample import ProtectedPolymorphic, ProtectedPolymorphicDaughter, ProtectedPolymorphicGrandDaughter +from sample import createProtectedProperty, ProtectedProperty, ProtectedEnumClass +from sample import PrivateDtor +from sample import Event, ObjectType, Point + +class ExtendedProtectedPolymorphic(ProtectedPolymorphic): + def __init__(self, name): + ProtectedPolymorphic.__init__(self, name) + self.protectedName_called = False + def protectedName(self): + self.protectedName_called = True + self._name = 'Extended' + ProtectedPolymorphic.protectedName(self) + return self._name + +class ExtendedProtectedPolymorphicDaughter(ProtectedPolymorphicDaughter): + def __init__(self, name): + self.protectedName_called = False + ProtectedPolymorphicDaughter.__init__(self, name) + def protectedName(self): + self.protectedName_called = True + self._name = 'ExtendedDaughter' + ProtectedPolymorphicDaughter.protectedName(self) + return self._name + +class ExtendedProtectedPolymorphicGrandDaughter(ProtectedPolymorphicGrandDaughter): + def __init__(self, name): + self.protectedName_called = False + ProtectedPolymorphicGrandDaughter.__init__(self, name) + def protectedName(self): + self.protectedName_called = True + self._name = 'ExtendedGrandDaughter' + ProtectedPolymorphicGrandDaughter.protectedName(self) + return self._name + +class ExtendedProtectedVirtualDestructor(ProtectedVirtualDestructor): + def __init__(self): + ProtectedVirtualDestructor.__init__(self) + +class ProtectedNonPolymorphicTest(unittest.TestCase): + '''Test cases for protected method in a class without virtual methods.''' + + def tearDown(self): + self.assertEqual(cacheSize(), 0) + + def testProtectedCall(self): + '''Calls a non-virtual protected method.''' + p = ProtectedNonPolymorphic('NonPoly') + self.assertEqual(p.publicName(), p.protectedName()) + a0, a1 = 1, 2 + self.assertEqual(p.protectedSum(a0, a1), a0 + a1) + + def testProtectedCallWithInstanceCreatedOnCpp(self): + '''Calls a non-virtual protected method on an instance created in C++.''' + p = ProtectedNonPolymorphic.create() + self.assertEqual(p.publicName(), p.protectedName()) + a0, a1 = 1, 2 + self.assertEqual(p.protectedSum(a0, a1), a0 + a1) + + def testModifiedProtectedCall(self): + '''Calls a non-virtual protected method modified with code injection.''' + p = ProtectedNonPolymorphic('NonPoly') + self.assertEqual(p.dataTypeName(), 'integer') + self.assertEqual(p.dataTypeName(1), 'integer') + self.assertEqual(p.dataTypeName(Point(1, 2)), 'pointer') + +class ProtectedPolymorphicTest(unittest.TestCase): + '''Test cases for protected method in a class with virtual methods.''' + + def tearDown(self): + self.assertEqual(cacheSize(), 0) + + def testProtectedCall(self): + '''Calls a virtual protected method.''' + p = ProtectedNonPolymorphic('Poly') + self.assertEqual(p.publicName(), p.protectedName()) + a0, a1 = 1, 2 + self.assertEqual(p.protectedSum(a0, a1), a0 + a1) + + def testProtectedCallWithInstanceCreatedOnCpp(self): + '''Calls a virtual protected method on an instance created in C++.''' + p = ProtectedPolymorphic.create() + self.assertEqual(p.publicName(), p.protectedName()) + self.assertEqual(p.callProtectedName(), p.protectedName()) + + def testReimplementedProtectedCall(self): + '''Calls a reimplemented virtual protected method.''' + original_name = 'Poly' + p = ExtendedProtectedPolymorphic(original_name) + name = p.callProtectedName() + self.assertTrue(p.protectedName_called) + self.assertEqual(p.protectedName(), name) + self.assertEqual(ProtectedPolymorphic.protectedName(p), original_name) +class ProtectedPolymorphicDaugherTest(unittest.TestCase): + '''Test cases for protected method in a class inheriting for a class with virtual methods.''' + + def testProtectedCallWithInstanceCreatedOnCpp(self): + '''Calls a virtual protected method from parent class on an instance created in C++.''' + p = ProtectedPolymorphicDaughter.create() + self.assertEqual(p.publicName(), p.protectedName()) + self.assertEqual(p.callProtectedName(), p.protectedName()) + + def testReimplementedProtectedCall(self): + '''Calls a reimplemented virtual protected method from parent class.''' + original_name = 'Poly' + p = ExtendedProtectedPolymorphicDaughter(original_name) + name = p.callProtectedName() + self.assertTrue(p.protectedName_called) + self.assertEqual(p.protectedName(), name) + self.assertEqual(ProtectedPolymorphicDaughter.protectedName(p), original_name) + + +class ProtectedPolymorphicGrandDaugherTest(unittest.TestCase): + '''Test cases for protected method in a class inheriting for a class that inherits from + another with protected virtual methods.''' + + def tearDown(self): + self.assertEqual(cacheSize(), 0) + + def testProtectedCallWithInstanceCreatedOnCpp(self): + '''Calls a virtual protected method from parent class on an instance created in C++.''' + p = ProtectedPolymorphicGrandDaughter.create() + self.assertEqual(p.publicName(), p.protectedName()) + self.assertEqual(p.callProtectedName(), p.protectedName()) + + def testReimplementedProtectedCall(self): + '''Calls a reimplemented virtual protected method from parent class.''' + original_name = 'Poly' + p = ExtendedProtectedPolymorphicGrandDaughter(original_name) + name = p.callProtectedName() + self.assertTrue(p.protectedName_called) + self.assertEqual(p.protectedName(), name) + self.assertEqual(ProtectedPolymorphicGrandDaughter.protectedName(p), original_name) + +class ProtectedVirtualDtorTest(unittest.TestCase): + '''Test cases for protected virtual destructor.''' + + def setUp(self): + ProtectedVirtualDestructor.resetDtorCounter() + + def tearDown(self): + self.assertEqual(cacheSize(), 0) + + def testVirtualProtectedDtor(self): + '''Original protected virtual destructor is being called.''' + dtor_called = ProtectedVirtualDestructor.dtorCalled() + for i in range(1, 10): + pvd = ProtectedVirtualDestructor() + del pvd + self.assertEqual(ProtectedVirtualDestructor.dtorCalled(), dtor_called + i) + + def testVirtualProtectedDtorOnCppCreatedObject(self): + '''Original protected virtual destructor is being called for a C++ created object.''' + dtor_called = ProtectedVirtualDestructor.dtorCalled() + for i in range(1, 10): + pvd = ProtectedVirtualDestructor.create() + del pvd + self.assertEqual(ProtectedVirtualDestructor.dtorCalled(), dtor_called + i) + + def testProtectedDtorOnDerivedClass(self): + '''Original protected virtual destructor is being called for a derived class.''' + dtor_called = ExtendedProtectedVirtualDestructor.dtorCalled() + for i in range(1, 10): + pvd = ExtendedProtectedVirtualDestructor() + del pvd + self.assertEqual(ExtendedProtectedVirtualDestructor.dtorCalled(), dtor_called + i) + + +class ExtendedProtectedEnumClass(ProtectedEnumClass): + def __init__(self): + ProtectedEnumClass.__init__(self) + def protectedEnumMethod(self, value): + if value == ProtectedEnumClass.ProtectedItem0: + return ProtectedEnumClass.ProtectedItem1 + return ProtectedEnumClass.ProtectedItem0 + def publicEnumMethod(self, value): + if value == ProtectedEnumClass.PublicItem0: + return ProtectedEnumClass.PublicItem1 + return ProtectedEnumClass.PublicItem0 + +class ProtectedEnumTest(unittest.TestCase): + '''Test cases for protected enum.''' + + def tearDown(self): + self.assertEqual(cacheSize(), 0) + + def testProtectedMethodWithProtectedEnumArgument(self): + '''Calls protected method with protected enum argument.''' + obj = ProtectedEnumClass() + + self.assertEqual(type(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedEnum) + + self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem0) + self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem1) + + self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem0) + self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem1) + + def testProtectedMethodWithPublicEnumArgument(self): + '''Calls protected method with public enum argument.''' + obj = ProtectedEnumClass() + + self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem0) + self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem1) + + self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem0) + self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem1) + + def testOverriddenProtectedMethodWithProtectedEnumArgument(self): + '''Calls overridden protected method with protected enum argument.''' + obj = ExtendedProtectedEnumClass() + + self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem1) + self.assertEqual(obj.protectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem0) + + self.assertEqual(ProtectedEnumClass.protectedEnumMethod(obj, ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem0) + self.assertEqual(ProtectedEnumClass.protectedEnumMethod(obj, ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem1) + + self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem0), ProtectedEnumClass.ProtectedItem1) + self.assertEqual(obj.callProtectedEnumMethod(ProtectedEnumClass.ProtectedItem1), ProtectedEnumClass.ProtectedItem0) + + def testOverriddenProtectedMethodWithPublicEnumArgument(self): + '''Calls overridden protected method with public enum argument.''' + obj = ExtendedProtectedEnumClass() + + self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem1) + self.assertEqual(obj.publicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem0) + + self.assertEqual(ProtectedEnumClass.publicEnumMethod(obj, ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem0) + self.assertEqual(ProtectedEnumClass.publicEnumMethod(obj, ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem1) + + self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem0), ProtectedEnumClass.PublicItem1) + self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem0) + + +class ProtectedPropertyTest(unittest.TestCase): + '''Test cases for a class with a protected property (or field in C++).''' + + def setUp(self): + self.obj = ProtectedProperty() + + def tearDown(self): + del self.obj + self.assertEqual(cacheSize(), 0) + + def testProtectedProperty(self): + '''Writes and reads a protected integer property.''' + self.obj.protectedProperty = 3 + self.assertEqual(self.obj.protectedProperty, 3) + + def testProtectedContainerProperty(self): + '''Writes and reads a protected list of integers property.''' + lst = [1, 2, 3, 4] + self.obj.protectedContainerProperty = lst + self.assertEqual(self.obj.protectedContainerProperty, lst) + + def testProtectedEnumProperty(self): + '''Writes and reads a protected enum property.''' + self.obj.protectedEnumProperty = Event.SOME_EVENT + self.assertEqual(self.obj.protectedEnumProperty, Event.SOME_EVENT) + + def testProtectedValueTypeProperty(self): + '''Writes and reads a protected value type property.''' + point = Point(12, 34) + self.obj.protectedValueTypeProperty = point + self.assertEqual(self.obj.protectedValueTypeProperty, point) + self.assertFalse(self.obj.protectedValueTypeProperty is point) + pointProperty = self.obj.protectedValueTypeProperty + self.assertTrue(self.obj.protectedValueTypeProperty is pointProperty) + + def testProtectedValueTypePropertyWrapperRegistration(self): + '''Access colocated protected value type property.''' + cache_size = cacheSize() + point = Point(12, 34) + obj = createProtectedProperty() + obj.protectedValueTypeProperty + self.assertEqual(obj.protectedValueTypeProperty.copy(), + obj.protectedValueTypeProperty) + obj.protectedValueTypeProperty = point + self.assertEqual(obj.protectedValueTypeProperty, point) + self.assertFalse(obj.protectedValueTypeProperty is point) + pointProperty = obj.protectedValueTypeProperty + self.assertTrue(obj.protectedValueTypeProperty is pointProperty) + del obj, point, pointProperty + self.assertEqual(cacheSize(), cache_size) + + def testProtectedValueTypePointerProperty(self): + '''Writes and reads a protected value type pointer property.''' + pt1 = Point(12, 34) + pt2 = Point(12, 34) + self.obj.protectedValueTypePointerProperty = pt1 + self.assertEqual(self.obj.protectedValueTypePointerProperty, pt1) + self.assertEqual(self.obj.protectedValueTypePointerProperty, pt2) + self.assertTrue(self.obj.protectedValueTypePointerProperty is pt1) + self.assertFalse(self.obj.protectedValueTypePointerProperty is pt2) + + def testProtectedObjectTypeProperty(self): + '''Writes and reads a protected object type property.''' + obj = ObjectType() + self.obj.protectedObjectTypeProperty = obj + self.assertEqual(self.obj.protectedObjectTypeProperty, obj) + + +class PrivateDtorProtectedMethodTest(unittest.TestCase): + '''Test cases for classes with private destructors and protected methods.''' + + def tearDown(self): + self.assertEqual(cacheSize(), 0) + + def testProtectedMethod(self): + '''Calls protected method of a class with a private destructor.''' + obj = PrivateDtor.instance() + + self.assertEqual(type(obj), PrivateDtor) + self.assertEqual(obj.instanceCalls(), 1) + self.assertEqual(obj.instanceCalls(), obj.protectedInstanceCalls()) + obj = PrivateDtor.instance() + self.assertEqual(obj.instanceCalls(), 2) + self.assertEqual(obj.instanceCalls(), obj.protectedInstanceCalls()) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/pstrlist_test.py b/sources/shiboken2/tests/samplebinding/pstrlist_test.py new file mode 100644 index 000000000..47e89bacb --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/pstrlist_test.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +import sample + +class PStrListTest(unittest.TestCase): + + def testPStrList(self): + a = 'str0' + b = 'str1' + lst = sample.createPStrList(a, b) + self.assertEqual(lst, [a, b]) + + def testListOfPStr(self): + a = 'str0' + b = 'str1' + lst = sample.createListOfPStr(a, b) + self.assertEqual(lst, [a, b]) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/pystr_test.py b/sources/shiboken2/tests/samplebinding/pystr_test.py new file mode 100644 index 000000000..9056e96b7 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/pystr_test.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for definition of __str__ method.''' + +import sys +import unittest + +from sample import Point + +class PyStrTest(unittest.TestCase): + '''Test case for definition of __str__ method.''' + + def testPyStr(self): + '''Test case for defined __str__ method.''' + pt = Point(5, 2) + self.assertEqual(str(pt), 'Point(5.0, 2.0)') + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/python_thread_test.py b/sources/shiboken2/tests/samplebinding/python_thread_test.py new file mode 100644 index 000000000..dcd59ee1f --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/python_thread_test.py @@ -0,0 +1,116 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +#!/usr/bin/env python + +'''Tests for using Shiboken-based bindings with python threads''' + +import unittest +import threading +import sample +import time + +import logging + +#logging.basicConfig(level=logging.DEBUG) + +from random import random + +class Producer(threading.Thread): + '''Producer thread''' + + def __init__(self, bucket, max_runs, *args): + #Constructor. Receives the bucket + super(Producer, self).__init__(*args) + self.runs = 0 + self.bucket = bucket + self.max_runs = max_runs + self.production_list = [] + + def run(self): + while self.runs < self.max_runs: + value = int(random()*10) % 10 + self.bucket.push(value) + self.production_list.append(value) + logging.debug('PRODUCER - pushed %d' % value) + self.runs += 1 + #self.msleep(5) + time.sleep(0.01) + + +class Consumer(threading.Thread): + '''Consumer thread''' + def __init__(self, bucket, max_runs, *args): + #Constructor. Receives the bucket + super(Consumer, self).__init__(*args) + self.runs = 0 + self.bucket = bucket + self.max_runs = max_runs + self.consumption_list = [] + + def run(self): + while self.runs < self.max_runs: + if not self.bucket.empty(): + value = self.bucket.pop() + self.consumption_list.append(value) + logging.debug('CONSUMER - got %d' % value) + self.runs += 1 + else: + logging.debug('CONSUMER - empty bucket') + time.sleep(0.01) + +class ProducerConsumer(unittest.TestCase): + '''Basic test case for producer-consumer QThread''' + + def finishCb(self): + #Quits the application + self.app.exit(0) + + def testProdCon(self): + #QThread producer-consumer example + bucket = sample.Bucket() + prod = Producer(bucket, 10) + cons = Consumer(bucket, 10) + + prod.start() + cons.start() + + #QObject.connect(prod, SIGNAL('finished()'), self.finishCb) + #QObject.connect(cons, SIGNAL('finished()'), self.finishCb) + + prod.join() + cons.join() + + self.assertEqual(prod.production_list, cons.consumption_list) + + + + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/receive_null_cstring_test.py b/sources/shiboken2/tests/samplebinding/receive_null_cstring_test.py new file mode 100644 index 000000000..12cc67142 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/receive_null_cstring_test.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for a function that could receive a NULL pointer in a '[const] char*' parameter.''' + +import unittest + +from sample import countCharacters + +class ReceiveNullCStringTest(unittest.TestCase): + '''Test case for a function that could receive a NULL pointer in a '[const] char*' parameter.''' + + def testBasic(self): + '''The test function should be working for the basic cases.''' + a = '' + b = 'abc' + self.assertEqual(countCharacters(a), len(a)) + self.assertEqual(countCharacters(b), len(b)) + + def testReceiveNull(self): + '''The test function returns '-1' when receives a None value instead of a string.''' + self.assertEqual(countCharacters(None), -1) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/reference_test.py b/sources/shiboken2/tests/samplebinding/reference_test.py new file mode 100644 index 000000000..aaf809451 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/reference_test.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for methods that receive references to objects.''' + +import sys +import unittest + +from sample import * + +class ExtendedReference(Reference): + def __init__(self): + Reference.__init__(self) + self.uses_reference_virtual_called = False + self.uses_const_reference_virtual_called = False + self.reference_inc = 1 + self.const_reference_inc = 2 + self.multiplier = 333 + + def usesReferenceVirtual(self, ref, inc): + self.uses_reference_virtual_called = True + return ref.objId() + inc + self.reference_inc + + def usesConstReferenceVirtual(self, ref, inc): + self.uses_const_reference_virtual_called = True + return ref.objId() + inc + self.const_reference_inc + + def alterReferenceIdVirtual(self, ref): + ref.setObjId(ref.objId() * self.multiplier) + + +class ReferenceTest(unittest.TestCase): + '''Test case for methods that receive references to objects.''' + + def testMethodThatReceivesReference(self): + '''Test a method that receives a reference to an object as argument.''' + objId = 123 + r = Reference(objId) + self.assertEqual(Reference.usesReference(r), objId) + + def testCantSegFaultWhenReceiveNone(self): + '''do not segfault when receiving None as argument.''' + s = Str() + self.assertTrue(None == s) + + def testMethodThatReceivesConstReference(self): + '''Test a method that receives a const reference to an object as argument.''' + objId = 123 + r = Reference(objId) + self.assertEqual(Reference.usesConstReference(r), objId) + + def testModificationOfReference(self): + '''Tests if the identity of a reference argument is preserved when passing it to be altered in C++.''' + objId = 123 + r1 = Reference(objId) + r1.alterReferenceIdVirtual(r1) + self.assertEqual(r1.objId(), objId * Reference.multiplier()) + + def testModificationOfReferenceCallingAVirtualIndirectly(self): + '''Tests if the identity of a reference argument is preserved when passing it to be altered in C++ through a method that calls a virtual method.''' + objId = 123 + r1 = Reference(objId) + r1.callAlterReferenceIdVirtual(r1) + self.assertEqual(r1.objId(), objId * Reference.multiplier()) + + def testModificationOfReferenceCallingAReimplementedVirtualIndirectly(self): + '''Test if a Python override of a virtual method with a reference parameter called from C++ alters the argument properly.''' + objId = 123 + r = Reference(objId) + er = ExtendedReference() + result = er.callAlterReferenceIdVirtual(r) + self.assertEqual(r.objId(), objId * er.multiplier) + + def testReimplementedVirtualMethodCallWithReferenceParameter(self): + '''Test if a Python override of a virtual method with a reference parameter is correctly called from C++.''' + inc = 9 + objId = 123 + r = Reference(objId) + er = ExtendedReference() + result = er.callUsesReferenceVirtual(r, inc) + self.assertEqual(result, objId + inc + er.reference_inc) + + def testReimplementedVirtualMethodCallWithConstReferenceParameter(self): + '''Test if a Python override of a virtual method with a const reference parameter is correctly called from C++.''' + inc = 9 + objId = 123 + r = Reference(objId) + er = ExtendedReference() + result = er.callUsesConstReferenceVirtual(r, inc) + self.assertEqual(result, objId + inc + er.const_reference_inc) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/referencetopointer_test.py b/sources/shiboken2/tests/samplebinding/referencetopointer_test.py new file mode 100644 index 000000000..8d85032a0 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/referencetopointer_test.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for a reference to pointer argument type.''' + +import unittest + +from sample import VirtualMethods, Str + +class ExtendedVirtualMethods(VirtualMethods): + def __init__(self): + VirtualMethods.__init__(self) + self.prefix = 'Ext' + + def createStr(self, text): + ext_text = text + if text is not None: + ext_text = self.prefix + text + return VirtualMethods.createStr(self, ext_text) + + +class ReferenceToPointerTest(unittest.TestCase): + '''Test cases for a reference to pointer argument type.''' + + def testSimpleCallWithNone(self): + '''Simple call to createStr method with a None argument.''' + obj = VirtualMethods() + ok, string = obj.createStr(None) + self.assertFalse(ok) + self.assertEqual(string, None) + + def testSimpleCallWithString(self): + '''Simple call to createStr method with a Python string argument.''' + obj = VirtualMethods() + ok, string = obj.createStr('foo') + self.assertTrue(ok) + self.assertEqual(string, Str('foo')) + + def testCallNonReimplementedMethodWithNone(self): + '''Calls createStr method from C++ with a None argument.''' + obj = VirtualMethods() + ok, string = obj.callCreateStr(None) + self.assertFalse(ok) + self.assertEqual(string, None) + + def testCallNonReimplementedMethodWithString(self): + '''Calls createStr method from C++ with a Python string argument.''' + obj = VirtualMethods() + ok, string = obj.callCreateStr('foo') + self.assertTrue(ok) + self.assertEqual(string, Str('foo')) + + def testCallReimplementedMethodWithNone(self): + '''Calls reimplemented createStr method from C++ with a None argument.''' + obj = ExtendedVirtualMethods() + ok, string = obj.callCreateStr(None) + self.assertFalse(ok) + self.assertEqual(string, None) + + def testCallReimplementedMethodWithString(self): + '''Calls reimplemented createStr method from C++ with a Python string argument.''' + obj = ExtendedVirtualMethods() + ok, string = obj.callCreateStr('foo') + self.assertTrue(ok) + self.assertEqual(string, Str(obj.prefix + 'foo')) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/return_null_test.py b/sources/shiboken2/tests/samplebinding/return_null_test.py new file mode 100644 index 000000000..377624482 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/return_null_test.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for functions that could return a NULL pointer.''' + +import sys +import unittest + +from sample import returnNullPrimitivePointer, returnNullValueTypePointer, returnNullObjectTypePointer + +class ReturnNullTest(unittest.TestCase): + '''Test case for functions that could return a NULL pointer.''' + + def testReturnNull(self): + '''Function returns a NULL pointer to a primitive type.''' + o = returnNullPrimitivePointer() + self.assertEqual(o, None) + + def testReturnNullObjectType(self): + '''Function returns a NULL pointer to an object-type.''' + o = returnNullObjectTypePointer() + self.assertEqual(o, None) + + def testReturnNullValueType(self): + '''Function returns a NULL pointer to a value-type.''' + o = returnNullValueTypePointer() + self.assertEqual(o, None) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/richcompare_test.py b/sources/shiboken2/tests/samplebinding/richcompare_test.py new file mode 100644 index 000000000..8c5eea694 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/richcompare_test.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import * + +class TestRichCompare(unittest.TestCase): + + def testIt(self): + a = Expression(2) + b = Expression(3) + c = a + b + d = a + c < b + a + self.assertEqual(d.toString(), "((2+(2+3))<(3+2))") + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/sample-binding.txt.in b/sources/shiboken2/tests/samplebinding/sample-binding.txt.in new file mode 100644 index 000000000..317f76f09 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/sample-binding.txt.in @@ -0,0 +1,15 @@ +[generator-project] + +generator-set = shiboken + +header-file = @CMAKE_CURRENT_SOURCE_DIR@/global.h +typesystem-file = @sample_TYPESYSTEM@ + +output-directory = @CMAKE_CURRENT_BINARY_DIR@ + +include-path = @libsample_SOURCE_DIR@ + +typesystem-path = @CMAKE_CURRENT_SOURCE_DIR@ + +enable-parent-ctor-heuristic +use-isnull-as-nb_nonzero diff --git a/sources/shiboken2/tests/samplebinding/sample_test.py b/sources/shiboken2/tests/samplebinding/sample_test.py new file mode 100644 index 000000000..65d1d2871 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/sample_test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for libsample bindings module''' + +import unittest + +import sample + +class ModuleTest(unittest.TestCase): + '''Test case for module and global functions''' + + def testModuleMembers(self): + '''Test availability of classes, global functions and other members on binding''' + expected_members = set(['Abstract', 'Derived', 'Point', + 'ListUser', 'PairUser', 'MapUser', + 'gimmeComplexList', 'gimmeDouble', 'gimmeInt', + 'makeCString', 'multiplyPair', 'returnCString', + 'SampleNamespace', 'transmuteComplexIntoPoint', + 'transmutePointIntoComplex', 'sumComplexPair', + 'FirstThing', 'SecondThing', 'ThirdThing', + 'GlobalEnum', 'NoThing']) + self.assertTrue(expected_members.issubset(dir(sample))) + + def testAbstractPrintFormatEnum(self): + '''Test availability of PrintFormat enum from Abstract class''' + enum_members = set(['PrintFormat', 'Short', 'Verbose', + 'OnlyId', 'ClassNameAndId']) + self.assertTrue(enum_members.issubset(dir(sample.Abstract))) + + def testSampleNamespaceOptionEnum(self): + '''Test availability of Option enum from SampleNamespace namespace''' + enum_members = set(['Option', 'None_', 'RandomNumber', 'UnixTime']) + self.assertTrue(enum_members.issubset(dir(sample.SampleNamespace))) + + def testAddedFunctionAtModuleLevel(self): + '''Calls function added to module from type system description.''' + str1 = 'Foo' + self.assertEqual(sample.multiplyString(str1, 3), str1 * 3) + self.assertEqual(sample.multiplyString(str1, 0), str1 * 0) + + def testAddedFunctionWithVarargs(self): + '''Calls function that receives varargs added to module from type system description.''' + self.assertEqual(sample.countVarargs(1), 0) + self.assertEqual(sample.countVarargs(1, 2), 1) + self.assertEqual(sample.countVarargs(1, 2, 3, 'a', 'b', 4, (5, 6)), 6) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/simplefile_glue.cpp b/sources/shiboken2/tests/samplebinding/simplefile_glue.cpp new file mode 100644 index 000000000..a7f05eb99 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/simplefile_glue.cpp @@ -0,0 +1,34 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +if (!%CPPSELF.%FUNCTION_NAME()) { + PyObject* error_msg = PyBytes_FromFormat( + "Could not open file: \"%s\"", %CPPSELF->filename()); + PyErr_SetObject(PyExc_IOError, error_msg); + return 0; +} diff --git a/sources/shiboken2/tests/samplebinding/simplefile_test.py b/sources/shiboken2/tests/samplebinding/simplefile_test.py new file mode 100644 index 000000000..c2a1afe59 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/simplefile_test.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for SimpleFile class''' + +import os +import unittest + +from sample import SimpleFile + +class SimpleFileTest(unittest.TestCase): + '''Test cases for SimpleFile class.''' + + def setUp(self): + filename = 'simplefile%d.txt' % os.getpid() + self.existing_filename = os.path.join(os.path.curdir, filename) + self.delete_file = False + if not os.path.exists(self.existing_filename): + f = open(self.existing_filename, 'w') + for line in range(10): + f.write('sbrubbles\n') + f.close() + self.delete_file = True + + self.non_existing_filename = os.path.join(os.path.curdir, 'inexistingfile.txt') + i = 0 + while os.path.exists(self.non_existing_filename): + i += 1 + filename = 'inexistingfile-%d.txt' % i + self.non_existing_filename = os.path.join(os.path.curdir, filename) + + def tearDown(self): + if self.delete_file: + os.remove(self.existing_filename) + + def testExistingFile(self): + '''Test SimpleFile class with existing file.''' + f = SimpleFile(self.existing_filename) + self.assertEqual(f.filename(), self.existing_filename) + f.open() + self.assertNotEqual(f.size(), 0) + f.close() + + def testNonExistingFile(self): + '''Test SimpleFile class with non-existing file.''' + f = SimpleFile(self.non_existing_filename) + self.assertEqual(f.filename(), self.non_existing_filename) + self.assertRaises(IOError, f.open) + self.assertEqual(f.size(), 0) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/size_test.py b/sources/shiboken2/tests/samplebinding/size_test.py new file mode 100644 index 000000000..502539689 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/size_test.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for operator overloads on Size class''' + +import unittest + +from sample import Size + +class PointTest(unittest.TestCase): + '''Test case for Size class, including operator overloads.''' + + def testConstructor(self): + '''Test Size class constructor.''' + width, height = (5.0, 2.3) + size = Size(width, height) + self.assertEqual(size.width(), width) + self.assertEqual(size.height(), height) + self.assertEqual(size.calculateArea(), width * height) + + def testCopyConstructor(self): + '''Test Size class copy constructor.''' + width, height = (5.0, 2.3) + s1 = Size(width, height) + s2 = Size(s1) + self.assertFalse(s1 is s2) + self.assertEqual(s1, s2) + + def testPlusOperator(self): + '''Test Size class + operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(0.5, 3.2) + self.assertEqual(s1 + s2, Size(5.0 + 0.5, 2.3 + 3.2)) + + def testEqualOperator(self): + '''Test Size class == operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(5.0, 2.3) + s3 = Size(0.5, 3.2) + self.assertTrue(s1 == s1) + self.assertTrue(s1 == s2) + self.assertFalse(s1 == s3) + + def testNotEqualOperator(self): + '''Test Size class != operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(5.0, 2.3) + s3 = Size(0.5, 3.2) + self.assertFalse(s1 != s1) + self.assertFalse(s1 != s2) + self.assertTrue(s1 != s3) + + def testMinorEqualOperator(self): + '''Test Size class <= operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(5.0, 2.3) + s3 = Size(0.5, 3.2) + self.assertTrue(s1 <= s1) + self.assertTrue(s1 <= s2) + self.assertTrue(s3 <= s1) + self.assertFalse(s1 <= s3) + + def testMinorOperator(self): + '''Test Size class < operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(0.5, 3.2) + self.assertFalse(s1 < s1) + self.assertFalse(s1 < s2) + self.assertTrue(s2 < s1) + + def testMajorEqualOperator(self): + '''Test Size class >= operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(5.0, 2.3) + s3 = Size(0.5, 3.2) + self.assertTrue(s1 >= s1) + self.assertTrue(s1 >= s2) + self.assertTrue(s1 >= s3) + self.assertFalse(s3 >= s1) + + def testMajorOperator(self): + '''Test Size class > operator.''' + s1 = Size(5.0, 2.3) + s2 = Size(0.5, 3.2) + self.assertFalse(s1 > s1) + self.assertTrue(s1 > s2) + self.assertFalse(s2 > s1) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/static_nonstatic_methods_test.py b/sources/shiboken2/tests/samplebinding/static_nonstatic_methods_test.py new file mode 100644 index 000000000..6451c4440 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/static_nonstatic_methods_test.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for overloads involving static and non-static versions of a method.''' + +import os +import unittest + +from sample import SimpleFile + +class SimpleFile2 (SimpleFile): + def exists(self): + return "Mooo" + +class SimpleFile3 (SimpleFile): + pass + +class SimpleFile4 (SimpleFile): + exists = 5 + +class StaticNonStaticMethodsTest(unittest.TestCase): + '''Test cases for overloads involving static and non-static versions of a method.''' + + def setUp(self): + filename = 'simplefile%d.txt' % os.getpid() + self.existing_filename = os.path.join(os.path.curdir, filename) + self.delete_file = False + if not os.path.exists(self.existing_filename): + f = open(self.existing_filename, 'w') + for line in range(10): + f.write('sbrubbles\n') + f.close() + self.delete_file = True + + self.non_existing_filename = os.path.join(os.path.curdir, 'inexistingfile.txt') + i = 0 + while os.path.exists(self.non_existing_filename): + i += 1 + filename = 'inexistingfile-%d.txt' % i + self.non_existing_filename = os.path.join(os.path.curdir, filename) + + def tearDown(self): + if self.delete_file: + os.remove(self.existing_filename) + + def testCallingStaticMethodWithClass(self): + '''Call static method using class.''' + self.assertTrue(SimpleFile.exists(self.existing_filename)) + self.assertFalse(SimpleFile.exists(self.non_existing_filename)) + + def testCallingStaticMethodWithInstance(self): + '''Call static method using instance of class.''' + f = SimpleFile(self.non_existing_filename) + self.assertTrue(f.exists(self.existing_filename)) + self.assertFalse(f.exists(self.non_existing_filename)) + + def testCallingInstanceMethod(self): + '''Call instance method.''' + f1 = SimpleFile(self.non_existing_filename) + self.assertFalse(f1.exists()) + f2 = SimpleFile(self.existing_filename) + self.assertTrue(f2.exists()) + + def testOverridingStaticNonStaticMethod(self): + f = SimpleFile2(self.existing_filename) + self.assertEqual(f.exists(), "Mooo") + + f = SimpleFile3(self.existing_filename) + self.assertTrue(f.exists()) + + f = SimpleFile4(self.existing_filename) + self.assertEqual(f.exists, 5) + + def testDuckPunchingStaticNonStaticMethod(self): + f = SimpleFile(self.existing_filename) + f.exists = lambda : "Meee" + self.assertEqual(f.exists(), "Meee") + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/str_test.py b/sources/shiboken2/tests/samplebinding/str_test.py new file mode 100644 index 000000000..dca7e1f44 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/str_test.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for a method that receives a reference to class that is implicitly convertible from a Python native type.''' + +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.assertEqual(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!') + + def testSequenceOperators(self): + s1 = Str("abcdef") + self.assertEqual(len(s1), 6); + self.assertEqual(len(Str()), 0); + + # getitem + self.assertEqual(s1[0], "a"); + self.assertEqual(s1[1], "b"); + self.assertEqual(s1[2], "c"); + self.assertEqual(s1[3], "d"); + self.assertEqual(s1[4], "e"); + self.assertEqual(s1[5], "f"); + self.assertEqual(s1[-1], "f"); + self.assertEqual(s1[-2], "e"); + + self.assertRaises(TypeError, s1.__getitem__, 6) + + # setitem + s1[0] = 'A' + s1[1] = 'B' + self.assertEqual(s1[0], 'A'); + self.assertEqual(s1[1], 'B'); + self.assertRaises(TypeError, s1.__setitem__(6, 67)) + + def testReverseOperator(self): + s1 = Str("hello") + n1 = 2 + self.assertEqual(s1+2, "hello2") + self.assertEqual(2+s1, "2hello") + + def testToIntError(self): + self.assertEqual(Str('Z').toInt(), (0, False)) + + def testToIntWithDecimal(self): + decimal = Str('37') + val, ok = decimal.toInt() + self.assertEqual(type(val), int) + self.assertEqual(type(ok), bool) + self.assertEqual(val, int(str(decimal))) + + def testToIntWithOctal(self): + octal = Str('52') + val, ok = octal.toInt(8) + self.assertEqual(type(val), int) + self.assertEqual(type(ok), bool) + self.assertEqual(val, int(str(octal), 8)) + + def testToIntWithHexadecimal(self): + hexa = Str('2A') + val, ok = hexa.toInt(16) + self.assertEqual(type(val), int) + self.assertEqual(type(ok), bool) + self.assertEqual(val, int(str(hexa), 16)) + self.assertEqual(hexa.toInt(), (0, False)) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/strlist_test.py b/sources/shiboken2/tests/samplebinding/strlist_test.py new file mode 100644 index 000000000..341fcfa32 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/strlist_test.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for StrList class that inherits from std::list<Str>.''' + +import unittest + +from sample import Str, StrList + +class StrListTest(unittest.TestCase): + '''Test cases for StrList class that inherits from std::list<Str>.''' + + def testStrListCtor_NoParams(self): + '''StrList constructor receives no parameter.''' + sl = StrList() + self.assertEqual(len(sl), 0) + self.assertEqual(sl.constructorUsed(), StrList.NoParamsCtor) + + def testStrListCtor_Str(self): + '''StrList constructor receives a Str object.''' + s = Str('Foo') + sl = StrList(s) + self.assertEqual(len(sl), 1) + self.assertEqual(sl[0], s) + self.assertEqual(sl.constructorUsed(), StrList.StrCtor) + + def testStrListCtor_PythonString(self): + '''StrList constructor receives a Python string.''' + s = 'Foo' + sl = StrList(s) + self.assertEqual(len(sl), 1) + self.assertEqual(sl[0], s) + self.assertEqual(sl.constructorUsed(), StrList.StrCtor) + + def testStrListCtor_StrList(self): + '''StrList constructor receives a StrList object.''' + sl1 = StrList(Str('Foo')) + sl2 = StrList(sl1) + #self.assertEqual(len(sl1), len(sl2)) + #self.assertEqual(sl1, sl2) + self.assertEqual(sl2.constructorUsed(), StrList.CopyCtor) + + def testStrListCtor_ListOfStrs(self): + '''StrList constructor receives a Python list of Str objects.''' + strs = [Str('Foo'), Str('Bar')] + sl = StrList(strs) + self.assertEqual(len(sl), len(strs)) + self.assertEqual(sl, strs) + self.assertEqual(sl.constructorUsed(), StrList.ListOfStrCtor) + + def testStrListCtor_MixedListOfStrsAndPythonStrings(self): + '''StrList constructor receives a Python list of mixed Str objects and Python strings.''' + strs = [Str('Foo'), 'Bar'] + sl = StrList(strs) + self.assertEqual(len(sl), len(strs)) + self.assertEqual(sl, strs) + self.assertEqual(sl.constructorUsed(), StrList.ListOfStrCtor) + + def testCompareStrListWithTupleOfStrs(self): + '''Compares StrList with a Python tuple of Str objects.''' + sl = StrList() + sl.append(Str('Foo')) + sl.append(Str('Bar')) + self.assertEqual(len(sl), 2) + self.assertEqual(sl, (Str('Foo'), Str('Bar'))) + + def testCompareStrListWithTupleOfPythonStrings(self): + '''Compares StrList with a Python tuple of Python strings.''' + sl = StrList() + sl.append(Str('Foo')) + sl.append(Str('Bar')) + self.assertEqual(len(sl), 2) + self.assertEqual(sl, ('Foo', 'Bar')) + + def testCompareStrListWithTupleOfStrAndPythonString(self): + '''Compares StrList with a Python tuple of mixed Str objects and Python strings.''' + sl = StrList() + sl.append(Str('Foo')) + sl.append(Str('Bar')) + self.assertEqual(len(sl), 2) + self.assertEqual(sl, (Str('Foo'), 'Bar')) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/templateinheritingclass_test.py b/sources/shiboken2/tests/samplebinding/templateinheritingclass_test.py new file mode 100644 index 000000000..7acb6a597 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/templateinheritingclass_test.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import Photon + +'''This tests classes that inherit from template classes, +simulating a situation found in Qt's phonon module.''' + +class TemplateInheritingClassTest(unittest.TestCase): + def testClassBasics(self): + self.assertEqual(Photon.ValueIdentity.classType(), Photon.IdentityType) + self.assertEqual(Photon.ValueDuplicator.classType(), Photon.DuplicatorType) + + def testInstanceBasics(self): + value = 123 + samer = Photon.ValueIdentity(value) + self.assertEqual(samer.multiplicator(), 1) + doubler = Photon.ValueDuplicator(value) + self.assertEqual(doubler.multiplicator(), 2) + self.assertEqual(samer.value(), doubler.value()) + self.assertEqual(samer.calculate() * 2, doubler.calculate()) + + def testPassToFunctionAsPointer(self): + obj = Photon.ValueDuplicator(123) + self.assertEqual(Photon.callCalculateForValueDuplicatorPointer(obj), obj.calculate()) + + def testPassToFunctionAsReference(self): + obj = Photon.ValueDuplicator(321) + self.assertEqual(Photon.callCalculateForValueDuplicatorReference(obj), obj.calculate()) + + def testPassToMethodAsValue(self): + value1, value2 = 123, 321 + one = Photon.ValueIdentity(value1) + other = Photon.ValueIdentity(value2) + self.assertEqual(one.sumValueUsingPointer(other), value1 + value2) + + def testPassToMethodAsReference(self): + value1, value2 = 123, 321 + one = Photon.ValueDuplicator(value1) + other = Photon.ValueDuplicator(value2) + self.assertEqual(one.sumValueUsingReference(other), value1 + value2) + + def testPassPointerThrough(self): + obj1 = Photon.ValueIdentity(123) + self.assertEqual(obj1, obj1.passPointerThrough(obj1)) + obj2 = Photon.ValueDuplicator(321) + self.assertEqual(obj2, obj2.passPointerThrough(obj2)) + self.assertRaises(TypeError, obj1.passPointerThrough, obj2) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/time_test.py b/sources/shiboken2/tests/samplebinding/time_test.py new file mode 100644 index 000000000..db8818fd3 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/time_test.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for constructor and method signature decisor on Time class.''' + +import sys +import unittest +import datetime + +from sample import Time, ImplicitConv, ObjectType + +class TimeTest(unittest.TestCase): + '''Test cases for constructor and method signature decisor on Time class. + The constructor and one method have these signatures: CTORMETHOD() and + CTORMETHOD(int h, int m, int s = 0, int ms = 0); there another method + with a more complex signature METH(int, int, ImplicitConv=DEFVALUE, ObjectType=0), + to produce an even worse scenario. + ''' + + def testConstructorWithoutParamers(self): + '''Constructor without parameters: Time()''' + time = Time() + self.assertTrue(time.isNull()) + + def testConstructorWithAllParamers(self): + '''Constructor with all parameters: Time(int h, int m, int s = 0, int ms = 0)''' + time = Time(1, 2, 3, 4) + self.assertTrue(time.toString(), '01:02:03.004') + + def testConstructorWithThreeParamers(self): + '''Constructor with 3 parameters: Time(int h, int m, int s = 0, int ms = 0)''' + time = Time(1, 2, 3) + self.assertTrue(time.toString(), '01:02:03.000') + + def testConstructorWithTwoParamers(self): + '''Constructor with 2 parameters: Time(int h, int m, int s = 0, int ms = 0)''' + time = Time(1, 2) + self.assertTrue(time.toString(), '01:02:00.000') + + def testSimpleMethodWithoutParamers(self): + '''Constructor without parameters: Time.setTime()''' + time = Time(1, 2, 3, 4) + time.setTime() + self.assertTrue(time.isNull()) + + def testSimpleMethodWithAllParamers(self): + '''Simple method with all parameters: Time.setTime(int h, int m, int s = 0, int ms = 0)''' + time = Time() + time.setTime(1, 2, 3, 4) + self.assertTrue(time.toString(), '01:02:03.004') + + def testSimpleMethodWithThreeParamers(self): + '''Simple method with 3 parameters: Time.setTime(int h, int m, int s = 0, int ms = 0)''' + time = Time() + time.setTime(1, 2, 3) + self.assertTrue(time.toString(), '01:02:03.000') + + def testSimpleMethodWithTwoParamers(self): + '''Simple method with 2 parameters: Time.setTime(int h, int m, int s = 0, int ms = 0)''' + time = Time() + time.setTime(1, 2) + self.assertTrue(time.toString(), '01:02:00.000') + + def testMethodWithoutParamers(self): + '''Method without parameters: Time.somethingCompletelyDifferent()''' + time = Time() + result = time.somethingCompletelyDifferent() + self.assertEqual(result, Time.ZeroArgs) + + def testMethodWithAllParamers(self): + '''Method with all parameters: + Time.somethingCompletelyDifferent( + int h, int m, ImplicitConv ic = ImplicitConv::CtorThree, ObjectType* type = 0 + ); + ''' + time = Time() + obj = ObjectType() + result = time.somethingCompletelyDifferent(1, 2, ImplicitConv(2), obj) + self.assertEqual(result, Time.FourArgs) + + def testMethodWithThreeParamers(self): + '''Method with 3 parameters: Time.somethingCompletelyDifferent(...)''' + time = Time() + result = time.somethingCompletelyDifferent(1, 2, ImplicitConv(ImplicitConv.CtorOne)) + self.assertEqual(result, Time.ThreeArgs) + + def testMethodWithTwoParamers(self): + '''Method with 2 parameters: Time.somethingCompletelyDifferent(...)''' + time = Time() + result = time.somethingCompletelyDifferent(1, 2) + self.assertEqual(result, Time.TwoArgs) + + def testMethodWithThreeParamersAndImplicitConversion(self): + '''Method with 3 parameters, the last one triggers an implicit conversion.''' + time = Time() + result = time.somethingCompletelyDifferent(1, 2, ImplicitConv.CtorOne) + self.assertEqual(result, Time.ThreeArgs) + + def testCompareWithPythonTime(self): + time = Time(12, 32, 5) + py = datetime.time(12, 32, 5) + self.assertEqual(time, py) + + def testNotEqual(self): + time = Time(12, 32, 6) + py = datetime.time(12, 32, 5) + self.assertNotEqual(time, py) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/transform_test.py b/sources/shiboken2/tests/samplebinding/transform_test.py new file mode 100644 index 000000000..f490945b8 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/transform_test.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for argument modification with more than nine arguments.''' + +import unittest + +from sample import Point, applyHomogeneousTransform + +class TransformTest(unittest.TestCase): + '''Test cases for modifying a function with > 9 arguments.''' + + def testTransform_ValidMatrix(self): + '''Transform applies successfully.''' + p = Point(3, 4) + r = applyHomogeneousTransform(p, 0, 1, 0, -1, 0, 0, 0, 0, 1) + self.assertTrue(type(r) is Point) + self.assertEqual(r.x(), 4) + self.assertEqual(r.y(), -3) + + def testTransform_InvalidMatrix(self): + '''Transform does not apply successfully.''' + p = Point(3, 4) + r = applyHomogeneousTransform(p, 1, 0, 0, 0, 1, 0, 0, 0, 0) + self.assertTrue(r is None) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/typeconverters_test.py b/sources/shiboken2/tests/samplebinding/typeconverters_test.py new file mode 100644 index 000000000..b3b469c56 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/typeconverters_test.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Tests various usages of the type converters.''' + +import unittest +import sample + +class GetPythonTypeByNameTest(unittest.TestCase): + + '''Uses an added function with inject code that uses the libshiboken + function "Shiboken::Conversions::getPythonTypeObject(typeName)".''' + + def testGetObjectType(self): + pyType1 = sample.getPythonType('ObjectType') + self.assertEqual(pyType1, sample.ObjectType) + pyType2 = sample.getPythonType('ObjectType*') + self.assertEqual(pyType2, sample.ObjectType) + self.assertEqual(pyType1, pyType2) + + def testGetValueType(self): + pyType1 = sample.getPythonType('Point') + self.assertEqual(pyType1, sample.Point) + pyType2 = sample.getPythonType('Point*') + self.assertEqual(pyType2, sample.Point) + self.assertEqual(pyType1, pyType2) + + def testGetUsersPrimitiveType(self): + pyType = sample.getPythonType('OddBool') + self.assertEqual(pyType, bool) + + def testGetUsersPrimitiveTypeWithoutTargetLangApiName(self): + '''If the primitive type attribute "target-lang-api-name" is not set + there'll be no Python type associated with the C++ type.''' + pyType = sample.getPythonType('PStr') + self.assertEqual(pyType, None) + + def testPrimitiveTypeAndTypedef(self): + pyType = sample.getPythonType('double') + self.assertEqual(pyType, float) + pyTypedef = sample.getPythonType('real') + self.assertEqual(pyType, pyTypedef) + + def testPairContainerType(self): + pyType = sample.getPythonType('std::pair<Complex,int>') + self.assertEqual(pyType, list) + + def testListContainerType(self): + pyType = sample.getPythonType('std::list<int>') + self.assertEqual(pyType, list) + + def testMapContainerType(self): + pyType = sample.getPythonType('std::map<std::string,int>') + self.assertEqual(pyType, dict) + + def testGlobalEnumType(self): + pyType = sample.getPythonType('GlobalEnum') + self.assertEqual(pyType, sample.GlobalEnum) + + def testScopedEnumType(self): + pyType = sample.getPythonType('Abstract::Type') + self.assertEqual(pyType, sample.Abstract.Type) + + +class CheckValueAndObjectTypeByNameTest(unittest.TestCase): + + '''Uses an added function with inject code that uses the libshiboken + functions that check if a type is Object or Value, based on its converter.''' + + def testErrors(self): + '''not existent type''' + self.assertRaises(ValueError, sample.cppTypeIsValueType, 'NotExistentType') + self.assertRaises(ValueError, sample.cppTypeIsObjectType, 'NotExistentType') + + def testObjectType1(self): + self.assertTrue(sample.cppTypeIsObjectType('ObjectType')) + self.assertFalse(sample.cppTypeIsValueType('ObjectType')) + + def testObjectType2(self): + self.assertTrue(sample.cppTypeIsObjectType('ObjectType*')) + self.assertFalse(sample.cppTypeIsValueType('ObjectType*')) + + def testValueType1(self): + self.assertTrue(sample.cppTypeIsValueType('Point')) + self.assertFalse(sample.cppTypeIsObjectType('Point')) + + def testValueType2(self): + self.assertTrue(sample.cppTypeIsValueType('Point*')) + self.assertFalse(sample.cppTypeIsObjectType('Point*')) + + def testUsersPrimitiveType(self): + self.assertFalse(sample.cppTypeIsValueType('Complex')) + self.assertFalse(sample.cppTypeIsObjectType('Complex')) + + def testContainerType(self): + self.assertFalse(sample.cppTypeIsValueType('std::list<int>')) + self.assertFalse(sample.cppTypeIsObjectType('std::list<int>')) + + +class SpecificConverterTest(unittest.TestCase): + + '''Uses an added function with inject code that uses the libshiboken + adapter class "Shiboken::Conversions::SpecificConverter".''' + + def testNotExistentType(self): + conversion = sample.getConversionTypeString('NotExistentType') + self.assertEqual(conversion, 'Invalid conversion') + + def testObjectType(self): + conversion = sample.getConversionTypeString('ObjectType') + self.assertEqual(conversion, 'Pointer conversion') + conversion = sample.getConversionTypeString('ObjectType*') + self.assertEqual(conversion, 'Pointer conversion') + conversion = sample.getConversionTypeString('ObjectType&') + self.assertEqual(conversion, 'Reference conversion') + + def testValueType(self): + conversion = sample.getConversionTypeString('Point') + self.assertEqual(conversion, 'Copy conversion') + conversion = sample.getConversionTypeString('Point*') + self.assertEqual(conversion, 'Pointer conversion') + conversion = sample.getConversionTypeString('Point&') + self.assertEqual(conversion, 'Reference conversion') + + +class StringBasedConversionTest(unittest.TestCase): + + def testValueType(self): + pts = (sample.Point(1, 1), sample.Point(2, 2), sample.Point(3, 3)) + result = sample.convertValueTypeToCppAndThenToPython(pts[0], pts[1], pts[2]) + for orig, new in zip(pts, result): + self.assertEqual(orig, new) + self.assertFalse(pts[0] is result[0]) + self.assertTrue(pts[1] is result[1]) + self.assertTrue(pts[2] is result[2]) + + def testObjectType(self): + objs = (sample.ObjectType(), sample.ObjectType()) + objs[0].setObjectName('obj0') + objs[1].setObjectName('obj1') + result = sample.convertObjectTypeToCppAndThenToPython(objs[0], objs[1]) + for orig, new in zip(objs, result): + self.assertEqual(orig, new) + self.assertEqual(orig.objectName(), new.objectName()) + self.assertTrue(orig is new) + + def testContainerType(self): + lst = range(4) + result = sample.convertListOfIntegersToCppAndThenToPython(lst) + self.assertTrue(len(result), 1) + self.assertTrue(lst, result[0]) + + def testCppPrimitiveType(self): + integers = (12, 34) + result = sample.convertIntegersToCppAndThenToPython(integers[0], integers[1]) + for orig, new in zip(integers, result): + self.assertEqual(orig, new) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/typedealloc_test.py b/sources/shiboken2/tests/samplebinding/typedealloc_test.py new file mode 100644 index 000000000..b0fe763e0 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/typedealloc_test.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test deallocation of type extended in Python.''' + +import gc +import weakref +import unittest + +from sample import Point + + +class TypeDeallocTest(unittest.TestCase): + + def setUp(self): + self.called = False + + def tearDown(self): + del self.called + + def callback(self, *args): + self.called = True + + def testScopeEnd(self): + ref = None + def scope(): + class Ext(Point): + pass + o = Ext() + global ref + ref = weakref.ref(Ext, self.callback) + scope() + gc.collect() + self.assertTrue(self.called) + + def testDeleteType(self): + class Ext(Point): + pass + ref = weakref.ref(Ext, self.callback) + del Ext + gc.collect() + self.assertTrue(self.called) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/typedtordoublefree_test.py b/sources/shiboken2/tests/samplebinding/typedtordoublefree_test.py new file mode 100644 index 000000000..1106c8761 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/typedtordoublefree_test.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import ObjectType + +class TestTypeDestructorDoubleFree(unittest.TestCase): + def testTypeDestructorDoubleFree(self): + '''Causes the type destructors of two derived classes to be called.''' + def scope(): + class ExtObj1(ObjectType): + def __init__(self): + ObjectType.__init__(self) + obj = ExtObj1() + child = ObjectType(parent=obj) + self.assertEqual(obj.takeChild(child), child) + class ExtObj2(ObjectType): + def __init__(self): + ObjectType.__init__(self) + obj = ExtObj2() + child = ObjectType(parent=obj) + self.assertEqual(obj.takeChild(child), child) + scope() + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/typesystem_sample.xml b/sources/shiboken2/tests/samplebinding/typesystem_sample.xml new file mode 100644 index 000000000..089f835fc --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/typesystem_sample.xml @@ -0,0 +1,2426 @@ +<?xml version="1.0"?> +<typesystem package="sample"> + <suppress-warning text="Duplicate type entry: 'sample'" /> + <suppress-warning text="Duplicate type entry: 'SampleNamespace'" /> + + <custom-type name="str"/> + <custom-type name="PyBytes"/> + <custom-type name="PyDate"/> + <custom-type name="PyObject"/> + <custom-type name="PyUnicode"/> + + <primitive-type name="bool"/> + <primitive-type name="double"/> + <primitive-type name="real"/> + <primitive-type name="int"/> + <primitive-type name="short"/> + <primitive-type name="unsigned short"/> + <primitive-type name="ushort"/> + <primitive-type name="unsigned int" /> + <primitive-type name="signed int" /> + <primitive-type name="char"/> + <primitive-type name="unsigned char"/> + <primitive-type name="long"/> + <primitive-type name="unsigned long"/> + <primitive-type name="long long"/> + <primitive-type name="unsigned long long"/> + <primitive-type name="std::string"/> + <primitive-type name="ObjectType::Identifier"/> + + <primitive-type name="Foo::HANDLE" target-lang-api-name="PyLong"/> + + <primitive-type name="std::size_t" target-lang-api-name="PyLong"> + <conversion-rule> + <native-to-target> + return PyLong_FromSize_t(%in); + </native-to-target> + <target-to-native> + <add-conversion type="PyLong"> + %out = %OUTTYPE(PyLong_AsSsize_t(%in)); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <inject-code class="native" position="beginning"> + static bool Check2TupleOfNumbers(PyObject* pyIn) { + if (!PySequence_Check(pyIn) || !(PySequence_Size(pyIn) == 2)) + return false; + Shiboken::AutoDecRef pyReal(PySequence_GetItem(pyIn, 0)); + if (!SbkNumber_Check(pyReal)) + return false; + Shiboken::AutoDecRef pyImag(PySequence_GetItem(pyIn, 1)); + if (!SbkNumber_Check(pyImag)) + return false; + return true; + } + </inject-code> + <primitive-type name="Complex" target-lang-api-name="PyComplex"> + <include file-name="complex.h" location="global"/> + <conversion-rule> + <native-to-target> + return PyComplex_FromDoubles(%in.real(), %in.imag()); + </native-to-target> + <target-to-native> + <!-- The 'check' attribute can be derived from the 'type' attribute, + it is defined here to test the CHECKTYPE type system variable. --> + <add-conversion type="PyComplex" check="%CHECKTYPE[Complex](%in)"> + double real = PyComplex_RealAsDouble(%in); + double imag = PyComplex_ImagAsDouble(%in); + %out = %OUTTYPE(real, imag); + </add-conversion> + <add-conversion type="PySequence" check="Check2TupleOfNumbers(%in)"> + Shiboken::AutoDecRef pyReal(PySequence_GetItem(%in, 0)); + Shiboken::AutoDecRef pyImag(PySequence_GetItem(%in, 1)); + double real = %CONVERTTOCPP[double](pyReal); + double imag = %CONVERTTOCPP[double](pyImag); + %out = %OUTTYPE(real, imag); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <primitive-type name="Null"> + <include file-name="null.h" location="global"/> + <conversion-rule> + <native-to-target> + SBK_UNUSED(%in); + Py_RETURN_NONE; + </native-to-target> + <target-to-native> + <add-conversion type="PyObject" check="%in == 0 || %in == Py_None"> + %out = %OUTTYPE(%in == 0); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <primitive-type name="HANDLE" target-lang-api-name="PyComplex"> + <include file-name="handle.h" location="local"/> + <conversion-rule> + <native-to-target> + if (!%in) + Py_RETURN_NONE; + #ifdef IS_PY3K + return PyCapsule_New(%in, 0, 0); + #else + return PyCObject_FromVoidPtr(%in, 0); + #endif + </native-to-target> + <target-to-native> + <add-conversion type="PyNone"> + %out = 0; + </add-conversion> + <add-conversion check="checkPyCapsuleOrPyCObject(%in)" type="PyObject"> + void* ptr; + #ifdef IS_PY3K + ptr = PyCapsule_GetPointer(%in, 0); + #else + ptr = PyCObject_AsVoidPtr(%in); + #endif + %out = (%OUTTYPE)ptr; + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <inject-code class="native" position="beginning"> + static bool checkPyCapsuleOrPyCObject(PyObject* pyObj) + { + #ifdef IS_PY3K + return PyCapsule_CheckExact(pyObj); + #else + return PyCObject_Check(pyObj); + #endif + } + </inject-code> + + <primitive-type name="PrimitiveStructPtr"> + <include file-name="handle.h" location="local"/> + <conversion-rule> + <native-to-target> + #ifdef IS_PY3K + return PyCapsule_New(&%in, 0, 0); + #else + return PyCObject_FromVoidPtr(&%in, 0); + #endif + </native-to-target> + <target-to-native> + <add-conversion check="checkPyCapsuleOrPyCObject(%in)" type="PyObject"> + void* ptr; + #ifdef IS_PY3K + ptr = PyCapsule_GetPointer(%in, 0); + #else + ptr = PyCObject_AsVoidPtr(%in); + #endif + %out = *((%OUTTYPE*)ptr); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <primitive-type name="OddBool" target-lang-api-name="PyBool" default-constructor="OddBool(false)"> + <include file-name="oddbool.h" location="global"/> + <include file-name="complex.h" location="global"/> + <conversion-rule> + <native-to-target> + return PyBool_FromLong(%in.value()); + </native-to-target> + <target-to-native> + <add-conversion type="PyBool"> + // Tests CONVERTTOCPP macro with C++ primitive type. + bool b = %CONVERTTOCPP[bool](%in); + %out = %OUTTYPE(b); + </add-conversion> + <add-conversion type="PyComplex"> + // Tests CONVERTTOCPP macro with user's primitive type. + Complex cpx = %CONVERTTOCPP[Complex](%in); + %out = %OUTTYPE(cpx.real() != 0.0 || cpx.imag() != 0.0); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <primitive-type name="PStr"> + <include file-name="str.h" location="global"/> + <conversion-rule> + <native-to-target> + return Shiboken::String::fromCString(%in.cstring(), %in.size()); + </native-to-target> + <target-to-native> + <add-conversion type="PyUnicode" check="Shiboken::String::check(%in)"> + const char* str = %CONVERTTOCPP[const char*](%in); + %out = %OUTTYPE(str); + </add-conversion> + <add-conversion type="Py_None"> + %out = %OUTTYPE(); + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <function signature="changePStr(PStr*, const char*)"> + <!-- + Comment out these modifications and the Shiboken generator + will issue a fatal error, because it can't handle a pointer + to a primitive type (PStr*) without help from the binding + developer. + --> + <modify-function signature="changePStr(PStr*, const char*)"> + <modify-argument index="1"> + <replace-type modified-type="PStr"/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject"/> + </modify-argument> + <inject-code class="target" position="beginning"> + %FUNCTION_NAME(&%1, %2); + %PYARG_0 = %CONVERTTOPYTHON[PStr](%1); + </inject-code> + </modify-function> + </function> + + <function signature="duplicatePStr(PStr*)"> + <modify-function signature="duplicatePStr(PStr*)"> + <modify-argument index="return"> + <replace-type modified-type="PyObject"/> + </modify-argument> + <modify-argument index="1"> + <replace-type modified-type="PStr"/> + <remove-default-expression/> + <replace-default-expression with="PStr()"/> + </modify-argument> + <inject-code class="target" position="end"> + %FUNCTION_NAME(&%1); + %PYARG_0 = %CONVERTTOPYTHON[PStr](%1); + </inject-code> + </modify-function> + </function> + + <primitive-type name="PStrList"> + <include file-name="strlist.h" location="global"/> + <conversion-rule> + <native-to-target> + PyObject* %out = PyList_New((int) %in.size()); + PStrList::const_iterator it = %in.begin(); + for (int idx = 0; it != %in.end(); ++it, ++idx) { + PStr cppItem(*it); + PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[PStr](cppItem)); + } + return %out; + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + %OUTTYPE& list = %out; + Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0)); + for (int i = 0; i < PySequence_Fast_GET_SIZE(seq.object()); i++) { + PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), i); + PStr cppItem = %CONVERTTOCPP[PStr](pyItem); + list.push_back(cppItem); + } + </add-conversion> + </target-to-native> + </conversion-rule> + </primitive-type> + + <add-function signature="createPStrList(PStr, PStr)" return-type="PyObject"> + <inject-code class="target"> + PStrList %0; + %0.push_back(%1); + %0.push_back(%2); + %PYARG_0 = %CONVERTTOPYTHON[PStrList](%0); + </inject-code> + </add-function> + <add-function signature="createListOfPStr(PStr, PStr)" return-type="PyObject"> + <inject-code class="target"> + std::list<PStr> %0; + %0.push_back(%1); + %0.push_back(%2); + %PYARG_0 = %CONVERTTOPYTHON[std::list<PStr>](%0); + </inject-code> + </add-function> + + <add-function signature="getPythonType(const char*)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + %PYARG_0 = (PyObject*) Shiboken::Conversions::getPythonTypeObject(%1); + if (!%PYARG_0) + %PYARG_0 = Py_None; + Py_INCREF(%PYARG_0); + </inject-code> + </add-function> + + <template name="cpp_type_is_object_or_value_type"> + SbkConverter* converter = Shiboken::Conversions::getConverter(%1); + if (converter) { + if (Shiboken::Conversions::pythonTypeIs$TYPEType(converter)) + %PYARG_0 = Py_True; + else + %PYARG_0 = Py_False; + Py_INCREF(%PYARG_0); + } else { + PyErr_Format(PyExc_ValueError, "Type '%s' has no converter associated to it", %1); + } + </template> + <add-function signature="cppTypeIsObjectType(const char*)" return-type="bool"> + <inject-code class="target" position="beginning"> + <insert-template name="cpp_type_is_object_or_value_type"> + <replace from="$TYPE" to="Object" /> + </insert-template> + </inject-code> + </add-function> + <add-function signature="cppTypeIsValueType(const char*)" return-type="bool"> + <inject-code class="target" position="beginning"> + <insert-template name="cpp_type_is_object_or_value_type"> + <replace from="$TYPE" to="Value" /> + </insert-template> + </inject-code> + </add-function> + + <add-function signature="getConversionTypeString(const char*)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + Shiboken::Conversions::SpecificConverter converter(%1); + const char* %0 = 0; + switch (converter.conversionType()) { + case Shiboken::Conversions::SpecificConverter::CopyConversion: + %0 = "Copy conversion"; + break; + case Shiboken::Conversions::SpecificConverter::PointerConversion: + %0 = "Pointer conversion"; + break; + case Shiboken::Conversions::SpecificConverter::ReferenceConversion: + %0 = "Reference conversion"; + break; + default: + %0 = "Invalid conversion"; + } + %PYARG_0 = %CONVERTTOPYTHON[const char*](%0); + </inject-code> + </add-function> + + <inject-code class="native" position="beginning"> + static PyObject* __convertCppValuesToPython(const char** typeName, void** values, int size) + { + PyObject* result = PyTuple_New(size); + for (int i = 0; i < size; ++i) { + Shiboken::Conversions::SpecificConverter converter(typeName[i]); + PyTuple_SET_ITEM(result, i, converter.toPython(values[i])); + } + return result; + } + </inject-code> + <add-function signature="convertValueTypeToCppAndThenToPython(Point,Point*,Point&)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "Point", "Point*", "Point&" }; + void* values[] = { &%1, &%2, &(%3) }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 3); + </inject-code> + </add-function> + <add-function signature="convertObjectTypeToCppAndThenToPython(ObjectType*,ObjectType&)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "ObjectType*", "ObjectType&" }; + void* values[] = { &%1, &(%2) }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 2); + </inject-code> + </add-function> + <add-function signature="convertListOfIntegersToCppAndThenToPython(std::list<int>)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "std::list<int>" }; + void* values[] = { &%1 }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 1); + </inject-code> + </add-function> + <add-function signature="convertIntegersToCppAndThenToPython(int,int)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "int", "int" }; + void* values[] = { &%1, &%2 }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 2); + </inject-code> + </add-function> + + <container-type name="std::pair" type="pair"> + <include file-name="utility" location="global"/> + <conversion-rule> + <native-to-target> + PyObject* %out = PyTuple_New(2); + PyTuple_SET_ITEM(%out, 0, %CONVERTTOPYTHON[%INTYPE_0](%in.first)); + PyTuple_SET_ITEM(%out, 1, %CONVERTTOPYTHON[%INTYPE_1](%in.second)); + return %out; + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + %out.first = %CONVERTTOCPP[%OUTTYPE_0](PySequence_Fast_GET_ITEM(%in, 0)); + %out.second = %CONVERTTOCPP[%OUTTYPE_1](PySequence_Fast_GET_ITEM(%in, 1)); + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + <template name="cpplist_to_pylist_convertion"> + PyObject* %out = PyList_New((int) %in.size()); + %INTYPE::const_iterator it = %in.begin(); + for (int idx = 0; it != %in.end(); ++it, ++idx) { + %INTYPE_0 cppItem(*it); + PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem)); + } + return %out; + </template> + <template name="pyseq_to_cpplist_convertion"> + Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0)); + for (int i = 0; i < PySequence_Fast_GET_SIZE(seq.object()); i++) { + PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), i); + %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem); + %out.push_back(cppItem); + } + </template> + <container-type name="std::list" type="list"> + <include file-name="list" location="global"/> + <conversion-rule> + <native-to-target> + <insert-template name="cpplist_to_pylist_convertion"/> + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + <insert-template name="pyseq_to_cpplist_convertion"/> + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + <container-type name="List" type="list"> + <include file-name="list" location="global"/> + <conversion-rule> + <native-to-target> + <insert-template name="cpplist_to_pylist_convertion"/> + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + <insert-template name="pyseq_to_cpplist_convertion"/> + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + <container-type name="std::map" type="map"> + <include file-name="map" location="global"/> + <conversion-rule> + <native-to-target> + PyObject* %out = PyDict_New(); + %INTYPE::const_iterator it = %in.begin(); + for (; it != %in.end(); ++it) { + %INTYPE_0 key = it->first; + %INTYPE_1 value = it->second; + PyDict_SetItem(%out, + %CONVERTTOPYTHON[%INTYPE_0](key), + %CONVERTTOPYTHON[%INTYPE_1](value)); + } + return %out; + </native-to-target> + <target-to-native> + <add-conversion type="PyDict"> + PyObject* key; + PyObject* value; + Py_ssize_t pos = 0; + while (PyDict_Next(%in, &pos, &key, &value)) { + %OUTTYPE_0 cppKey = %CONVERTTOCPP[%OUTTYPE_0](key); + %OUTTYPE_1 cppValue = %CONVERTTOCPP[%OUTTYPE_1](value); + %out.insert(%OUTTYPE::value_type(cppKey, cppValue)); + } + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + + <add-function signature="cacheSize()" return-type="int"> + <inject-code class="target"> + %RETURN_TYPE %0 = Shiboken::BindingManager::instance().getAllPyObjects().size(); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + + <namespace-type name="sample"> + <value-type name="sample" /> + </namespace-type> + + <function signature="sumComplexPair(std::pair<Complex, Complex>)" /> + <function signature="gimmeComplexList()" /> + <function signature="transmuteComplexIntoPoint(const Complex&)" /> + <function signature="transmutePointIntoComplex(const Point&)" /> + <function signature="sumComplexPair(std::pair<Complex, Complex>)" /> + <function signature="doubleUnsignedInt(unsigned int)" /> + <function signature="doubleLongLong(long long)" /> + <function signature="doubleUnsignedLongLong(unsigned long long)" /> + <function signature="doubleShort(short)" /> + <function signature="returnNullPrimitivePointer()" /> + <function signature="returnNullValueTypePointer()" /> + <function signature="returnNullObjectTypePointer()" /> + <function signature="acceptInt(int)" /> + <function signature="acceptUInt(unsigned int)" /> + <function signature="acceptLong(long)" /> + <function signature="acceptULong(unsigned long)" /> + <function signature="acceptDouble(double)" /> + <function signature="acceptIntReference(int&)" /> + <function signature="acceptOddBoolReference(OddBool&)" /> + <function signature="countCharacters(const char*)" /> + <function signature="gimmeInt()" /> + <function signature="gimmeDouble()" /> + <function signature="makeCString()" /> + <function signature="multiplyPair(std::pair<double, double>)" /> + <function signature="returnCString()" /> + <function signature="overloadedFunc(double)" /> + <function signature="overloadedFunc(int)" /> + + <value-type name="ClassWithFunctionPointer"> + <suppress-warning text="skipping function 'ClassWithFunctionPointer::callFunctionPointer', unmatched parameter type 'void (*)(void*)'" /> + </value-type> + + <enum-type name="OverloadedFuncEnum"/> + <!-- BUG: + renaming the ICOverloadedFuncEnum to the same name + of a global enum causes the generator to confuse the + two types. + --> + <enum-type name="GlobalEnum"/> + <enum-type name="GlobalOverloadFuncEnum"/> + + <enum-type identified-by-value="AnonymousGlobalEnum_Value0"/> + + <namespace-type name="Invisible" generate="no"> + <enum-type name="EnumOnNamespace" /> + </namespace-type> + + <namespace-type name="SampleNamespace"> + <enum-type name="Option"/> + <enum-type name="InValue"/> + <enum-type name="OutValue"/> + <enum-type identified-by-value="AnonymousClassEnum_Value1"/> + + <object-type name="DerivedFromNamespace"> + <enum-type name="SampleNamespace"/> + </object-type> + <value-type name="SomeClass"> + <value-type name="SomeInnerClass"> + <object-type name="OkThisIsRecursiveEnough"> + <enum-type name="NiceEnum" /> + </object-type> + <enum-type name="ProtectedEnum"/> + </value-type> + <value-type name="SomeOtherInnerClass"/> + <enum-type name="ProtectedEnum"/> + </value-type> + + <modify-function signature="doSomethingWithArray(const unsigned char*, unsigned int, const char*)"> + <modify-argument index="1"> + <replace-type modified-type="const char*"/> + <conversion-rule class="native"> + const unsigned char* %out = reinterpret_cast<const unsigned char*>(Shiboken::String::toCString(%PYARG_1)); + </conversion-rule> + </modify-argument> + <modify-argument index="2"> + <remove-argument/> + <conversion-rule class="native"> + unsigned int %out = static_cast<unsigned int>(Shiboken::String::len(%PYARG_1)); + </conversion-rule> + </modify-argument> + </modify-function> + <add-function signature="ImInsideANamespace(int, int)" return-type="int"> + <inject-code class="target"> + %RETURN_TYPE %0 = %1 + %2; + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + <add-function signature="passReferenceToValueType(Point&)" return-type="double"> + <inject-code> + %RETURN_TYPE %0 = %1.x() + %1.y(); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + + <!-- Do change the argument from pointer to reference to comply with the C++ overload + of this function. The generator must be able to deal with this for Object Types. --> + <add-function signature="passReferenceToObjectType(ObjectType*)" return-type="int"> + <inject-code> + // The dot in "%1." must be replaced with a "->" by the generator. + %RETURN_TYPE %0 = %1.objectName().size(); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + </namespace-type> + + <namespace-type name="RemovedNamespace1" generate='no'> + <enum-type name="RemovedNamespace1_Enum" /> + <namespace-type name="RemovedNamespace2" generate='no'> + <enum-type name="RemovedNamespace2_Enum" /> + </namespace-type> + <enum-type identified-by-value="RemovedNamespace1_AnonymousEnum_Value0" /> + </namespace-type> + + <namespace-type name="UnremovedNamespace"> + <namespace-type name="RemovedNamespace3" generate='no'> + <enum-type name="RemovedNamespace3_Enum" /> + <enum-type identified-by-value="RemovedNamespace3_AnonymousEnum_Value0" /> + </namespace-type> + </namespace-type> + + <namespace-type name="Photon"> + <enum-type name="ClassType"/> + <value-type name="Base"/> + <value-type name="TemplateBase" generate="no"/> + <value-type name="ValueIdentity"/> + <value-type name="ValueDuplicator"/> + </namespace-type> + + <value-type name="CVValueType"/> + <value-type name="CVListUser"/> + + <value-type name="IntList"> + <enum-type name="CtorEnum"/> + </value-type> + <value-type name="PointValueList"> + <enum-type name="CtorEnum"/> + </value-type> + <value-type name="ObjectTypePtrList"> + <enum-type name="CtorEnum"/> + </value-type> + + <object-type name="Abstract"> + <enum-type name="Type"/> + <enum-type name="PrintFormat"/> + <modify-function signature="id()" rename="id_"/> + <modify-function signature="hideFunction(HideType*)" remove="all"/> + </object-type> + + <object-type name="Derived" polymorphic-id-expression="%1->type() == Derived::TpDerived"> + <enum-type name="OtherOverloadedFuncEnum"/> + <value-type name="SomeInnerClass" /> + </object-type> + + <object-type name="ModifiedConstructor"> + <modify-function signature="ModifiedConstructor(int)"> + <modify-argument index="1"> + <replace-type modified-type="str"/> + </modify-argument> + <inject-code class='target' position='beginning'> + const char* tmpArg = %CONVERTTOCPP[const char*](%PYARG_1); + %0 = new %FUNCTION_NAME(atoi(tmpArg)); + </inject-code> + </modify-function> + </object-type> + + <object-type name="ObjectType" hash-function="objectTypeHash"> + <modify-function signature="deprecatedFunction()" deprecated="yes" /> + <!-- rename function to avoid Python signature conflit --> + <modify-function signature="setObject(const Null&)" rename="setNullObject" /> + + <modify-function signature="getCppParent()"> + <modify-argument index="this"> + <parent index="return" action="add" /> + </modify-argument> + <modify-argument index="return"> + <define-ownership class="target" owner="default"/> + </modify-argument> + </modify-function> + + <modify-function signature="event(Event*)"> + <modify-argument index="1" invalidate-after-use="yes"/> + </modify-function> + <modify-function signature="invalidateEvent(Event*)"> + <modify-argument index="1" invalidate-after-use="yes"/> + </modify-function> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + <modify-function signature="createWithChild()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + <modify-function signature="setParent(ObjectType*)"> + <modify-argument index="this"> + <parent index="1" action="add"/> + </modify-argument> + </modify-function> + <inject-code class="native" position="beginning"> + static void reparent_layout_items(PyObject* parent, PyObject* layout) + { + // CHECKTYPE and ISCONVERTIBLE are used here for test purposes, don't change them. + if (!%CHECKTYPE[ObjectTypeLayout*](layout) && !%ISCONVERTIBLE[ObjectTypeLayout*](layout)) + return; + // %CHECKTYPE[ObjectTypeLayout*](layout) + // %ISCONVERTIBLE[ObjectTypeLayout*](layout) + ObjectTypeLayout* var; + var = %CONVERTTOCPP[ObjectTypeLayout*](layout); + // TODO-CONVERTER: erase this + /* + ObjectTypeLayout* var2 = %CONVERTTOCPP[ObjectTypeLayout*](layout); + */ + const ObjectTypeList& objChildren = var->objects(); + ObjectTypeList::const_iterator it = objChildren.begin(); + for (; it != objChildren.end(); ++it) { + if ((*it)->isLayoutType()) { + ObjectTypeLayout* l = reinterpret_cast<ObjectTypeLayout*>(*it); + reparent_layout_items(parent, %CONVERTTOPYTHON[ObjectTypeLayout*](l)); + Shiboken::Object::setParent(layout, %CONVERTTOPYTHON[ObjectTypeLayout*](l)); + } else { + Shiboken::Object::setParent(parent, %CONVERTTOPYTHON[ObjectType*](*it)); + } + } + } + </inject-code> + <modify-function signature="setLayout(ObjectTypeLayout*)"> + <modify-argument index="1"> + <parent index="this" action="add"/> + </modify-argument> + <inject-code class="target" position="end"> + if (%PYARG_1 != Py_None) + reparent_layout_items(%PYSELF, %PYARG_1); + </inject-code> + </modify-function> + <modify-function signature="takeChild(ObjectType*)"> + <modify-argument index="return"> + <define-ownership owner="target"/> + <parent index="this" action="remove"/> + </modify-argument> + </modify-function> + <modify-function signature="takeChild(const Str&)"> + <modify-argument index="return"> + <define-ownership owner="target"/> + <parent index="this" action="remove"/> + </modify-argument> + </modify-function> + <modify-function signature="findChild(const Str&)"> + <modify-argument index="return"> + <parent index="this" action="add"/> + </modify-argument> + </modify-function> + <modify-function signature="children()const"> + <modify-argument index="return"> + <parent index="this" action="add"/> + </modify-argument> + </modify-function> + <modify-function signature="createChild(ObjectType*)"> + <modify-argument index="return"> + <define-ownership owner="c++" /> + </modify-argument> + </modify-function> + <modify-function signature="nextInFocusChain()"> + <modify-argument index="return"> + <parent index="this" action="add"/> + </modify-argument> + </modify-function> + </object-type> + + <object-type name="OtherBase" /> + <object-type name="ObjectTypeDerived" /> + + <object-type name="ObjectTypeLayout"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </object-type> + + <object-type name="ObjectView"> + <modify-function signature="ObjectView(ObjectModel*, ObjectType*)"> + <modify-argument index="1"> + <reference-count action="set" variable-name="setModel(ObjectModel*)1"/> + </modify-argument> + </modify-function> + <modify-function signature="setModel(ObjectModel*)"> + <modify-argument index="1"> + <reference-count action="set"/> + </modify-argument> + </modify-function> + </object-type> + + <value-type name="ObjectTypeHolder"/> + <value-type name="OnlyCopy"/> + <value-type name="FriendOfOnlyCopy"/> + + <object-type name="ObjectModel"> + <enum-type name="MethodCalled" /> + <modify-function signature="data() const"> + <modify-argument index="return"> + <define-ownership class="native" owner="c++"/> + </modify-argument> + </modify-function> + </object-type> + + <value-type name="Event"> + <enum-type name="EventType"/> + </value-type> + + <value-type name="BlackBox"> + <modify-function signature="keepObjectType(ObjectType*)"> + <modify-argument index="1"> + <define-ownership owner="c++"/> + </modify-argument> + </modify-function> + <modify-function signature="retrieveObjectType(int)"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + <modify-function signature="keepPoint(Point*)"> + <modify-argument index="1"> + <define-ownership owner="c++"/> + </modify-argument> + </modify-function> + <modify-function signature="retrievePoint(int)"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + + <value-type name="ProtectedNonPolymorphic"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + <modify-function signature="modifiedProtectedSum(int, int)"> + <inject-code class="target" position="beginning"> + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(%1, %2) * 10; + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </modify-function> + <modify-function signature="dataTypeName(void*) const" remove="all"/> + <modify-function signature="dataTypeName(int) const"> + <modify-argument index="1"> + <replace-default-expression with="0"/> + </modify-argument> + </modify-function> + <add-function signature="dataTypeName(PyObject*)const" return-type="const char*"> + <inject-code class="target" position="beginning"> + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(%PYARG_1); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + </value-type> + + <value-type name="ProtectedPolymorphic"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + + <value-type name="ProtectedPolymorphicDaughter"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + + <value-type name="ProtectedPolymorphicGrandDaughter"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + + <object-type name="ProtectedVirtualDestructor"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </object-type> + + + <object-type name="ProtectedEnumClass"> + <enum-type name="ProtectedEnum" /> + <enum-type name="PublicEnum" /> + </object-type> + + <value-type name="ProtectedProperty" /> + + <function signature="createProtectedProperty()" /> + + <template name="boolptr_at_end_fix_beginning"> + bool __ok__; + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(%ARGUMENT_NAMES, &__ok__); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </template> + + <template name="boolptr_at_start_fix_beginning"> + bool __ok__; + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(&__ok__, %ARGUMENT_NAMES); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </template> + + <template name="boolptr_at_start_and_one_arg_fix_beginning"> + bool __ok__; + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(&__ok__, %2); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </template> + + <template name="boolptr_fix_end"> + PyObject* _tuple_ = PyTuple_New(2); + PyTuple_SET_ITEM(_tuple_, 0, %PYARG_0); + PyObject* _item_ = %CONVERTTOPYTHON[bool](__ok__); + PyTuple_SET_ITEM(_tuple_, 1, _item_); + %PYARG_0 = _tuple_; + </template> + + <template name="return_4_arguments_as_tuple"> + %PYARG_0 = PyTuple_New(4); + PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[%ARG1_TYPE](%1)); + PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[%ARG2_TYPE](%2)); + PyTuple_SET_ITEM(%PYARG_0, 2, %CONVERTTOPYTHON[%ARG3_TYPE](%3)); + PyTuple_SET_ITEM(%PYARG_0, 3, %CONVERTTOPYTHON[%ARG4_TYPE](%4)); + </template> + + <template name="return_5_arguments_as_tuple"> + %PYARG_0 = PyTuple_New(5); + PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[%ARG1_TYPE](%1)); + PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[%ARG2_TYPE](%2)); + PyTuple_SET_ITEM(%PYARG_0, 2, %CONVERTTOPYTHON[%ARG3_TYPE](%3)); + PyTuple_SET_ITEM(%PYARG_0, 3, %CONVERTTOPYTHON[%ARG4_TYPE](%4)); + PyTuple_SET_ITEM(%PYARG_0, 4, %CONVERTTOPYTHON[%ARG5_TYPE](%5)); + </template> + + <template name="return_none"> + %PYARG_0 = Py_None; + Py_INCREF(Py_None); + </template> + + <object-type name="Modifications"> + <enum-type name="OverloadedModFunc"/> + + <modify-function signature="overloaded(int, bool, int, double)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <inject-code class="target" position="beginning"> + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(%1, true, %3, %4); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </modify-function> + + <modify-function signature="overloaded(int, bool, int, int)"> + <modify-argument index="3"> + <remove-argument/> + <replace-default-expression with="321"/> + </modify-argument> + <!-- + <modify-argument index="4"> + <remove-default-expression/> + </modify-argument> + --> + </modify-function> + + <modify-function signature="argRemoval0(int, bool, int, int)"> + <modify-argument index="3"> + <remove-argument/> + <replace-default-expression with="321"/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_4_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval1(int, bool, Point, Point, int)"> + <modify-argument index="3"> + <remove-argument/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_5_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval1(int, bool, int, bool)"> + <inject-code class="target" position="end"> + <insert-template name="return_none"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval2(int, bool, Point, Point, int)"> + <modify-argument index="3"> + <remove-argument/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_5_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval3(int, Point, bool, Point, int)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_5_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval4(int, Point, bool, Point, int)"> + <modify-argument index="2"> + <remove-argument/> + <replace-default-expression with="Point(6, 9)"/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_5_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval5(int, bool, Point, Point, int)"> + <modify-argument index="1"> + <remove-argument/> + <replace-default-expression with="100"/> + </modify-argument> + <modify-argument index="3"> + <remove-argument/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_5_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <modify-function signature="argRemoval5(int, bool, int, bool)"> + <modify-argument index="1"> + <remove-argument/> + <replace-default-expression with="200"/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + <insert-template name="return_4_arguments_as_tuple"/> + </inject-code> + </modify-function> + + <!-- + this alteration will trigger an interesting + compile time error on the binding + --> + <!-- + <modify-function signature="overloaded(int, bool, Point, Point)"> + <modify-argument index="3"> + <remove-argument/> + </modify-argument> + </modify-function> + --> + + <!-- + renaming this signature should remove it from the other + overloaded methods decision tree + --> + <modify-function signature="overloaded(int, bool, Point, Point)" rename="over"/> + + <!-- + 'ok' must be removed and the return value will be changed + to a tuple (PyObject*) containing the expected result plus + the 'ok' value as a Python boolean + --> + <modify-function signature="pointToPair(Point, bool*)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="beginning"> + <insert-template name="boolptr_at_end_fix_beginning"/> + </inject-code> + <inject-code class="target" position="end"> + <insert-template name="boolptr_fix_end"/> + </inject-code> + </modify-function> + + <!-- same as 'pointToPair' except that this time 'ok' is the first argument --> + <modify-function signature="multiplyPointCoordsPlusValue(bool*, Point, double)"> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="beginning"> + <insert-template name="boolptr_at_start_fix_beginning"/> + </inject-code> + <inject-code class="target" position="end"> + <insert-template name="boolptr_fix_end"/> + </inject-code> + </modify-function> + + <!-- completely remove 'plus' from the Python side --> + <modify-function signature="doublePlus(int, int)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + </modify-function> + + <!-- the default value for both arguments must be changed in Python --> + <modify-function signature="power(int, int)"> + <modify-argument index="1"> + <replace-default-expression with="2"/> + </modify-argument> + <modify-argument index="2"> + <replace-default-expression with="1"/> + </modify-argument> + </modify-function> + + <!-- in Python set argument default value to 10 --> + <modify-function signature="timesTen(int)"> + <modify-argument index="1"> + <replace-default-expression with="10"/> + </modify-argument> + </modify-function> + + <!-- in Python remove the argument default value --> + <modify-function signature="increment(int)"> + <modify-argument index="1"> + <remove-default-expression/> + </modify-argument> + </modify-function> + + <!-- don't export this method to Python --> + <modify-function signature="exclusiveCppStuff()" remove="all"/> + + <!-- change the name of this regular method --> + <modify-function signature="cppMultiply(int, int)" rename="calculateArea"/> + + <!-- change the name of this virtual method --> + <modify-function signature="className()" rename="name"/> + + <modify-function signature="sumPointArray(int, const Point*)"> + <modify-argument index="1"> + <remove-argument/> + <conversion-rule class="native"> + int %out = PySequence_Size(%PYARG_1); + </conversion-rule> + </modify-argument> + <modify-argument index="2"> + <replace-type modified-type="PySequence" /> + <conversion-rule class="native"> + Shiboken::AutoArrayPointer<Point> %out(%1); + for (int i = 0; i < %1; ++i) + %out[i] = %CONVERTTOCPP[Point](PySequence_Fast_GET_ITEM(%PYARG_1, i)); + </conversion-rule> + </modify-argument> + </modify-function> + <modify-function signature="getSize(const void*,int)"> + <modify-argument index="1"> + <replace-type modified-type="ByteArray&"/> + </modify-argument> + <modify-argument index="2"> + <replace-default-expression with="-1"/> + </modify-argument> + <inject-code class="target" position="beginning"> + int size = (%2 < 0) ? %1.size() : %2; + %BEGIN_ALLOW_THREADS + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME((const void*) %1.data(), size); + %END_ALLOW_THREADS + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </modify-function> + <modify-function signature="sumPointCoordinates(const Point*)"> + <modify-argument index="1"> + <no-null-pointer/> + </modify-argument> + </modify-function> + <template name="differenceOfPointCoordinates_arg2"> + bool _status; + bool* %2 = &_status; + </template> + <template name="differenceOfPointCoordinates_returnTarget"> + %PYARG_0 = PyTuple_New(2); + PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[bool](*%2)); + PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[%RETURN_TYPE](%0)); + </template> + <modify-function signature="differenceOfPointCoordinates(const Point*, bool*)"> + <modify-argument index="2"> + <remove-argument/> + <conversion-rule class="native"> + <insert-template name="differenceOfPointCoordinates_arg2"/> + </conversion-rule> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PySequence"/> + <conversion-rule class="native"> + Shiboken::AutoDecRef _py_ok_(PySequence_GetItem(%PYARG_0, 0)); + Shiboken::AutoDecRef _py_ret_(PySequence_GetItem(%PYARG_0, 1)); + *%2 = %CONVERTTOCPP[bool](_py_ok_); + %RETURN_TYPE %out = %CONVERTTOCPP[%RETURN_TYPE](_py_ret_); + </conversion-rule> + <conversion-rule class="target"> + <insert-template name="differenceOfPointCoordinates_returnTarget"/> + </conversion-rule> + </modify-argument> + </modify-function> + <modify-function signature="callDifferenceOfPointCoordinates(const Point*, bool*)"> + <modify-argument index="2"> + <remove-argument/> + <conversion-rule class="native"> + <insert-template name="differenceOfPointCoordinates_arg2"/> + </conversion-rule> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PySequence"/> + <conversion-rule class="target"> + <insert-template name="differenceOfPointCoordinates_returnTarget"/> + </conversion-rule> + </modify-argument> + </modify-function> + <modify-function signature="nonConversionRuleForArgumentWithDefaultValue(ObjectType**)"> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="(status, object)"/> + </modify-argument> + <inject-code class="target" position="beginning"> + ObjectType* tmpObject = 0; + %BEGIN_ALLOW_THREADS + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(&tmpObject); + %END_ALLOW_THREADS + %PYARG_0 = PyTuple_New(2); + PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[%RETURN_TYPE](%0)); + PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[ObjectType*](tmpObject)); + </inject-code> + </modify-function> + <modify-function signature="passOddBool(OddBool)" rename="invertBoolean"> + <inject-code class="target" position="beginning"> + %RETURN_TYPE %0 = !%CPPSELF.%FUNCTION_NAME(%1); + %PYARG_0 = %CONVERTTOPYTHON[OddBool](%0); + </inject-code> + </modify-function> + </object-type> + + <object-type name="AbstractModifications"> + <!-- + completely removing the pure virtual method from this + class will generate an #error directive. + --> + <!-- + <modify-function signature="pointlessPureVirtualMethod()" remove="all"/> + --> + </object-type> + + <value-type name="Reference"> + <modify-function signature="returnMyFirstArg(Reference&)"> + <modify-argument index="return"> + <replace-default-expression with="%1"/> + </modify-argument> + </modify-function> + <modify-function signature="returnMySecondArg(int, Reference&)"> + <modify-argument index="return"> + <replace-default-expression with="%2"/> + </modify-argument> + </modify-function> + </value-type> + <object-type name="ObjTypeReference"> + <modify-function signature="returnMyFirstArg(ObjTypeReference&)"> + <modify-argument index="return"> + <replace-default-expression with="%1"/> + </modify-argument> + </modify-function> + <modify-function signature="returnMySecondArg(int, ObjTypeReference&)"> + <modify-argument index="return"> + <replace-default-expression with="%2"/> + </modify-argument> + </modify-function> + <modify-function signature="justAPureVirtualFunc(ObjTypeReference&)"> + <modify-argument index="return"> + <replace-default-expression with="%1"/> + </modify-argument> + </modify-function> + </object-type> + <value-type name="ImplicitConv"> + <enum-type name="CtorEnum"/> + <enum-type name="ICOverloadedFuncEnum"/> + </value-type> + + <value-type name="VirtualMethods"> + <modify-function signature="sum0(int, int, int)" rename="sumThree"/> + <modify-function signature="sum1(int, int, int)"> + <modify-argument index="3"> + <replace-default-expression with="1000"/> + </modify-argument> + </modify-function> + <modify-function signature="sum2(int, int, int)"> + <modify-argument index="3"> + <remove-argument/> + <replace-default-expression with="2000"/> + </modify-argument> + </modify-function> + <modify-function signature="sum3(int, int, int)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <inject-code class="target" position="beginning"> + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(%1, %1+%3, %3); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </modify-function> + <modify-function signature="sum4(int, int, int)"> + <modify-argument index="2"> + <remove-argument/> + <replace-default-expression with="3000"/> + </modify-argument> + <inject-code class="native" position="beginning"> + PyObject* new_arg0 = PyInt_FromLong(PyInt_AS_LONG(%PYARG_1) - %2); + Py_DECREF(%PYARG_1); + %PYARG_1 = new_arg0; + </inject-code> + </modify-function> + <modify-function signature="name()"> + <inject-code class="native" position="end"> + %0.prepend(Str("Pimped")); + </inject-code> + </modify-function> + <modify-function signature="callMe()"> + <inject-code class="native" position="end"> + PyObject_Call(%PYTHON_METHOD_OVERRIDE, %PYTHON_ARGUMENTS, NULL); + </inject-code> + </modify-function> + <modify-function signature="createStr(const char*, Str*&)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PySequence"/> + <conversion-rule class="native"> + Shiboken::AutoDecRef _py_ok_(PySequence_GetItem(%PYARG_0, 0)); + Shiboken::AutoDecRef _py_ret_(PySequence_GetItem(%PYARG_0, 1)); + %RETURN_TYPE %out = %CONVERTTOCPP[%RETURN_TYPE](_py_ok_); + %2 = %CONVERTTOCPP[Str*](_py_ret_); + </conversion-rule> + </modify-argument> + <inject-code class="target" position="beginning"> + Str* _str_arg_ = 0; + %RETURN_TYPE %0 = %CPPSELF.%TYPE::%FUNCTION_NAME(%1, _str_arg_); + </inject-code> + <inject-code class="target" position="end"> + %PYARG_0 = PyTuple_New(2); + PyObject* _item_ = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + PyTuple_SET_ITEM(%PYARG_0, 0, _item_); + _item_ = %CONVERTTOPYTHON[Str*](_str_arg_); + PyTuple_SET_ITEM(%PYARG_0, 1, _item_); + </inject-code> + </modify-function> + <modify-function signature="callCreateStr(const char*, Str*&)"> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PySequence"/> + </modify-argument> + <inject-code class="target" position="beginning"> + Str* _str_arg_ = 0; + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(%1, _str_arg_); + </inject-code> + <inject-code class="target" position="end"> + %PYARG_0 = PyTuple_New(2); + PyObject* _item_ = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + PyTuple_SET_ITEM(%PYARG_0, 0, _item_); + _item_ = %CONVERTTOPYTHON[Str*](_str_arg_); + PyTuple_SET_ITEM(%PYARG_0, 1, _item_); + </inject-code> + </modify-function> + <template name="fix_int*,int*,int*,int*"> + int a0, a1, a2, a3; + %BEGIN_ALLOW_THREADS + %CPPSELF->::%TYPE::%FUNCTION_NAME(&a0, &a1, &a2, &a3); + %END_ALLOW_THREADS + %PYARG_0 = PyTuple_New(4); + PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[int](a0)); + PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[int](a1)); + PyTuple_SET_ITEM(%PYARG_0, 2, %CONVERTTOPYTHON[int](a2)); + PyTuple_SET_ITEM(%PYARG_0, 3, %CONVERTTOPYTHON[int](a3)); + </template> + <template name="fix_native_return_int*,int*,int*,int*"> + PyObject* _obj = %PYARG_0.object(); + if (!PySequence_Check(_obj) + || PySequence_Fast_GET_SIZE(_obj) != 4 + || !SbkNumber_Check(PySequence_Fast_GET_ITEM(_obj, 0)) + || !SbkNumber_Check(PySequence_Fast_GET_ITEM(_obj, 1)) + || !SbkNumber_Check(PySequence_Fast_GET_ITEM(_obj, 2)) + || !SbkNumber_Check(PySequence_Fast_GET_ITEM(_obj, 3))) { + PyErr_SetString(PyExc_TypeError, "Sequence of 4 numbers expected"); + } else { + *%1 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 0)); + *%2 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 1)); + *%3 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 2)); + *%4 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(_obj, 3)); + } + </template> + <modify-function signature="getMargins(int*,int*,int*,int*)const"> + <modify-argument index="0"> + <replace-type modified-type="PyObject" /> + </modify-argument> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <modify-argument index="3"> + <remove-argument/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + <remove-default-expression/> + </modify-argument> + <inject-code class="target" position="beginning"> + <insert-template name="fix_int*,int*,int*,int*"/> + </inject-code> + <inject-code class="native" position="end"> + <insert-template name="fix_native_return_int*,int*,int*,int*"/> + </inject-code> + </modify-function> + <modify-function signature="callGetMargins(int*,int*,int*,int*)const"> + <modify-argument index="0"> + <replace-type modified-type="PyObject" /> + </modify-argument> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="2"> + <remove-argument/> + </modify-argument> + <modify-argument index="3"> + <remove-argument/> + </modify-argument> + <modify-argument index="4"> + <remove-argument/> + <remove-default-expression/> + </modify-argument> + <inject-code class="target" position="beginning"> + <insert-template name="fix_int*,int*,int*,int*"/> + </inject-code> + </modify-function> + <modify-function signature="recursionOnModifiedVirtual(Str)const"> + <inject-code class="target" position="beginning"> + %BEGIN_ALLOW_THREADS + // It's important for test purposes to use a constructor with parenthesis as argument. + %RETURN_TYPE %0 = %RETURN_TYPE(%CPPSELF.%FUNCTION_NAME(Str(%1))); + %END_ALLOW_THREADS + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </modify-function> + </value-type> + <value-type name="VirtualDaughter" /> + + <value-type name="VirtualDtor"> + <modify-function signature="create()"> + <modify-argument index="return"> + <define-ownership owner="target"/> + </modify-argument> + </modify-function> + </value-type> + + <value-type name="PointerHolder"> + <modify-function signature="PointerHolder(void*)" remove="all"/> + <add-function signature="PointerHolder(PyObject*)"> + <inject-code class="target" position="beginning"> + %0 = new %TYPE(%PYARG_1); + </inject-code> + </add-function> + <modify-function signature="pointer() const"> + <inject-code class="target" position="beginning"> + %PYARG_0 = reinterpret_cast<PyObject*>(%CPPSELF.%FUNCTION_NAME()); + if (!%PYARG_0) + %PYARG_0 = Py_None; + Py_INCREF(%PYARG_0); + </inject-code> + </modify-function> + </value-type> + + <function signature="applyHomogeneousTransform(Point,double,double,double,double,double,double,double,double,double,bool*)"> + <!-- + Tests handling of the '%#' substitution for # > 9. + --> + <modify-function signature="applyHomogeneousTransform(Point,double,double,double,double,double,double,double,double,double,bool*)"> + <modify-argument index="11"> + <remove-argument/> + </modify-argument> + <inject-code class="target" position="beginning"> + bool ok_; + %RETURN_TYPE retval_ = + %FUNCTION_NAME(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, &ok_); + if (!ok_) + %PYARG_0 = Py_None; + else + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](retval_); + </inject-code> + </modify-function> + </function> + + <!-- Tests add-function for nested template types --> + <add-function signature="sum2d(std::list<std::list<int> >)" return-type="int"> + <inject-code class="target" position="beginning"> + typedef std::list<int> Inner; + typedef std::list<Inner> Outer; + + int result = 0; + + Outer::const_iterator oiter, oend = %1.end(); + for (oiter = %1.begin(); oiter != oend; ++oiter) { + const Inner& inner = *oiter; + Inner::const_iterator iiter, iend = inner.end(); + for (iiter = inner.begin(); iiter != iend; ++iiter) + result += *iiter; + } + + %PYARG_0 = %CONVERTTOPYTHON[int](result); + </inject-code> + </add-function> + + <!-- Tests add-function for nested template types --> + <add-function signature="sumproduct(std::list<std::pair<int, int> >)" return-type="int"> + <inject-code class="target" position="beginning"> + typedef std::pair<int, int> Pair; + typedef std::list<Pair> List; + + int result = 0; + + List::const_iterator iter, end = %1.end(); + for (iter = %1.begin(); iter != end; ++iter) + result += iter->first * iter->second; + + %PYARG_0 = %CONVERTTOPYTHON[int](result); + </inject-code> + </add-function> + + + <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. + --> + + <modify-function signature="sumArrayAndLength(int *) const"> + <modify-argument index="1"> + <replace-type modified-type="PyObject"/> + </modify-argument> + <inject-code class="target" position="beginning"> + int* array = NULL; + bool errorOccurred = false; + + if (PySequence_Check(%PYARG_1)) { + if((array = Shiboken::sequenceToIntArray(%PYARG_1, true)) == NULL && PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "Should be a sequence of ints"); + errorOccurred = true; + } + } else { + PyErr_SetString(PyExc_TypeError, "Should be a sequence of ints"); + errorOccurred = true; + } + + if (!errorOccurred) { + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(array); + if (array) + delete[] array; + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + } + </inject-code> + </modify-function> + + <modify-function signature="arrayMethod(int, int*) const"> + <modify-argument index="1"> + <remove-argument/> + <conversion-rule class="native"> + int %out = PySequence_Size(%PYARG_1); + </conversion-rule> + </modify-argument> + <modify-argument index="2"> + <replace-type modified-type="PySequence"/> + <conversion-rule class="native"> + int numItems = PySequence_Size(%PYARG_1); + Shiboken::AutoArrayPointer<int> %out(numItems); + for (int i = 0; i < numItems; ++i) { + if (%CHECKTYPE[int](PySequence_Fast_GET_ITEM(%PYARG_1, i))) + %out[i] = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(%PYARG_1, i)); + else if (%ISCONVERTIBLE[int](PySequence_Fast_GET_ITEM(%PYARG_1, i))) + %out[i] = -1; + else + %out[i] = -2; + } + </conversion-rule> + <conversion-rule class="target"> + PyObject* %out = PyList_New(count); + for (int i = 0; i < count; ++i) + PyList_SET_ITEM(%out, i, %CONVERTTOPYTHON[int](%in[i])); + </conversion-rule> + </modify-argument> + </modify-function> + + <modify-function signature="callArrayMethod(int, int*) const"> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="2"> + <replace-type modified-type="PySequence"/> + </modify-argument> + <inject-code class="target" position="beginning"> + int numItems = PySequence_Size(%PYARG_1); + int *cppItems = new int[numItems]; + for (int i = 0; i < numItems; i++) + cppItems[i] = %CONVERTTOCPP[int](PySequence_GetItem(%PYARG_1, i)); + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(numItems, cppItems); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + delete[] cppItems; + </inject-code> + </modify-function> + + <!-- + 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 Shiboken::String::fromCString("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"> + %PYTHONTYPEOBJECT.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 = Shiboken::String::fromCString("end"); + Shiboken::String::concat(&%PYARG_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 = Shiboken::String::fromCString("end"); + Shiboken::String::concat(&%PYARG_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 (!Shiboken::sequenceToArgcArgv(%PYARG_1, &argc, &argv)) { + PyErr_SetString(PyExc_TypeError, "error"); + return 0; + } + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(argc, argv); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + for (int i = 0; i < argc; ++i) + free(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 (!Shiboken::sequenceToArgcArgv(%PYARG_1, &argc, &argv)) { + PyErr_SetString(PyExc_TypeError, "error"); + return 0; + } + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(argc, argv); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + for (int i = 0; i < argc; ++i) + free(argv[i]); + delete[] argv; + </inject-code> + </modify-function> + </value-type> + + <value-type name="ImplicitBase"/> + <value-type name="SortedOverload"> + <add-function signature="overload(PyObject *)" return-type="const char *"> + <inject-code class="target" position="beginning"> + return Shiboken::String::fromCString("PyObject"); + </inject-code> + </add-function> + + <add-function signature="overloadDeep(int, PyObject *)" return-type="const char *"> + <inject-code class="target" position="beginning"> + return Shiboken::String::fromCString("PyObject"); + </inject-code> + </add-function> + <modify-function signature="pyObjOverload(unsigned char*, int)"> + <modify-argument index="1"> + <replace-type modified-type="PyObject" /> + <conversion-rule class="native"> + unsigned char* %out = 0; + </conversion-rule> + </modify-argument> + </modify-function> + </value-type> + <value-type name="ImplicitTarget"/> + + <value-type name="Point"> + <add-function signature="__str__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + int x1 = (int) %CPPSELF.x(); + int x2 = ((int) (%CPPSELF.x() * 100)) - (x1 * 100); + int y1 = (int) %CPPSELF.y(); + int y2 = ((int) (%CPPSELF.y() * 100)) - (y1 * 100); + %PYARG_0 = Shiboken::String::fromFormat("Point(%d.%d, %d.%d)", x1, x2, y1, y2); + </inject-code> + </add-function> + <add-function signature="__repr__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + int x1 = (int) %CPPSELF.x(); + int x2 = ((int) (%CPPSELF.x() * 10)) - (x1 * 10); + int y1 = (int) %CPPSELF.y(); + int y2 = ((int) (%CPPSELF.y() * 10)) - (y1 * 10); + %PYARG_0 = Shiboken::String::fromFormat("<Point object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); + </inject-code> + </add-function> + + <add-function signature="__reduce__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + PyObject* type = PyObject_Type(%PYSELF); + PyObject* args = NULL; + + args = Py_BuildValue("(dd)", %CPPSELF.x(), %CPPSELF.y()); + + %PYARG_0 = Py_BuildValue("(OO)", type, args); + </inject-code> + </add-function> + + <modify-function signature="midpoint(const Point&, Point*)const"> + <modify-argument index="2"> + <remove-argument /> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="Point" /> + </modify-argument> + <inject-code class="target" position="beginning"> + Point _midpoint; + // The test consists in *NOT* using the ARGUMENT_NAMES type system variable. + %CPPSELF.%FUNCTION_NAME(%1, &_midpoint); + %PYARG_0 = %CONVERTTOPYTHON[Point](_midpoint); + </inject-code> + </modify-function> + + <template name="return_self"> + %PYARG_0 = %PYARG_1; + Py_INCREF(%PYARG_1); + </template> + <add-function signature="operator-(PyUnicode)"> + <inject-code> + <insert-template name="return_self" /> + </inject-code> + </add-function> + <!-- A reverse operator --> + <add-function signature="operator-(PyUnicode, Point)"> + <inject-code> + <insert-template name="return_self" /> + </inject-code> + </add-function> + </value-type> + + <value-type name="PointF"> + <add-function signature="__str__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + int x1 = (int) %CPPSELF.x(); + int x2 = ((int) (%CPPSELF.x() * 100)) - (x1 * 100); + int y1 = (int) %CPPSELF.y(); + int y2 = ((int) (%CPPSELF.y() * 100)) - (y1 * 100); + %PYARG_0 = Shiboken::String::fromFormat("PointF(%d.%d, %d.%d)", x1, x2, y1, y2); + </inject-code> + </add-function> + <add-function signature="__repr__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + int x1 = (int) %CPPSELF.x(); + int x2 = ((int) (%CPPSELF.x() * 10)) - (x1 * 10); + int y1 = (int) %CPPSELF.y(); + int y2 = ((int) (%CPPSELF.y() * 10)) - (y1 * 10); + %PYARG_0 = Shiboken::String::fromFormat("<PointF object at %p: (%d.%d, %d.%d)>", %CPPSELF, x1, x2, y1, y2); + </inject-code> + </add-function> + + <add-function signature="__reduce__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + PyObject *type = PyObject_Type(%PYSELF); + PyObject *args = NULL; + + args = Py_BuildValue("(dd)", %CPPSELF.x(), %CPPSELF.y()); + + %PYARG_0 = Py_BuildValue("(OO)", type, args); + </inject-code> + </add-function> + + <modify-function signature="midpoint(const PointF&, PointF*)const"> + <modify-argument index="2"> + <remove-argument /> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PointF" /> + </modify-argument> + <inject-code class="target" position="beginning"> + PointF _midpoint; + // The test consists in using the ARGUMENT_NAMES type system variable. + %CPPSELF.%FUNCTION_NAME(%ARGUMENT_NAMES, &_midpoint); + %PYARG_0 = %CONVERTTOPYTHON[PointF](_midpoint); + </inject-code> + </modify-function> + </value-type> + + <value-type name="Rect" /> + <value-type name="RectF" /> + + <value-type name="Polygon"> + <modify-function signature="stealOwnershipFromPython(Point*)"> + <modify-argument index="1"> + <define-ownership owner="c++"/> + </modify-argument> + </modify-function> + <modify-function signature="stealOwnershipFromPython(Polygon*)"> + <modify-argument index="1"> + <define-ownership owner="c++"/> + </modify-argument> + </modify-function> + </value-type> + + <value-type name="Time"> + <extra-includes> + <include file-name="datetime.h" location="global"/> + </extra-includes> + <enum-type name="NumArgs"/> + <add-function signature="operator!=(const PyObject*)" return-type="PyObject"> + <inject-code> + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + if (PyTime_Check(%1)) { + int pyH = PyDateTime_TIME_GET_HOUR(%1); + int pyM = PyDateTime_TIME_GET_MINUTE(%1); + int pyS = PyDateTime_TIME_GET_SECOND(%1); + if ((pyH == %CPPSELF.hour()) && + (pyM == %CPPSELF.minute()) && + (pyS == %CPPSELF.second())) + %PYARG_0 = Py_False; + else + %PYARG_0 = Py_True; + } + </inject-code> + </add-function> + <add-function signature="operator==(const PyObject*)" return-type="PyObject"> + <inject-code> + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + if (PyTime_Check(%1)) { + int pyH = PyDateTime_TIME_GET_HOUR(%1); + int pyM = PyDateTime_TIME_GET_MINUTE(%1); + int pyS = PyDateTime_TIME_GET_SECOND(%1); + if ((pyH == %CPPSELF.hour()) && + (pyM == %CPPSELF.minute()) && + (pyS == %CPPSELF.second())) + %PYARG_0 = Py_True; + else + %PYARG_0 = Py_False; + } + </inject-code> + </add-function> + + </value-type> + <value-type name="Size"> + <add-function signature="Size(const char*)"> + <inject-code class="target" position="beginning"> + %0 = new %TYPE(); + </inject-code> + <inject-code class="target" position="end"> + Shiboken::AutoDecRef result(PyObject_CallMethod(%PYSELF, const_cast<char*>("setHeight"), const_cast<char*>("i"), 2)); + </inject-code> + </add-function> + </value-type> + <value-type name="SizeF"/> + <value-type name="MapUser"/> + <value-type name="PairUser"/> + <value-type name="ListUser"> + <enum-type name="ListOfSomething"/> + </value-type> + <value-type name="NoImplicitConversion" /> + <value-type name="NonDefaultCtor" /> + <value-type name="OddBoolUser" /> + <object-type name="Overload"> + <enum-type name="FunctionEnum"/> + <enum-type name="ParamEnum"/> + <modify-function signature="intOverloads(int, int, double)"> + <modify-argument index="2"> + <remove-argument /> + </modify-argument> + <inject-code class="target"> + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(%1, 2, %3); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </modify-function> + <modify-function signature="singleOverload(Point*)"> + <modify-argument index="1"> + <define-ownership owner="c++"/> + </modify-argument> + </modify-function> + <modify-function signature="acceptSequence(const char**)"> + <modify-argument index="1"> + <replace-type modified-type="PySequence" /> + <conversion-rule class="native"> + { + Shiboken::AutoDecRef strList(PySequence_Fast(%PYARG_1, "The argument must be a sequence.")); + int lineCount = PySequence_Fast_GET_SIZE(strList.object()); + for (int line = 0; line < lineCount; ++line) { + if (!Shiboken::String::check(PySequence_Fast_GET_ITEM(strList.object(), line))) { + PyErr_SetString(PyExc_TypeError, "The argument must be a sequence of strings."); + break; + } + } + } + const char** %out = 0; + </conversion-rule> + </modify-argument> + </modify-function> + <modify-function signature="acceptSequence(void*)"> + <modify-argument index="1"> + <replace-type modified-type="PyObject" /> + <conversion-rule class="native"> + void* %out = 0; + </conversion-rule> + </modify-argument> + </modify-function> + + <template name="buffer_argument"> + unsigned char* %out = (unsigned char*) Shiboken::Buffer::getPointer(%PYARG_1); + </template> + + <modify-function signature="strBufferOverloads(unsigned char*,int)"> + <modify-argument index="1"> + <replace-type modified-type="PyBuffer"/> + <conversion-rule class="native"> + <insert-template name="buffer_argument" /> + </conversion-rule> + </modify-argument> + </modify-function> + <!-- + This added function simulates the solution given to PySide's QImage + constructor problem, as seen in PySide/bbba1cc4, and described in + bug #489 [http://bugs.pyside.org/show_bug.cgi?id=489]. + This is not the best solution, just one that works. The proper way + to handle it would be to fix the overload decisor. + --> + <add-function signature="strBufferOverloads(Str&,int)" return-type="Overload::FunctionEnum"> + <inject-code class="target" position="beginning"> + <insert-template name="buffer_argument"> + <replace from="%out" to="argOut" /> + </insert-template> + %BEGIN_ALLOW_THREADS + %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(argOut, %2); + %END_ALLOW_THREADS + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + </object-type> + <object-type name="Overload2" /> + + <object-type name="Collector" stream="yes"/> + + <value-type name="IntWrapper" /> + + <value-type name="Str" hash-function="strHash"> + <add-function signature="__str__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + %PYARG_0 = Shiboken::String::fromCString(%CPPSELF.cstring()); + </inject-code> + </add-function> + <add-function signature="__len__" > + <inject-code class="target" position="end"> + return %CPPSELF.size(); + </inject-code> + </add-function> + <add-function signature="__getitem__" > + <inject-code class="target" position="beginning"> + if (_i < 0 || _i >= %CPPSELF.size()) { + PyErr_BadArgument(); + return 0; + } else { + char res[2]; + res[0] = %CPPSELF.get_char(_i); + res[1] = 0; + return Shiboken::String::fromCString(res); + } + </inject-code> + </add-function> + <add-function signature="__setitem__" > + <inject-code class="target" position="beginning"> + PyObject* args = Py_BuildValue("(iO)", _i, _value); + PyObject* result = Sbk_StrFunc_set_char(self, args); + Py_DECREF(args); + int ok = result == Py_True; + if (result) { + Py_DECREF(result); + } + return !ok ? -1 : 0; + </inject-code> + </add-function> + <modify-function signature="toInt(bool*, int)const"> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="beginning"> + <insert-template name="boolptr_at_start_and_one_arg_fix_beginning"/> + </inject-code> + <inject-code class="target" position="end"> + <insert-template name="boolptr_fix_end"/> + </inject-code> + </modify-function> + </value-type> + + <value-type name="ByteArray" hash-function="ByteArray::hash"> + <conversion-rule> + <target-to-native> + <add-conversion type="Py_None"> + %out = %OUTTYPE(); + </add-conversion> + <add-conversion type="PyObject" check="Shiboken::String::check(%in) || PyBytes_Check(%in)"> + Py_ssize_t len; + const char* str = Shiboken::String::toCString(%in, &len); + %out = %OUTTYPE(str, len); + </add-conversion> + </target-to-native> + </conversion-rule> + + <modify-function signature="ByteArray(const char*,int)" remove="all" /> + <modify-function signature="ByteArray(const char*)" remove="all" > + <!-- Keep \x00 bytes passed in Python strings. --> + <modify-argument index="1"> + <replace-type modified-type="PyBytes"/> + </modify-argument> + <inject-code class="target" position="beginning"> + PyObject* data = 0; + if (PyUnicode_CheckExact(%PYARG_1)) { + data = PyUnicode_AsASCIIString(%PYARG_1); + } else { + data = %PYARG_1; + Py_INCREF(data); + } + + %0 = new %TYPE(PyBytes_AsString(data), PyBytes_GET_SIZE(data)); + Py_DECREF(data); + </inject-code> + </modify-function> + + <!-- buffer protocol --> + <inject-code class="native" position="beginning" file="bytearray_bufferprotocol.cpp" /> + <inject-code class="target" position="end"> + #if PY_VERSION_HEX < 0x03000000 + Shiboken::SbkType<ByteArray>()->tp_as_buffer = &SbkByteArrayBufferProc; + Shiboken::SbkType<ByteArray>()->tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER; + #endif + </inject-code> + + <modify-function signature="data() const"> + <inject-code class="target" position="beginning"> + %PYARG_0 = PyBytes_FromStringAndSize(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.size()); + </inject-code> + </modify-function> + + <modify-function signature="hash(const ByteArray&)" remove="all" /> + <!-- Functions removed to proper deal with strings containing zeroes --> + <modify-function signature="append(const char*)" remove="all" /> + <modify-function signature="append(const char*,int)" remove="all" /> + <modify-function signature="operator==(const char*,ByteArray)" remove="all" /> + <modify-function signature="operator==(ByteArray,const char*)" remove="all" /> + <modify-function signature="operator!=(const char*,ByteArray)" remove="all" /> + <modify-function signature="operator!=(ByteArray,const char*)" remove="all" /> + <modify-function signature="operator+(ByteArray,const char*)" remove="all" /> + <modify-function signature="operator+(const char*,ByteArray)" remove="all" /> + <modify-function signature="operator+=(const char*)" remove="all" /> + <modify-function signature="operator[](int)const" remove="all"/> + + <add-function signature="operator+(PyUnicode)" return-type="ByteArray"> + <inject-code> + Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(%PYARG_1)); + if (!data.isNull()) { + ByteArray ba(*%CPPSELF); + ba.append(PyBytes_AsString(data.object()), PyBytes_GET_SIZE(data.object())); + %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); + } + </inject-code> + </add-function> + <add-function signature="operator+(PyUnicode,ByteArray)" return-type="ByteArray"> + <inject-code> + Shiboken::AutoDecRef data(PyUnicode_AsASCIIString(%PYARG_1)); + if (!data.isNull()) { + ByteArray ba(PyBytes_AsString(data.object()), PyBytes_GET_SIZE(data.object())); + ba.append(*%CPPSELF); + %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); + } + </inject-code> + </add-function> + <add-function signature="operator+(PyBytes,ByteArray)"> + <inject-code> + ByteArray ba(PyBytes_AsString(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); + ba = ba + *%CPPSELF; + %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); + </inject-code> + </add-function> + <add-function signature="operator+(PyBytes)"> + <inject-code> + ByteArray ba(PyBytes_AsString(%PYARG_1), PyBytes_GET_SIZE(%PYARG_1)); + ba.append(*%CPPSELF); + %PYARG_0 = %CONVERTTOPYTHON[ByteArray](ba); + </inject-code> + </add-function> + <add-function signature="__repr__" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + ByteArray b(((PyObject*)%PYSELF)->ob_type->tp_name); + PyObject* aux = Shiboken::String::fromStringAndSize(%CPPSELF.data(), %CPPSELF.size()); + if (PyUnicode_CheckExact(aux)) { + PyObject* tmp = PyUnicode_AsASCIIString(aux); + Py_DECREF(aux); + aux = tmp; + } + b += "('"; + b += ByteArray(PyBytes_AS_STRING(aux), PyBytes_GET_SIZE(aux)); + b += "')"; + %PYARG_0 = Shiboken::String::fromStringAndSize(b.data(), b.size()); + </inject-code> + </add-function> + + <add-function signature="__str__" return-type="str"> + <inject-code class="target" position="beginning"> + %PYARG_0 = Shiboken::String::fromStringAndSize(%CPPSELF.data(), %CPPSELF.size()); + </inject-code> + </add-function> + + <add-function signature="__len__"> + <inject-code class="target" position="beginning"> + return %CPPSELF.size(); + </inject-code> + </add-function> + <add-function signature="__getitem__"> + <inject-code class="target" position="beginning"> + if (_i < 0 || _i >= %CPPSELF.size()) { + PyErr_SetString(PyExc_IndexError, "index out of bounds"); + return 0; + } else { + char res[2]; + res[0] = %CPPSELF.at(_i); + res[1] = 0; + return PyBytes_FromStringAndSize(res, 1); + } + </inject-code> + </add-function> + </value-type> + + <value-type name="StrList"> + <enum-type name="CtorEnum"/> + <add-function signature="__len__" > + <inject-code class="target" position="end"> + return %CPPSELF.size(); + </inject-code> + </add-function> + <add-function signature="__getitem__" > + <inject-code class="target" position="beginning"> + if (_i < 0 || _i >= static_cast<Py_ssize_t>(%CPPSELF.size())) { + PyErr_BadArgument(); + return 0; + } else { + %TYPE::const_iterator it = %CPPSELF.begin(); + for (Py_ssize_t i = 1; i <= _i; i++) + ++it; + return %CONVERTTOPYTHON[Str](*it); + } + </inject-code> + </add-function> + </value-type> + + <value-type name="SimpleFile"> + <modify-function signature="open()"> + <modify-argument index="return"> + <remove-argument/> + </modify-argument> + <inject-code class="target" position="end" file="simplefile_glue.cpp"/> + </modify-function> + </value-type> + + <value-type name="VoidHolder"> + <modify-function signature="gimmeMeSomeVoidPointer()"> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="end"> + %RETURN_TYPE %0 = %TYPE::%FUNCTION_NAME(); + %PYARG_0 = Shiboken::Object::newObject(&SbkObject_Type, %0, false, false); + </inject-code> + </modify-function> + </value-type> + + <object-type name="PrivateCtor" /> + <object-type name="PrivateDtor" /> + + <object-type name="Base1"/> + <object-type name="Base2"/> + <object-type name="Base3"/> + <object-type name="Base4"/> + <object-type name="Base5"/> + <object-type name="Base6"/> + <object-type name="MDerived1"/> + <object-type name="MDerived2"/> + <object-type name="MDerived3"/> + <object-type name="MDerived4"/> + <object-type name="MDerived5"/> + <object-type name="SonOfMDerived1"/> + + <object-type name="Bucket"> + <modify-function signature="lock()" allow-thread="yes" /> + <modify-function signature="virtualBlockerMethod()" allow-thread="yes"/> + <modify-function signature="callVirtualBlockerMethodButYouDontKnowThis()" allow-thread="yes"/> + </object-type> + + <value-type name="Echo"> + <add-function signature="echo(const char *)" return-type="PyObject*"> + <inject-code class="target" position="beginning"> + %PYARG_0 = Shiboken::String::fromCString(%1); + </inject-code> + </add-function> + <add-function signature="operator>(int)"> + <inject-code> + // This should test if code injections works inside rich comparison operators + Py_INCREF(Py_True); + %PYARG_0 = Py_True; + </inject-code> + </add-function> + </value-type> + + <value-type name="Color" /> + <value-type name="Pen"> + <enum-type identified-by-value="EnumCtor"/> + </value-type> + + <value-type name="CtorConvRule"> + <modify-function signature="CtorConvRule(long)"> + <modify-argument index="1"> + <!--<replace-type modified-type="long"/>--> + <conversion-rule class="native"> + // Does nothing really, just test the code generation + // of constructors whose arguments where + long %out = PyInt_AS_LONG(%PYARG_1) + 1; + </conversion-rule> + </modify-argument> + </modify-function> + </value-type> + + <add-function signature="multiplyString(str, unsigned int)" return-type="const char*"> + <inject-code class="target" position="beginning"> + %PYARG_0 = Shiboken::String::fromCString(""); + for (unsigned int i = 0; i < %2; ++i) + Shiboken::String::concat(&%PYARG_0, %PYARG_1); + </inject-code> + </add-function> + + <add-function signature="countVarargs(int, ...)" return-type="int"> + <inject-code class="target" position="beginning"> + %RETURN_TYPE %0 = PyTuple_GET_SIZE(%PYARG_2); + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + + <value-type name="SbkDate"> + <extra-includes> + <include file-name="datetime.h" location="global"/> + </extra-includes> + <inject-code class="native" position="beginning"> + static bool PyDate_ImportAndCheck(PyObject* pyIn) { + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + return PyDate_Check(pyIn); + } + </inject-code> + <conversion-rule> + <target-to-native> + <add-conversion type="PyDate" check="PyDate_ImportAndCheck(%in)"> + int day = PyDateTime_GET_DAY(%in); + int month = PyDateTime_GET_MONTH(%in); + int year = PyDateTime_GET_YEAR(%in); + %out = %OUTTYPE(day, month, year); + </add-conversion> + </target-to-native> + </conversion-rule> + <add-function signature="toPython()" return-type="PyDate"> + <inject-code class="target"> + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + %PYARG_0 = PyDate_FromDate(%CPPSELF.day(), %CPPSELF.month(), %CPPSELF.year()); + </inject-code> + </add-function> + </value-type> + + <object-type name="HandleHolder" /> + <value-type name="PrimitiveStructPointerHolder" /> + + <object-type name="ObjectTypeOperators"> + <add-function signature="operator!=(std::string)" return-type="bool"> + <inject-code class="target"> + %RETURN_TYPE %0 = %CPPSELF.key() != %1; + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + </object-type> + + <value-type name="Filter" /> + <value-type name="Data"> + <enum-type name="Field" /> + <add-function signature="operator&(const Union&)" return-type="Intersection"> + <inject-code class="target"> + %RETURN_TYPE %0 = *%CPPSELF & %1; + return %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + </value-type> + <value-type name="Union"> + <add-function signature="operator&(const Data&)" return-type="Intersection"> + <inject-code class="target"> + %RETURN_TYPE %0 = *%CPPSELF & %1; + return %CONVERTTOPYTHON[%RETURN_TYPE](%0); + </inject-code> + </add-function> + </value-type> + <value-type name="Intersection" /> + + <!-- type used in abstract method --> + <object-type name="HideType" generate="no" /> + + <value-type name="Expression" /> + + <value-type name="ModelIndex" /> + <value-type name="ReferentModelIndex"> + <modify-function signature="operator const ModelIndex&()const"> + <modify-argument index="return"> + <define-ownership owner="c++"/> + </modify-argument> + </modify-function> + </value-type> + <value-type name="PersistentModelIndex" /> + + <!-- test rejections using full signatures; this method is a template and + cannot be wrapped, but is otherwise recognized by shiboken and will + result in a compile error if the rejection is not matched --> + <rejection class="Photon::Base" function-name="isType()"/> + + <value-type name="ValueAndVirtual" /> + + <object-type name="ObjectTypeByValue" /> + + <object-type name="TemplatePtr"> + <modify-function signature="dummy(std::list<std::pair<BlackBox *, BlackBox *> > &)" rename="dummy_method" /> + </object-type> + + <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" /> + <suppress-warning text="Pure virtual method "Abstract::hideFunction(HideType*)" must be implement but was completely removed on typesystem." /> + <suppress-warning text="hiding of function 'takeChild' in class 'ObjectType'" /> + <suppress-warning text="Shadowing: MDerived2::castToBase3() and MDerived3::castToBase3()" /> + <suppress-warning text="hiding of function 'takeChild' in class 'Bucket'" /> + <suppress-warning text="visibility of function 'publicMethod' modified in class 'MDerived1'" /> + <suppress-warning text="hiding of function 'doNothingInPublic3' in class 'Overload2'" /> + <suppress-warning text="hiding of function 'takeChild' in class 'ObjectModel'" /> + <suppress-warning text="hiding of function 'takeChild' in class 'ObjectView'" /> + <suppress-warning text="visibility of function 'publicMethod' modified in class 'MDerived3'" /> + <suppress-warning text="skipping function 'InjectCode::toStr', unmatched parameter type 'T const&'" /> + + <!-- Do not fix this warning, the generator should be able to handle this situation for Object Types. --> + <suppress-warning text="Argument in position 1 of added function 'SampleNamespace::passReferenceToObjectType(ObjectType * arg__1)', has a type that is not a reference, while the argument in the corresponding position in C++ function 'SampleNamespace::passReferenceToObjectType(const ObjectType & obj, int multiplier)' is a reference." /> + +</typesystem> diff --git a/sources/shiboken2/tests/samplebinding/unsafe_parent_test.py b/sources/shiboken2/tests/samplebinding/unsafe_parent_test.py new file mode 100644 index 000000000..18da16458 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/unsafe_parent_test.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for ...''' + +import sys +import unittest + +from sample import ObjectType + +class DerivedObjectType(ObjectType): + def isPython(self): + return True + + def createChild(self, parent): + return DerivedObjectType(parent) + +class ParentTest(unittest.TestCase): + + def testUunsafeParent(self): + o = DerivedObjectType() + o.callVirtualCreateChild() + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/useraddedctor_test.py b/sources/shiboken2/tests/samplebinding/useraddedctor_test.py new file mode 100644 index 000000000..ea9d1f878 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/useraddedctor_test.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for user added constructors''' + +import unittest +from sample import * + +class PointTest(unittest.TestCase): + def testUsingSelfOnCtor(self): + # This is a user added ctor and no errors should happen! + s = Size("oi") + self.assertEqual(s.height(), 2) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/virtualdtor_test.py b/sources/shiboken2/tests/samplebinding/virtualdtor_test.py new file mode 100644 index 000000000..46f29179c --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/virtualdtor_test.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for virtual destructor.''' + +import sys +import unittest + +from sample import VirtualDtor + +class ExtendedVirtualDtor(VirtualDtor): + def __init__(self): + VirtualDtor.__init__(self) + +class VirtualDtorTest(unittest.TestCase): + '''Test case for virtual destructor.''' + + def setUp(self): + VirtualDtor.resetDtorCounter() + + def testVirtualDtor(self): + '''Original virtual destructor is being called.''' + dtor_called = VirtualDtor.dtorCalled() + for i in range(1, 10): + vd = VirtualDtor() + del vd + self.assertEqual(VirtualDtor.dtorCalled(), dtor_called + i) + + def testVirtualDtorOnCppCreatedObject(self): + '''Original virtual destructor is being called for a C++ created object.''' + dtor_called = VirtualDtor.dtorCalled() + for i in range(1, 10): + vd = VirtualDtor.create() + del vd + self.assertEqual(VirtualDtor.dtorCalled(), dtor_called + i) + + def testDtorOnDerivedClass(self): + '''Original virtual destructor is being called for a derived class.''' + dtor_called = ExtendedVirtualDtor.dtorCalled() + for i in range(1, 10): + evd = ExtendedVirtualDtor() + del evd + self.assertEqual(ExtendedVirtualDtor.dtorCalled(), dtor_called + i) + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/virtualmethods_test.py b/sources/shiboken2/tests/samplebinding/virtualmethods_test.py new file mode 100644 index 000000000..8a361c9f9 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/virtualmethods_test.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test cases for virtual methods.''' + +import sys +import unittest + +from sample import * + +import warnings + +class ExtendedVirtualMethods(VirtualMethods): + def __init__(self): + VirtualMethods.__init__(self) + self.virtual_method0_called = False + + def virtualMethod0(self, pt, val, cpx, b): + self.virtual_method0_called = True + return VirtualMethods.virtualMethod0(self, pt, val, cpx, b) * -1.0 + + def strListToStdList(self, arg): + warnings.simplefilter('error') + # returning wrong type for test purposes. + return True + + def recursionOnModifiedVirtual(self, arg): + # check if recursion is caused by injected code that calls C++. + return VirtualMethods.recursionOnModifiedVirtual(self, arg) + 10 + +class ExtendedVirtualDaughter(VirtualDaughter): + def __init__(self, name): + VirtualDaughter.__init__(self, name) + self.grand_daughter_name_called = False + + def name(self): + self.grand_daughter_name_called = True + return VirtualDaughter.name(self).prepend('Extended') + +class ExtendedExtendedVirtualDaughter(ExtendedVirtualDaughter): + def __init__(self, name): + ExtendedVirtualDaughter.__init__(self, name) + self.grand_grand_daughter_name_called = False + + def name(self): + self.grand_grand_daughter_name_called = True + return ExtendedVirtualDaughter.name(self).prepend('Extended') + +class VirtualMethodsTest(unittest.TestCase): + '''Test case for virtual methods''' + + def setUp(self): + self.prefix_from_codeinjection = Str('Pimped') + + def tearDown(self): + del self.prefix_from_codeinjection + + def testReimplementedVirtualMethod0(self): + '''Test Python override of a virtual method with various different parameters is correctly called from C++.''' + vm = VirtualMethods() + evm = ExtendedVirtualMethods() + pt = Point(1.1, 2.2) + val = 4 + cpx = complex(3.3, 4.4) + b = True + result0 = vm.callVirtualMethod0(pt, val, cpx, b) + result1 = evm.callVirtualMethod0(pt, val, cpx, b) + self.assertEqual(result0 * -1.0, result1) + + def testRecursionOnModifiedVirtual(self): + evm = ExtendedVirtualMethods() + self.assertEqual(evm.recursionOnModifiedVirtual(''), 10) + self.assertEqual(evm.callRecursionOnModifiedVirtual(''), 10) + + def testReimplementedVirtualMethodInheritedFromGrandParent(self): + '''Test Python override of a virtual method inherited from a grand parent.''' + original_name = 'Foo' + evd = ExtendedVirtualDaughter(original_name) + + self.assertEqual(VirtualDaughter.name(evd), original_name) + self.assertEqual(VirtualMethods.name(evd), original_name) + self.assertFalse(evd.grand_daughter_name_called) + + name = evd.callName() + self.assertTrue(evd.grand_daughter_name_called) + self.assertEqual(evd.name().prepend(self.prefix_from_codeinjection), name) + + def testReimplementedVirtualMethodInheritedFromGrandGrandParent(self): + '''Test Python override of a virtual method inherited from a grand grand parent.''' + original_name = 'Foo' + eevd = ExtendedExtendedVirtualDaughter(original_name) + + self.assertEqual(VirtualDaughter.name(eevd), original_name) + self.assertEqual(VirtualMethods.name(eevd), original_name) + self.assertFalse(eevd.grand_daughter_name_called) + self.assertFalse(eevd.grand_grand_daughter_name_called) + + name = eevd.callName() + self.assertTrue(eevd.grand_daughter_name_called) + self.assertTrue(eevd.grand_grand_daughter_name_called) + self.assertEqual(eevd.name().prepend(self.prefix_from_codeinjection), name) + +class PrettyErrorMessageTest(unittest.TestCase): + def testIt(self): + obj = ExtendedVirtualMethods() + self.assertRaises(RuntimeWarning, obj.callStrListToStdList, StrList()) + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/visibilitychange_test.py b/sources/shiboken2/tests/samplebinding/visibilitychange_test.py new file mode 100644 index 000000000..b89036a94 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/visibilitychange_test.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest + +from sample import * + +class VisibilityChangeTest(unittest.TestCase): + + def testVisibilityChange(self): + b1 = Base1() + b1.publicMethod() # ok... + d1 = MDerived1() + self.assertRaises(TypeError, d1.publicMethod); + +if __name__ == '__main__': + unittest.main() + + diff --git a/sources/shiboken2/tests/samplebinding/voidholder_test.py b/sources/shiboken2/tests/samplebinding/voidholder_test.py new file mode 100644 index 000000000..cb5cab6e5 --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/voidholder_test.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test case for a class that holds a void pointer.''' + +import unittest + +from sample import VoidHolder, Point + +class VoidHolderTest(unittest.TestCase): + '''Test case for void pointer manipulation.''' + + def testGetVoidPointerFromCppAndPutsOnVoidHolder(self): + '''Passes a void pointer created in C++ and to kept by VoidHolder.''' + voidptr = VoidHolder.gimmeMeSomeVoidPointer() + voidholder = VoidHolder(voidptr) + self.assertEqual(voidptr, voidholder.voidPointer()) + + def testPutRandomObjectInsideVoidHolder(self): + '''Passes a C++ pointer for an object created in Python to be kept by VoidHolder.''' + obj = Point(1, 2) + voidholder = VoidHolder(obj) + self.assertEqual(obj, voidholder.voidPointer()) + + def testGetNoneObjectFromVoidHolder(self): + '''A VoidHolder created without parameters returns a NULL pointer + that should be converted to a Python None.''' + voidholder = VoidHolder() + self.assertEqual(voidholder.voidPointer(), None) + + def testPutPythonObjectInsideVoidHolder(self): + '''Passes a native Python object to be kept by VoidHolder.''' + obj = 'Foo' + voidholder = VoidHolder(obj) + self.assertEqual(obj, voidholder.voidPointer()) + + + +if __name__ == '__main__': + unittest.main() + diff --git a/sources/shiboken2/tests/samplebinding/weakref_test.py b/sources/shiboken2/tests/samplebinding/weakref_test.py new file mode 100644 index 000000000..77be7ec9f --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/weakref_test.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +'''Test weakref support''' + +import weakref +import unittest + +from sample import ObjectType, PrivateDtor + + +class WeakrefBasicTest(unittest.TestCase): + '''Simple test case of using a weakref''' + + def setUp(self): + self.called = False + + def cb(self, *args): + self.called = True + + def testBasic(self): + '''ObjectType weakref''' + obj = ObjectType() + ref = weakref.ref(obj, self.cb) + del obj + self.assertTrue(self.called) + + def testPrivateDtor(self): + '''PrivateDtor weakref''' + obj = PrivateDtor.instance() + ref = weakref.ref(obj, self.cb) + del obj + self.assertTrue(self.called) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/samplebinding/writableclassdict_test.py b/sources/shiboken2/tests/samplebinding/writableclassdict_test.py new file mode 100644 index 000000000..828ac686a --- /dev/null +++ b/sources/shiboken2/tests/samplebinding/writableclassdict_test.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from sample import Point + +class ExtPoint(Point): pass + +class TestWritableClassDict(unittest.TestCase): + def testSetattrOnClass(self): + setattr(Point, 'foo', 123) + self.assertEqual(Point.foo, 123) + pt = Point() + self.assertEqual(pt.foo, 123) + + def testSetattrOnInheritingClass(self): + setattr(Point, 'bar', 321) + self.assertEqual(Point.bar, 321) + self.assertEqual(ExtPoint.bar, 321) + pt = ExtPoint() + self.assertEqual(pt.bar, 321) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/shibokenmodule/module_test.py b/sources/shiboken2/tests/shibokenmodule/module_test.py new file mode 100644 index 000000000..bc2931251 --- /dev/null +++ b/sources/shiboken2/tests/shibokenmodule/module_test.py @@ -0,0 +1,116 @@ +############################################################################# +## +## Copyright (C) 2016 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import shiboken2 as shiboken +import unittest +from sample import * + +class MultipleInherited (ObjectType, Point): + def __init__(self): + ObjectType.__init__(self) + Point.__init__(self) + +class TestShiboken(unittest.TestCase): + def testIsValid(self): + self.assertTrue(shiboken.isValid(object())) + self.assertTrue(shiboken.isValid(None)) + + bb = BlackBox() + item = ObjectType() + ticket = bb.keepObjectType(item) + bb.disposeObjectType(ticket) + self.assertFalse(shiboken.isValid(item)) + + def testWrapInstance(self): + addr = ObjectType.createObjectType() + obj = shiboken.wrapInstance(addr, ObjectType) + self.assertFalse(shiboken.createdByPython(obj)) + obj.setObjectName("obj") + self.assertEqual(obj.objectName(), "obj") + self.assertEqual(addr, obj.identifier()) + self.assertFalse(shiboken.createdByPython(obj)) + + # avoid mem leak =] + bb = BlackBox() + self.assertTrue(shiboken.createdByPython(bb)) + bb.disposeObjectType(bb.keepObjectType(obj)) + + def testIsOwnedByPython(self): + obj = ObjectType() + self.assertTrue(shiboken.ownedByPython(obj)) + p = ObjectType() + obj.setParent(p) + self.assertFalse(shiboken.ownedByPython(obj)) + + def testDump(self): + """Just check if dump doesn't crash on certain use cases""" + p = ObjectType() + obj = ObjectType(p) + obj2 = ObjectType(obj) + obj3 = ObjectType(obj) + self.assertEqual(shiboken.dump(None), "Ordinary Python type.") + shiboken.dump(obj) + + model = ObjectModel(p) + v = ObjectView(model, p) + shiboken.dump(v) + + m = MultipleInherited() + shiboken.dump(m) + self.assertEqual(len(shiboken.getCppPointer(m)), 2) + + # Don't crash even after deleting an object + shiboken.invalidate(obj) + shiboken.dump(obj) # deleted + shiboken.dump(p) # child deleted + shiboken.dump(obj2) # parent deleted + + def testDelete(self): + obj = ObjectType() + child = ObjectType(obj) + self.assertTrue(shiboken.isValid(obj)) + self.assertTrue(shiboken.isValid(child)) + # Note: this test doesn't assure that the object dtor was really called + shiboken.delete(obj) + self.assertFalse(shiboken.isValid(obj)) + self.assertFalse(shiboken.isValid(child)) + + def testVersionAttr(self): + self.assertEqual(type(shiboken.__version__), str) + self.assertTrue(len(shiboken.__version__) >= 5) + self.assertEqual(type(shiboken.__version_info__), tuple) + self.assertEqual(len(shiboken.__version_info__), 5) + + def testAllWrappers(self): + obj = ObjectType() + self.assertTrue(obj in shiboken.getAllValidWrappers()) + shiboken.delete(obj) + self.assertFalse(obj in shiboken.getAllValidWrappers()) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/smartbinding/CMakeLists.txt b/sources/shiboken2/tests/smartbinding/CMakeLists.txt new file mode 100644 index 000000000..e9c52d447 --- /dev/null +++ b/sources/shiboken2/tests/smartbinding/CMakeLists.txt @@ -0,0 +1,39 @@ +project(smart) + +set(smart_TYPESYSTEM +${CMAKE_CURRENT_SOURCE_DIR}/typesystem_smart.xml +) + +set(smart_SRC +${CMAKE_CURRENT_BINARY_DIR}/smart/smart_module_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/smart/obj_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/smart/integer_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/smart/sharedptr_obj_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/smart/sharedptr_integer_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/smart/registry_wrapper.cpp +) + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/smart-binding.txt.in" + "${CMAKE_CURRENT_BINARY_DIR}/smart-binding.txt" @ONLY) + +add_custom_command(OUTPUT ${smart_SRC} +COMMAND shiboken2 --project-file=${CMAKE_CURRENT_BINARY_DIR}/smart-binding.txt ${GENERATOR_EXTRA_FLAGS} +DEPENDS ${smart_TYPESYSTEM} ${CMAKE_CURRENT_SOURCE_DIR}/global.h shiboken2 +WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +COMMENT "Running generator for 'smart' test binding..." +) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR} + ${SBK_PYTHON_INCLUDE_DIR} + ${libsmart_SOURCE_DIR} + ${libshiboken_SOURCE_DIR}) +add_library(smart MODULE ${smart_SRC}) +set_property(TARGET smart PROPERTY PREFIX "") +if(WIN32) + set_property(TARGET smart PROPERTY SUFFIX ".pyd") +endif() +target_link_libraries(smart + libsmart + ${SBK_PYTHON_LIBRARIES} + libshiboken) diff --git a/sources/shiboken2/tests/smartbinding/global.h b/sources/shiboken2/tests/smartbinding/global.h new file mode 100644 index 000000000..a72cd21a5 --- /dev/null +++ b/sources/shiboken2/tests/smartbinding/global.h @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "smart.h" diff --git a/sources/shiboken2/tests/smartbinding/smart-binding.txt.in b/sources/shiboken2/tests/smartbinding/smart-binding.txt.in new file mode 100644 index 000000000..699f0bfe6 --- /dev/null +++ b/sources/shiboken2/tests/smartbinding/smart-binding.txt.in @@ -0,0 +1,15 @@ +[generator-project] + +generator-set = shiboken + +header-file = @CMAKE_CURRENT_SOURCE_DIR@/global.h +typesystem-file = @smart_TYPESYSTEM@ + +output-directory = @CMAKE_CURRENT_BINARY_DIR@ + +include-path = @libsmart_SOURCE_DIR@ + +typesystem-path = @CMAKE_CURRENT_SOURCE_DIR@ + +enable-parent-ctor-heuristic +use-isnull-as-nb_nonzero diff --git a/sources/shiboken2/tests/smartbinding/smart_pointer_test.py b/sources/shiboken2/tests/smartbinding/smart_pointer_test.py new file mode 100644 index 000000000..c8eb1b2c5 --- /dev/null +++ b/sources/shiboken2/tests/smartbinding/smart_pointer_test.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +############################################################################# +## +## Copyright (C) 2017 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of PySide2. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import unittest +from copy import copy +from smart import Obj, Registry, Integer + +def objCount(): + return Registry.getInstance().countObjects() + +def integerCount(): + return Registry.getInstance().countIntegers() + +class SmartPointerTests(unittest.TestCase): + def testObjSmartPointer(self): + # Uncomment to see more debug info about creation of objects and ref counts. + # Registry.getInstance().setShouldPrint(True) + + # Create Obj. + o = Obj() + self.assertEqual(objCount(), 1) + + # Create a shared pointer to an Obj together with an Obj. + ptrToObj = o.giveSharedPtrToObj() + self.assertEqual(objCount(), 2) + + # Delete the old Obj. + o = None + self.assertEqual(objCount(), 1) + + # Get a wrapper to the Obj inside of the shared pointer, object count should not change. + obj = ptrToObj.data() + self.assertEqual(objCount(), 1) + obj.m_integer = 50 + self.assertEqual(obj.m_integer, 50) + + # Set and get a member value via shared pointer (like operator->). + ptrToObj.m_integer = 100 + self.assertEqual(ptrToObj.m_integer, 100) + + # Get inner PyObject via shared pointer (like operator->) and set value in it. + ptrToObj.m_internalInteger.m_int = 200 + self.assertEqual(ptrToObj.m_internalInteger.m_int, 200) + + # Pass smart pointer as argument to a method, return value is the value of m_integer of + # passed Obj inside the smart pointer. + result = ptrToObj.takeSharedPtrToObj(ptrToObj) + self.assertEqual(result, 100) + + # Pass an Integer as an argument that returns itself. + result = ptrToObj.takeInteger(ptrToObj.m_internalInteger) + self.assertEqual(integerCount(), 2) + result = None + self.assertEqual(integerCount(), 1) + + # Make a copy of the shared pointer, object count should not change. + ptrToObj2 = copy(ptrToObj) + self.assertEqual(objCount(), 1) + + # Delete the first shared pointer, object count should not change because the second + # one still has a reference. + del ptrToObj + self.assertEqual(objCount(), 1) + + # Delete the second smart pointer, object should be deleted. + del ptrToObj2 + self.assertEqual(objCount(), 0) + self.assertEqual(integerCount(), 0) + + def testIntegerSmartPointer(self): + # Uncomment to see more debug info about creation of objects and ref counts. + # Registry.getInstance().setShouldPrint(True) + + # Create Obj. + o = Obj() + self.assertEqual(objCount(), 1) + + # Create a shared pointer to an Integer together with an Integer. + ptrToInteger = o.giveSharedPtrToInteger() + self.assertEqual(objCount(), 1) + self.assertEqual(integerCount(), 2) + + # Get a wrapper to the Integer inside of the shared pointer, integer count should not + # change. + integer = ptrToInteger.data() + self.assertEqual(integerCount(), 2) + integer.m_int = 50 + self.assertEqual(integer.m_int, 50) + + # Set and get a member value via shared pointer (like operator->). + ptrToInteger.m_int = 100 + self.assertEqual(ptrToInteger.m_int, 100) + + # Pass smart pointer as argument to a method, return value is the value of m_int of + # passed Integer inside the smart pointer. + result = o.takeSharedPtrToInteger(ptrToInteger) + self.assertEqual(result, 100) + + # Make a copy of the shared pointer, integer count should not change. + ptrToInteger2 = copy(ptrToInteger) + self.assertEqual(integerCount(), 2) + + # Delete the first shared pointer, integer count should not change because the second + # one still has a reference. + del ptrToInteger + self.assertEqual(integerCount(), 2) + + # Delete the second smart pointer, integer should be deleted. + del ptrToInteger2 + self.assertEqual(objCount(), 1) + self.assertEqual(integerCount(), 1) + + # Delete the original object which was used to create the integer. + del o + self.assertEqual(objCount(), 0) + self.assertEqual(integerCount(), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken2/tests/smartbinding/typesystem_smart.xml b/sources/shiboken2/tests/smartbinding/typesystem_smart.xml new file mode 100644 index 000000000..a2654730d --- /dev/null +++ b/sources/shiboken2/tests/smartbinding/typesystem_smart.xml @@ -0,0 +1,23 @@ +<?xml version="1.0"?> +<typesystem package="smart"> + <primitive-type name="int" /> + <primitive-type name="char" /> + <primitive-type name="float" /> + <primitive-type name="bool" /> + + <!-- Used in tests to check what C++ objects are allocated. --> + <object-type name="Registry" /> + + <!-- Current limitation: shared pointer python objects can only be instantiated from API usage, + like when they are returned as a result of a method, or passed as arguments. It is not + possible to explicitly instantiate a new shared pointer in python e.g. o = SharedPtr_Foo() + won't work. + --> + <smart-pointer-type name="SharedPtr" type="shared" getter="data" ref-count-method="useCount" /> + + <object-type name="Obj" /> + <value-type name="Integer" /> + + <!-- Just used to silence the warnings that shiboken doens't know what to do with this type --> + <custom-type name="RefData" /> +</typesystem> diff --git a/sources/shiboken2/tests/sphinxtabletest.cpp b/sources/shiboken2/tests/sphinxtabletest.cpp new file mode 100644 index 000000000..6a8ac7703 --- /dev/null +++ b/sources/shiboken2/tests/sphinxtabletest.cpp @@ -0,0 +1,332 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "sphinxtabletest.h" +#include "qtdocgenerator.h" +#include <QtTest/QTest> +#include <QDebug> + +QString SphinxTableTest::transformXml(const char* xml) +{ + return QtXmlToSphinx(m_generator, xml).result(); +} + +void SphinxTableTest::setUp() +{ + m_generator = new QtDocGenerator; +} + +void SphinxTableTest::tearDown() +{ + delete m_generator; +} + +void SphinxTableTest::testEmptyString() +{ + const char* xml = ""; + QCOMPARE(transformXml(xml), QString()); +} + +void SphinxTableTest::testSimpleTable() +{ + const char* xml = "\ +<table>\ + <header>\ + <item>\ + <para>Header 1</para>\ + </item>\ + <item>\ + <para>Header 2</para>\ + </item>\ + </header>\ + <row>\ + <item>\ + <para>1 1</para>\ + </item>\ + <item>\ + <para>1 2</para>\ + </item>\ + </row>\ + <row>\ + <item>\ + <para>2 1</para>\ + </item>\ + <item>\ + <para>2 2</para>\ + </item>\ + </row>\ +</table>"; + QCOMPARE(transformXml(xml), QString("\ + +--------+--------+\n\ + |Header 1|Header 2|\n\ + +--------+--------+\n\ + |1 1 |1 2 |\n\ + +--------+--------+\n\ + |2 1 |2 2 |\n\ + +--------+--------+\n\ +\n")); +} + +void SphinxTableTest::testColSpan() +{ + const char* xml = "\ +<table>\ + <header>\ + <item>\ + <para>Header 1</para>\ + </item>\ + <item>\ + <para>Header 2</para>\ + </item>\ + </header>\ + <row>\ + <item colspan=\"2\">\ + <para>I'm a big text!</para>\ + </item>\ + </row>\ + <row>\ + <item>\ + <para>2 1</para>\ + </item>\ + <item>\ + <para>2 2</para>\ + </item>\ + </row>\ +</table>"; + QCOMPARE(transformXml(xml), QString("\ + +---------------+--------+\n\ + |Header 1 |Header 2|\n\ + +---------------+--------+\n\ + |I'm a big text! |\n\ + +---------------+--------+\n\ + |2 1 |2 2 |\n\ + +---------------+--------+\n\ +\n")); +} + + +void SphinxTableTest::testRowSpan() +{ + const char* xml = "\ +<table>\ + <header>\ + <item>\ + <para>Header 1</para>\ + </item>\ + <item>\ + <para>Header 2</para>\ + </item>\ + </header>\ + <row>\ + <item rowspan=\"2\">\ + <para>1.1</para>\ + </item>\ + <item>\ + <para>1.2</para>\ + </item>\ + </row>\ + <row>\ + <item>\ + <para>2 2</para>\ + </item>\ + </row>\ +</table>"; + QCOMPARE(transformXml(xml), QString("\ + +--------+--------+\n\ + |Header 1|Header 2|\n\ + +--------+--------+\n\ + |1.1 |1.2 |\n\ + + +--------+\n\ + | |2 2 |\n\ + +--------+--------+\n\ +\n")); +} + + +void SphinxTableTest::testComplexTable() +{ + const char* xml = "\ +<table>\ + <header>\ + <item>\ + <para>Header 1</para>\ + </item>\ + <item>\ + <para>Header 2</para>\ + </item>\ + <item>\ + <para>Header 3</para>\ + </item>\ + </header>\ + <row>\ + <item rowspan=\"2\">\ + <para>1.1</para>\ + </item>\ + <item colspan=\"2\">\ + <para>1.2</para>\ + </item>\ + </row>\ + <row>\ + <item>\ + <para>2 2</para>\ + </item>\ + <item>\ + <para>2 3</para>\ + </item>\ + </row>\ +</table>"; + QCOMPARE(transformXml(xml), QString("\ + +--------+--------+--------+\n\ + |Header 1|Header 2|Header 3|\n\ + +--------+--------+--------+\n\ + |1.1 |1.2 |\n\ + + +--------+--------+\n\ + | |2 2 |2 3 |\n\ + +--------+--------+--------+\n\ +\n")); +} + +void SphinxTableTest::testRowSpan2() +{ + const char* xml = "\ +<table>\ + <header>\ + <item><para>h1</para></item>\ + <item><para>h2</para></item>\ + <item><para>h3</para></item>\ + <item><para>h4</para></item>\ + </header>\ + <row>\ + <item rowspan=\"6\"><para>A</para></item>\ + <item rowspan=\"6\"><para>B</para></item>\ + <item><para>C</para></item>\ + <item><para>D</para></item>\ + </row>\ + <row>\ + <item><para>E</para></item>\ + <item><para>F</para></item>\ + </row>\ + <row>\ + <item><para>E</para></item>\ + <item><para>F</para></item>\ + </row>\ + <row>\ + <item><para>E</para></item>\ + <item><para>F</para></item>\ + </row>\ + <row>\ + <item><para>E</para></item>\ + <item><para>F</para></item>\ + </row>\ + <row>\ + <item><para>E</para></item>\ + <item><para>F</para></item>\ + </row>\ +</table>"; + QCOMPARE(transformXml(xml), QString("\ + +--+--+--+--+\n\ + |h1|h2|h3|h4|\n\ + +--+--+--+--+\n\ + |A |B |C |D |\n\ + + + +--+--+\n\ + | | |E |F |\n\ + + + +--+--+\n\ + | | |E |F |\n\ + + + +--+--+\n\ + | | |E |F |\n\ + + + +--+--+\n\ + | | |E |F |\n\ + + + +--+--+\n\ + | | |E |F |\n\ + +--+--+--+--+\n\ +\n")); +} + +void SphinxTableTest::testBrokenTable() +{ + const char* xml = "\ +<table>\ + <header>\ + <item>\ + <para>Header 1</para>\ + </item>\ + <item>\ + <para>Header 2</para>\ + </item>\ + </header>\ + <row>\ + <item>\ + <para>1.1</para>\ + </item>\ + <item>\ + <para>1.2</para>\ + </item>\ + </row>\ + <row>\ + <item colspan=\"2\">\ + <para>2 2</para>\ + </item>\ + <item>\ + <para>2 3</para>\ + </item>\ + <item>\ + <para>2 4</para>\ + </item>\ + <item>\ + <para>2 5</para>\ + </item>\ + </row>\ + <row>\ + <item>\ + <para>3 1</para>\ + </item>\ + <item>\ + <para>3 2</para>\ + </item>\ + <item>\ + <para>3 3</para>\ + </item>\ + </row>\ +</table>"; + QCOMPARE(transformXml(xml), QString("\ + +--------+------------+\n\ + |Header 1|Header 2 |\n\ + +--------+------------+\n\ + |1.1 |1.2 |\n\ + +--------+------------+\n\ + |2 2 2 3 2 4 2 5|\n\ + +--------+------------+\n\ + |3 1 |3 2 3 3 |\n\ + +--------+------------+\n\ +\n")); +} + + +QTEST_APPLESS_MAIN( SphinxTableTest ) + +#include "sphinxtabletest.moc" diff --git a/sources/shiboken2/tests/sphinxtabletest.h b/sources/shiboken2/tests/sphinxtabletest.h new file mode 100644 index 000000000..4779b75cd --- /dev/null +++ b/sources/shiboken2/tests/sphinxtabletest.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SPHINXTABLETEST_H +#define SPHINXTABLETEST_H + +#include <QObject> + +class QtDocGenerator; +class SphinxTableTest : public QObject { + Q_OBJECT + +private slots: + void setUp(); + void tearDown(); + void testEmptyString(); + void testSimpleTable(); + void testRowSpan(); + void testColSpan(); + void testComplexTable(); + void testRowSpan2(); + void testBrokenTable(); +private: + QtDocGenerator* m_generator; + + QString transformXml(const char* xml); +}; + +#endif diff --git a/sources/shiboken2/tests/test_generator/CMakeLists.txt b/sources/shiboken2/tests/test_generator/CMakeLists.txt new file mode 100644 index 000000000..5f5099897 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/CMakeLists.txt @@ -0,0 +1,68 @@ +project(test_generator) + +set(dummy_generator_SRC dummygenerator.cpp) +add_library(dummy_generator SHARED ${dummy_generator_SRC}) +target_link_libraries(dummy_generator ${APIEXTRACTOR_LIBRARY} ${QT_QTCORE_LIBRARY} genrunner) +set_property(TARGET dummy_generator PROPERTY PREFIX "") + +add_executable(dummygenerator main.cpp) +set(DUMMYGENERATOR_EXECUTABLE dummygenerator${generator_SUFFIX}) +set_target_properties(dummygenerator PROPERTIES OUTPUT_NAME ${DUMMYGENERATOR_EXECUTABLE}) +target_link_libraries(dummygenerator ${Qt5Core_LIBRARIES}) + +configure_file(dummygentestconfig.h.in "${CMAKE_CURRENT_BINARY_DIR}/dummygentestconfig.h" @ONLY) + +get_filename_component(APIEXTRACTOR_LIBRARY_DIRS ${APIEXTRACTOR_LIBRARY} PATH) +if(WIN32) + set(PATH_SEP ";") + find_program(APIEXTRACTOR_BINARY apiextractor.dll HINTS ${APIEXTRACTOR_LIBRARY_DIRS}) + get_filename_component(APIEXTRACTOR_BINARY_DIR ${APIEXTRACTOR_BINARY} PATH) + set(APIEXTRACTOR_LIBRARY_DIRS "${APIEXTRACTOR_LIBRARY_DIRS}${PATH_SEP}${APIEXTRACTOR_BINARY_DIR}") +else() + set(PATH_SEP ":") +endif() + +set(ENV_PATH "${generatorrunner_BINARY_DIR}${PATH_SEP}${CMAKE_CURRENT_BINARY_DIR}${PATH_SEP}$ENV{PATH}${PATH_SEP}${APIEXTRACTOR_LIBRARY_DIRS}") +set(ENV_QT_PLUGIN_PATH "${CMAKE_CURRENT_BINARY_DIR}${PATH_SEP}$ENV{QT_PLUGIN_PATH}") +if(WIN32) + string(REPLACE "\\;" ";" ENV_PATH "${ENV_PATH}") + string(REPLACE ";" "\\;" ENV_PATH "${ENV_PATH}") + string(REPLACE "\\;" ";" ENV_QT_PLUGIN_PATH "${ENV_QT_PLUGIN_PATH}") + string(REPLACE ";" "\\;" ENV_QT_PLUGIN_PATH "${ENV_QT_PLUGIN_PATH}") +endif() + +macro(m_add_test testname) + if(CMAKE_VERSION VERSION_LESS 2.8) + add_test(${testname} ${CMAKE_COMMAND} -DTEST=${testname} + -DWORKDIR=${CMAKE_CURRENT_BINARY_DIR} + -DENV_PATH=${ENV_PATH} -DENV_QT_PLUGIN_PATH=${ENV_QT_PLUGIN_PATH} + -P ${CMAKE_CURRENT_SOURCE_DIR}/run_test.cmake) + else() + add_test(${testname} ${testname}) + set_property(TEST ${testname} PROPERTY ENVIRONMENT "PATH=${ENV_PATH}" "QT_PLUGIN_PATH=${ENV_QT_PLUGIN_PATH}") + endif() +endmacro() + +macro(declare_test testname) + qt4_automoc("${testname}.cpp") + add_executable(${testname} "${testname}.cpp") + include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(${testname} + ${QT_QTTEST_LIBRARY} + ${QT_QTCORE_LIBRARY} + ${Qt5Test_LIBRARIES} + ${Qt5Core_LIBRARIES} + ) + m_add_test(${testname}) +endmacro(declare_test testname) + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/test_global.h" + "${CMAKE_CURRENT_BINARY_DIR}/test_global.h" COPYONLY) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/test_typesystem.xml" + "${CMAKE_CURRENT_BINARY_DIR}/test_typesystem.xml" COPYONLY) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/dummygentest-project.txt.in" + "${CMAKE_CURRENT_BINARY_DIR}/dummygentest-project.txt" @ONLY) +declare_test(dummygentest) + +add_dependencies(dummygenerator generatorrunner) + diff --git a/sources/shiboken2/tests/test_generator/dummygenerator.cpp b/sources/shiboken2/tests/test_generator/dummygenerator.cpp new file mode 100644 index 000000000..51d2b33da --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygenerator.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "dummygenerator.h" + +EXPORT_GENERATOR_PLUGIN(new DummyGenerator) + +using namespace std; + +QString +DummyGenerator::fileNameForClass(const AbstractMetaClass* metaClass) const +{ + return QString("%1_generated.txt").arg(metaClass->name().toLower()); +} + +void +DummyGenerator::generateClass(QTextStream& s, const AbstractMetaClass* metaClass) +{ + s << "// Generated code for class: " << qPrintable(metaClass->name()) << endl; +} + +bool +DummyGenerator::doSetup(const QMap<QString, QString>& args) +{ + if (args.contains("dump-arguments") && !args["dump-arguments"].isEmpty()) { + QFile logFile(args["dump-arguments"]); + logFile.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream out(&logFile); + foreach (const QString& key, args.keys()) { + if (key == "arg-1") + out << "header-file"; + else if (key == "arg-2") + out << "typesystem-file"; + else + out << key; + if (!args[key].isEmpty()) + out << " = " << args[key]; + out << endl; + } + } + return true; +} + diff --git a/sources/shiboken2/tests/test_generator/dummygenerator.h b/sources/shiboken2/tests/test_generator/dummygenerator.h new file mode 100644 index 000000000..5ab95a925 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygenerator.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef DUMMYGENERATOR_H +#define DUMMYGENERATOR_H + +#include "generator.h" + +class GENRUNNER_API DummyGenerator : public Generator +{ +public: + DummyGenerator() {} + ~DummyGenerator() {} + bool doSetup(const QMap<QString, QString>& args); + const char* name() const { return "DummyGenerator"; } + +protected: + void writeFunctionArguments(QTextStream&, const AbstractMetaFunction*, Options) const {} + void writeArgumentNames(QTextStream&, const AbstractMetaFunction*, Options) const {} + QString fileNameForClass(const AbstractMetaClass* metaClass) const; + void generateClass(QTextStream& s, const AbstractMetaClass* metaClass); + void finishGeneration() {} +}; + +#endif // DUMMYGENERATOR_H diff --git a/sources/shiboken2/tests/test_generator/dummygentest-project.txt.in b/sources/shiboken2/tests/test_generator/dummygentest-project.txt.in new file mode 100644 index 000000000..0a076d8bd --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentest-project.txt.in @@ -0,0 +1,20 @@ +[generator-project] + +generator-set = dummy +header-file = @CMAKE_CURRENT_BINARY_DIR@/test_global.h +typesystem-file = @CMAKE_CURRENT_BINARY_DIR@/test_typesystem.xml +output-directory = /tmp/output + +dump-arguments = @CMAKE_CURRENT_BINARY_DIR@/dummygen-args.log + +include-path = /include/path/location1 +include-path = /include/path/location2 + +typesystem-path = /typesystem/path/location1 +typesystem-path = /typesystem/path/location2 + +api-version = 1.2.3 +debug = sparse + +no-suppress-warnings + diff --git a/sources/shiboken2/tests/test_generator/dummygentest.cpp b/sources/shiboken2/tests/test_generator/dummygentest.cpp new file mode 100644 index 000000000..ae028e2c8 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentest.cpp @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dummygentest.h" +#include "dummygenerator.h" +#include "dummygentestconfig.h" +#include <QTemporaryFile> +#include <QtTest/QTest> +#include <QProcess> + +#define GENERATED_CONTENTS "// Generated code for class: Dummy" + +void DummyGenTest::initTestCase() +{ + int argc = 0; + char* argv[] = {NULL}; + QCoreApplication app(argc, argv); + workDir = QDir::currentPath(); + + headerFilePath = workDir + "/test_global.h"; + typesystemFilePath = workDir + "/test_typesystem.xml"; + projectFilePath = workDir + "/dummygentest-project.txt"; + generatedFilePath = QString("%1/dummy/dummy_generated.txt").arg(QDir::tempPath()); +} + +void DummyGenTest::testCallGenRunnerWithFullPathToDummyGenModule() +{ + QStringList args; + args.append("--generator-set=" DUMMYGENERATOR_BINARY_DIR "/dummy_generator" MODULE_EXTENSION); + args.append(QString("--output-directory=%1").arg(QDir::tempPath())); + args.append(headerFilePath); + args.append(typesystemFilePath); + int result = QProcess::execute("generatorrunner", args); + QCOMPARE(result, 0); + + QFile generatedFile(generatedFilePath); + generatedFile.open(QIODevice::ReadOnly); + QCOMPARE(generatedFile.readAll().trimmed(), QByteArray(GENERATED_CONTENTS).trimmed()); + generatedFile.close(); + + QVERIFY(generatedFile.remove()); +} + +void DummyGenTest::testCallGenRunnerWithNameOfDummyGenModule() +{ + QStringList args; + args.append("--generator-set=dummy"); + args.append(QString("--output-directory=%1").arg(QDir::tempPath())); + args.append(headerFilePath); + args.append(typesystemFilePath); + int result = QProcess::execute("generatorrunner", args); + QCOMPARE(result, 0); + + QFile generatedFile(generatedFilePath); + generatedFile.open(QIODevice::ReadOnly); + QCOMPARE(generatedFile.readAll().trimmed(), QByteArray(GENERATED_CONTENTS).trimmed()); + generatedFile.close(); + + QVERIFY(generatedFile.remove()); +} + +void DummyGenTest::testCallDummyGeneratorExecutable() +{ + QStringList args; + args.append(QString("--output-directory=%1").arg(QDir::tempPath())); + args.append(headerFilePath); + args.append(typesystemFilePath); + int result = QProcess::execute(DUMMYGENERATOR_BINARY, args); + QCOMPARE(result, 0); + + QFile generatedFile(generatedFilePath); + generatedFile.open(QIODevice::ReadOnly); + QCOMPARE(generatedFile.readAll().trimmed(), QByteArray(GENERATED_CONTENTS).trimmed()); + generatedFile.close(); + + QVERIFY(generatedFile.remove()); +} + +void DummyGenTest::testProjectFileArgumentsReading() +{ + QStringList args(QString("--project-file=%1/dummygentest-project.txt").arg(workDir)); + int result = QProcess::execute("generatorrunner", args); + QCOMPARE(result, 0); + + QFile logFile(workDir + "/dummygen-args.log"); + logFile.open(QIODevice::ReadOnly); + QStringList logContents; + while (!logFile.atEnd()) + logContents << logFile.readLine().trimmed(); + logContents.sort(); + QCOMPARE(logContents[0], QString("api-version = 1.2.3")); + QCOMPARE(logContents[1], QString("debug = sparse")); + QVERIFY(logContents[2].startsWith("dump-arguments = ")); + QVERIFY(logContents[2].endsWith("dummygen-args.log")); + QCOMPARE(logContents[3], QString("generator-set = dummy")); + QVERIFY(logContents[4].startsWith("header-file = ")); + QVERIFY(logContents[4].endsWith("test_global.h")); + QCOMPARE(logContents[5], + QDir::toNativeSeparators(QString("include-paths = /include/path/location1%1/include/path/location2").arg(PATH_SPLITTER))); + QCOMPARE(logContents[6], QString("no-suppress-warnings")); + QCOMPARE(logContents[7], QString("output-directory = /tmp/output")); + QVERIFY(logContents[8].startsWith("project-file = ")); + QVERIFY(logContents[8].endsWith("dummygentest-project.txt")); + QVERIFY(logContents[9].startsWith("typesystem-file = ")); + QVERIFY(logContents[9].endsWith("test_typesystem.xml")); + QCOMPARE(logContents[10], + QDir::toNativeSeparators(QString("typesystem-paths = /typesystem/path/location1%1/typesystem/path/location2").arg(PATH_SPLITTER))); +} + +QTEST_APPLESS_MAIN(DummyGenTest) + +#include "dummygentest.moc" + diff --git a/sources/shiboken2/tests/test_generator/dummygentest.h b/sources/shiboken2/tests/test_generator/dummygentest.h new file mode 100644 index 000000000..be8ca42af --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentest.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DUMMYGENTABLETEST_H +#define DUMMYGENTABLETEST_H + +#include <QObject> + +class DummyGenerator; + +class DummyGenTest : public QObject +{ + Q_OBJECT + +private: + QString workDir; + QString headerFilePath; + QString typesystemFilePath; + QString generatedFilePath; + QString projectFilePath; + +private slots: + void initTestCase(); + void testCallGenRunnerWithFullPathToDummyGenModule(); + void testCallGenRunnerWithNameOfDummyGenModule(); + void testCallDummyGeneratorExecutable(); + void testProjectFileArgumentsReading(); +}; + +#endif + diff --git a/sources/shiboken2/tests/test_generator/dummygentestconfig.h.in b/sources/shiboken2/tests/test_generator/dummygentestconfig.h.in new file mode 100644 index 000000000..9da17dcd3 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentestconfig.h.in @@ -0,0 +1,15 @@ +#ifndef DUMMYGENTESTCONFIG_H +#define DUMMYGENTESTCONFIG_H + +#define MODULE_EXTENSION "@CMAKE_SHARED_LIBRARY_SUFFIX@" +#define DUMMYGENERATOR_BINARY "@DUMMYGENERATOR_EXECUTABLE@" +#define DUMMYGENERATOR_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@" + +#ifdef _WINDOWS + #define PATH_SPLITTER ";" +#else + #define PATH_SPLITTER ":" +#endif + +#endif // DUMMYGENTESTCONFIG_H + diff --git a/sources/shiboken2/tests/test_generator/main.cpp b/sources/shiboken2/tests/test_generator/main.cpp new file mode 100644 index 000000000..5c46ea006 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/main.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore> + +int main(int argc, char *argv[]) +{ + QStringList args; + args.append("--generator-set=dummy"); + for (int i = 1; i < argc; i++) + args.append(argv[i]); + return QProcess::execute("generatorrunner", args); +} + diff --git a/sources/shiboken2/tests/test_generator/run_test.cmake b/sources/shiboken2/tests/test_generator/run_test.cmake new file mode 100644 index 000000000..34a821d80 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/run_test.cmake @@ -0,0 +1,11 @@ +# The tests are run through this script due to a limitation +# on versions of CMake lesser than 2.8, that prevent setting +# environment variables for tests from working. + +set(ENV{PATH} "${ENV_PATH}") +set(ENV{QT_PLUGIN_PATH} "${ENV_QT_PLUGIN_PATH}") +execute_process(COMMAND ${TEST} WORKING_DIRECTORY "${WORKDIR}" RESULT_VARIABLE OK) + +if(NOT OK EQUAL 0) + message(SEND_ERROR "${TEST} failed!") +endif() diff --git a/sources/shiboken2/tests/test_generator/test_global.h b/sources/shiboken2/tests/test_generator/test_global.h new file mode 100644 index 000000000..6a95200cf --- /dev/null +++ b/sources/shiboken2/tests/test_generator/test_global.h @@ -0,0 +1 @@ +struct Dummy {}; diff --git a/sources/shiboken2/tests/test_generator/test_typesystem.xml b/sources/shiboken2/tests/test_generator/test_typesystem.xml new file mode 100644 index 000000000..c19a4e95e --- /dev/null +++ b/sources/shiboken2/tests/test_generator/test_typesystem.xml @@ -0,0 +1,3 @@ +<typesystem package='dummy'> + <value-type name='Dummy'/> +</typesystem> |