blob: 4d347ed0fcaa23139355c74df9f993fa05bbd48f [file] [log] [blame]
Eric Smith799c3002008-03-18 15:35:27 +00001"""Test correct operation of the print function.
2"""
3
Eric Smith87824082008-03-20 23:02:08 +00004from __future__ import print_function
5
Eric Smith799c3002008-03-18 15:35:27 +00006import unittest
7from test import test_support
8
9import sys
Eric Smith01a3e2b2008-03-18 23:48:28 +000010try:
11 # 3.x
12 from io import StringIO
13except ImportError:
14 # 2.x
15 from StringIO import StringIO
16
Eric Smith799c3002008-03-18 15:35:27 +000017NotDefined = object()
18
19# A dispatch table all 8 combinations of providing
20# sep, end, and file
21# I use this machinery so that I'm not just passing default
22# values to print, I'm eiher passing or not passing in the
23# arguments
24dispatch = {
25 (False, False, False):
26 lambda args, sep, end, file: print(*args),
27 (False, False, True):
28 lambda args, sep, end, file: print(file=file, *args),
29 (False, True, False):
30 lambda args, sep, end, file: print(end=end, *args),
31 (False, True, True):
32 lambda args, sep, end, file: print(end=end, file=file, *args),
33 (True, False, False):
34 lambda args, sep, end, file: print(sep=sep, *args),
35 (True, False, True):
36 lambda args, sep, end, file: print(sep=sep, file=file, *args),
37 (True, True, False):
38 lambda args, sep, end, file: print(sep=sep, end=end, *args),
39 (True, True, True):
40 lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
41 }
42
Eric Smith799c3002008-03-18 15:35:27 +000043# Class used to test __str__ and print
44class ClassWith__str__:
45 def __init__(self, x):
46 self.x = x
47 def __str__(self):
48 return self.x
49
50class TestPrint(unittest.TestCase):
Eric Smith01a3e2b2008-03-18 23:48:28 +000051 def check(self, expected, args,
Eric Smith799c3002008-03-18 15:35:27 +000052 sep=NotDefined, end=NotDefined, file=NotDefined):
53 # Capture sys.stdout in a StringIO. Call print with args,
54 # and with sep, end, and file, if they're defined. Result
55 # must match expected.
56
57 # Look up the actual function to call, based on if sep, end, and file
58 # are defined
59 fn = dispatch[(sep is not NotDefined,
60 end is not NotDefined,
61 file is not NotDefined)]
62
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000063 with test_support.captured_stdout() as t:
Eric Smith799c3002008-03-18 15:35:27 +000064 fn(args, sep, end, file)
65
66 self.assertEqual(t.getvalue(), expected)
67
68 def test_print(self):
Eric Smith01a3e2b2008-03-18 23:48:28 +000069 def x(expected, args, sep=NotDefined, end=NotDefined):
Eric Smith799c3002008-03-18 15:35:27 +000070 # Run the test 2 ways: not using file, and using
71 # file directed to a StringIO
72
73 self.check(expected, args, sep=sep, end=end)
74
75 # When writing to a file, stdout is expected to be empty
Eric Smith01a3e2b2008-03-18 23:48:28 +000076 o = StringIO()
Eric Smith799c3002008-03-18 15:35:27 +000077 self.check('', args, sep=sep, end=end, file=o)
78
79 # And o will contain the expected output
80 self.assertEqual(o.getvalue(), expected)
81
82 x('\n', ())
83 x('a\n', ('a',))
84 x('None\n', (None,))
85 x('1 2\n', (1, 2))
Eric Smith01a3e2b2008-03-18 23:48:28 +000086 x('1 2\n', (1, ' ', 2))
Eric Smith799c3002008-03-18 15:35:27 +000087 x('1*2\n', (1, 2), sep='*')
88 x('1 s', (1, 's'), end='')
89 x('a\nb\n', ('a', 'b'), sep='\n')
90 x('1.01', (1.0, 1), sep='', end='')
91 x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
Eric Smith01a3e2b2008-03-18 23:48:28 +000092 x('a\n\nb\n', ('a\n', 'b'), sep='\n')
93 x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
94
95 x('a\n b\n', ('a\n', 'b'))
96 x('a\n b\n', ('a\n', 'b'), sep=None)
97 x('a\n b\n', ('a\n', 'b'), end=None)
98 x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
Eric Smith799c3002008-03-18 15:35:27 +000099
100 x('*\n', (ClassWith__str__('*'),))
Eric Smith01a3e2b2008-03-18 23:48:28 +0000101 x('abc 1\n', (ClassWith__str__('abc'), 1))
102
Eric Smith87824082008-03-20 23:02:08 +0000103# # 2.x unicode tests
104# x(u'1 2\n', ('1', u'2'))
105# x(u'u\1234\n', (u'u\1234',))
106# x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1))
107
Eric Smith01a3e2b2008-03-18 23:48:28 +0000108 # errors
109 self.assertRaises(TypeError, print, '', sep=3)
110 self.assertRaises(TypeError, print, '', end=3)
111 self.assertRaises(AttributeError, print, '', file='')
Eric Smith799c3002008-03-18 15:35:27 +0000112
113def test_main():
114 test_support.run_unittest(TestPrint)
115
116if __name__ == "__main__":
117 test_main()