blob: e9405c55da05d7ebbaf9a8f713fa77a3cb8a856c [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
Eric Smith7c478942008-03-18 23:45:49 +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 Smith7c478942008-03-18 23:45:49 +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):
51 def check(self, expected, args,
52 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
Eric Smithe5044452008-03-19 12:09:55 +000063 with test_support.captured_stdout() as t:
Eric Smith7c478942008-03-18 23:45:49 +000064 fn(args, sep, end, file)
65
66 self.assertEqual(t.getvalue(), expected)
67
68 def test_print(self):
69 def x(expected, args, sep=NotDefined, end=NotDefined):
70 # 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
76 o = StringIO()
77 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))
86 x('1 2\n', (1, ' ', 2))
87 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='+')
92 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)
99
100 x('*\n', (ClassWith__str__('*'),))
101 x('abc 1\n', (ClassWith__str__('abc'), 1))
102
103 # 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
108 # errors
109 self.assertRaises(TypeError, print, '', sep=3)
110 self.assertRaises(TypeError, print, '', end=3)
111 self.assertRaises(AttributeError, print, '', file='')
112
113def test_main():
114 test_support.run_unittest(TestPrint)
115
116if __name__ == "__main__":
117 test_main()