Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 1 | import unittest |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 2 | from io import StringIO |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 3 | |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 4 | from test import support |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 5 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 6 | NotDefined = object() |
| 7 | |
| 8 | # A dispatch table all 8 combinations of providing |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 9 | # sep, end, and file. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 10 | # I use this machinery so that I'm not just passing default |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 11 | # values to print, I'm either passing or not passing in the |
| 12 | # arguments. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 13 | dispatch = { |
| 14 | (False, False, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 15 | lambda args, sep, end, file: print(*args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 16 | (False, False, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 17 | lambda args, sep, end, file: print(file=file, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 18 | (False, True, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 19 | lambda args, sep, end, file: print(end=end, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 20 | (False, True, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 21 | lambda args, sep, end, file: print(end=end, file=file, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 22 | (True, False, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 23 | lambda args, sep, end, file: print(sep=sep, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 24 | (True, False, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 25 | lambda args, sep, end, file: print(sep=sep, file=file, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 26 | (True, True, False): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 27 | lambda args, sep, end, file: print(sep=sep, end=end, *args), |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 28 | (True, True, True): |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 29 | lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), |
| 30 | } |
| 31 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 32 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 33 | # Class used to test __str__ and print |
| 34 | class ClassWith__str__: |
| 35 | def __init__(self, x): |
| 36 | self.x = x |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 37 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 38 | def __str__(self): |
| 39 | return self.x |
| 40 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 41 | |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 42 | class TestPrint(unittest.TestCase): |
| 43 | """Test correct operation of the print function.""" |
| 44 | |
| 45 | def check(self, expected, args, |
| 46 | sep=NotDefined, end=NotDefined, file=NotDefined): |
| 47 | # Capture sys.stdout in a StringIO. Call print with args, |
| 48 | # and with sep, end, and file, if they're defined. Result |
| 49 | # must match expected. |
| 50 | |
| 51 | # Look up the actual function to call, based on if sep, end, |
| 52 | # and file are defined. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 53 | fn = dispatch[(sep is not NotDefined, |
| 54 | end is not NotDefined, |
| 55 | file is not NotDefined)] |
| 56 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 57 | with support.captured_stdout() as t: |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 58 | fn(args, sep, end, file) |
| 59 | |
| 60 | self.assertEqual(t.getvalue(), expected) |
| 61 | |
| 62 | def test_print(self): |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 63 | def x(expected, args, sep=NotDefined, end=NotDefined): |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 64 | # Run the test 2 ways: not using file, and using |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 65 | # file directed to a StringIO. |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 66 | |
| 67 | self.check(expected, args, sep=sep, end=end) |
| 68 | |
| 69 | # When writing to a file, stdout is expected to be empty |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 70 | o = StringIO() |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 71 | self.check('', args, sep=sep, end=end, file=o) |
| 72 | |
| 73 | # And o will contain the expected output |
| 74 | self.assertEqual(o.getvalue(), expected) |
| 75 | |
| 76 | x('\n', ()) |
| 77 | x('a\n', ('a',)) |
| 78 | x('None\n', (None,)) |
| 79 | x('1 2\n', (1, 2)) |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 80 | x('1 2\n', (1, ' ', 2)) |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 81 | x('1*2\n', (1, 2), sep='*') |
| 82 | x('1 s', (1, 's'), end='') |
| 83 | x('a\nb\n', ('a', 'b'), sep='\n') |
| 84 | x('1.01', (1.0, 1), sep='', end='') |
| 85 | x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 86 | x('a\n\nb\n', ('a\n', 'b'), sep='\n') |
| 87 | x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') |
| 88 | |
| 89 | x('a\n b\n', ('a\n', 'b')) |
| 90 | x('a\n b\n', ('a\n', 'b'), sep=None) |
| 91 | x('a\n b\n', ('a\n', 'b'), end=None) |
| 92 | x('a\n b\n', ('a\n', 'b'), sep=None, end=None) |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 93 | |
| 94 | x('*\n', (ClassWith__str__('*'),)) |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 95 | x('abc 1\n', (ClassWith__str__('abc'), 1)) |
| 96 | |
| 97 | # errors |
| 98 | self.assertRaises(TypeError, print, '', sep=3) |
| 99 | self.assertRaises(TypeError, print, '', end=3) |
| 100 | self.assertRaises(AttributeError, print, '', file='') |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 101 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 102 | def test_print_flush(self): |
| 103 | # operation of the flush flag |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 104 | class filelike: |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 105 | def __init__(self): |
| 106 | self.written = '' |
| 107 | self.flushed = 0 |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 108 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 109 | def write(self, str): |
| 110 | self.written += str |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 111 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 112 | def flush(self): |
| 113 | self.flushed += 1 |
| 114 | |
| 115 | f = filelike() |
| 116 | print(1, file=f, end='', flush=True) |
| 117 | print(2, file=f, end='', flush=True) |
| 118 | print(3, file=f, flush=False) |
| 119 | self.assertEqual(f.written, '123\n') |
| 120 | self.assertEqual(f.flushed, 2) |
| 121 | |
| 122 | # ensure exceptions from flush are passed through |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 123 | class noflush: |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 124 | def write(self, str): |
| 125 | pass |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 126 | |
Georg Brandl | bc3b682 | 2012-01-13 19:41:25 +0100 | [diff] [blame] | 127 | def flush(self): |
| 128 | raise RuntimeError |
| 129 | self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True) |
| 130 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 131 | if __name__ == "__main__": |
Andrew Svetlov | ace34dd | 2013-04-04 22:32:28 +0300 | [diff] [blame] | 132 | unittest.main() |