Work with Python 3

This commit is contained in:
Jonathan Lange
2011-06-30 16:51:58 +01:00
parent 4dcd3c33dd
commit 4a8e920e25
5 changed files with 43 additions and 41 deletions

View File

@@ -30,7 +30,7 @@ class ContentType(object):
if self.parameters:
params = '; '
params += ', '.join(
'%s="%s"' % (k, v) for k, v in self.parameters.iteritems())
'%s="%s"' % (k, v) for k, v in self.parameters.items())
else:
params = ''
return "%s/%s%s" % (self.type, self.subtype, params)

View File

@@ -1,10 +1,12 @@
# Copyright (c) 2010 testtools developers. See LICENSE for details.
# Copyright (c) 2010-2011 testtools developers. See LICENSE for details.
__all__ = [
'try_import',
'try_imports',
]
import sys
def try_import(name, alternative=None, error_callback=None):
"""Attempt to import ``name``. If it fails, return ``alternative``.
@@ -25,8 +27,8 @@ def try_import(name, alternative=None, error_callback=None):
module_name = '.'.join(module_segments)
try:
module = __import__(module_name)
except ImportError, e:
last_error = e
except ImportError:
last_error = sys.exc_info()[1]
module_segments.pop()
continue
else:

View File

@@ -628,7 +628,7 @@ def _details_to_str(details, special=None):
if content.content_type.type != 'text':
binary_attachments.append((key, content.content_type))
continue
text = _b('').join(content.iter_text()).strip()
text = _u('').join(content.iter_text()).strip()
if not text:
empty_attachments.append(key)
continue

View File

@@ -10,12 +10,12 @@ from testtools.matchers import (
Is,
Not,
)
def check_error_callback(test, function, arg, expected_error_count,
def check_error_callback(test, function, arg, expected_error_count,
expect_result):
"""General test template for error_callback argument.
:param test: Test case instance.
:param function: Either try_import or try_imports.
:param arg: Name or names to import.
@@ -29,7 +29,7 @@ def check_error_callback(test, function, arg, expected_error_count,
cb_calls.append(e)
try:
result = function(arg, error_callback=cb)
except ImportError, e:
except ImportError:
test.assertFalse(expect_result)
else:
if expect_result:
@@ -79,20 +79,20 @@ class TestTryImport(TestCase):
result = try_import('os.path.join')
import os
self.assertThat(result, Is(os.path.join))
def test_error_callback(self):
# the error callback is called on failures.
check_error_callback(self, try_import, 'doesntexist', 1, False)
def test_error_callback_missing_module_member(self):
# the error callback is called on failures to find an object
# the error callback is called on failures to find an object
# inside an existing module.
check_error_callback(self, try_import, 'os.nonexistent', 1, False)
def test_error_callback_not_on_success(self):
# the error callback is not called on success.
check_error_callback(self, try_import, 'os.path', 0, True)
class TestTryImports(TestCase):
@@ -140,16 +140,16 @@ class TestTryImports(TestCase):
result = try_imports(['os.doesntexist', 'os.path'])
import os
self.assertThat(result, Is(os.path))
def test_error_callback(self):
# One error for every class that doesn't exist.
check_error_callback(self, try_imports,
check_error_callback(self, try_imports,
['os.doesntexist', 'os.notthiseither'],
2, False)
check_error_callback(self, try_imports,
check_error_callback(self, try_imports,
['os.doesntexist', 'os.notthiseither', 'os'],
2, True)
check_error_callback(self, try_imports,
check_error_callback(self, try_imports,
['os.path'],
0, True)

View File

@@ -376,14 +376,14 @@ class TestTestResult(TestCase):
self.assertThat(
result.errors[0][1],
DocTestMatches(
u'Traceback (most recent call last):\n'
u' File "testtools/runtest.py", line ..., in _run_user\n'
u' return fn(*args, **kwargs)\n'
u' File "testtools/testcase.py", line ..., in _run_test_method\n'
u' return self._get_test_method()()\n'
u' File "testtools/tests/test_testresult.py", line ..., in error\n'
u' 1/0\n'
u'ZeroDivisionError: integer division or modulo by zero\n',
'Traceback (most recent call last):\n'
' File "testtools/runtest.py", line ..., in _run_user\n'
' return fn(*args, **kwargs)\n'
' File "testtools/testcase.py", line ..., in _run_test_method\n'
' return self._get_test_method()()\n'
' File "testtools/tests/test_testresult.py", line ..., in error\n'
' 1/0\n'
'ZeroDivisionError: ...\n',
doctest.ELLIPSIS))
@@ -1401,14 +1401,14 @@ class TestDetailsToStr(TestCase):
def test_no_details(self):
string = _details_to_str({})
self.assertThat(string, Equals(u''))
self.assertThat(string, Equals(''))
def test_binary_content(self):
content = content_from_stream(
StringIO('foo'), content_type=ContentType('image', 'jpeg'))
string = _details_to_str({'attachment': content})
self.assertThat(
string, Equals(u"""\
string, Equals("""\
Binary content:
attachment (image/jpeg)
"""))
@@ -1416,37 +1416,37 @@ Binary content:
def test_single_line_content(self):
content = text_content('foo')
string = _details_to_str({'attachment': content})
self.assertThat(string, Equals(u'attachment: {{{foo}}}\n'))
self.assertThat(string, Equals('attachment: {{{foo}}}\n'))
def test_multi_line_text_content(self):
content = text_content('foo\nbar\nbaz')
string = _details_to_str({'attachment': content})
self.assertThat(string, Equals(u'attachment: {{{\nfoo\nbar\nbaz\n}}}\n'))
self.assertThat(string, Equals('attachment: {{{\nfoo\nbar\nbaz\n}}}\n'))
def test_special_text_content(self):
content = text_content('foo')
string = _details_to_str({'attachment': content}, special='attachment')
self.assertThat(string, Equals(u'foo\n'))
self.assertThat(string, Equals('foo\n'))
def test_multiple_text_content(self):
string = _details_to_str(
{'attachment': text_content('foo\nfoo'),
'attachment-1': text_content('bar\nbar')})
self.assertThat(
string, Equals(u'attachment: {{{\n'
u'foo\n'
u'foo\n'
u'}}}\n'
u'\n'
u'attachment-1: {{{\n'
u'bar\n'
u'bar\n'
u'}}}\n'))
string, Equals('attachment: {{{\n'
'foo\n'
'foo\n'
'}}}\n'
'\n'
'attachment-1: {{{\n'
'bar\n'
'bar\n'
'}}}\n'))
def test_empty_attachment(self):
string = _details_to_str({'attachment': text_content('')})
self.assertThat(
string, Equals(u"""\
string, Equals("""\
Empty attachments:
attachment
"""))
@@ -1464,7 +1464,7 @@ Empty attachments:
}
string = _details_to_str(attachments, special='attachment-1')
self.assertThat(
string, Equals(u"""\
string, Equals("""\
Binary content:
attachment-2 (image/jpeg)
attachment-5 (image/jpeg)