Skip to content

Commit 50fed0b

Browse files
GPHemsleyscoder
authored andcommitted
bpo-32424: Improve test coverage for xml.etree.ElementTree (GH-12891)
* Fix typo in test_cyclic_gc subtest * Improve test coverage for xml.etree.ElementTree
1 parent 21a9ba1 commit 50fed0b

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

Lib/test/test_xml_etree.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import functools
1010
import html
1111
import io
12+
import itertools
1213
import locale
1314
import operator
1415
import pickle
@@ -1929,6 +1930,88 @@ def test_expat224_utf8_bug_file(self):
19291930

19301931

19311932
class BasicElementTest(ElementTestCase, unittest.TestCase):
1933+
1934+
def test___init__(self):
1935+
tag = "foo"
1936+
attrib = { "zix": "wyp" }
1937+
1938+
element_foo = ET.Element(tag, attrib)
1939+
1940+
# traits of an element
1941+
self.assertIsInstance(element_foo, ET.Element)
1942+
self.assertIn("tag", dir(element_foo))
1943+
self.assertIn("attrib", dir(element_foo))
1944+
self.assertIn("text", dir(element_foo))
1945+
self.assertIn("tail", dir(element_foo))
1946+
1947+
# string attributes have expected values
1948+
self.assertEqual(element_foo.tag, tag)
1949+
self.assertIsNone(element_foo.text)
1950+
self.assertIsNone(element_foo.tail)
1951+
1952+
# attrib is a copy
1953+
self.assertIsNot(element_foo.attrib, attrib)
1954+
self.assertEqual(element_foo.attrib, attrib)
1955+
1956+
# attrib isn't linked
1957+
attrib["bar"] = "baz"
1958+
self.assertIsNot(element_foo.attrib, attrib)
1959+
self.assertNotEqual(element_foo.attrib, attrib)
1960+
1961+
def test___copy__(self):
1962+
element_foo = ET.Element("foo", { "zix": "wyp" })
1963+
element_foo.append(ET.Element("bar", { "baz": "qix" }))
1964+
1965+
element_foo2 = copy.copy(element_foo)
1966+
1967+
# elements are not the same
1968+
self.assertIsNot(element_foo2, element_foo)
1969+
1970+
# string attributes are equal
1971+
self.assertEqual(element_foo2.tag, element_foo.tag)
1972+
self.assertEqual(element_foo2.text, element_foo.text)
1973+
self.assertEqual(element_foo2.tail, element_foo.tail)
1974+
1975+
# number of children is the same
1976+
self.assertEqual(len(element_foo2), len(element_foo))
1977+
1978+
# children are the same
1979+
for (child1, child2) in itertools.zip_longest(element_foo, element_foo2):
1980+
self.assertIs(child1, child2)
1981+
1982+
# attrib is a copy
1983+
self.assertEqual(element_foo2.attrib, element_foo.attrib)
1984+
1985+
def test___deepcopy__(self):
1986+
element_foo = ET.Element("foo", { "zix": "wyp" })
1987+
element_foo.append(ET.Element("bar", { "baz": "qix" }))
1988+
1989+
element_foo2 = copy.deepcopy(element_foo)
1990+
1991+
# elements are not the same
1992+
self.assertIsNot(element_foo2, element_foo)
1993+
1994+
# string attributes are equal
1995+
self.assertEqual(element_foo2.tag, element_foo.tag)
1996+
self.assertEqual(element_foo2.text, element_foo.text)
1997+
self.assertEqual(element_foo2.tail, element_foo.tail)
1998+
1999+
# number of children is the same
2000+
self.assertEqual(len(element_foo2), len(element_foo))
2001+
2002+
# children are not the same
2003+
for (child1, child2) in itertools.zip_longest(element_foo, element_foo2):
2004+
self.assertIsNot(child1, child2)
2005+
2006+
# attrib is a copy
2007+
self.assertIsNot(element_foo2.attrib, element_foo.attrib)
2008+
self.assertEqual(element_foo2.attrib, element_foo.attrib)
2009+
2010+
# attrib isn't linked
2011+
element_foo.attrib["bar"] = "baz"
2012+
self.assertIsNot(element_foo2.attrib, element_foo.attrib)
2013+
self.assertNotEqual(element_foo2.attrib, element_foo.attrib)
2014+
19322015
def test_augmentation_type_errors(self):
19332016
e = ET.Element('joe')
19342017
self.assertRaises(TypeError, e.append, 'b')
@@ -1984,9 +2067,9 @@ class Dummy:
19842067
e1 = ET.Element('e1')
19852068
e2 = ET.Element('e2')
19862069
e3 = ET.Element('e3')
1987-
e1.append(e2)
1988-
e2.append(e2)
19892070
e3.append(e1)
2071+
e2.append(e3)
2072+
e1.append(e2)
19902073
wref = weakref.ref(e1)
19912074
del e1, e2, e3
19922075
gc_collect()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ Christian Heimes
647647
Thomas Heller
648648
Malte Helmert
649649
Lance Finn Helsten
650+
Gordon P. Hemsley
650651
Jonathan Hendry
651652
Nathan Henrie
652653
Michael Henry
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix typo in test_cyclic_gc() test for xml.etree.ElementTree. Patch by Gordon
2+
P. Hemsley.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve test coverage for xml.etree.ElementTree. Patch by Gordon P. Hemsley.

0 commit comments

Comments
 (0)