-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_dom.py
202 lines (144 loc) · 4.41 KB
/
test_dom.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
from htag import Tag,HTagException
import pytest
def test_base():
t=Tag.div()
assert t.parent == None
assert t.root == t
def test_adding_None():
parent = Tag.div()
parent.add(None) # does nothing !
parent <= None # does nothing !
parent += None # does nothing !
parent += [None,None] # does nothing !
assert len(parent.childs)==0
def test_try_to_override_important_props():
t = Tag.div()
with pytest.raises( AttributeError ):
t.parent=None
with pytest.raises( AttributeError ):
t.root=None
with pytest.raises( AttributeError ):
t.event=None
def test_unparenting_remove():
parent = Tag.div()
child = Tag.span()
childchild = Tag.b()
parent <= child <= childchild
assert str(parent)=="<div><span><b></b></span></div>"
assert child.root == childchild.root == parent.root == parent
assert child.parent == parent
assert childchild.parent == child
assert parent.childs[0] == child
child.remove()
assert str(parent)=="<div></div>"
assert len(parent.childs) == 0
assert child.parent == None
assert childchild.parent == child
assert parent.root == parent
assert child.root == child
assert childchild.root == child
def test_unparenting_clear():
parent = Tag.div()
child = Tag.span()
childchild = Tag.b()
parent <= child <= childchild
assert str(parent)=="<div><span><b></b></span></div>"
assert child.root == childchild.root == parent.root == parent
parent.clear()
assert str(parent)=="<div></div>"
assert len(parent.childs) == 0
assert child.parent == None
assert childchild.parent == child
assert parent.root == parent
assert child.root == child
assert childchild.root == child
def test_cant_add_many_times():
parent1 = Tag.div()
parent2 = Tag.div()
parent1.STRICT_MODE=True
parent2.STRICT_MODE=True
a_child = Tag.span()
parent1 += a_child
# can't be added to another one
with pytest.raises(HTagException):
parent2 += a_child
assert a_child.parent == parent1
# clear parent1
parent1.clear()
# so the child is no more in parent1
# we can add it to parent2
parent2 += a_child
assert a_child.parent == parent2
#####################################################################
#####################################################################
#####################################################################
def t0():
parent = Tag.div()
a_child = Tag.span()
parent.add( a_child, True) # force reparent
parent.add( a_child, True) # force reparent
def t00():
parent = Tag.div()
a_child = Tag.span()
parent.add( a_child, False) # don't force reparent (default)
parent.add( a_child, False) # don't force reparent (default)
def t1():
parent = Tag.div()
a_child = Tag.span()
parent += a_child
parent += a_child # raise
def t2():
parent = Tag.div()
a_child = Tag.span()
parent += [a_child,a_child] # raise
def t3():
parent = Tag.div()
a_child = Tag.span()
parent += Tag.div(a_child)
parent += Tag.div(a_child) # raise
def t4():
parent = Tag.div()
a_child = Tag.span()
parent <= Tag.div() <= a_child
parent <= Tag.div() <= a_child # raise
def t5():
parent = Tag.div()
a_child = Tag.span()
parent.childs.append( a_child ) # since 'childs' is a tuple -> AttributeError
parent.childs.append( a_child )
def test_strictmode_off():
old=Tag.STRICT_MODE
try:
Tag.STRICT_MODE=False
t0()
t00()
t1()
t2()
t3()
t4()
with pytest.raises(AttributeError): # AttributeError: 'tuple' object has no attribute 'append'
t5()
finally:
Tag.STRICT_MODE=old
def test_strictmode_on():
old=Tag.STRICT_MODE
try:
Tag.STRICT_MODE=True
t0()
with pytest.raises(HTagException):
t00()
with pytest.raises(HTagException):
t1()
with pytest.raises(HTagException):
t2()
with pytest.raises(HTagException):
t3()
with pytest.raises(HTagException):
t4()
with pytest.raises(AttributeError): # AttributeError: 'tuple' object has no attribute 'append'
t5()
finally:
Tag.STRICT_MODE=old
if __name__=="__main__":
# test_unparenting_clear()
t0()