diff --git a/tests/GenerateSipBindings/cpplib.cpp b/tests/GenerateSipBindings/cpplib.cpp index c5b7a5f..76b97b0 100644 --- a/tests/GenerateSipBindings/cpplib.cpp +++ b/tests/GenerateSipBindings/cpplib.cpp @@ -1,143 +1,148 @@ #include "cpplib.h" MyObject::MyObject(QObject* parent) : QObject(parent) { } double MyObject::unnamedParameters(int i, double d) { return i * d; } int MyObject::addThree(int input) const { return input + 3; } QList MyObject::addThree(QList input) const { auto output = input; std::transform(output.begin(), output.end(), output.begin(), [](int in) { return in + 3; }); return output; } const QString MyObject::addThree(const QString& input, const QString& prefix) const { return prefix + input + QStringLiteral("Three"); } int MyObject::findNeedle(QStringList list, QString needle, Qt::MatchFlags flags) const { if (flags & Qt::MatchStartsWith) { auto it = std::find_if(list.begin(), list.end(), [needle](QString cand) { return cand.startsWith(needle); }); if (it != list.end()) { return std::distance(list.begin(), it); } return -1; } return list.indexOf(needle); } int MyObject::qtEnumTest(QFlags flags) { return flags; } int MyObject::localEnumTest(QFlags flags) { return flags; } int MyObject::functionParam(std::function fn) { return fn(); } int MyObject::groups(unsigned int maxCount) const { return maxCount; } class FwdDecl { }; int MyObject::fwdDecl(const FwdDecl&) { return 42; } int MyObject::const_parameters(const int input, QObject* const obj) const { if (obj) return input / 3; return input / 2; } NonCopyable::NonCopyable() : mNum(new int(42)) { } NonCopyable::~NonCopyable() { delete mNum; } NonCopyableByMacro::NonCopyableByMacro() { } namespace SomeNS { NonCopyableInNS::NonCopyableInNS() : mNum(new int(42)) { } NonCopyableInNS::~NonCopyableInNS() { delete mNum; } } void MyObject::publicSlot1() { Q_EMIT publicSlotCalled(); } void MyObject::publicSlot2() { Q_EMIT publicSlotCalled(); } void MyObject::protectedSlot1() { Q_EMIT protectedSlotCalled(); } void MyObject::protectedSlot2() { Q_EMIT protectedSlotCalled(); } void MyObject::privateSlot1() { Q_EMIT privateSlotCalled(); } void MyObject::privateSlot2() { Q_EMIT privateSlotCalled(); } + +qreal SomeNS::useEnum(MyFlags flags) +{ + return flags; +} diff --git a/tests/GenerateSipBindings/cpplib.h b/tests/GenerateSipBindings/cpplib.h index 6ec3f57..ba8e9f2 100644 --- a/tests/GenerateSipBindings/cpplib.h +++ b/tests/GenerateSipBindings/cpplib.h @@ -1,124 +1,132 @@ #pragma once #include #include #include #include #include class FwdDecl; class MyObject : public QObject { Q_OBJECT public: MyObject(QObject* parent = nullptr); enum LocalEnum { Val1 = 1, Val2 }; Q_DECLARE_FLAGS(LocalEnums, LocalEnum) enum { AnonVal1, AnonVal2 }; double unnamedParameters(int, double); int addThree(int input) const; QList addThree(QList input) const; const QString addThree(const QString& input, const QString& prefix = QStringLiteral("Default")) const; int findNeedle(QStringList list, QString needle, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap)) const; int qtEnumTest(QFlags flags); int localEnumTest(QFlags flags); int functionParam(std::function fn); int groups(unsigned int maxCount = std::numeric_limits::max()) const; int const_parameters(const int input, QObject* const obj = 0) const; int fwdDecl(const FwdDecl& f); int fwdDeclRef(FwdDecl& f); mode_t dummyFunc(QObject* parent) { return 0; } signals: void publicSlotCalled(); Q_SIGNALS: void privateSlotCalled(); void protectedSlotCalled(); public slots: void publicSlot1(); public Q_SLOTS: void publicSlot2(); protected slots: void protectedSlot1(); protected Q_SLOTS: void protectedSlot2(); private slots: void privateSlot1(); private Q_SLOTS: void privateSlot2(); }; class NonCopyable { public: NonCopyable(); ~NonCopyable(); private: int* const mNum; }; class NonCopyableByMacro { public: NonCopyableByMacro(); private: Q_DISABLE_COPY(NonCopyableByMacro) }; Q_DECLARE_METATYPE(NonCopyableByMacro*) class HasPrivateDefaultCtor { public: private: HasPrivateDefaultCtor(int param = 0); }; namespace SomeNS { class NonCopyableInNS { public: NonCopyableInNS(); ~NonCopyableInNS(); private: int* const mNum; }; +enum MyFlagType { + EnumValueOne = 0x01, + EnumValueTwo = 0x02 +}; +Q_DECLARE_FLAGS(MyFlags, MyFlagType) + +qreal useEnum(MyFlags flags = EnumValueOne); + } enum __attribute__((visibility("default"))) EnumWithAttributes { Foo, Bar = 2 }; diff --git a/tests/GenerateSipBindings/testscript.py b/tests/GenerateSipBindings/testscript.py index 973d428..deefb96 100644 --- a/tests/GenerateSipBindings/testscript.py +++ b/tests/GenerateSipBindings/testscript.py @@ -1,93 +1,100 @@ import sys from PyQt5 import QtCore sys.path.append(sys.argv[1]) import PyTest.CppLib mo = PyTest.CppLib.MyObject() assert(mo.addThree(39) == 42) assert(mo.addThree([38, 39, 40]) == [41, 42, 43]) assert(mo.addThree("SomeString") == "DefaultSomeStringThree") assert(mo.findNeedle(["One", "Two", "Three"], "Two") == 1) assert(mo.findNeedle(["One", "Two", "Three"], "Four") == -1) assert(mo.findNeedle(["One", "Two", "Three"], "Th") == 2) assert(mo.findNeedle(["One", "Two", "Three"], "Th", QtCore.Qt.MatchExactly) == -1) assert(mo.const_parameters(30) == 15) assert(mo.const_parameters(30, mo) == 10) assert(mo.qtEnumTest(QtCore.Qt.MatchContains | QtCore.Qt.MatchStartsWith) == 3) assert(mo.localEnumTest(PyTest.CppLib.MyObject.Val2) == 2) # # Verify that an enum with attributes can be read. # assert(PyTest.CppLib.Foo == 0) assert(PyTest.CppLib.Bar == 2) class Reactor(QtCore.QObject): def __init__(self, obj): QtCore.QObject.__init__(self) self.gotPrivateSlotCalledSignal = False self.gotProtectedSlotCalledSignal = False self.gotPublicSlotCalledSignal = False obj.privateSlotCalled.connect(self.react_to_privateSlotCalled) obj.protectedSlotCalled.connect(self.react_to_protectedSlotCalled) obj.publicSlotCalled.connect(self.react_to_publicSlotCalled) def react_to_privateSlotCalled(self): self.gotPrivateSlotCalledSignal = True def react_to_protectedSlotCalled(self): self.gotProtectedSlotCalledSignal = True def react_to_publicSlotCalled(self): self.gotPublicSlotCalledSignal = True class Emitter(QtCore.QObject): privateTrigger = QtCore.pyqtSignal() protectedTrigger = QtCore.pyqtSignal() publicTrigger = QtCore.pyqtSignal() def __init__(self, obj): QtCore.QObject.__init__(self) self.privateTrigger.connect(obj.privateSlot1) self.protectedTrigger.connect(obj.protectedSlot1) self.publicTrigger.connect(obj.publicSlot1) def emitSignalForPublic(self): self.publicTrigger.emit() def emitSignalForPrivate(self): self.privateTrigger.emit() def emitSignalForProtected(self): self.protectedTrigger.emit() e = Emitter(mo) r = Reactor(mo) assert(not r.gotPrivateSlotCalledSignal) assert(not r.gotProtectedSlotCalledSignal) assert(not r.gotPublicSlotCalledSignal) e.emitSignalForPrivate() assert(r.gotPrivateSlotCalledSignal) e.emitSignalForProtected() assert(r.gotProtectedSlotCalledSignal) e.emitSignalForPublic() assert(r.gotPublicSlotCalledSignal) + +assert(PyTest.CppLib.SomeNS.EnumValueOne == 1) +assert(PyTest.CppLib.SomeNS.EnumValueTwo == 2) + +assert(PyTest.CppLib.SomeNS.useEnum() == 1.0) +assert(PyTest.CppLib.SomeNS.useEnum(PyTest.CppLib.SomeNS.EnumValueOne) == 1.0) +assert(PyTest.CppLib.SomeNS.useEnum(PyTest.CppLib.SomeNS.EnumValueTwo) == 2.0)