Skip to content

Commit e7033b6

Browse files
committed
fix tests and improve coverage
1 parent fd29203 commit e7033b6

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

Lib/test/test_xml_etree.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,10 +2643,6 @@ def test_pickle_issue18997(self):
26432643

26442644
class BadElementTest(ElementTestCase, unittest.TestCase):
26452645

2646-
@classmethod
2647-
def setUpClass(cls):
2648-
cls.is_c = ET is not pyET
2649-
26502646
def test_extend_mutable_list(self):
26512647
class X:
26522648
@property
@@ -2695,80 +2691,84 @@ def test_remove_with_clear(self):
26952691

26962692
E = ET.Element
26972693

2698-
class X1(E):
2699-
def __eq__(self, o):
2700-
del root[:]
2701-
return False
2702-
2703-
class X2(E):
2704-
def __eq__(self, o):
2705-
root.clear()
2706-
return False
2707-
2708-
class Y1(E):
2709-
def __eq__(self, o):
2710-
del root[:]
2711-
return True
2712-
2713-
class Y2(E):
2714-
def __eq__(self, o):
2715-
root.clear()
2716-
return True
2717-
27182694
def test_remove(root, target, raises):
27192695
if raises:
27202696
self.assertRaises(ValueError, root.remove, target)
27212697
else:
27222698
root.remove(target)
27232699
self.assertNotIn(target, root)
27242700

2725-
for etype, rem_type, raises in [
2726-
(E, X1, True), (E, X2, True),
2727-
(X1, E, True), (X2, E, True),
2728-
(Y1, E, self.is_c), (Y2, E, self.is_c),
2729-
(E, Y1, self.is_c), (E, Y2, self.is_c),
2730-
]:
2731-
with self.subTest(etype=etype, rem_type=rem_type, raises=raises):
2732-
with self.subTest("single child"):
2733-
root = E('.')
2734-
root.append(etype('one'))
2735-
test_remove(root, rem_type('baz'), raises)
2736-
2737-
with self.subTest("with children"):
2738-
root = E('.')
2739-
root.extend([etype('one'), rem_type('two')])
2740-
test_remove(root, rem_type('baz'), raises)
2741-
2742-
def test_remove_with_mutate_root(self):
2701+
for raises in [True, False]:
2702+
2703+
class X(E):
2704+
def __eq__(self, o):
2705+
del root[:]
2706+
return not raises
2707+
2708+
class Y(E):
2709+
def __eq__(self, o):
2710+
root.clear()
2711+
return not raises
2712+
2713+
for etype, rem_type in [(E, X), (X, E), (E, Y), (Y, E)]:
2714+
with self.subTest(
2715+
etype=etype, rem_type=rem_type, raises=raises,
2716+
):
2717+
with self.subTest("single child"):
2718+
root = E('.')
2719+
root.append(etype('one'))
2720+
test_remove(root, rem_type('baz'), raises)
2721+
2722+
with self.subTest("with children"):
2723+
root = E('.')
2724+
root.extend([etype('one'), rem_type('two')])
2725+
test_remove(root, rem_type('baz'), raises)
2726+
2727+
def test_remove_with_mutate_root_1(self):
27432728
# See: https://github.com/python/cpython/issues/126033
27442729

27452730
E = ET.Element
27462731

2747-
class X(ET.Element):
2732+
class X(E):
27482733
def __eq__(self, o):
27492734
del root[0]
27502735
return False
27512736

2752-
class Y(ET.Element):
2737+
for etype, rem_type in [(E, X), (X, E)]:
2738+
with self.subTest('missing', etype=etype, rem_type=rem_type):
2739+
root = E('.')
2740+
root.extend([E('one'), etype('two')])
2741+
to_remove = rem_type('baz')
2742+
self.assertRaises(ValueError, root.remove, to_remove)
2743+
2744+
with self.subTest('existing', etype=etype, rem_type=rem_type):
2745+
root = E('.')
2746+
root.extend([E('one'), etype('same')])
2747+
to_remove = rem_type('same')
2748+
self.assertRaises(ValueError, root.remove, to_remove)
2749+
2750+
def test_remove_with_mutate_root_2(self):
2751+
# See: https://github.com/python/cpython/issues/126033
2752+
2753+
E = ET.Element
2754+
2755+
class X(E):
27532756
def __eq__(self, o):
27542757
del root[0]
27552758
return True
27562759

2757-
for bar_type, rem_type, raises in [
2758-
(E, X, True),
2759-
(X, E, True),
2760-
(Y, E, True),
2761-
(E, Y, False),
2762-
]:
2763-
with self.subTest(bar_type=bar_type, rem_type=rem_type, raises=raises):
2760+
for etype, rem_type in [(E, X), (X, E)]:
2761+
with self.subTest('missing', etype=etype, rem_type=rem_type):
27642762
root = E('.')
2765-
root.extend([E('first'), rem_type('bar')])
2763+
root.extend([E('one'), etype('two')])
27662764
to_remove = rem_type('baz')
2767-
if raises:
2768-
self.assertRaises(ValueError, root.remove, to_remove)
2769-
else:
2770-
root.remove(to_remove)
2771-
self.assertNotIn(to_remove, root)
2765+
root.remove(to_remove)
2766+
2767+
with self.subTest('existing', etype=etype, rem_type=rem_type):
2768+
root = E('.')
2769+
root.extend([E('one'), etype('same')])
2770+
to_remove = rem_type('same')
2771+
root.remove(to_remove)
27722772

27732773
@support.infinite_recursion(25)
27742774
def test_recursive_repr(self):

0 commit comments

Comments
 (0)