-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_callbacks.py
381 lines (304 loc) · 11.9 KB
/
test_callbacks.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!./venv/bin/python3
import pytest
import re
from htag import Tag,HTagException,expose
from htag.render import HRenderer
import asyncio
from test_interactions import Simu
from htag.tag import Caller
def test_simple_callback():
def action():
pass
s=Tag.button("hello",_onclick=action)
assert isinstance( s["onclick"], Caller )
caller=s["onclick"]
assert caller.instance == s
assert caller.callback == action
assert caller.args == ()
assert caller.kargs == {}
assert f"onclick-{id(s)}" == caller._assigned
assert caller._assigned in s._callbacks_
assert s._callbacks_[ caller._assigned ] == caller
# try to rebind
def action2():
pass
s["onclick"] = action2
assert len(s._callbacks_)==1
caller=s["onclick"]
assert caller.instance == s
assert caller.callback == action2
assert caller.args == ()
assert caller.kargs == {}
assert f"onclick-{id(s)}" == caller._assigned
assert caller._assigned in s._callbacks_
assert s._callbacks_[ caller._assigned ] == caller
def test_simple_binded_himself_callback():
def action():
pass
s=Tag.button("hello")
s["onclick"]=s.bind( action )
assert isinstance( s["onclick"], Caller )
caller=s["onclick"]
assert caller.instance == s
assert caller.callback == action
assert caller.args == ()
assert caller.kargs == {}
assert f"onclick-{id(s)}" == caller._assigned
assert caller._assigned in s._callbacks_
assert s._callbacks_[ caller._assigned ] == caller
def test_multiple_binded_himself_callback():
def action1():
pass
def action2():
pass
s=Tag.button("hello")
s["onclick"]=s.bind( action1 ).bind( action2 )
assert isinstance( s["onclick"], Caller )
caller=s["onclick"]
assert caller.instance == s
assert caller.callback == action1
assert caller.args == ()
assert caller.kargs == {}
assert f"onclick-{id(s)}" == caller._assigned
assert caller._assigned in s._callbacks_
assert s._callbacks_[ caller._assigned ] == caller
assert caller._others==[ (action2,(),{})]
def test_binded_concatenate_strings(): #TODO: need more tests
def action1(o):
pass
s=Tag.button("hello")
s["onclick"]=s.bind( action1 )+"let x=42"
assert ";let x=42;}" in str(s)
s["onclick"]="let x=41"+s.bind( action1 )
assert "try{let x=41;" in str(s)
# can't add str to a callback not binded (logical!)
with pytest.raises(TypeError):
s["onclick"]=action1+"let x=42;"
# cant add non-str to a Caller
with pytest.raises(HTagException):
s["onclick"]=s.bind(action1)+42
with pytest.raises(HTagException):
s["onclick"]=42+s.bind(action1)
# and it works too for old binders (self.bind.<method>())
class A(Tag.div):
def init(self):
self <= Tag.button("1",_onclick=self.bind.action()+"let x=42")
self <= Tag.button("2",_onclick="let x=41"+self.bind.action())
def action(self):
pass
s=str(A()) # TODO: not terrible
assert ";let x=42;}" in s
assert "try{let x=41;" in s
def test_binded_parent_callback(): # args/kargs
def action(o):
pass
p=Tag.div()
s=Tag.button("hello",_onclick=p.bind( action, b"this.value", p=42 ) )
assert isinstance( s["onclick"], Caller )
caller=s["onclick"]
assert caller.instance == p
assert caller.callback == action
assert caller.args == (b"this.value",)
assert caller.kargs == dict(p=42)
assert f"onclick-{id(s)}" == caller._assigned
assert caller._assigned not in s._callbacks_
assert caller._assigned in p._callbacks_
assert p._callbacks_[ caller._assigned ] == caller
print(p)
print(s)
print(caller)
def test_ko():
x=Tag.a("link",_onclick="alert(42)")
x["onclick"] = "alert(42)"
rt=Tag.div()
a=Tag.a("link")
a["onclick"]=rt.bind( test_ko ) # possible
assert isinstance( a["onclick"], Caller )
a=Tag.a("link")
a["onclick"]=test_ko # possible but non sens
assert isinstance( a["onclick"], Caller )
a=Tag.a("link")
with pytest.raises(TypeError):
a["onclick"]=a.bind()
with pytest.raises(HTagException):
a["onclick"]=a.bind(None)
with pytest.raises(HTagException):
a["onclick"]=a.bind(a.bind(None)) # not double bind
with pytest.raises(HTagException):
a["onclick"]=a.bind("")
with pytest.raises(HTagException):
a["onclick"]=a.bind(test_ko).bind("nimp")
with pytest.raises(HTagException):
a["onclick"]=a.bind(test_ko).bind( test_ko, b"this.value") # not possible to bind a js in a second binder (just the first)
with pytest.raises(TypeError):
a["onclick"]=a.bind(test_ko).bind() # non sens
with pytest.raises(HTagException):
str( a.bind(test_ko) ) # not possible coz not assigned to a Tag !
a["onclick"]=a.bind(test_ko).bind(None).bind(None) # possible
# def test_prior():
# def action1():
# pass
# def action2():
# pass
# s=Tag.button("hello")
# callback = s.bind( action1 )
# #----------------------------------------
# s["onclick"]=callback
# assert isinstance( s["onclick"], Caller )
# caller=s["onclick"]
# assert caller.instance == s
# assert caller.callback == action1 # action1 is first !
# assert caller._others == []
# assert caller.args == ()
# assert caller.kargs == {}
# assert f"onclick-{id(s)}" == caller._assigned
# assert caller._assigned in s._callbacks_
# assert s._callbacks_[ caller._assigned ] == caller
# #----------------------------------------
# s["onclick"]=callback.prior( action2 )
# assert isinstance( s["onclick"], Caller )
# caller=s["onclick"]
# assert caller.instance == s
# assert caller.callback == action2 # action2 is now first !
# assert caller._others[0][0] == action1 # action1 is next
# assert caller.args == ()
# assert caller.kargs == {}
# assert f"onclick-{id(s)}" == caller._assigned
# assert caller._assigned in s._callbacks_
# assert s._callbacks_[ caller._assigned ] == caller
def test_on_event():
def action1(obj): # classic
assert isinstance(obj,Tag) and obj.tag=="button"
async def action2(obj): # async method
assert isinstance(obj,Tag) and obj.tag=="button"
def action3(obj): # classic generator
assert isinstance(obj,Tag) and obj.tag=="button"
yield "hello"
async def action4(obj): # async generator
assert isinstance(obj,Tag) and obj.tag=="button"
yield "hello"
async def test():
evt = s["onclick"]._assigned
return [i async for i in s.__on__( evt )]
############################################################
# test btn handling simple callback
############################################################
s=Tag.button("hello", _onclick = action1)
assert asyncio.run( test() ) == []
#----------------------------------------------------
s=Tag.button("hello", _onclick = action2)
assert asyncio.run( test() ) == []
#----------------------------------------------------
s=Tag.button("hello", _onclick = action3)
assert asyncio.run( test() ) == [ "hello" ]
#----------------------------------------------------
s=Tag.button("hello", _onclick = action4)
assert asyncio.run( test() ) == [ "hello" ]
############################################################
# test btn handling binded callback
############################################################
s=Tag.button("hello")
s["onclick"]= s.bind( action1)
assert asyncio.run( test() ) == []
#----------------------------------------------------
s=Tag.button("hello")
s["onclick"]= s.bind( action2)
assert asyncio.run( test() ) == []
#----------------------------------------------------
s=Tag.button("hello")
s["onclick"]= s.bind( action3)
assert asyncio.run( test() ) == [ "hello" ]
#----------------------------------------------------
s=Tag.button("hello")
s["onclick"]= s.bind( action4)
assert asyncio.run( test() ) == [ "hello" ]
############################################################
# test btn handling theirs events
############################################################
class App(Tag.div):
def __init__(self):
super().__init__()
self <= Tag.button("hello", _onclick = self.action1)
self <= Tag.button("hello", _onclick = self.action2)
self <= Tag.button("hello", _onclick = self.action3)
self <= Tag.button("hello", _onclick = self.action4)
def action1(self, o): # classic
assert isinstance(o,Tag) and o.tag=="button"
async def action2(self, o): # async method
assert isinstance(o,Tag) and o.tag=="button"
def action3(self, o): # classic generator
assert isinstance(o,Tag) and o.tag=="button"
yield "hello"
async def action4(self, o): # async generator
assert isinstance(o,Tag) and o.tag=="button"
yield "hello"
async def test(nb):
app=App()
o=app.childs[nb]
evt = o["onclick"]._assigned
return [i async for i in o.__on__( evt )] # execute on button
assert asyncio.run( test(0) ) ==[]
assert asyncio.run( test(1) ) ==[]
assert asyncio.run( test(2) ) ==["hello"]
assert asyncio.run( test(3) ) ==["hello"]
############################################################
# test app handling btn events
############################################################
class App(Tag.div):
def __init__(self):
super().__init__()
self <= Tag.button("hello", _onclick = self.bind( self.action1 ))
self <= Tag.button("hello", _onclick = self.bind( self.action2 ))
self <= Tag.button("hello", _onclick = self.bind( self.action3 ))
self <= Tag.button("hello", _onclick = self.bind( self.action4 ))
def action1(self): # classic
pass
async def action2(self): # async method
pass
def action3(self): # classic generator
yield "hello"
async def action4(self): # async generator
yield "hello"
async def test(nb):
app=App()
o=app.childs[nb]
evt = o["onclick"]._assigned
return [i async for i in app.__on__( evt )] # execute on app
assert asyncio.run( test(0) ) ==[]
assert asyncio.run( test(1) ) ==[]
assert asyncio.run( test(2) ) ==["hello"]
assert asyncio.run( test(3) ) ==["hello"]
def test_auto_expose():
class Jo(Tag.div):
def init(self):
self <= Tag.button("hello",_onclick="self.action(42)" )
def action(self,val):
assert val == 42
# assert no @expose, no auto declared
s=Simu(Jo)
assert "self.action = function(_)" not in str(s.hr)
class Jo(Tag.div):
def init(self):
self <= Tag.button("hello",_onclick="self.action(42)" )
@expose
def action(self,val):
assert val == 42
# assert with @expose, it's now autodeclared
s=Simu(Jo)
assert "self.action = function(_)" in str(s.hr)
#TODO: should test that the click do what it can do
if __name__=="__main__":
import logging
logging.basicConfig(format='[%(levelname)-5s] %(name)s: %(message)s',level=logging.DEBUG)
# logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.DEBUG)
logger = logging.getLogger("htag.tag")
logger.setLevel(logging.INFO)
# test_simple_callback()
# test_simple_binded_himself_callback()
# test_binded_parent_callback()
# test_multiple_binded_himself_callback()
# test_on_event()
# test_ko()
# test_try_to_bind_on_tagbase()
# test_binded_concatenate_strings()
test_auto_expose()