Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 1 | """Test correct operation of the print function. |
| 2 | """ |
| 3 | |
| 4 | import unittest |
| 5 | from test import test_support |
| 6 | |
| 7 | import sys |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 8 | try: |
| 9 | # 3.x |
| 10 | from io import StringIO |
| 11 | except ImportError: |
| 12 | # 2.x |
| 13 | from StringIO import StringIO |
| 14 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 15 | NotDefined = object() |
| 16 | |
| 17 | # A dispatch table all 8 combinations of providing |
| 18 | # sep, end, and file |
| 19 | # I use this machinery so that I'm not just passing default |
| 20 | # values to print, I'm eiher passing or not passing in the |
| 21 | # arguments |
| 22 | dispatch = { |
| 23 | (False, False, False): |
| 24 | lambda args, sep, end, file: print(*args), |
| 25 | (False, False, True): |
| 26 | lambda args, sep, end, file: print(file=file, *args), |
| 27 | (False, True, False): |
| 28 | lambda args, sep, end, file: print(end=end, *args), |
| 29 | (False, True, True): |
| 30 | lambda args, sep, end, file: print(end=end, file=file, *args), |
| 31 | (True, False, False): |
| 32 | lambda args, sep, end, file: print(sep=sep, *args), |
| 33 | (True, False, True): |
| 34 | lambda args, sep, end, file: print(sep=sep, file=file, *args), |
| 35 | (True, True, False): |
| 36 | lambda args, sep, end, file: print(sep=sep, end=end, *args), |
| 37 | (True, True, True): |
| 38 | lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args), |
| 39 | } |
| 40 | |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 41 | # Class used to test __str__ and print |
| 42 | class ClassWith__str__: |
| 43 | def __init__(self, x): |
| 44 | self.x = x |
| 45 | def __str__(self): |
| 46 | return self.x |
| 47 | |
| 48 | class TestPrint(unittest.TestCase): |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 49 | def check(self, expected, args, |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 50 | sep=NotDefined, end=NotDefined, file=NotDefined): |
| 51 | # Capture sys.stdout in a StringIO. Call print with args, |
| 52 | # and with sep, end, and file, if they're defined. Result |
| 53 | # must match expected. |
| 54 | |
| 55 | # Look up the actual function to call, based on if sep, end, and file |
| 56 | # are defined |
| 57 | fn = dispatch[(sep is not NotDefined, |
| 58 | end is not NotDefined, |
| 59 | file is not NotDefined)] |
| 60 | |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 61 | with test_support.captured_stdout() as t: |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 62 | fn(args, sep, end, file) |
| 63 | |
| 64 | self.assertEqual(t.getvalue(), expected) |
| 65 | |
| 66 | def test_print(self): |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 67 | def x(expected, args, sep=NotDefined, end=NotDefined): |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 68 | # Run the test 2 ways: not using file, and using |
| 69 | # file directed to a StringIO |
| 70 | |
| 71 | self.check(expected, args, sep=sep, end=end) |
| 72 | |
| 73 | # When writing to a file, stdout is expected to be empty |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 74 | o = StringIO() |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 75 | self.check('', args, sep=sep, end=end, file=o) |
| 76 | |
| 77 | # And o will contain the expected output |
| 78 | self.assertEqual(o.getvalue(), expected) |
| 79 | |
| 80 | x('\n', ()) |
| 81 | x('a\n', ('a',)) |
| 82 | x('None\n', (None,)) |
| 83 | x('1 2\n', (1, 2)) |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 84 | x('1 2\n', (1, ' ', 2)) |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 85 | x('1*2\n', (1, 2), sep='*') |
| 86 | x('1 s', (1, 's'), end='') |
| 87 | x('a\nb\n', ('a', 'b'), sep='\n') |
| 88 | x('1.01', (1.0, 1), sep='', end='') |
| 89 | x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+') |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 90 | x('a\n\nb\n', ('a\n', 'b'), sep='\n') |
| 91 | x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+') |
| 92 | |
| 93 | x('a\n b\n', ('a\n', 'b')) |
| 94 | x('a\n b\n', ('a\n', 'b'), sep=None) |
| 95 | x('a\n b\n', ('a\n', 'b'), end=None) |
| 96 | x('a\n b\n', ('a\n', 'b'), sep=None, end=None) |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 97 | |
| 98 | x('*\n', (ClassWith__str__('*'),)) |
Eric Smith | 01a3e2b | 2008-03-18 23:48:28 +0000 | [diff] [blame] | 99 | x('abc 1\n', (ClassWith__str__('abc'), 1)) |
| 100 | |
| 101 | # errors |
| 102 | self.assertRaises(TypeError, print, '', sep=3) |
| 103 | self.assertRaises(TypeError, print, '', end=3) |
| 104 | self.assertRaises(AttributeError, print, '', file='') |
Eric Smith | 799c300 | 2008-03-18 15:35:27 +0000 | [diff] [blame] | 105 | |
| 106 | def test_main(): |
| 107 | test_support.run_unittest(TestPrint) |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | test_main() |