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