File indexing completed on 2024-04-14 03:50:22
0001 0002 import sys 0003 0004 from PyQt5 import QtCore 0005 0006 sys.path.append(sys.argv[1]) 0007 0008 import PyTest.CppLib 0009 0010 mo = PyTest.CppLib.MyObject() 0011 0012 assert(mo.addThree(39) == 42) 0013 0014 assert(mo.addThree([38, 39, 40]) == [41, 42, 43]) 0015 0016 assert(mo.addThree("SomeString") == "DefaultSomeStringThree") 0017 0018 assert(mo.findNeedle(["One", "Two", "Three"], "Two") == 1) 0019 assert(mo.findNeedle(["One", "Two", "Three"], "Four") == -1) 0020 assert(mo.findNeedle(["One", "Two", "Three"], "Th") == 2) 0021 assert(mo.findNeedle(["One", "Two", "Three"], "Th", QtCore.Qt.MatchExactly) == -1) 0022 0023 assert(mo.const_parameters(30) == 15) 0024 assert(mo.const_parameters(30, mo) == 10) 0025 0026 assert(mo.qtEnumTest(QtCore.Qt.MatchContains | QtCore.Qt.MatchStartsWith) == 3) 0027 assert(mo.localEnumTest(PyTest.CppLib.MyObject.Val2) == 2) 0028 0029 lfd = PyTest.CppLib.LocalFwdDecl(18) 0030 0031 assert(mo.localFwdDecl(lfd) == 18) 0032 0033 import PyTest.ExternalLib 0034 0035 efd = PyTest.ExternalLib.ExternalFwdDecl(18) 0036 0037 assert(mo.externalFwdDecl(efd) == 18) 0038 0039 assert(mo.localListDecl([1, 5, 7]) == 13) 0040 0041 lfdl = [PyTest.CppLib.LocalFwdDecl(3), PyTest.CppLib.LocalFwdDecl(6)] 0042 0043 assert(mo.localDeclListDecl(lfdl) == 9) 0044 0045 # 0046 # Verify that an enum with attributes can be read. 0047 # 0048 assert(PyTest.CppLib.Foo == 0) 0049 assert(PyTest.CppLib.Bar == 2) 0050 0051 class Reactor(QtCore.QObject): 0052 def __init__(self, obj): 0053 QtCore.QObject.__init__(self) 0054 self.gotPrivateSlotCalledSignal = False 0055 self.gotProtectedSlotCalledSignal = False 0056 self.gotPublicSlotCalledSignal = False 0057 0058 obj.privateSlotCalled.connect(self.react_to_privateSlotCalled) 0059 obj.protectedSlotCalled.connect(self.react_to_protectedSlotCalled) 0060 obj.publicSlotCalled.connect(self.react_to_publicSlotCalled) 0061 0062 def react_to_privateSlotCalled(self): 0063 self.gotPrivateSlotCalledSignal = True 0064 0065 def react_to_protectedSlotCalled(self): 0066 self.gotProtectedSlotCalledSignal = True 0067 0068 def react_to_publicSlotCalled(self): 0069 self.gotPublicSlotCalledSignal = True 0070 0071 class Emitter(QtCore.QObject): 0072 privateTrigger = QtCore.pyqtSignal() 0073 protectedTrigger = QtCore.pyqtSignal() 0074 publicTrigger = QtCore.pyqtSignal() 0075 0076 def __init__(self, obj): 0077 QtCore.QObject.__init__(self) 0078 self.privateTrigger.connect(obj.privateSlot1) 0079 self.protectedTrigger.connect(obj.protectedSlot1) 0080 self.publicTrigger.connect(obj.publicSlot1) 0081 0082 def emitSignalForPublic(self): 0083 self.publicTrigger.emit() 0084 0085 def emitSignalForPrivate(self): 0086 self.privateTrigger.emit() 0087 0088 def emitSignalForProtected(self): 0089 self.protectedTrigger.emit() 0090 0091 e = Emitter(mo) 0092 0093 r = Reactor(mo) 0094 0095 assert(not r.gotPrivateSlotCalledSignal) 0096 assert(not r.gotProtectedSlotCalledSignal) 0097 assert(not r.gotPublicSlotCalledSignal) 0098 0099 e.emitSignalForPrivate() 0100 0101 assert(r.gotPrivateSlotCalledSignal) 0102 0103 e.emitSignalForProtected() 0104 0105 assert(r.gotProtectedSlotCalledSignal) 0106 0107 e.emitSignalForPublic() 0108 0109 assert(r.gotPublicSlotCalledSignal) 0110 0111 assert(PyTest.CppLib.SomeNS.EnumValueOne == 1) 0112 assert(PyTest.CppLib.SomeNS.EnumValueTwo == 2) 0113 0114 assert(PyTest.CppLib.SomeNS.useEnum() == 1.0) 0115 assert(PyTest.CppLib.SomeNS.useEnum(PyTest.CppLib.SomeNS.EnumValueOne) == 1.0) 0116 assert(PyTest.CppLib.SomeNS.useEnum(PyTest.CppLib.SomeNS.EnumValueTwo) == 2.0) 0117 0118 assert(PyTest.CppLib.SomeNS.customMethod([2, 3, 5]) == 10) 0119 0120 assert(PyTest.CppLib.anotherCustomMethod([2, 3, 5]) == 52) 0121 0122 sdo = PyTest.CppLib.SubdirObject() 0123 assert(sdo.mul(5, 6) == 30) 0124 0125 visible = PyTest.CppLib.Visible() 0126 assert visible.visible_fn() 0127 0128 try: 0129 assert visible.invisible_fn() 0130 assert False 0131 except AttributeError as e: 0132 assert str(e) == "'Visible' object has no attribute 'invisible_fn'" 0133 try: 0134 invisible = PyTest.CppLib.Invisible() 0135 assert False 0136 except AttributeError as e: 0137 assert str(e) == "module 'PyTest.CppLib' has no attribute 'Invisible'"