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