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