From 6b0a836c9c4a311440c2259af9c689933e70703a Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 15 Oct 2012 22:18:47 +0200 Subject: [PATCH] Fixed assertXMLEqual when first node was a comment --- django/test/utils.py | 16 +++++++++++----- tests/regressiontests/test_utils/tests.py | 5 +++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/django/test/utils.py b/django/test/utils.py index 71252eaac8c..f10d3882277 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -227,9 +227,10 @@ class override_settings(object): def compare_xml(want, got): - """Tries to do a 'xml-comparision' of want and got. Plain string - comparision doesn't always work because, for example, attribute - ordering should not be important. + """Tries 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. Comment nodes are not considered in the + comparison. Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py """ @@ -267,6 +268,11 @@ def compare_xml(want, got): return False return True + def first_node(document): + for node in document.childNodes: + if node.nodeType != Node.COMMENT_NODE: + return node + want, got = strip_quotes(want, got) want = want.replace('\\n','\n') got = got.replace('\\n','\n') @@ -279,8 +285,8 @@ def compare_xml(want, got): got = wrapper % got # Parse the want and got strings, and compare the parsings. - want_root = parseString(want).firstChild - got_root = parseString(got).firstChild + want_root = first_node(parseString(want)) + got_root = first_node(parseString(got)) return check_element(want_root, got_root) diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py index dec157eacbc..95913b5aab9 100644 --- a/tests/regressiontests/test_utils/tests.py +++ b/tests/regressiontests/test_utils/tests.py @@ -484,6 +484,11 @@ class XMLEqualTests(TestCase): with self.assertRaises(AssertionError): self.assertXMLNotEqual(xml_unvalid, xml2) + def test_comment_root(self): + xml1 = "" + xml2 = "" + self.assertXMLEqual(xml1, xml2) + class SkippingExtraTests(TestCase): fixtures = ['should_not_be_loaded.json']