/Lib/test/test_print.py
Python | 142 lines | 111 code | 13 blank | 18 comment | 3 complexity | 4d28f9a2f652d3bacd525ad1f9d3bf7f MD5 | raw file
1"""Test correct operation of the print function. 2""" 3 4# In 2.6, this gives us the behavior we want. In 3.0, it has 5# no function, but it still must parse correctly. 6from __future__ import print_function 7 8import unittest 9from test import test_support 10 11import sys 12from StringIO import StringIO 13 14NotDefined = object() 15 16# A dispatch table all 8 combinations of providing 17# sep, end, and file 18# I use this machinery so that I'm not just passing default 19# values to print, I'm eiher passing or not passing in the 20# arguments 21dispatch = { 22 (False, False, False): 23 lambda args, sep, end, file: print(*args), 24 (False, False, True): 25 lambda args, sep, end, file: print(file=file, *args), 26 (False, True, False): 27 lambda args, sep, end, file: print(end=end, *args), 28 (False, True, True): 29 lambda args, sep, end, file: print(end=end, file=file, *args), 30 (True, False, False): 31 lambda args, sep, end, file: print(sep=sep, *args), 32 (True, False, True): 33 lambda args, sep, end, file: print(sep=sep, file=file, *args), 34 (True, True, False): 35 lambda args, sep, end, file: print(sep=sep, end=end, *args), 36 (True, True, True): 37 lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), 38 } 39 40# Class used to test __str__ and print 41class ClassWith__str__: 42 def __init__(self, x): 43 self.x = x 44 def __str__(self): 45 return self.x 46 47class TestPrint(unittest.TestCase): 48 def check(self, expected, args, 49 sep=NotDefined, end=NotDefined, file=NotDefined): 50 # Capture sys.stdout in a StringIO. Call print with args, 51 # and with sep, end, and file, if they're defined. Result 52 # must match expected. 53 54 # Look up the actual function to call, based on if sep, end, and file 55 # are defined 56 fn = dispatch[(sep is not NotDefined, 57 end is not NotDefined, 58 file is not NotDefined)] 59 60 with test_support.captured_stdout() as t: 61 fn(args, sep, end, file) 62 63 self.assertEqual(t.getvalue(), expected) 64 65 def test_print(self): 66 def x(expected, args, sep=NotDefined, end=NotDefined): 67 # Run the test 2 ways: not using file, and using 68 # file directed to a StringIO 69 70 self.check(expected, args, sep=sep, end=end) 71 72 # When writing to a file, stdout is expected to be empty 73 o = StringIO() 74 self.check('', args, sep=sep, end=end, file=o) 75 76 # And o will contain the expected output 77 self.assertEqual(o.getvalue(), expected) 78 79 x('\n', ()) 80 x('a\n', ('a',)) 81 x('None\n', (None,)) 82 x('1 2\n', (1, 2)) 83 x('1 2\n', (1, ' ', 2)) 84 x('1*2\n', (1, 2), sep='*') 85 x('1 s', (1, 's'), end='') 86 x('a\nb\n', ('a', 'b'), sep='\n') 87 x('1.01', (1.0, 1), sep='', end='') 88 x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') 89 x('a\n\nb\n', ('a\n', 'b'), sep='\n') 90 x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') 91 92 x('a\n b\n', ('a\n', 'b')) 93 x('a\n b\n', ('a\n', 'b'), sep=None) 94 x('a\n b\n', ('a\n', 'b'), end=None) 95 x('a\n b\n', ('a\n', 'b'), sep=None, end=None) 96 97 x('*\n', (ClassWith__str__('*'),)) 98 x('abc 1\n', (ClassWith__str__('abc'), 1)) 99 100 # 2.x unicode tests 101 x(u'1 2\n', ('1', u'2')) 102 x(u'u\1234\n', (u'u\1234',)) 103 x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1)) 104 105 # errors 106 self.assertRaises(TypeError, print, '', sep=3) 107 self.assertRaises(TypeError, print, '', end=3) 108 self.assertRaises(AttributeError, print, '', file='') 109 110 def test_mixed_args(self): 111 # If an unicode arg is passed, sep and end should be unicode, too. 112 class Recorder(object): 113 114 def __init__(self, must_be_unicode): 115 self.buf = [] 116 self.force_unicode = must_be_unicode 117 118 def write(self, what): 119 if self.force_unicode and not isinstance(what, unicode): 120 raise AssertionError("{0!r} is not unicode".format(what)) 121 self.buf.append(what) 122 123 buf = Recorder(True) 124 print(u'hi', file=buf) 125 self.assertEqual(u''.join(buf.buf), 'hi\n') 126 del buf.buf[:] 127 print(u'hi', u'nothing', file=buf) 128 self.assertEqual(u''.join(buf.buf), 'hi nothing\n') 129 buf = Recorder(False) 130 print('hi', 'bye', end=u'\n', file=buf) 131 self.assertTrue(isinstance(buf.buf[1], unicode)) 132 self.assertTrue(isinstance(buf.buf[3], unicode)) 133 del buf.buf[:] 134 print(sep=u'x', file=buf) 135 self.assertTrue(isinstance(buf.buf[-1], unicode)) 136 137 138def test_main(): 139 test_support.run_unittest(TestPrint) 140 141if __name__ == "__main__": 142 test_main()