1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# Test suite for kaa notifier classes
import kaa
def test(result, expected):
if result != expected:
raise ValueError, "expected: %s - got: %s" % (str(expected), str(result))
def cb_func(*args, **kwargs):
return args, kwargs
class Cls(object):
def meth(self, *args, **kwargs):
return args, kwargs
##############################################################
# Callbacks
#######################
print "Callbacks ..."
cb = kaa.Callable(cb_func)
test( cb(42), ((42,), {}) )
cb = kaa.Callable(cb_func, 42)
test( cb(), ((42,), {}) )
cb = kaa.Callable(cb_func)
test( cb(42, "foo", bar="baz"), ((42,"foo"), {"bar":"baz"}) )
cb_meth = Cls().meth
cb = kaa.Callable(cb_meth)
test( cb(42), ((42,), {}) )
cb = kaa.Callable(cb_meth)
del cb_meth
test( cb(42), ((42,), {}) )
##############################################################
# Weak callbacks
#######################
print "Weak Callbacks ..."
def cb_func2(*args, **kwargs):
return args, kwargs
cb = kaa.WeakCallable(cb_func)
test( cb(42), ((42,), {}) )
# Lambdas are not weakref'd
cb = kaa.WeakCallable(lambda arg: arg)
test( cb(42), 42)
# Functions are weakref'd
cb = kaa.WeakCallable(cb_func2)
del cb_func2
try:
cb(42)
except kaa.CallableError:
pass
cb_meth = Cls().meth
cb = kaa.WeakCallable(cb_meth)
del cb_meth
try:
cb(42)
except kaa.CallableError:
pass
##############################################################
# Timers
#######################
print "Timers ..."
def test_OneShotTimer(arg):
result.append(arg)
def test_Timer(arg):
result.append(arg)
class Cls(object):
def meth(self, arg):
result.append(arg)
result = []
kaa.OneShotTimer(test_OneShotTimer, 42).start(0)
kaa.main.step()
test(result, [42])
result = []
kaa.OneShotTimer(Cls().meth, 42).start(0)
kaa.main.step()
test(result, [42])
result = []
kaa.WeakOneShotTimer(Cls().meth, 42).start(0)
kaa.main.step()
test(result, [])
result = []
cb = Cls().meth
kaa.WeakOneShotTimer(cb, 42).start(0)
kaa.main.step()
test(result, [42])
result = []
timer = kaa.Timer(Cls().meth, 42)
timer.start(0)
for i in range(5):
kaa.main.step()
timer.stop()
test(result, [42, 42, 42, 42, 42])
result = []
# Tests proper destruction of a weak timer. We hold a weak ref to Cls().meth
# and since there are no other refs, the weak timer never gets activated.
timer = kaa.WeakTimer(Cls().meth, 42)
timer.start(0)
# Notifier technically has nothing to do, so it may sleep for 30 seconds.
print '(This could take 30 seconds or so.)'
for i in range(2):
kaa.main.step()
test(result, [])
test(timer.active, False)
##############################################################
# Signals
#######################
print "Signals ..."
def cb_func(*arg):
result.extend(arg)
class Cls(object):
def meth(self, *arg):
result.extend(arg)
sig = kaa.Signal()
sig.connect(cb_func, 42)
result = []
sig.emit()
test(result, [42])
result = []
sig.emit(42)
test(result, [42, 42])
test(sig.count(), 1)
sig.disconnect(cb_func)
test(sig.count(), 0)
sig.connect(Cls().meth, 42)
result = []
sig.emit()
test(result, [42])
sig.connect(Cls().meth, 42)
test(sig.count(), 2)
result = []
sig.emit()
test(result, [42, 42])
sig.disconnect_all()
test(sig.count(), 0)
cb = Cls().meth
sig.connect_weak(cb, 42)
result = []
sig.emit()
test(result, [42])
test(sig.count(), 1)
del cb
test(sig.count(), 0)
sig.emit()
test(sig.count(), 0)
result = []
sig.emit()
test(result, [])
cb = Cls().meth
sig.connect_weak(cb, Cls())
test(sig.count(), 0)
sig.emit()
test(sig.count(), 0)
# TODO: test threads too.
|