#17365: Remove Python 2 code from test_print

Patch by Berker Peksag
This commit is contained in:
Andrew Svetlov 2013-04-04 22:32:28 +03:00
parent b9e2147dee
commit ace34dda0a
1 changed files with 34 additions and 46 deletions

View File

@ -1,27 +1,15 @@
"""Test correct operation of the print function.
"""
# In 2.6, this gives us the behavior we want. In 3.0, it has
# no function, but it still must parse correctly.
from __future__ import print_function
import unittest import unittest
from test import support from io import StringIO
try: from test import support
# 3.x
from io import StringIO
except ImportError:
# 2.x
from StringIO import StringIO
NotDefined = object() NotDefined = object()
# A dispatch table all 8 combinations of providing # A dispatch table all 8 combinations of providing
# sep, end, and file # sep, end, and file.
# I use this machinery so that I'm not just passing default # I use this machinery so that I'm not just passing default
# values to print, I'm either passing or not passing in the # values to print, I'm either passing or not passing in the
# arguments # arguments.
dispatch = { dispatch = {
(False, False, False): (False, False, False):
lambda args, sep, end, file: print(*args), lambda args, sep, end, file: print(*args),
@ -39,24 +27,29 @@ dispatch = {
lambda args, sep, end, file: print(sep=sep, end=end, *args), lambda args, sep, end, file: print(sep=sep, end=end, *args),
(True, True, True): (True, True, True):
lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
} }
# Class used to test __str__ and print # Class used to test __str__ and print
class ClassWith__str__: class ClassWith__str__:
def __init__(self, x): def __init__(self, x):
self.x = x self.x = x
def __str__(self): def __str__(self):
return self.x return self.x
class TestPrint(unittest.TestCase): class TestPrint(unittest.TestCase):
"""Test correct operation of the print function."""
def check(self, expected, args, def check(self, expected, args,
sep=NotDefined, end=NotDefined, file=NotDefined): sep=NotDefined, end=NotDefined, file=NotDefined):
# Capture sys.stdout in a StringIO. Call print with args, # Capture sys.stdout in a StringIO. Call print with args,
# and with sep, end, and file, if they're defined. Result # and with sep, end, and file, if they're defined. Result
# must match expected. # must match expected.
# Look up the actual function to call, based on if sep, end, and file # Look up the actual function to call, based on if sep, end,
# are defined # and file are defined.
fn = dispatch[(sep is not NotDefined, fn = dispatch[(sep is not NotDefined,
end is not NotDefined, end is not NotDefined,
file is not NotDefined)] file is not NotDefined)]
@ -69,7 +62,7 @@ class TestPrint(unittest.TestCase):
def test_print(self): def test_print(self):
def x(expected, args, sep=NotDefined, end=NotDefined): def x(expected, args, sep=NotDefined, end=NotDefined):
# Run the test 2 ways: not using file, and using # Run the test 2 ways: not using file, and using
# file directed to a StringIO # file directed to a StringIO.
self.check(expected, args, sep=sep, end=end) self.check(expected, args, sep=sep, end=end)
@ -101,11 +94,6 @@ class TestPrint(unittest.TestCase):
x('*\n', (ClassWith__str__('*'),)) x('*\n', (ClassWith__str__('*'),))
x('abc 1\n', (ClassWith__str__('abc'), 1)) x('abc 1\n', (ClassWith__str__('abc'), 1))
# # 2.x unicode tests
# x(u'1 2\n', ('1', u'2'))
# x(u'u\1234\n', (u'u\1234',))
# x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1))
# errors # errors
self.assertRaises(TypeError, print, '', sep=3) self.assertRaises(TypeError, print, '', sep=3)
self.assertRaises(TypeError, print, '', end=3) self.assertRaises(TypeError, print, '', end=3)
@ -113,12 +101,14 @@ class TestPrint(unittest.TestCase):
def test_print_flush(self): def test_print_flush(self):
# operation of the flush flag # operation of the flush flag
class filelike(): class filelike:
def __init__(self): def __init__(self):
self.written = '' self.written = ''
self.flushed = 0 self.flushed = 0
def write(self, str): def write(self, str):
self.written += str self.written += str
def flush(self): def flush(self):
self.flushed += 1 self.flushed += 1
@ -130,15 +120,13 @@ class TestPrint(unittest.TestCase):
self.assertEqual(f.flushed, 2) self.assertEqual(f.flushed, 2)
# ensure exceptions from flush are passed through # ensure exceptions from flush are passed through
class noflush(): class noflush:
def write(self, str): def write(self, str):
pass pass
def flush(self): def flush(self):
raise RuntimeError raise RuntimeError
self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True) self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)
def test_main():
support.run_unittest(TestPrint)
if __name__ == "__main__": if __name__ == "__main__":
test_main() unittest.main()