From 2085d8d5bc2625076983d3be14b30ea684c3f0d5 Mon Sep 17 00:00:00 2001 From: Mattia Larentis Date: Sat, 7 Nov 2015 14:31:25 +0100 Subject: [PATCH] Fixed #25170 -- Made assertXMLEqual()/assertXMLNotEqual() ignore leading and trailing whitespace. Thanks Jacek Bzdak for indepdently contributing a similar fix. --- AUTHORS | 2 ++ django/test/utils.py | 6 +++--- tests/test_utils/tests.py | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index dbeb3e9e61..6a4f2e5d6d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -234,6 +234,7 @@ answer newbie questions, and generally made Django that much better: Fabrice Aneche favo@exoweb.net fdr + Federico Capoano Filip Noetzel Filip Wasilewski Finn Gruwier Larsen @@ -483,6 +484,7 @@ answer newbie questions, and generally made Django that much better: Matt McClanahan Matt Riggott Matt Robenolt + Mattia Larentis mattycakes@gmail.com Max Burstein Max Derkachev diff --git a/django/test/utils.py b/django/test/utils.py index 95757dbf1b..e665ea1ec3 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -293,7 +293,7 @@ def compare_xml(want, got): """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. + comparison. Leading and trailing whitespace is ignored on both chunks. Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py """ @@ -338,8 +338,8 @@ def compare_xml(want, got): return node want, got = strip_quotes(want, got) - want = want.replace('\\n', '\n') - got = got.replace('\\n', '\n') + want = want.strip().replace('\\n', '\n') + got = got.strip().replace('\\n', '\n') # If the string is not a complete xml document, we may need to add a # root element. This allow us to compare fragments, like "" diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 00c9c0fa2b..a495ff1010 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -753,6 +753,16 @@ class XMLEqualTests(SimpleTestCase): xml2 = "" self.assertXMLEqual(xml1, xml2) + def test_simple_equal_with_leading_or_trailing_whitespace(self): + xml1 = "foo \t\n" + xml2 = " \t\nfoo" + self.assertXMLEqual(xml1, xml2) + + def test_simple_not_equal_with_whitespace_in_the_middle(self): + xml1 = "foobar" + xml2 = "foo bar" + self.assertXMLNotEqual(xml1, xml2) + class SkippingExtraTests(TestCase): fixtures = ['should_not_be_loaded.json']