-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_interactions.py
397 lines (282 loc) · 10.2 KB
/
test_interactions.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import pytest
import re
"""
Theses are End-2-End tests
"""
from htag import Tag,HTagException
from htag.render import HRenderer
import asyncio
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
import logging
logger = logging.getLogger(__name__)
class Simu:
""" just a class helper for simplify interaction / unittest
* await init() -> dict
for first interaction
* await interact( Tag|None ).method(*args,**kargs) -> dict
for interact on object Tag by calling method
* await doNext -> dict
to continue generator interactions
"""
def __init__(self,tagClass:type):
self.hr = HRenderer(tagClass,"//")
self.tag = self.hr.tag
assert self.tag.parent is None # ensure no parent !
assert self.tag._hr == self.hr # the tag has received its "_hr" !
assert self.tag.tag == "body" # tag converted to body
# self._1stInteraction = False
# async def init(self):
# """ do the 1st interaction, and control that it update '0' only ! """
# logger.debug("first interaction/init phase")
# r = await self.hr.interact( 0,None,None,None )
# assert len(r["update"])==1 # only one render (the main)
# assert r["update"][0] # first is 0 ;-)
# assert r["update"][0]==str(self.tag) # it's the str(hr.tag) !
# self._1stInteraction = True
# return r
def interact(self,o: Tag = None):
# assert self._1stInteraction, "First interaction not done !"
if o is None: o = self.tag
assert isinstance(o,Tag)
class Binder:
def __getattr__(this,name:str):
async def _(*a,**k):
logger.debug("interaction with %s, call %s(*%s,**%s)",repr(o),name,a,k)
return await self.hr.interact( id(o),name,a,k)
return _
return Binder()
async def doNext(self,r):
# ensure next statement is here
assert "next" in r
# ensure all params are null
assert r["next"].count(": null") == 3
# except the first (id)
id=int(re.findall( r"(\d+)", r["next"])[0])
return await self.hr.interact( id,None,None,None)
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
@pytest.mark.asyncio
async def test_empty():
class Object(Tag.div):
pass
########################################################
s=Simu( Object )
# await s.init() # it controls the basics
@pytest.mark.asyncio
async def test_js_at_init1():
class Object(Tag.div):
def init(self):
self.call("interaction_js();")
########################################################
s=Simu( Object )
# r=await s.init() # it controls the basics
assert "interaction_js();" in str(s.hr)
# assert r["post"].count("function(tag)")==1
@pytest.mark.asyncio
async def test_js_at_init_new_InternalCall():
class Object(Tag.div):
def init(self):
self.call.mymethod("hello") # same as self.call( self.bind.mymethod('hello') )
def mymethod(self,msg):
assert msg=="hello"
########################################################
s=Simu( Object )
# r=await s.init() # it controls the basics
assert "function(self){ try{interact(" in str(s.hr)
# assert r["post"].count("function(tag)")==1
@pytest.mark.asyncio
async def test_js_at_init2():
class Object(Tag.div):
js = "static_js();"
########################################################
s=Simu( Object )
# r=await s.init() # it controls the basics
assert "static_js();" in str(s.hr)
# assert r["post"].count("function(tag)")==1
@pytest.mark.asyncio
async def test_js_at_init3():
class Object(Tag.div):
js = "static_js();"
def init(self):
self.call("interaction_js();")
########################################################
s=Simu( Object )
# r=await s.init() # it controls the basics
assert "interaction_js();" in str(s.hr)
assert "static_js();" in str(s.hr)
# assert r["post"].count("function(tag)")==2
@pytest.mark.asyncio
async def test_simplest():
class Object(Tag.div):
def init(self):
self["nb"]=0
def changeContent(self):
self["nb"]+=1
def doNothing(self):
self.done = "nothing"
def addContent(self):
self <= "content"
def all(self):
self.changeContent()
self.addContent()
########################################################
s=Simu( Object )
# await s.init() # it controls the basics
assert s.tag["nb"]==0
r=await s.interact().changeContent()
assert s.tag["nb"]==1
assert list(r["update"].keys()) == [ id(s.tag) ]
assert r["update"][id(s.tag)]==str(s.tag)
assert ' nb="1" ' in r["update"][id(s.tag)]
r=await s.interact().doNothing()
assert "update" not in r
r=await s.interact().addContent()
assert len(s.tag.childs)==1
assert list(r["update"].keys()) == [ id(s.tag) ]
assert r["update"][id(s.tag)]==str(s.tag)
assert ' nb="1" ' in r["update"][id(s.tag)]
r=await s.interact().all()
assert s.tag["nb"]==2
assert len(s.tag.childs)==2
assert list(r["update"].keys()) == [ id(s.tag) ]
assert r["update"][id(s.tag)]==str(s.tag)
assert ' nb="2" ' in r["update"][id(s.tag)]
assert 'contentcontent' in r["update"][id(s.tag)]
@pytest.mark.asyncio
async def test_rendering(): #TODO: redefine coz norender is gone, so this test is perhaps non sense
class Object(Tag.div):
def init(self):
self["nb"]=0
def incNormal(self):
self["nb"]+=1
def incNoRedraw(self):
self["nb"]+=1
class P(Tag.div):
def init(self):
self.o1 = Object()
self <= self.o1
self["my"]=0
def testModO1_1(self):
self.o1.incNormal()
def testModO1_2(self):
self.o1.incNoRedraw()
# use incNormal from o1
def testModMe_0(self):
self["my"]+=1
def testModMe_1(self):
self["my"]+=1
self.o1.incNormal() # has no efect
# @Tag.NoRender
# def testModMe_2(self):
# self["my"]+=1
# self.o1.incNormal() # has no efect
# use incNoRedraw from o1
def testModMe_10(self):
self["my"]+=1
def testModMe_11(self):
self["my"]+=1
self.o1.incNoRedraw() # has no efect
# @Tag.NoRender
# def testModMe_12(self):
# self["my"]+=1
# self.o1.incNoRedraw() # has no efect
#====================================================================
hr=Simu( P )
o=hr.tag
# r=await hr.init()
# will interact on "o1", by calling main (o's) methods
r=await hr.interact( o ).testModO1_1()
assert id(o.o1) in r["update"] # just o1 has changed
# r=await hr.interact( o ).testModO1_2()
# assert id(o.o1) in r["update"] # just o1 has changed
#========================
r=await hr.interact( o ).testModMe_0()
assert id(o) in r["update"]
r=await hr.interact( o ).testModMe_1()
assert id(o) in r["update"] # in fact, redraw main
# r=await hr.interact( o ).testModMe_2()
# assert id(o.o1) in r["update"] # redraw just the child # not the main, coz main return 0
# #========================
r=await hr.interact( o ).testModMe_10()
assert id(o) in r["update"]
r=await hr.interact( o ).testModMe_11()
assert id(o) in r["update"] # in fact, redraw main
# r=await hr.interact( o ).testModMe_12()
# assert id(o.o1) in r["update"] # redraw just the child # not the main, coz main return 0
@pytest.mark.asyncio
async def test_simplest_async(): #TODO: redefine coz norender is gone, so this test is perhaps non sense
class Object(Tag.div):
def init(self):
self["nb"]=0
def inc(self):
self["nb"]+=1
yield
hr=Simu(Object)
o=hr.tag
# r=await hr.init()
r=await hr.interact(o).inc()
assert "update" in r
await hr.doNext( r)
assert "update" in r
@pytest.mark.asyncio
async def test_yield():
class Object(Tag.div):
def inc(self):
yield
yield list("abc")
yield "d"
hr=Simu(Object)
o=hr.tag
# r=await hr.init()
assert o.childs==()
r=await hr.interact(o).inc()
# first yield, yield nothing
assert o.childs==()
assert r["next"]
assert "stream" not in r
r=await hr.doNext( r)
# seconf yield, yield list("abc")
assert o.childs==('a', 'b', 'c')
assert "stream" in r
assert r["stream"][id(o)]=="abc"
assert r["next"]
assert ">abc<" in str(o)
r=await hr.doNext( r)
assert o.childs==('a', 'b', 'c', 'd')
assert "stream" in r
assert r["stream"][id(o)]=="d"
assert r["next"]
assert ">abcd<" in str(o)
r=await hr.doNext( r)
assert r=={}
@pytest.mark.asyncio
async def test_bug():
S=Simu( Tag.div )
r=await S.hr.interact(15654654654546,"m",(),{})
assert r["err"] # 15654654654546 is not an existing Tag or generator (dead objects ?)?!"
@pytest.mark.asyncio
async def test_bug_0_8_5():
""" many js statements rendered """
class Comp(Tag.div):
def init(self):
self.js = "console.log('YOLO');"
class App(Tag.body):
def init(self):
self += Tag.div( Tag.div( Comp() ) )
#TODO: use Simu ;-)
hr=HRenderer( App, "// my starter")
assert len(App()._getAllJs()) == 1
# # first interaction
# r=await hr.interact(hr.tag,None,None,None,None)
# # I should just find 1 YOLO (1 js) ... (but in 0.8.5 -> 4 ;-( )
assert str(hr).count("YOLO") == 1
# print(r)
if __name__=="__main__":
import logging
logging.basicConfig(format='[%(levelname)-5s] %(name)s: %(message)s',level=logging.DEBUG)
# asyncio.run( test_empty() )
# asyncio.run( test_js_at_init1() )
# asyncio.run( test_js_at_init2() )
# asyncio.run( test_js_at_init3() )
# asyncio.run( test_simplest() )
# asyncio.run( test_bug_0_8_5() )
asyncio.run( test_js_at_init_new_InternalCall() )