Fixed #31250 -- Ignored processing instructions in assertXMLEqual()/assertXMLNotEqual().

This commit is contained in:
yura 2020-02-12 13:38:06 +03:00 committed by Mariusz Felisiak
parent 4070d6ceb0
commit 54b7af7eb4
3 changed files with 24 additions and 5 deletions

View File

@ -537,8 +537,8 @@ def compare_xml(want, got):
"""
Try to do a 'xml-comparison' of want and got. Plain string comparison
doesn't always work because, for example, attribute ordering should not be
important. Ignore comment nodes, document type node, and leading and
trailing whitespaces.
important. Ignore comment nodes, processing instructions, document type
node, and leading and trailing whitespaces.
Based on https://github.com/lxml/lxml/blob/master/src/lxml/doctestcompare.py
"""
@ -576,7 +576,11 @@ def compare_xml(want, got):
def first_node(document):
for node in document.childNodes:
if node.nodeType not in (Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE):
if node.nodeType not in (
Node.COMMENT_NODE,
Node.DOCUMENT_TYPE_NODE,
Node.PROCESSING_INSTRUCTION_NODE,
):
return node
want = want.strip().replace('\\n', '\n')

View File

@ -1613,8 +1613,8 @@ your test suite.
syntax differences. When invalid XML is passed in any parameter, an
``AssertionError`` is always raised, even if both string are identical.
XML declaration, document type, and comments are ignored. Only the root
element and its children are compared.
XML declaration, document type, processing instructions, and comments are
ignored. Only the root element and its children are compared.
Output in case of error can be customized with the ``msg`` argument.

View File

@ -926,6 +926,21 @@ class XMLEqualTests(SimpleTestCase):
xml2 = '<?xml version="1.0"?><!DOCTYPE root SYSTEM "example2.dtd"><root />'
self.assertXMLEqual(xml1, xml2)
def test_processing_instruction(self):
xml1 = (
'<?xml version="1.0"?>'
'<?xml-model href="http://www.example1.com"?><root />'
)
xml2 = (
'<?xml version="1.0"?>'
'<?xml-model href="http://www.example2.com"?><root />'
)
self.assertXMLEqual(xml1, xml2)
self.assertXMLEqual(
'<?xml-stylesheet href="style1.xslt" type="text/xsl"?><root />',
'<?xml-stylesheet href="style2.xslt" type="text/xsl"?><root />',
)
class SkippingExtraTests(TestCase):
fixtures = ['should_not_be_loaded.json']