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