← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-equals-ignore-whitespace into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-equals-ignore-whitespace into launchpad:master.

Commit message:
Fix EqualsIgnoreWhitespace and its tests for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/398393

It's convenient for it to handle both bytes and text, and the tests need to handle the different reprs on Python 2 and 3.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-equals-ignore-whitespace into launchpad:master.
diff --git a/lib/lp/testing/__init__.py b/lib/lp/testing/__init__.py
index a44121d..61624ce 100644
--- a/lib/lp/testing/__init__.py
+++ b/lib/lp/testing/__init__.py
@@ -1377,7 +1377,8 @@ def normalize_whitespace(string):
     # whitespace is roughly 6 times faster than using an uncompiled
     # regex (for the expression \s+), and 4 times faster than a
     # compiled regex.
-    return " ".join(string.split())
+    joiner = b" " if isinstance(string, bytes) else u" "
+    return joiner.join(string.split())
 
 
 def map_branch_contents(branch):
diff --git a/lib/lp/testing/matchers.py b/lib/lp/testing/matchers.py
index e83618e..5b14a38 100644
--- a/lib/lp/testing/matchers.py
+++ b/lib/lp/testing/matchers.py
@@ -475,12 +475,12 @@ class EqualsIgnoringWhitespace(Equals):
     """
 
     def __init__(self, expected):
-        if isinstance(expected, six.string_types):
+        if isinstance(expected, (bytes, six.text_type)):
             expected = normalize_whitespace(expected)
         super(EqualsIgnoringWhitespace, self).__init__(expected)
 
     def match(self, observed):
-        if isinstance(observed, six.string_types):
+        if isinstance(observed, (bytes, six.text_type)):
             observed = normalize_whitespace(observed)
         return super(EqualsIgnoringWhitespace, self).match(observed)
 
diff --git a/lib/lp/testing/tests/test_matchers.py b/lib/lp/testing/tests/test_matchers.py
index e4e5955..db2c6dd 100644
--- a/lib/lp/testing/tests/test_matchers.py
+++ b/lib/lp/testing/tests/test_matchers.py
@@ -364,19 +364,19 @@ class ContainsTests(TestCase):
 
 class EqualsIgnoringWhitespaceTests(TestCase):
 
-    def test_str(self):
-        matcher = EqualsIgnoringWhitespace("abc")
-        self.assertEqual("EqualsIgnoringWhitespace('abc')", str(matcher))
+    def test_bytes(self):
+        matcher = EqualsIgnoringWhitespace(b"abc")
+        self.assertEqual("EqualsIgnoringWhitespace(%r)" % b"abc", str(matcher))
 
-    def test_match_str(self):
-        matcher = EqualsIgnoringWhitespace("one \t two \n three")
-        self.assertIs(None, matcher.match(" one \r two     three "))
+    def test_match_bytes(self):
+        matcher = EqualsIgnoringWhitespace(b"one \t two \n three")
+        self.assertIs(None, matcher.match(b" one \r two     three "))
 
-    def test_mismatch_str(self):
-        matcher = EqualsIgnoringWhitespace("one \t two \n three")
-        mismatch = matcher.match(" one \r three ")
+    def test_mismatch_bytes(self):
+        matcher = EqualsIgnoringWhitespace(b"one \t two \n three")
+        mismatch = matcher.match(b" one \r three ")
         self.assertEqual(
-            "'one three' != 'one two three'",
+            "%r != %r" % (b"one three", b"one two three"),
             mismatch.describe())
 
     def test_match_unicode(self):
@@ -387,7 +387,7 @@ class EqualsIgnoringWhitespaceTests(TestCase):
         matcher = EqualsIgnoringWhitespace(u"one \t two \n \u1234  ")
         mismatch = matcher.match(u" one \r \u1234 ")
         self.assertEqual(
-            u"u'one \\u1234' != u'one two \\u1234'",
+            u"%r != %r" % (u"one \u1234", u"one two \u1234"),
             mismatch.describe())
 
     def test_match_non_string(self):