← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~jml/testtools/doctest-unicode-safety-672056 into lp:testtools

 

Jonathan Lange has proposed merging lp:~jml/testtools/doctest-unicode-safety-672056 into lp:testtools.

Requested reviews:
  testtools developers (testtools-dev)
Related bugs:
  Bug #672056 in testtools: "UnicodeEncodeError: 'ascii' codec can't encode characters in position 2217-2258: ordinal not in range(128)"
  https://bugs.launchpad.net/testtools/+bug/672056

For more details, see:
https://code.launchpad.net/~jml/testtools/doctest-unicode-safety-672056/+merge/66500

I think this branch makes DocTestMatches safe even when someone has crazy characters.
-- 
https://code.launchpad.net/~jml/testtools/doctest-unicode-safety-672056/+merge/66500
Your team testtools developers is requested to review the proposed merge of lp:~jml/testtools/doctest-unicode-safety-672056 into lp:testtools.
=== modified file 'Makefile'
--- Makefile	2011-06-30 16:57:12 +0000
+++ Makefile	2011-06-30 17:14:22 +0000
@@ -1,6 +1,6 @@
 # See README for copyright and licensing details.
 
-PYTHON=python3
+PYTHON=python
 SOURCES=$(shell find testtools -name "*.py")
 
 check:

=== modified file 'testtools/compat.py'
--- testtools/compat.py	2011-04-24 21:48:56 +0000
+++ testtools/compat.py	2011-06-30 17:14:22 +0000
@@ -9,6 +9,7 @@
     'advance_iterator',
     'str_is_unicode',
     'StringIO',
+    'object_to_text',
     'unicode_output_stream',
     ]
 
@@ -195,6 +196,15 @@
     return locale.getlocale(locale.LC_MESSAGES)[1] or "ascii"
 
 
+def object_to_text(obj):
+    """Take an object, get a text representation."""
+    try:
+        return unicode(obj)
+    except NameError:
+        # We are probably in Python 3.
+        return str(obj)
+
+
 def _exception_to_text(evalue):
     """Try hard to get a sensible text value out of an exception instance"""
     try:

=== modified file 'testtools/matchers.py'
--- testtools/matchers.py	2011-06-30 16:57:12 +0000
+++ testtools/matchers.py	2011-06-30 17:14:22 +0000
@@ -47,6 +47,8 @@
     _error_repr,
     isbaseexception,
     istext,
+    object_to_text,
+    _u,
     )
 
 
@@ -177,9 +179,9 @@
         return 'DocTestMatches(%r%s)' % (self.want, flagstr)
 
     def _with_nl(self, actual):
-        result = str(actual)
-        if not result.endswith('\n'):
-            result += '\n'
+        result = object_to_text(actual)
+        if not result.endswith(_u('\n')):
+            result += _u('\n')
         return result
 
     def match(self, actual):

=== modified file 'testtools/tests/test_matchers.py'
--- testtools/tests/test_matchers.py	2011-06-30 11:42:40 +0000
+++ testtools/tests/test_matchers.py	2011-06-30 17:14:22 +0000
@@ -12,6 +12,7 @@
     )
 from testtools.compat import (
     StringIO,
+    _u,
     )
 from testtools.matchers import (
     AfterPreproccessing,
@@ -111,6 +112,12 @@
         '    Ran 1 test in 0.123s\n', "Ran 1 test in 0.123s",
         DocTestMatches("Ran 1 tests in ...s", doctest.ELLIPSIS))]
 
+    def test_unicode(self):
+        # Bug #672056: The matcher can handle being passed a unicode object
+        # with unusual characters.
+        text = _u('foo\u2026')
+        self.assertThat(text, DocTestMatches(text))
+
 
 class TestDocTestMatchesSpecific(TestCase):
 


Follow ups