blob: 9b177672b432f6e96e4c978af1ad7cb696b2aa40 [file] [log] [blame]
Nick Coghlan8bd24fe2012-08-20 23:02:28 +10001"Test InteractiveConsole and InteractiveInterpreter from code module"
2import sys
3import unittest
R David Murrayc31e6222014-09-29 11:25:00 -04004from textwrap import dedent
Nick Coghlan8bd24fe2012-08-20 23:02:28 +10005from contextlib import ExitStack
6from unittest import mock
7from test import support
8
9code = support.import_module('code')
10
11
12class TestInteractiveConsole(unittest.TestCase):
13
14 def setUp(self):
15 self.console = code.InteractiveConsole()
16 self.mock_sys()
17
18 def mock_sys(self):
19 "Mock system environment for InteractiveConsole"
20 # use exit stack to match patch context managers to addCleanup
21 stack = ExitStack()
22 self.addCleanup(stack.close)
23 self.infunc = stack.enter_context(mock.patch('code.input',
24 create=True))
25 self.stdout = stack.enter_context(mock.patch('code.sys.stdout'))
26 self.stderr = stack.enter_context(mock.patch('code.sys.stderr'))
27 prepatch = mock.patch('code.sys', wraps=code.sys, spec=code.sys)
28 self.sysmod = stack.enter_context(prepatch)
29 if sys.excepthook is sys.__excepthook__:
30 self.sysmod.excepthook = self.sysmod.__excepthook__
31
32 def test_ps1(self):
33 self.infunc.side_effect = EOFError('Finished')
34 self.console.interact()
35 self.assertEqual(self.sysmod.ps1, '>>> ')
36
37 def test_ps2(self):
38 self.infunc.side_effect = EOFError('Finished')
39 self.console.interact()
40 self.assertEqual(self.sysmod.ps2, '... ')
41
42 def test_console_stderr(self):
43 self.infunc.side_effect = ["'antioch'", "", EOFError('Finished')]
44 self.console.interact()
45 for call in list(self.stdout.method_calls):
46 if 'antioch' in ''.join(call[1]):
47 break
48 else:
49 raise AssertionError("no console stdout")
50
51 def test_syntax_error(self):
52 self.infunc.side_effect = ["undefined", EOFError('Finished')]
53 self.console.interact()
54 for call in self.stderr.method_calls:
doko@ubuntu.com15bec9c2014-05-13 11:28:12 +020055 if 'NameError' in ''.join(call[1]):
Nick Coghlan8bd24fe2012-08-20 23:02:28 +100056 break
57 else:
58 raise AssertionError("No syntax error from console")
59
60 def test_sysexcepthook(self):
61 self.infunc.side_effect = ["raise ValueError('')",
62 EOFError('Finished')]
63 hook = mock.Mock()
64 self.sysmod.excepthook = hook
65 self.console.interact()
66 self.assertTrue(hook.called)
67
Georg Brandlfbc3c3c2013-10-13 21:49:06 +020068 def test_banner(self):
69 # with banner
70 self.infunc.side_effect = EOFError('Finished')
71 self.console.interact(banner='Foo')
72 self.assertEqual(len(self.stderr.method_calls), 2)
73 banner_call = self.stderr.method_calls[0]
74 self.assertEqual(banner_call, ['write', ('Foo\n',), {}])
75
76 # no banner
77 self.stderr.reset_mock()
78 self.infunc.side_effect = EOFError('Finished')
79 self.console.interact(banner='')
80 self.assertEqual(len(self.stderr.method_calls), 1)
81
R David Murrayc31e6222014-09-29 11:25:00 -040082 def test_cause_tb(self):
83 self.infunc.side_effect = ["raise ValueError('') from AttributeError",
84 EOFError('Finished')]
85 self.console.interact()
86 output = ''.join(''.join(call[1]) for call in self.stderr.method_calls)
87 expected = dedent("""
88 AttributeError
89
90 The above exception was the direct cause of the following exception:
91
92 Traceback (most recent call last):
93 File "<console>", line 1, in <module>
94 ValueError
95 """)
96 self.assertIn(expected, output)
97
98 def test_context_tb(self):
99 self.infunc.side_effect = ["try: ham\nexcept: eggs\n",
100 EOFError('Finished')]
101 self.console.interact()
102 output = ''.join(''.join(call[1]) for call in self.stderr.method_calls)
103 expected = dedent("""
104 Traceback (most recent call last):
105 File "<console>", line 1, in <module>
106 NameError: name 'ham' is not defined
107
108 During handling of the above exception, another exception occurred:
109
110 Traceback (most recent call last):
111 File "<console>", line 2, in <module>
112 NameError: name 'eggs' is not defined
113 """)
114 self.assertIn(expected, output)
115
Nick Coghlan8bd24fe2012-08-20 23:02:28 +1000116
117def test_main():
118 support.run_unittest(TestInteractiveConsole)
119
120if __name__ == "__main__":
121 unittest.main()