Clean up packaging.tests.test_mixin2to3

This commit is contained in:
Éric Araujo 2011-06-16 23:21:01 +02:00
parent 4e97abce2d
commit 434812d569
1 changed files with 43 additions and 34 deletions

View File

@ -1,5 +1,5 @@
"""Tests for packaging.command.build_py."""
import sys
import textwrap
from packaging.tests import unittest, support
from packaging.compat import Mixin2to3
@ -12,60 +12,69 @@ class Mixin2to3TestCase(support.TempdirManager,
@unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher')
def test_convert_code_only(self):
# used to check if code gets converted properly.
code_content = "print 'test'\n"
code_handle = self.mktempfile()
code_name = code_handle.name
code = "print 'test'"
code_handle.write(code_content)
code_handle.flush()
with self.mktempfile() as fp:
fp.write(code)
mixin2to3 = Mixin2to3()
mixin2to3._run_2to3([code_name])
converted_code_content = "print('test')\n"
with open(code_name) as fp:
new_code_content = "".join(fp.readlines())
mixin2to3._run_2to3([fp.name])
expected = "print('test')"
self.assertEqual(new_code_content, converted_code_content)
with open(fp.name) as fp:
converted = fp.read()
self.assertEqual(expected, converted)
@unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher')
def test_doctests_only(self):
# used to check if doctests gets converted properly.
doctest_content = '"""\n>>> print test\ntest\n"""\nprint test\n\n'
doctest_handle = self.mktempfile()
doctest_name = doctest_handle.name
doctest = textwrap.dedent('''\
"""Example docstring.
doctest_handle.write(doctest_content)
doctest_handle.flush()
>>> print test
test
It works.
"""''')
with self.mktempfile() as fp:
fp.write(doctest)
mixin2to3 = Mixin2to3()
mixin2to3._run_2to3([doctest_name])
mixin2to3._run_2to3([fp.name])
expected = textwrap.dedent('''\
"""Example docstring.
converted_doctest_content = ['"""', '>>> print(test)', 'test', '"""',
'print(test)', '', '', '']
converted_doctest_content = '\n'.join(converted_doctest_content)
with open(doctest_name) as fp:
new_doctest_content = "".join(fp.readlines())
>>> print(test)
test
self.assertEqual(new_doctest_content, converted_doctest_content)
It works.
"""\n''')
with open(fp.name) as fp:
converted = fp.read()
self.assertEqual(expected, converted)
@unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher')
def test_additional_fixers(self):
# used to check if use_2to3_fixers works
code_content = "type(x) is T"
code_handle = self.mktempfile()
code_name = code_handle.name
code = 'type(x) is not T'
code_handle.write(code_content)
code_handle.flush()
with self.mktempfile() as fp:
fp.write(code)
mixin2to3 = Mixin2to3()
mixin2to3._run_2to3(files=[code_name], doctests=[code_name],
mixin2to3._run_2to3(files=[fp.name], doctests=[fp.name],
fixers=['packaging.tests.fixer'])
converted_code_content = "isinstance(x, T)"
with open(code_name) as fp:
new_code_content = "".join(fp.readlines())
self.assertEqual(new_code_content, converted_code_content)
expected = 'not isinstance(x, T)'
with open(fp.name) as fp:
converted = fp.read()
self.assertEqual(expected, converted)
def test_suite():