Commits

Marc Abramowitz committed 2149626

Make tests succeed in Python 3.0 and 3.1

Comments (0)

Files changed (4)

 envlist = py24,py25,py26,py27,py33,py34,jython,pypy
 
 [testenv]
+setenv =
+    PIP_INSECURE=1
 commands=python unit2.py discover []

unittest2/compatibility.py

 try:
     # Python 2
     unicode = unicode
+    def u(s):
+        return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
 except NameError:
     # Python 3
     unicode = str
+    def u(s):
+        return s
 
 try:
     # Python 2

unittest2/test/_test_unittest2_with.py

 import unittest2
 from unittest2.test.support import OldTestResult
 from unittest2.compatibility import catch_warnings
+from unittest2.compatibility import u
 
 import sys
 import warnings
             one = ''.join(chr(i) for i in range(255))
             # this used to cause a UnicodeDecodeError constructing the failure msg
             with self.assertRaises(self.failureException):
-                self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'})
+                self.assertDictContainsSubset({'foo': one}, {'foo': u('\uFFFD')})
 
     def test_formatMessage_unicode_error(self):
         with catch_warnings(record=True):
             # This causes a UnicodeWarning due to its craziness
             one = ''.join(chr(i) for i in range(255))
             # this used to cause a UnicodeDecodeError constructing msg
-            self._formatMessage(one, u'\uFFFD')
+            self._formatMessage(one, u('\uFFFD'))
 
     def assertOldResultWarning(self, test, failures):
         with self.assertWarns(RuntimeWarning):

unittest2/test/test_case.py

 from unittest2.test.support import (
     OldTestResult, EqualityMixin, HashingMixin, LoggingResult
 )
+from unittest2.compatibility import u
 
 
 class MyException(Exception):
         self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant')
 
         # Try Unicode
-        self.assertGreater(u'bug', u'ant')
-        self.assertGreaterEqual(u'bug', u'ant')
-        self.assertGreaterEqual(u'ant', u'ant')
-        self.assertLess(u'ant', u'bug')
-        self.assertLessEqual(u'ant', u'bug')
-        self.assertLessEqual(u'ant', u'ant')
-        self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug')
-        self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant')
-        self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',
-                          u'bug')
-        self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant')
-        self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant')
-        self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant')
+        self.assertGreater(u('bug'), u('ant'))
+        self.assertGreaterEqual(u('bug'), u('ant'))
+        self.assertGreaterEqual(u('ant'), u('ant'))
+        self.assertLess(u('ant'), u('bug'))
+        self.assertLessEqual(u('ant'), u('bug'))
+        self.assertLessEqual(u('ant'), u('ant'))
+        self.assertRaises(self.failureException, self.assertGreater, u('ant'), u('bug'))
+        self.assertRaises(self.failureException, self.assertGreater, u('ant'), u('ant'))
+        self.assertRaises(self.failureException, self.assertGreaterEqual, u('ant'),
+                          u('bug'))
+        self.assertRaises(self.failureException, self.assertLess, u('bug'), u('ant'))
+        self.assertRaises(self.failureException, self.assertLess, u('ant'), u('ant'))
+        self.assertRaises(self.failureException, self.assertLessEqual, u('bug'), u('ant'))
 
         # Try Mixed String/Unicode
-        self.assertGreater('bug', u'ant')
-        self.assertGreater(u'bug', 'ant')
-        self.assertGreaterEqual('bug', u'ant')
-        self.assertGreaterEqual(u'bug', 'ant')
-        self.assertGreaterEqual('ant', u'ant')
-        self.assertGreaterEqual(u'ant', 'ant')
-        self.assertLess('ant', u'bug')
-        self.assertLess(u'ant', 'bug')
-        self.assertLessEqual('ant', u'bug')
-        self.assertLessEqual(u'ant', 'bug')
-        self.assertLessEqual('ant', u'ant')
-        self.assertLessEqual(u'ant', 'ant')
-        self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug')
-        self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug')
-        self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant')
-        self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant')
+        self.assertGreater('bug', u('ant'))
+        self.assertGreater(u('bug'), 'ant')
+        self.assertGreaterEqual('bug', u('ant'))
+        self.assertGreaterEqual(u('bug'), 'ant')
+        self.assertGreaterEqual('ant', u('ant'))
+        self.assertGreaterEqual(u('ant'), 'ant')
+        self.assertLess('ant', u('bug'))
+        self.assertLess(u('ant'), 'bug')
+        self.assertLessEqual('ant', u('bug'))
+        self.assertLessEqual(u('ant'), 'bug')
+        self.assertLessEqual('ant', u('ant'))
+        self.assertLessEqual(u('ant'), 'ant')
+        self.assertRaises(self.failureException, self.assertGreater, 'ant', u('bug'))
+        self.assertRaises(self.failureException, self.assertGreater, u('ant'), 'bug')
+        self.assertRaises(self.failureException, self.assertGreater, 'ant', u('ant'))
+        self.assertRaises(self.failureException, self.assertGreater, u('ant'), 'ant')
         self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant',
-                          u'bug')
-        self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',
+                          u('bug'))
+        self.assertRaises(self.failureException, self.assertGreaterEqual, u('ant'),
                           'bug')
-        self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant')
-        self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant')
-        self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant')
-        self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant')
-        self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant')
-        self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant')
+        self.assertRaises(self.failureException, self.assertLess, 'bug', u('ant'))
+        self.assertRaises(self.failureException, self.assertLess, u('bug'), 'ant')
+        self.assertRaises(self.failureException, self.assertLess, 'ant', u('ant'))
+        self.assertRaises(self.failureException, self.assertLess, u('ant'), 'ant')
+        self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u('ant'))
+        self.assertRaises(self.failureException, self.assertLessEqual, u('bug'), 'ant')
 
     def testAssertMultiLineEqual(self):
         sample_text = """\
 
         self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
         self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
-        self.assertRaisesRegex(ExceptionMock, u'expect$', Stub)
+        self.assertRaisesRegex(ExceptionMock, u('expect$'), Stub)
 
     def testAssertNotRaisesRegex(self):
         self.assertRaisesRegex(
                 lambda: None)
         self.assertRaisesRegex(
                 self.failureException, '^Exception not raised$',
-                self.assertRaisesRegex, Exception, u'x',
+                self.assertRaisesRegex, Exception, u('x'),
                 lambda: None)
 
     def testAssertRaisesRegexMismatch(self):
         self.assertRaisesRegex(
                 self.failureException,
                 r'"\^Expected\$" does not match "Unexpected"',
-                self.assertRaisesRegex, Exception, u'^Expected$',
+                self.assertRaisesRegex, Exception, u('^Expected$'),
                 Stub)
         self.assertRaisesRegex(
                 self.failureException,