ยปCore Development>Code coverage>Lib/test/test_print.py

Python code coverage for Lib/test/test_print.py

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