blob: db09c9c982dd2517fe9339538e508d7b8622f264 [file] [log] [blame]
Eric Smith7c478942008-03-18 23:45:49 +00001"""Test correct operation of the print function.
2"""
3
4from __future__ import print_function
5
6import unittest
7from test import test_support
8
9import sys
10try:
11 # 3.x
12 from io import StringIO
13except ImportError:
14 # 2.x
15 from StringIO import StringIO
16
17from contextlib import contextmanager
18
19NotDefined = object()
20
21# A dispatch table all 8 combinations of providing
22# sep, end, and file
23# I use this machinery so that I'm not just passing default
24# values to print, I'm eiher passing or not passing in the
25# arguments
26dispatch = {
27 (False, False, False):
28 lambda args, sep, end, file: print(*args),
29 (False, False, True):
30 lambda args, sep, end, file: print(file=file, *args),
31 (False, True, False):
32 lambda args, sep, end, file: print(end=end, *args),
33 (False, True, True):
34 lambda args, sep, end, file: print(end=end, file=file, *args),
35 (True, False, False):
36 lambda args, sep, end, file: print(sep=sep, *args),
37 (True, False, True):
38 lambda args, sep, end, file: print(sep=sep, file=file, *args),
39 (True, True, False):
40 lambda args, sep, end, file: print(sep=sep, end=end, *args),
41 (True, True, True):
42 lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
43 }
44
45@contextmanager
46def stdout_redirected(new_stdout):
47 save_stdout = sys.stdout
48 sys.stdout = new_stdout
49 try:
50 yield None
51 finally:
52 sys.stdout = save_stdout
53
54# Class used to test __str__ and print
55class ClassWith__str__:
56 def __init__(self, x):
57 self.x = x
58 def __str__(self):
59 return self.x
60
61class TestPrint(unittest.TestCase):
62 def check(self, expected, args,
63 sep=NotDefined, end=NotDefined, file=NotDefined):
64 # Capture sys.stdout in a StringIO. Call print with args,
65 # and with sep, end, and file, if they're defined. Result
66 # must match expected.
67
68 # Look up the actual function to call, based on if sep, end, and file
69 # are defined
70 fn = dispatch[(sep is not NotDefined,
71 end is not NotDefined,
72 file is not NotDefined)]
73
74 t = StringIO()
75 with stdout_redirected(t):
76 fn(args, sep, end, file)
77
78 self.assertEqual(t.getvalue(), expected)
79
80 def test_print(self):
81 def x(expected, args, sep=NotDefined, end=NotDefined):
82 # Run the test 2 ways: not using file, and using
83 # file directed to a StringIO
84
85 self.check(expected, args, sep=sep, end=end)
86
87 # When writing to a file, stdout is expected to be empty
88 o = StringIO()
89 self.check('', args, sep=sep, end=end, file=o)
90
91 # And o will contain the expected output
92 self.assertEqual(o.getvalue(), expected)
93
94 x('\n', ())
95 x('a\n', ('a',))
96 x('None\n', (None,))
97 x('1 2\n', (1, 2))
98 x('1 2\n', (1, ' ', 2))
99 x('1*2\n', (1, 2), sep='*')
100 x('1 s', (1, 's'), end='')
101 x('a\nb\n', ('a', 'b'), sep='\n')
102 x('1.01', (1.0, 1), sep='', end='')
103 x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
104 x('a\n\nb\n', ('a\n', 'b'), sep='\n')
105 x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
106
107 x('a\n b\n', ('a\n', 'b'))
108 x('a\n b\n', ('a\n', 'b'), sep=None)
109 x('a\n b\n', ('a\n', 'b'), end=None)
110 x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
111
112 x('*\n', (ClassWith__str__('*'),))
113 x('abc 1\n', (ClassWith__str__('abc'), 1))
114
115 # 2.x unicode tests
116 x(u'1 2\n', ('1', u'2'))
117 x(u'u\1234\n', (u'u\1234',))
118 x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1))
119
120 # errors
121 self.assertRaises(TypeError, print, '', sep=3)
122 self.assertRaises(TypeError, print, '', end=3)
123 self.assertRaises(AttributeError, print, '', file='')
124
125def test_main():
126 test_support.run_unittest(TestPrint)
127
128if __name__ == "__main__":
129 test_main()