blob: 7eea349115a9471cbfdfd01712398d9fbe9aacd5 [file] [log] [blame]
Eric Smith799c3002008-03-18 15:35:27 +00001import unittest
Andrew Svetlovace34dd2013-04-04 22:32:28 +03002from io import StringIO
Eric Smith799c3002008-03-18 15:35:27 +00003
Andrew Svetlovace34dd2013-04-04 22:32:28 +03004from test import support
Eric Smith01a3e2b2008-03-18 23:48:28 +00005
Eric Smith799c3002008-03-18 15:35:27 +00006NotDefined = object()
7
8# A dispatch table all 8 combinations of providing
Andrew Svetlovace34dd2013-04-04 22:32:28 +03009# sep, end, and file.
Eric Smith799c3002008-03-18 15:35:27 +000010# I use this machinery so that I'm not just passing default
Andrew Svetlovace34dd2013-04-04 22:32:28 +030011# values to print, I'm either passing or not passing in the
12# arguments.
Eric Smith799c3002008-03-18 15:35:27 +000013dispatch = {
14 (False, False, False):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030015 lambda args, sep, end, file: print(*args),
Eric Smith799c3002008-03-18 15:35:27 +000016 (False, False, True):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030017 lambda args, sep, end, file: print(file=file, *args),
Eric Smith799c3002008-03-18 15:35:27 +000018 (False, True, False):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030019 lambda args, sep, end, file: print(end=end, *args),
Eric Smith799c3002008-03-18 15:35:27 +000020 (False, True, True):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030021 lambda args, sep, end, file: print(end=end, file=file, *args),
Eric Smith799c3002008-03-18 15:35:27 +000022 (True, False, False):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030023 lambda args, sep, end, file: print(sep=sep, *args),
Eric Smith799c3002008-03-18 15:35:27 +000024 (True, False, True):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030025 lambda args, sep, end, file: print(sep=sep, file=file, *args),
Eric Smith799c3002008-03-18 15:35:27 +000026 (True, True, False):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030027 lambda args, sep, end, file: print(sep=sep, end=end, *args),
Eric Smith799c3002008-03-18 15:35:27 +000028 (True, True, True):
Andrew Svetlovace34dd2013-04-04 22:32:28 +030029 lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
30}
31
Eric Smith799c3002008-03-18 15:35:27 +000032
Eric Smith799c3002008-03-18 15:35:27 +000033# Class used to test __str__ and print
34class ClassWith__str__:
35 def __init__(self, x):
36 self.x = x
Andrew Svetlovace34dd2013-04-04 22:32:28 +030037
Eric Smith799c3002008-03-18 15:35:27 +000038 def __str__(self):
39 return self.x
40
Eric Smith799c3002008-03-18 15:35:27 +000041
Andrew Svetlovace34dd2013-04-04 22:32:28 +030042class TestPrint(unittest.TestCase):
43 """Test correct operation of the print function."""
44
45 def check(self, expected, args,
46 sep=NotDefined, end=NotDefined, file=NotDefined):
47 # Capture sys.stdout in a StringIO. Call print with args,
48 # and with sep, end, and file, if they're defined. Result
49 # must match expected.
50
51 # Look up the actual function to call, based on if sep, end,
52 # and file are defined.
Eric Smith799c3002008-03-18 15:35:27 +000053 fn = dispatch[(sep is not NotDefined,
54 end is not NotDefined,
55 file is not NotDefined)]
56
Benjamin Petersonee8712c2008-05-20 21:35:26 +000057 with support.captured_stdout() as t:
Eric Smith799c3002008-03-18 15:35:27 +000058 fn(args, sep, end, file)
59
60 self.assertEqual(t.getvalue(), expected)
61
62 def test_print(self):
Eric Smith01a3e2b2008-03-18 23:48:28 +000063 def x(expected, args, sep=NotDefined, end=NotDefined):
Eric Smith799c3002008-03-18 15:35:27 +000064 # Run the test 2 ways: not using file, and using
Andrew Svetlovace34dd2013-04-04 22:32:28 +030065 # file directed to a StringIO.
Eric Smith799c3002008-03-18 15:35:27 +000066
67 self.check(expected, args, sep=sep, end=end)
68
69 # When writing to a file, stdout is expected to be empty
Eric Smith01a3e2b2008-03-18 23:48:28 +000070 o = StringIO()
Eric Smith799c3002008-03-18 15:35:27 +000071 self.check('', args, sep=sep, end=end, file=o)
72
73 # And o will contain the expected output
74 self.assertEqual(o.getvalue(), expected)
75
76 x('\n', ())
77 x('a\n', ('a',))
78 x('None\n', (None,))
79 x('1 2\n', (1, 2))
Eric Smith01a3e2b2008-03-18 23:48:28 +000080 x('1 2\n', (1, ' ', 2))
Eric Smith799c3002008-03-18 15:35:27 +000081 x('1*2\n', (1, 2), sep='*')
82 x('1 s', (1, 's'), end='')
83 x('a\nb\n', ('a', 'b'), sep='\n')
84 x('1.01', (1.0, 1), sep='', end='')
85 x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
Eric Smith01a3e2b2008-03-18 23:48:28 +000086 x('a\n\nb\n', ('a\n', 'b'), sep='\n')
87 x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
88
89 x('a\n b\n', ('a\n', 'b'))
90 x('a\n b\n', ('a\n', 'b'), sep=None)
91 x('a\n b\n', ('a\n', 'b'), end=None)
92 x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
Eric Smith799c3002008-03-18 15:35:27 +000093
94 x('*\n', (ClassWith__str__('*'),))
Eric Smith01a3e2b2008-03-18 23:48:28 +000095 x('abc 1\n', (ClassWith__str__('abc'), 1))
96
97 # errors
98 self.assertRaises(TypeError, print, '', sep=3)
99 self.assertRaises(TypeError, print, '', end=3)
100 self.assertRaises(AttributeError, print, '', file='')
Eric Smith799c3002008-03-18 15:35:27 +0000101
Georg Brandlbc3b6822012-01-13 19:41:25 +0100102 def test_print_flush(self):
103 # operation of the flush flag
Andrew Svetlovace34dd2013-04-04 22:32:28 +0300104 class filelike:
Georg Brandlbc3b6822012-01-13 19:41:25 +0100105 def __init__(self):
106 self.written = ''
107 self.flushed = 0
Andrew Svetlovace34dd2013-04-04 22:32:28 +0300108
Georg Brandlbc3b6822012-01-13 19:41:25 +0100109 def write(self, str):
110 self.written += str
Andrew Svetlovace34dd2013-04-04 22:32:28 +0300111
Georg Brandlbc3b6822012-01-13 19:41:25 +0100112 def flush(self):
113 self.flushed += 1
114
115 f = filelike()
116 print(1, file=f, end='', flush=True)
117 print(2, file=f, end='', flush=True)
118 print(3, file=f, flush=False)
119 self.assertEqual(f.written, '123\n')
120 self.assertEqual(f.flushed, 2)
121
122 # ensure exceptions from flush are passed through
Andrew Svetlovace34dd2013-04-04 22:32:28 +0300123 class noflush:
Georg Brandlbc3b6822012-01-13 19:41:25 +0100124 def write(self, str):
125 pass
Andrew Svetlovace34dd2013-04-04 22:32:28 +0300126
Georg Brandlbc3b6822012-01-13 19:41:25 +0100127 def flush(self):
128 raise RuntimeError
129 self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)
130
Eric Smith799c3002008-03-18 15:35:27 +0000131if __name__ == "__main__":
Andrew Svetlovace34dd2013-04-04 22:32:28 +0300132 unittest.main()