Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add comments to tests.
  • Loading branch information
serhiy-storchaka committed May 29, 2026
commit 57b573937f94ecc968f2d9a3d4a3e2dfcc4f31fd
9 changes: 8 additions & 1 deletion Lib/test/test_xml_etree.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It would be good to add some comments to clearly state the intent of the tests (e.g. comments shouldn't be excaped, comments are omitted in text serialization, empty attrs only work in html, etc.).
  2. Is there any reason why the there are 3 similar but different test strings ('<spam> & ham', 'ham & eggs < spam', '<spam>&ham')?

Original file line number Diff line number Diff line change
Expand Up @@ -1289,8 +1289,10 @@ def check(p, expected, namespaces=None):

def test_comment_serialization(self):
comm = ET.Comment('<spam> & ham')
# comments are not escaped
self.assertEqual(ET.tostring(comm), b'<!--<spam> & ham-->')
self.assertEqual(ET.tostring(comm, method='html'), b'<!--<spam> & ham-->')
# no comments in text serialization
self.assertEqual(ET.tostring(comm, method='text'), b'')

def test_processinginstruction_serialization(self):
Expand All @@ -1302,7 +1304,7 @@ def test_processinginstruction_serialization(self):
b'<?test instruction?>')

# Issue #2746

# processing instructions are not escaped
self.assertEqual(ET.tostring(ET.PI('test', '<testing&>')),
b'<?test <testing&>?>')
self.assertEqual(ET.tostring(ET.PI('test', '<testing&>\xe3'), 'latin-1'),
Expand All @@ -1311,15 +1313,18 @@ def test_processinginstruction_serialization(self):
pi = ET.PI('test', 'ham & eggs < spam')
self.assertEqual(ET.tostring(pi), b'<?test ham & eggs < spam?>')
self.assertEqual(ET.tostring(pi, method='html'), b'<?test ham & eggs < spam?>')
# no processing instructions in text serialization
self.assertEqual(ET.tostring(pi, method='text'), b'')

def test_empty_attribute_serialization(self):
# empty attrs only work in html
elem = ET.Element('tag', attrib={'attr': None})
self.assertRaises(TypeError, ET.tostring, elem)
self.assertEqual(ET.tostring(elem, method='html'), b'<tag attr></tag>')

@support.subTests('tag', ("script", "style", "xmp", "iframe", "noembed", "noframes"))
def test_html_cdata_elems_serialization(self, tag):
# content of raw text elements is not escaped in html
tag = tag.title()
elem = ET.Element(tag)
elem.text = '<spam>&ham'
Expand All @@ -1341,6 +1346,8 @@ def test_html_empty_elems_serialization(self):
self.assertEqual(serialized, expected)

def test_html_plaintext_serialization(self):
# content of plaintext is not escaped in html
# no end tag for plaintext
elem = ET.Element('PlainText')
elem.text = '<spam>&ham'
self.assertEqual(ET.tostring(elem, method='html'),
Expand Down
Loading