-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_renderer.py
764 lines (549 loc) · 20.5 KB
/
test_renderer.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/usr/bin/python3 -u
# -*- coding: utf-8 -*-
from collections import UserDict
from dataclasses import replace
import pytest
import asyncio
import re
from htag import Tag,HTagException
from htag.render import Stater,HRenderer,fmtcaller
anon=lambda t: str(t).replace( str(id(t)),"<id>" )
def test_fmtcaller():
fmtcaller("jo",(b"yo","yo",b'nom et prenom du gars', 'nom et prenom du gars',42,True,3.2, None), {'test': 3,"byt":b"jkjjkjkjkjjkjkjkjkjkj"})
fmtcaller("jo",(42,),{})
fmtcaller("jo",("court",),{})
fmtcaller("jo",("court, mais tres long",),{})
fmtcaller("jo",(b"court",),{})
fmtcaller("jo",(b"court, mais tres long",),{})
fmtcaller("jo",(),dict(a="court"))
fmtcaller("jo",(),dict(a="court, mais tres long"))
fmtcaller("jo",(),dict(a=42,b=None,c=True,d=3.14))
fmtcaller("jo",(),dict(a=[1,2,3],b=dict(a=42)))
def getGeneratorId(resp):
return int(re.findall( r"(\d+)", resp["next"])[0])
# def test_ko_try_render_a_tagbase():
# with pytest.raises(HTagException):
# HRenderer(H.div,"function interact() {}; start(); // the starter")
def test_ok_including_a_Tag_in_statics():
class Style(Tag):
tag="mystyle"
class O(Tag):
statics= [Style()] # <= avoid that (does nothing!!!)
r=HRenderer(O,"function interact() {}; start(); // the starter")
assert "<mystyle>" in str(r) # will not be included
# and a logger.warning is outputed
def test_statics_in_real_statics():
# s1=Tag.H.div( [1,"h",Tag.div("dyn"),Tag.H.div("dyn",_id=12)] )
s2=Tag.div( [1,"h",Tag.div("stat"),Tag.Section( Tag.div("yolo") )] )
# assert "id=" in str(s1)
# assert str(s1._ensureTagBase()) =='<div>1h<div>dyn</div><div id="12">dyn</div></div>'
# assert "id=" in str(s2)
assert str(s2) =="<div>1h<div>stat</div><Section><div>yolo</div></Section></div>"
def test_render_title():
class MyDiv(Tag.div):
pass
r=HRenderer(MyDiv,"function interact() {}; start(); // the starter")
assert str(r).find("<title>MyDiv</title>") > 0
class MyDiv(Tag.div):
statics = Tag.title("hello")
r=HRenderer(MyDiv,"function interact() {}; start(); // the starter")
p1= str(r).find("<title>hello</title>")
p2 = str(r).find("<title>MyDiv</title>")
assert p1 < p2
def test_render_a_tag_with_interaction():
class MyDiv(Tag.div):
js="SCRIPT1"
def __init__(self,**a):
Tag.__init__(self,**a)
self["class"]="my"
def action(self):
self["class"]="my2"
self.call("SCRIPT2")
t=MyDiv()
assert t.tag == "div" # the default one
assert t["id"] == None
assert t["class"] == "my"
assert str(t).startswith("<div class") # solo rendering as a div
js=lambda t: "\n".join(t._getAllJs())
assert "SCRIPT1" in js(t) # the script is declared in inner object
assert str(id(t)) in js(t) # and referenced to this tag
assert "SCRIPT2" not in js(t)
r=HRenderer(MyDiv,"function interact() {}; start(); // the starter")
t=r.tag
assert t.tag == "body" # tag is now a body tag (coz main tag of renderer)
# assert ">Loading...<" in str(r) # first rendering is the loader
# # START
# resp = asyncio.run( r.interact( 0, None, None, None) )
# assert 0 in resp["update"]
# assert "SCRIPT1" in resp["post"]
# assert "SCRIPT2" not in resp["post"] # action not executed ... S2 not present
# assert t["class"] == "my"
# # 1st interaction
resp = asyncio.run( r.interact( id(t), "action", [], {}) )
assert id(t) in resp["update"]
assert "SCRIPT1" in resp["post"] #\_ The 2 scripts are here
assert "SCRIPT2" in resp["post"] #/
assert t["class"] == "my2" # the class has changed
def test_render_a_tag_with_child_interactions():
class Obj(Tag):
statics=Tag.script("my")
def __init__(self,name,**a):
Tag.__init__(self,**a)
self.name = name
self.js = "INIT%s"%self.name
def action(self):
self.call("ACTION%s"%self.name)
class MyDiv(Tag):
statics=Tag.script("my")
js="SCRIPT1"
def __init__(self,**a):
Tag.__init__(self,**a)
self.a=Obj("A")
self.b=Obj("B")
self <= self.a # NEEDED
self <= self.b # NEEDED
async def action(self):
self.call("SCRIPT2")
self.a.action()
self.b.action()
r=HRenderer(MyDiv,"function interact() {}; start(); // the starter")
t=r.tag
assert str(r).count("<script>my</script>") == 1 # it's the same static, ensure just one !
# # START
# resp = asyncio.run( r.interact( 0, None, None, None) )
# assert 0 in resp["update"]
# assert "SCRIPT1" in resp["post"]
# assert "INITA" in resp["post"]
# assert "INITB" in resp["post"]
resp = asyncio.run( r.interact( id(t), "action", (), {}) )
assert "update" not in resp
print("=====",resp)
# assert "SCRIPT1" not in resp["post"] # embbed script is not served
# assert "INITA" not in resp["post"] # embbed script is not served
# assert "INITB" not in resp["post"] # embbed script is not served
# assert "ACTIONA" in resp["post"]
# assert "ACTIONB" in resp["post"]
def test_render_yield_with_scripts():
class MyDiv(Tag):
js="SCRIPT0"
def init(self):
pass
def action(self):
self.call("SCRIPT1")
yield
self.call("SCRIPT2")
yield
self.call("SCRIPT3")
async def as_action(self):
self.call("SCRIPT1")
yield
self.call("SCRIPT2")
yield
self.call("SCRIPT3")
r=HRenderer(MyDiv,"function interact() {}; start(); // the starter", lambda: "ok")
t=r.tag
assert t.exit() == "ok", "tag.exit() doesn't work !?"
async def testGenerator(method):
# resp = await r.interact( 0, None, None, None)
# assert 0 in resp["update"]
assert "SCRIPT0" in str(r)
# assert "SCRIPT0" in resp["post"]
# assert "next" not in resp
resp = await r.interact( id(t), method, [],{})
assert "update" not in resp # NO RENDERING
assert "SCRIPT0" not in resp["post"] # embbed script is not served
assert "SCRIPT1" in resp["post"]
resp = await r.interact( getGeneratorId(resp), method, None,None) # none,none important !!!
assert "update" not in resp # NO RENDERING
assert "SCRIPT0" not in resp["post"] # embbed script is not served
assert "SCRIPT1" not in resp["post"]
assert "SCRIPT2" in resp["post"]
resp = await r.interact( getGeneratorId(resp), method, None,None) # none,none important !!!
assert "next" not in resp
assert "update" not in resp # NO RENDERING
assert "SCRIPT0" not in resp["post"]
assert "SCRIPT1" not in resp["post"]
assert "SCRIPT2" not in resp["post"]
assert "SCRIPT3" in resp["post"]
asyncio.run( testGenerator("action") )
asyncio.run( testGenerator("as_action") )
def test_interact_error():
class MyDiv(Tag):
def action(self):
return 12/0
async def as_action(self):
return 12/0
r=HRenderer(MyDiv,"function interact() {}; start(); // the starter", lambda: "ok")
t=r.tag
assert t.exit() == "ok", "tag.exit() doesn't work !?"
async def testGenerator(method):
# resp = await r.interact( 0, None, None, None)
# assert 0 in resp["update"]
resp = await r.interact( id(t), method, [],{})
assert "err" in resp
asyncio.run( testGenerator("action") )
asyncio.run( testGenerator("as_action") )
def test_intelligent_rendering():
class Obj(Tag.div):
def __init__(self,name,**a):
Tag.__init__(self,**a)
self["name"]=name
self["nb"] = 0
def inc(self):
self["nb"] += 1
class MyDiv(Tag.div):
js="SCRIPT1"
def __init__(self,**a):
Tag.__init__(self,**a)
self["nb"]=0
self.a=Obj("A")
self <= self.a # NEEDED
self.b=Obj("B")
self <= self.b # NEEDED
def incMe(self):
self["nb"] += 1
def incA(self):
self.a.inc()
def incB(self):
self.b.inc()
t=MyDiv()
s=Stater(t)
assert s.guess() == []
s=Stater(t)
t.incA()
assert s.guess() == [t.a]
s=Stater(t)
t.incB()
assert s.guess() == [t.b]
s=Stater(t)
t.incMe()
assert s.guess() == [t]
s=Stater(t)
t.incA()
t.incB()
assert s.guess() == [t.a, t.b]
s=Stater(t)
t.incMe()
t.incA()
t.incB()
assert s.guess() == [t]
def test_intelligent_rendering2():
class Obj(Tag.div):
def __init__(self,**a):
super().__init__(**a)
self.nb = 0
def inc(self):
self.nb += 1
def render(self):
self.clear()
self <= self.nb
# test that the base feature works ;-)
o=Obj()
assert str(o)=='<div>0</div>'
o.inc()
assert str(o)=='<div>1</div>'
# test that the state image mechanism works as expected
s_before=o._getStateImage()
o.inc()
s_after=o._getStateImage()
######################################################
######################################################
######################################################
# assert s_after == s_before.replace(">1<",">2<") with _getStateImage() -> pure str
######################################################
######################################################
######################################################
assert s_after == s_before.replace("['1']","['2']")
# test the stater
o=Obj()
s=Stater(o)
assert s.guess() == []
s=Stater(o)
o.inc()
assert s.guess() == [o]
def test_build_immediatly_vs_lately():
""" test same behaviour """
class Obj(Tag.div):
""" build immediatly """
def __init__(self,name,**a):
Tag.div.__init__(self,**a)
self["name"]=name
self["nb"] = 0
def inc(self):
self["nb"] += 1
self.clear()
for i in range(self["nb"]):
self <= Tag.span("*")
class Obj2(Tag.div):
""" build lately """
def __init__(self,name,**a):
Tag.__init__(self,**a)
self.name=name
self.nb=0
def inc(self):
self.nb+=1
def render(self):
self.clear()
self["name"]=self.name
self["nb"]=self.nb
for i in range(self.nb):
self <= Tag.span("*")
o1=Obj("toto")
o2=Obj2("toto")
assert anon(o1) == anon(o2)
o1.inc()
o2.inc()
assert anon(o1) == anon(o2)
o1["name"]="tata"
o2.name = "tata"
assert anon(o1) == anon(o2)
o1.inc()
o2.inc()
assert anon(o1) == anon(o2)
# def test_renderer_same_str():
# class Nimp(Tag):
# def __init__(self,name,**a):
# Tag.__init__(self,**a)
# self["name"] = name
# self <= Tag.div(name)
# o1=Nimp("hello1",_class="kiki")
# o2=Nimp(name="hello2")
# h1=HRenderer(o1,"//js")
# h2=HRenderer(o2,"//js")
# assert str(h1) == str(h2)
def test_discovering_js():
class O(Tag.div):
js="/*JS1*/"
# dynamic js init is present (THE BIGGEST CHANGE since the beginning of no_0_interaction)
assert "/*JS1*/" in str(HRenderer( O, "//"))
class OOI(Tag.div): # immediate rendering
def init(self):
self.clear( O() ) # Tag directly in Tag
class OOOI(Tag.div): # immediate rendering
def init(self):
self.clear( Tag.div( O() ) ) # Tag in a TagBase
class OOL(Tag.div): # lately rendering
def render(self):
self.clear()
self.clear( O() ) # Tag directly in Tag
class OOOL(Tag.div): # lately rendering
def render(self): # BAD PRACTICE (creating a tag in render will always force rendering) !!!!!!!!!!!!!!!!!!
self.clear()
self.clear( Tag.div(O()) ) # Tag in a TagBase
async def test(r): # first call (init obj)
# resp = await r.interact( r.tag, None, None, None)
# assert 0 in resp["update"]
# assert "/*JS1*/" in resp["post"]
print("===",r)
assert "/*JS1*/" in str(r)
r=HRenderer(OOI,"//js interact")
asyncio.run(test(r))
r=HRenderer(OOOI,"//js interact")
asyncio.run(test(r))
# r=HRenderer(OOL,"//js interact")
# asyncio.run(test(r))
# r=HRenderer(OOOL,"//js interact")
# asyncio.run(test(r))
# this test is NON SENSE, til statics are imported in static (not dynamic anymore)
# this test is NON SENSE, til statics are imported in static (not dynamic anymore)
# this test is NON SENSE, til statics are imported in static (not dynamic anymore)
def test_discovering_css():
class O(Tag):
statics=[Tag.style("/*CSS1*/")]
class OOI(Tag.div): # immediate rendering
def init(self):
self.clear( O() ) # Tag directly in Tag
class OOOI(Tag.div): # immediate rendering
def init(self):
self.clear( Tag.div( O() ) ) # Tag in a TagBase
class OOL(Tag.div): # lately rendering
def render(self):
self.clear( O() ) # Tag directly in Tag
class OOOL(Tag.div): # lately rendering
def render(self):
self.clear( Tag.div(O()) ) # Tag in a TagBase
def test(r): # first call (init obj)
assert "/*CSS1*/" in str(r)
r=HRenderer(OOI,"//js interact")
test(r)
r=HRenderer(OOOI,"//js interact")
test(r)
r=HRenderer(OOL,"//js interact")
test(r)
r=HRenderer(OOOL,"//js interact")
test(r)
def test_imports():
class Ya(Tag.div):
statics = Tag.style("""body {font-size:100px}""", _id="Ya")
def init(self):
self <= "Ya"
class Yo(Tag.div):
statics = Tag.style("""body {background:#CFC;}""", _id="Yo")
def init(self):
self <= "Yo"
class AppWithImport(Tag.body):
statics = Tag.style("""body {color: #080}""", _id="main")
imports = Yo
def init(self):
self <= Yo()
class AppWithoutImport(Tag.body):
statics = Tag.style("""body {color: #080}""", _id="main")
def init(self):
self <= Yo()
html=str(HRenderer( AppWithImport, ""))
styles = re.findall("(<style[^<]+</style>)",html)
assert len(styles)==2
html=str(HRenderer( AppWithoutImport, ""))
styles = re.findall("(<style[^<]+</style>)",html)
assert len(styles)>2
class AppWithBrokenImport(Tag.body):
imports = "nimpnawak" # not a Tag class !? -> error
def init(self):
self <= Yo()
# this works ...
str(AppWithBrokenImport())
# this not !
with pytest.raises(HTagException):
HRenderer(AppWithBrokenImport,"//js")
def test_new():
class APP(Tag.div):
def init(self,val=42):
self <= f"=={val}=="
# syntax of versions < 0.4 is not supported anymore
with pytest.raises(TypeError):
HRenderer( APP(),"//" )
# new syntax
hr=HRenderer( APP,"//" )
assert str(hr).startswith("<!DOCTYPE html>")
async def assertion(txt):
# actions = await hr.interact( 0, None, None, None)
assert txt in str(hr)
# ensure first interaction produce the right thing
asyncio.run( assertion(">==42==</body>") )
#-----------------
# new syntax (with init first arg) : OK
hr=HRenderer( APP,"//", init=((43,),{}) )
assert str(hr).startswith("<!DOCTYPE html>")
# async def assertion(txt):
# # actions = await hr.interact( 0, None, None, None)
# # assert txt in actions["update"][0]
# assert txt in str(hr)
# ensure first interaction produce the right thing
asyncio.run( assertion(">==43==</body>") )
#-----------------
# new syntax (with init as keyword arg) : ok
hr=HRenderer( APP,"//", init=((),dict(val=44)) )
assert str(hr).startswith("<!DOCTYPE html>")
# async def assertion(txt):
# # actions = await hr.interact( 0, None, None, None)
# # assert txt in actions["update"][0]
# assert txt in str(hr)
# ensure first interaction produce the right thing
asyncio.run( assertion(">==44==</body>") )
#-----------------
# new syntax (with init first arg) : KO (coz too many args)
hr=HRenderer( APP,"//", init=((45,49),{}) )
assert str(hr).startswith("<!DOCTYPE html>")
# async def assertion(txt):
# # actions = await hr.interact( 0, None, None, None)
# # assert txt in actions["update"][0]
# assert txt in str(hr)
# ensure first interaction produce the right thing
asyncio.run( assertion(">==42==</body>") )
#-----------------
# new syntax (with init as bad keyword arg) : KO (bad args)
hr=HRenderer( APP,"//", init=((),dict(myval=50)) )
assert str(hr).startswith("<!DOCTYPE html>")
# async def assertion(txt):
# # actions = await hr.interact( 0, None, None, None)
# # assert txt in actions["update"][0]
# assert txt in str(hr)
# ensure first interaction produce the right thing
asyncio.run( assertion(">==42==</body>") )
def test_new_base():
class App(Tag.div):
def init(self,m="default",**a):
self <= m
self <= Tag.span("world")
#til it's not in a hrenderer, there is no @id
assert str( App() ) == "<div>default<span>world</span></div>"
# and you can add yourself, if needed
assert str( App(_id="myid") ) == '<div id="myid">default<span>world</span></div>'
hr=HRenderer(App,"")
assert str(hr.tag).count("id=") == 2 # body & span
def test_just_4_coverage():
""" will enter in the exception trapped, but will produce a body tag"""
hr=HRenderer(Tag.div,"",init=("kk",{None:None},42))
assert hr.tag.tag=="body"
def test_STRICT_MODE_in_render():
try:
Tag.STRICT_MODE = True
#--------------------------------- a good case
class Good(Tag.div): # good because render() doesn't modify its direct childs
def render(self):
a=42
print(Good()) # force rendering
#--------------------------------- a bad case
class Bad(Tag.div):
def render(self):
self += Tag.div("yo")
with pytest.raises(HTagException):
print( Bad() )
#--------------------------------- a good case
class Good2(Tag.div):
def render(self):
self += "simple text is not enough !!!"
print( Good2() )
finally:
Tag.STRICT_MODE = False
def test_state_and_session():
class LocalApp(Tag.div):
imports=[]
def init(self):
self.state["hello"]=42
self <= Tag.span("hello")
# assert that the state works in all cases
str( LocalApp() ) # TypeError: 'NoneType' object does not support item assignment
ses=dict(user="moi")
hr=HRenderer(LocalApp,"",session=ses,fullerror=True)
assert str(hr).count("id=") == 2 # body & span
assert hr.tag.state
assert "hello" in hr.tag.state
assert hr.tag.state["hello"]==42
assert hr.tag.state.export() == dict(hello=42)
assert len(ses)==2
hr.tag.state.clear()
assert not hr.tag.state
assert len(ses)==1
hr.tag.state.load( dict(world=43) )
assert len(ses)==2
assert hr.tag.state["world"]==43
del hr.tag.state["world"]
assert not hr.tag.state
assert len(ses)==1
if __name__=="__main__":
# test_ko_try_render_a_tagbase()
# test_render_a_tag_with_interaction()
# test_render_a_tag_with_child_interactions()
# test_render_yield_with_scripts()
# test_intelligent_rendering()
# test_build_immediatly_vs_lately()
# test_renderer_same_str()
# test_intelligent_rendering2()
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)
# test_intelligent_rendering()
# test_statics_in_real_statics()
# test_render_yield_with_scripts()
# test_statics_in_real_statics()
# test_render_title()
# test_new()
# test_render_a_tag_with_child_interactions()
# test_render_a_tag_with_interaction()
# test_new_base()
# test_intelligent_rendering2()
# test_just_4_coverage()
# test_avoid_tagcreation_in_render()
# test_discovering_js()
test_state_and_session()