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

Python code coverage for Lib/test/test_code_module.py

#countcontent
1n/a"Test InteractiveConsole and InteractiveInterpreter from code module"
2n/aimport sys
3n/aimport unittest
4n/afrom textwrap import dedent
5n/afrom contextlib import ExitStack
6n/afrom unittest import mock
7n/afrom test import support
8n/a
9n/acode = support.import_module('code')
10n/a
11n/a
12n/aclass TestInteractiveConsole(unittest.TestCase):
13n/a
14n/a def setUp(self):
15n/a self.console = code.InteractiveConsole()
16n/a self.mock_sys()
17n/a
18n/a def mock_sys(self):
19n/a "Mock system environment for InteractiveConsole"
20n/a # use exit stack to match patch context managers to addCleanup
21n/a stack = ExitStack()
22n/a self.addCleanup(stack.close)
23n/a self.infunc = stack.enter_context(mock.patch('code.input',
24n/a create=True))
25n/a self.stdout = stack.enter_context(mock.patch('code.sys.stdout'))
26n/a self.stderr = stack.enter_context(mock.patch('code.sys.stderr'))
27n/a prepatch = mock.patch('code.sys', wraps=code.sys, spec=code.sys)
28n/a self.sysmod = stack.enter_context(prepatch)
29n/a if sys.excepthook is sys.__excepthook__:
30n/a self.sysmod.excepthook = self.sysmod.__excepthook__
31n/a
32n/a def test_ps1(self):
33n/a self.infunc.side_effect = EOFError('Finished')
34n/a self.console.interact()
35n/a self.assertEqual(self.sysmod.ps1, '>>> ')
36n/a
37n/a def test_ps2(self):
38n/a self.infunc.side_effect = EOFError('Finished')
39n/a self.console.interact()
40n/a self.assertEqual(self.sysmod.ps2, '... ')
41n/a
42n/a def test_console_stderr(self):
43n/a self.infunc.side_effect = ["'antioch'", "", EOFError('Finished')]
44n/a self.console.interact()
45n/a for call in list(self.stdout.method_calls):
46n/a if 'antioch' in ''.join(call[1]):
47n/a break
48n/a else:
49n/a raise AssertionError("no console stdout")
50n/a
51n/a def test_syntax_error(self):
52n/a self.infunc.side_effect = ["undefined", EOFError('Finished')]
53n/a self.console.interact()
54n/a for call in self.stderr.method_calls:
55n/a if 'NameError' in ''.join(call[1]):
56n/a break
57n/a else:
58n/a raise AssertionError("No syntax error from console")
59n/a
60n/a def test_sysexcepthook(self):
61n/a self.infunc.side_effect = ["raise ValueError('')",
62n/a EOFError('Finished')]
63n/a hook = mock.Mock()
64n/a self.sysmod.excepthook = hook
65n/a self.console.interact()
66n/a self.assertTrue(hook.called)
67n/a
68n/a def test_banner(self):
69n/a # with banner
70n/a self.infunc.side_effect = EOFError('Finished')
71n/a self.console.interact(banner='Foo')
72n/a self.assertEqual(len(self.stderr.method_calls), 3)
73n/a banner_call = self.stderr.method_calls[0]
74n/a self.assertEqual(banner_call, ['write', ('Foo\n',), {}])
75n/a
76n/a # no banner
77n/a self.stderr.reset_mock()
78n/a self.infunc.side_effect = EOFError('Finished')
79n/a self.console.interact(banner='')
80n/a self.assertEqual(len(self.stderr.method_calls), 2)
81n/a
82n/a def test_exit_msg(self):
83n/a # default exit message
84n/a self.infunc.side_effect = EOFError('Finished')
85n/a self.console.interact(banner='')
86n/a self.assertEqual(len(self.stderr.method_calls), 2)
87n/a err_msg = self.stderr.method_calls[1]
88n/a expected = 'now exiting InteractiveConsole...\n'
89n/a self.assertEqual(err_msg, ['write', (expected,), {}])
90n/a
91n/a # no exit message
92n/a self.stderr.reset_mock()
93n/a self.infunc.side_effect = EOFError('Finished')
94n/a self.console.interact(banner='', exitmsg='')
95n/a self.assertEqual(len(self.stderr.method_calls), 1)
96n/a
97n/a # custom exit message
98n/a self.stderr.reset_mock()
99n/a message = (
100n/a 'bye! \N{GREEK SMALL LETTER ZETA}\N{CYRILLIC SMALL LETTER ZHE}'
101n/a )
102n/a self.infunc.side_effect = EOFError('Finished')
103n/a self.console.interact(banner='', exitmsg=message)
104n/a self.assertEqual(len(self.stderr.method_calls), 2)
105n/a err_msg = self.stderr.method_calls[1]
106n/a expected = message + '\n'
107n/a self.assertEqual(err_msg, ['write', (expected,), {}])
108n/a
109n/a
110n/a def test_cause_tb(self):
111n/a self.infunc.side_effect = ["raise ValueError('') from AttributeError",
112n/a EOFError('Finished')]
113n/a self.console.interact()
114n/a output = ''.join(''.join(call[1]) for call in self.stderr.method_calls)
115n/a expected = dedent("""
116n/a AttributeError
117n/a
118n/a The above exception was the direct cause of the following exception:
119n/a
120n/a Traceback (most recent call last):
121n/a File "<console>", line 1, in <module>
122n/a ValueError
123n/a """)
124n/a self.assertIn(expected, output)
125n/a
126n/a def test_context_tb(self):
127n/a self.infunc.side_effect = ["try: ham\nexcept: eggs\n",
128n/a EOFError('Finished')]
129n/a self.console.interact()
130n/a output = ''.join(''.join(call[1]) for call in self.stderr.method_calls)
131n/a expected = dedent("""
132n/a Traceback (most recent call last):
133n/a File "<console>", line 1, in <module>
134n/a NameError: name 'ham' is not defined
135n/a
136n/a During handling of the above exception, another exception occurred:
137n/a
138n/a Traceback (most recent call last):
139n/a File "<console>", line 2, in <module>
140n/a NameError: name 'eggs' is not defined
141n/a """)
142n/a self.assertIn(expected, output)
143n/a
144n/a
145n/aif __name__ == "__main__":
146n/a unittest.main()