|
9 | 9 | import functools
|
10 | 10 | import html
|
11 | 11 | import io
|
| 12 | +import itertools |
12 | 13 | import locale
|
13 | 14 | import operator
|
14 | 15 | import pickle
|
@@ -1929,6 +1930,88 @@ def test_expat224_utf8_bug_file(self):
|
1929 | 1930 |
|
1930 | 1931 |
|
1931 | 1932 | 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 | + |
1932 | 2015 | def test_augmentation_type_errors(self):
|
1933 | 2016 | e = ET.Element('joe')
|
1934 | 2017 | self.assertRaises(TypeError, e.append, 'b')
|
@@ -1984,9 +2067,9 @@ class Dummy:
|
1984 | 2067 | e1 = ET.Element('e1')
|
1985 | 2068 | e2 = ET.Element('e2')
|
1986 | 2069 | e3 = ET.Element('e3')
|
1987 |
| - e1.append(e2) |
1988 |
| - e2.append(e2) |
1989 | 2070 | e3.append(e1)
|
| 2071 | + e2.append(e3) |
| 2072 | + e1.append(e2) |
1990 | 2073 | wref = weakref.ref(e1)
|
1991 | 2074 | del e1, e2, e3
|
1992 | 2075 | gc_collect()
|
|
0 commit comments