@@ -133,53 +133,83 @@ def testSetter_Raises_Exception(self):
133
133
134
134
class Test_InsertBefore (unittest .TestCase ):
135
135
def test_WithoutRefNode (self ):
136
+ # ======================================
137
+ # <document>
138
+ # <parent_node/>
139
+ # <new_child_node/>
140
+ # <document>
141
+ # ======================================
136
142
document = _create_document_node ()
137
143
parent_node = _create_element_node (document )
138
- child_node = _create_element_node (document )
144
+ new_child_node = _create_element_node (document )
139
145
# Testing
140
- parent_node .insert_before (child_node )
141
- self .assertEqual (child_node .parent_node , parent_node )
142
- self .assertEqual (parent_node .child_nodes .item (0 ), child_node )
146
+ parent_node .insert_before (new_child_node )
147
+ self .assertEqual (new_child_node .parent_node , parent_node )
148
+ self .assertEqual (parent_node .child_nodes .item (0 ), new_child_node )
143
149
self .assertEqual (parent_node .child_nodes .length , 1 )
144
150
145
151
def test_WithRefNode (self ):
152
+ # ======================================
153
+ # <document>
154
+ # <parent_node>
155
+ # <ref_child_node/>
156
+ # </parent_node>
157
+ #
158
+ # <new_child_node/>
159
+ # <document>
160
+ # ======================================
146
161
document = _create_document_node ()
147
162
parent_node = _create_element_node (document )
148
- first_child_node = _create_element_node (document )
149
- second_child_node = _create_element_node (document )
150
- # Warning: this is assuming that `append_child()` method works properly.
151
- parent_node .append_child (second_child_node )
163
+ new_child_node = _create_element_node (document )
164
+ ref_child_node = _create_element_node (document )
165
+ parent_node .append_child (ref_child_node )
152
166
# Testing
153
- parent_node .insert_before (first_child_node , second_child_node )
154
- self .assertEqual (first_child_node .parent_node , parent_node )
155
- self .assertEqual (parent_node .child_nodes .item (0 ), first_child_node )
156
- self .assertEqual (parent_node .child_nodes .item (1 ), second_child_node )
167
+ parent_node .insert_before (new_child_node , ref_child_node )
168
+ self .assertEqual (new_child_node .parent_node , parent_node )
169
+ self .assertEqual (parent_node .child_nodes .item (0 ), new_child_node )
170
+ self .assertEqual (parent_node .child_nodes .item (1 ), ref_child_node )
157
171
self .assertEqual (parent_node .child_nodes .length , 2 )
158
172
159
173
def test_InsertExistingChild (self ):
174
+ # ======================================
175
+ # <document>
176
+ # <parent_node>
177
+ # <ref_child_node/>
178
+ # <new_child_node/>
179
+ # </parent_node>
180
+ # <document>
181
+ # ======================================
160
182
document = _create_document_node ()
161
183
parent_node = _create_element_node (document )
162
- first_child_node = _create_element_node (document )
163
- second_child_node = _create_element_node (document )
164
- # Warning: this is assuming that `append_child()` method works properly.
165
- parent_node .append_child (first_child_node )
166
- parent_node .append_child (second_child_node )
184
+ ref_child_node = _create_element_node (document )
185
+ new_child_node = _create_element_node (document )
186
+ parent_node .append_child (ref_child_node )
187
+ parent_node .append_child (new_child_node )
167
188
# Testing
168
- parent_node .insert_before (second_child_node , first_child_node )
169
- self .assertEqual (second_child_node .parent_node , parent_node )
170
- self .assertEqual (parent_node .child_nodes .item (0 ), second_child_node )
171
- self .assertEqual (parent_node .child_nodes .item (1 ), first_child_node )
189
+ parent_node .insert_before (new_child_node , ref_child_node )
190
+ self .assertEqual (new_child_node .parent_node , parent_node )
191
+ self .assertEqual (parent_node .child_nodes .item (0 ), new_child_node )
192
+ self .assertEqual (parent_node .child_nodes .item (1 ), ref_child_node )
172
193
self .assertEqual (parent_node .child_nodes .length , 2 )
173
194
174
195
def test_InsertDocumentFragmentNode (self ):
196
+ # ======================================
197
+ # <document>
198
+ # <parent_node/>
199
+ #
200
+ # <docfrag_node>
201
+ # <first_child_node/>
202
+ # <second_child_node/>
203
+ # </docfrag_node>
204
+ # <document>
205
+ # ======================================
175
206
document = _create_document_node ()
176
207
parent_node = _create_element_node (document )
177
208
docfrag_node = Node (node_type = NodeType .DOCUMENT_FRAGMENT_NODE ,
178
209
node_name = '#document-fragment' ,
179
210
owner_document = document )
180
211
first_child_node = _create_element_node (document )
181
212
second_child_node = _create_element_node (document )
182
- # Warning: this is assuming that `append_child()` method works properly.
183
213
docfrag_node .append_child (first_child_node )
184
214
docfrag_node .append_child (second_child_node )
185
215
# Testing
@@ -192,6 +222,15 @@ def test_Raises_HIERARCHY_REQUEST_ERR(self):
192
222
self .skipTest (NotImplemented ) # TODO
193
223
194
224
def test_Raises_WRONG_DOCUMENT_ERR (self ):
225
+ # ======================================
226
+ # <document1>
227
+ # <elem_node_1/>
228
+ # <document1>
229
+ #
230
+ # <document2>
231
+ # <elem_node_2/>
232
+ # <document2>
233
+ # ======================================
195
234
document_1 = _create_document_node ()
196
235
document_2 = _create_document_node ()
197
236
elem_node_1 = _create_element_node (document_1 )
@@ -205,28 +244,41 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
205
244
self .fail ()
206
245
207
246
def test_Raises_NO_MODIFICATION_ALLOWED_ERR (self ):
247
+ # ======================================
248
+ # <document>
249
+ # <parent_node readonly="true"/>
250
+ # <new_child_node/>
251
+ # <document>
252
+ # ======================================
208
253
document = _create_document_node ()
209
- elem_node_readonly = Node (node_type = NodeType .ELEMENT_NODE ,
210
- node_name = 'tagName' ,
211
- owner_document = document ,
212
- read_only = True )
213
- elem_node_child = _create_element_node (document )
254
+ parent_node_readonly = Node (node_type = NodeType .ELEMENT_NODE ,
255
+ node_name = 'tagName' ,
256
+ owner_document = document ,
257
+ read_only = True )
258
+ new_child_node = _create_element_node (document )
214
259
try :
215
- elem_node_readonly .insert_before (elem_node_child )
260
+ parent_node_readonly .insert_before (new_child_node )
216
261
except DOMException as e :
217
262
code = e .args [0 ]
218
263
self .assertEqual (code , DOMException .NO_MODIFICATION_ALLOWED_ERR )
219
264
else :
220
265
self .fail ()
221
266
222
267
def test_Raises_NOT_FOUND_ERR (self ):
268
+ # ======================================
269
+ # <document>
270
+ # <parent_node/>
271
+ # <new_child_node/>
272
+ # <ref_child_node/>
273
+ # <document>
274
+ # ======================================
223
275
document = _create_document_node ()
224
- elem_node_parent = _create_element_node (document )
225
- elem_node_child = _create_element_node (document )
226
- elem_node_error = _create_element_node (document )
276
+ parent_node = _create_element_node (document )
277
+ new_child_node = _create_element_node (document )
278
+ ref_child_node = _create_element_node (document )
227
279
# Testing
228
280
try :
229
- elem_node_parent .insert_before (elem_node_child , elem_node_error )
281
+ parent_node .insert_before (new_child_node , ref_child_node )
230
282
except DOMException as e :
231
283
code = e .args [0 ]
232
284
self .assertEqual (code , DOMException .NOT_FOUND_ERR )
@@ -236,39 +288,61 @@ def test_Raises_NOT_FOUND_ERR(self):
236
288
237
289
class Test_AppendChild (unittest .TestCase ):
238
290
def test_AppendNewChild (self ):
291
+ # ======================================
292
+ # <document>
293
+ # <parent_node/>
294
+ # <new_child_node/>
295
+ # <document>
296
+ # ======================================
239
297
document = _create_document_node ()
240
298
parent_node = _create_element_node (document )
241
- child_node = _create_element_node (document )
299
+ new_child_node = _create_element_node (document )
242
300
# Testing
243
- parent_node .append_child (child_node )
244
- self .assertEqual (child_node .parent_node , parent_node )
245
- self .assertEqual (parent_node .child_nodes .item (0 ), child_node )
301
+ parent_node .append_child (new_child_node )
302
+ self .assertEqual (new_child_node .parent_node , parent_node )
303
+ self .assertEqual (parent_node .child_nodes .item (0 ), new_child_node )
246
304
self .assertEqual (parent_node .child_nodes .length , 1 )
247
305
248
306
def test_AppendExistingChild (self ):
307
+ # ======================================
308
+ # <document>
309
+ # <parent_node>
310
+ # <new_child_node/>
311
+ # <old_child_node/>
312
+ # </parent_node>
313
+ # <document>
314
+ # ======================================
249
315
document = _create_document_node ()
250
316
parent_node = _create_element_node (document )
251
- first_child_node = _create_element_node (document )
252
- second_child_node = _create_element_node (document )
253
- # Warning: this is assuming that `append_child()` has passed `test_AppendNewChild()` test.
254
- parent_node .append_child (first_child_node )
255
- parent_node .append_child (second_child_node )
317
+ new_child_node = _create_element_node (document )
318
+ old_child_node = _create_element_node (document )
319
+ parent_node .append_child (new_child_node )
320
+ parent_node .append_child (old_child_node )
256
321
# Testing
257
- parent_node .append_child (first_child_node )
258
- self .assertEqual (second_child_node .parent_node , parent_node )
259
- self .assertEqual (parent_node .child_nodes .item (0 ), second_child_node )
260
- self .assertEqual (parent_node .child_nodes .item (1 ), first_child_node )
322
+ parent_node .append_child (new_child_node )
323
+ self .assertEqual (old_child_node .parent_node , parent_node )
324
+ self .assertEqual (parent_node .child_nodes .item (0 ), old_child_node )
325
+ self .assertEqual (parent_node .child_nodes .item (1 ), new_child_node )
261
326
self .assertEqual (parent_node .child_nodes .length , 2 )
262
327
263
328
def test_AppendDocumentFragmentNode (self ):
329
+ # ======================================
330
+ # <document>
331
+ # <parent_node/>
332
+ #
333
+ # <docfrag_node>
334
+ # <first_child_node/>
335
+ # <second_child_node/>
336
+ # </docfrag_node>
337
+ # <document>
338
+ # ======================================
264
339
document = _create_document_node ()
265
340
parent_node = _create_element_node (document )
266
341
docfrag_node = Node (node_type = NodeType .DOCUMENT_FRAGMENT_NODE ,
267
342
node_name = '#document-fragment' ,
268
343
owner_document = document )
269
344
first_child_node = _create_element_node (document )
270
345
second_child_node = _create_element_node (document )
271
- # Warning: this is assuming that `append_child()` has passed `test_AppendNewChild()` test.
272
346
docfrag_node .append_child (first_child_node )
273
347
docfrag_node .append_child (second_child_node )
274
348
# Testing
@@ -281,6 +355,15 @@ def test_Raises_HIERARCHY_REQUEST_ERR(self):
281
355
self .skipTest (NotImplemented ) # TODO
282
356
283
357
def test_Raises_WRONG_DOCUMENT_ERR (self ):
358
+ # ======================================
359
+ # <document1>
360
+ # <elem_node_1/>
361
+ # <document1>
362
+ #
363
+ # <document2>
364
+ # <elem_node_2/>
365
+ # <document2>
366
+ # ======================================
284
367
document_1 = _create_document_node ()
285
368
document_2 = _create_document_node ()
286
369
elem_node_1 = _create_element_node (document_1 )
@@ -294,14 +377,95 @@ def test_Raises_WRONG_DOCUMENT_ERR(self):
294
377
self .fail ()
295
378
296
379
def test_Raises_NO_MODIFICATION_ALLOWED_ERR (self ):
380
+ # ======================================
381
+ # <document>
382
+ # <parent_node readonly="true"/>
383
+ # <new_child_node/>
384
+ # <document>
385
+ # ======================================
386
+ document = _create_document_node ()
387
+ parent_node_readonly = Node (node_type = NodeType .ELEMENT_NODE ,
388
+ node_name = 'tagName' ,
389
+ owner_document = document ,
390
+ read_only = True )
391
+ new_child_node = _create_element_node (document )
392
+ try :
393
+ parent_node_readonly .append_child (new_child_node )
394
+ except DOMException as e :
395
+ code = e .args [0 ]
396
+ self .assertEqual (code , DOMException .NO_MODIFICATION_ALLOWED_ERR )
397
+ else :
398
+ self .fail ()
399
+
400
+
401
+ class Test_ReplaceChild (unittest .TestCase ):
402
+ def test_ReplaceExistingChild (self ):
403
+ # ======================================
404
+ # <document>
405
+ # <parent_node>
406
+ # <old_child_node/>
407
+ # </parent_node>
408
+ #
409
+ # <new_child_node/>
410
+ # <document>
411
+ # ======================================
412
+ document = _create_document_node ()
413
+ parent_node = _create_element_node (document )
414
+ new_child_node = _create_element_node (document )
415
+ old_child_node = _create_element_node (document )
416
+ parent_node .append_child (old_child_node )
417
+ # Testing
418
+ parent_node .replace_child (new_child_node , old_child_node )
419
+ self .assertIn (new_child_node , parent_node .child_nodes )
420
+ self .assertNotIn (old_child_node , parent_node .child_nodes )
421
+ self .assertEqual (new_child_node .parent_node , parent_node )
422
+ self .assertEqual (parent_node .child_nodes .item (0 ), new_child_node )
423
+ self .assertEqual (parent_node .child_nodes .length , 1 )
424
+
425
+ def test_Raises_HIERARCHY_REQUEST_ERR (self ):
426
+ self .skipTest (NotImplemented ) # TODO
427
+
428
+ def test_Raises_WRONG_DOCUMENT_ERR (self ):
429
+ # ======================================
430
+ # <document1>
431
+ # <parent_node>
432
+ # <old_child_node/>
433
+ # </parent_node>
434
+ # <document1>
435
+ #
436
+ # <document2>
437
+ # <new_child_node/>
438
+ # <document2>
439
+ # ======================================
440
+ document_1 = _create_document_node ()
441
+ document_2 = _create_document_node ()
442
+ parent_node = _create_element_node (document_1 )
443
+ old_child_node = _create_element_node (document_1 )
444
+ new_child_node = _create_element_node (document_2 )
445
+ parent_node .append_child (old_child_node )
446
+ try :
447
+ parent_node .replace_child (new_child_node , old_child_node )
448
+ except DOMException as e :
449
+ code = e .args [0 ]
450
+ self .assertEqual (code , DOMException .WRONG_DOCUMENT_ERR )
451
+ else :
452
+ self .fail ()
453
+
454
+ def test_Raises_NO_MODIFICATION_ALLOWED_ERR (self ):
455
+ # ======================================
456
+ # <document>
457
+ # <parent_node readonly="true"/>
458
+ # <new_child_node/>
459
+ # <document>
460
+ # ======================================
297
461
document = _create_document_node ()
298
- elem_node_readonly = Node (node_type = NodeType .ELEMENT_NODE ,
299
- node_name = 'tagName' ,
300
- owner_document = document ,
301
- read_only = True )
302
- elem_node_child = _create_element_node (document )
462
+ parent_node_readonly = Node (node_type = NodeType .ELEMENT_NODE ,
463
+ node_name = 'tagName' ,
464
+ owner_document = document ,
465
+ read_only = True )
466
+ new_child_node = _create_element_node (document )
303
467
try :
304
- elem_node_readonly .append_child (elem_node_child )
468
+ parent_node_readonly .append_child (new_child_node )
305
469
except DOMException as e :
306
470
code = e .args [0 ]
307
471
self .assertEqual (code , DOMException .NO_MODIFICATION_ALLOWED_ERR )
0 commit comments