blob: 10ef60a7efe8617b71d4e885257dc1d43a2a0692 [file] [log] [blame]
Eric Smith799c3002008-03-18 15:35:27 +00001"""Test correct operation of the print function.
2"""
3
4import unittest
5from test import test_support
6
7import sys
Eric Smith01a3e2b2008-03-18 23:48:28 +00008try:
9 # 3.x
10 from io import StringIO
11except ImportError:
12 # 2.x
13 from StringIO import StringIO
14
Eric Smith799c3002008-03-18 15:35:27 +000015NotDefined = object()
16
17# A dispatch table all 8 combinations of providing
18# sep, end, and file
19# I use this machinery so that I'm not just passing default
20# values to print, I'm eiher passing or not passing in the
21# arguments
22dispatch = {
23 (False, False, False):
24 lambda args, sep, end, file: print(*args),
25 (False, False, True):
26 lambda args, sep, end, file: print(file=file, *args),
27 (False, True, False):
28 lambda args, sep, end, file: print(end=end, *args),
29 (False, True, True):
30 lambda args, sep, end, file: print(end=end, file=file, *args),
31 (True, False, False):
32 lambda args, sep, end, file: print(sep=sep, *args),
33 (True, False, True):
34 lambda args, sep, end, file: print(sep=sep, file=file, *args),
35 (True, True, False):
36 lambda args, sep, end, file: print(sep=sep, end=end, *args),
37 (True, True, True):
38 lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
39 }
40
Eric Smith799c3002008-03-18 15:35:27 +000041# Class used to test __str__ and print
42class ClassWith__str__:
43 def __init__(self, x):
44 self.x = x
45 def __str__(self):
46 return self.x
47
48class TestPrint(unittest.TestCase):
Eric Smith01a3e2b2008-03-18 23:48:28 +000049 def check(self, expected, args,
Eric Smith799c3002008-03-18 15:35:27 +000050 sep=NotDefined, end=NotDefined, file=NotDefined):
51 # Capture sys.stdout in a StringIO. Call print with args,
52 # and with sep, end, and file, if they're defined. Result
53 # must match expected.
54
55 # Look up the actual function to call, based on if sep, end, and file
56 # are defined
57 fn = dispatch[(sep is not NotDefined,
58 end is not NotDefined,
59 file is not NotDefined)]
60
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000061 with test_support.captured_stdout() as t:
Eric Smith799c3002008-03-18 15:35:27 +000062 fn(args, sep, end, file)
63
64 self.assertEqual(t.getvalue(), expected)
65
66 def test_print(self):
Eric Smith01a3e2b2008-03-18 23:48:28 +000067 def x(expected, args, sep=NotDefined, end=NotDefined):
Eric Smith799c3002008-03-18 15:35:27 +000068 # Run the test 2 ways: not using file, and using
69 # file directed to a StringIO
70
71 self.check(expected, args, sep=sep, end=end)
72
73 # When writing to a file, stdout is expected to be empty
Eric Smith01a3e2b2008-03-18 23:48:28 +000074 o = StringIO()
Eric Smith799c3002008-03-18 15:35:27 +000075 self.check('', args, sep=sep, end=end, file=o)
76
77 # And o will contain the expected output
78 self.assertEqual(o.getvalue(), expected)
79
80 x('\n', ())
81 x('a\n', ('a',))
82 x('None\n', (None,))
83 x('1 2\n', (1, 2))
Eric Smith01a3e2b2008-03-18 23:48:28 +000084 x('1 2\n', (1, ' ', 2))
Eric Smith799c3002008-03-18 15:35:27 +000085 x('1*2\n', (1, 2), sep='*')
86 x('1 s', (1, 's'), end='')
87 x('a\nb\n', ('a', 'b'), sep='\n')
88 x('1.01', (1.0, 1), sep='', end='')
89 x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
Eric Smith01a3e2b2008-03-18 23:48:28 +000090 x('a\n\nb\n', ('a\n', 'b'), sep='\n')
91 x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
92
93 x('a\n b\n', ('a\n', 'b'))
94 x('a\n b\n', ('a\n', 'b'), sep=None)
95 x('a\n b\n', ('a\n', 'b'), end=None)
96 x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
Eric Smith799c3002008-03-18 15:35:27 +000097
98 x('*\n', (ClassWith__str__('*'),))
Eric Smith01a3e2b2008-03-18 23:48:28 +000099 x('abc 1\n', (ClassWith__str__('abc'), 1))
100
101 # errors
102 self.assertRaises(TypeError, print, '', sep=3)
103 self.assertRaises(TypeError, print, '', end=3)
104 self.assertRaises(AttributeError, print, '', file='')
Eric Smith799c3002008-03-18 15:35:27 +0000105
106def test_main():
107 test_support.run_unittest(TestPrint)
108
109if __name__ == "__main__":
110 test_main()