-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathtest_curtsies_repl.py
More file actions
140 lines (117 loc) · 5.11 KB
/
test_curtsies_repl.py
File metadata and controls
140 lines (117 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# coding: utf8
import code
import os
import sys
import tempfile
from contextlib import contextmanager
from StringIO import StringIO
import unittest
try:
from unittest import skip
except ImportError:
def skip(f):
return lambda self: None
py3 = (sys.version_info[0] == 3)
from bpython.curtsiesfrontend import repl as curtsiesrepl
from bpython import config
from bpython import args
def setup_config(conf):
config_struct = config.Struct()
config.loadini(config_struct, os.devnull)
for key, value in conf.items():
if not hasattr(config_struct, key):
raise ValueError("%r is not a valid config attribute" % (key,))
setattr(config_struct, key, value)
return config_struct
class TestCurtsiesRepl(unittest.TestCase):
def setUp(self):
self.repl = create_repl()
def test_code_finished_will_parse(self):
self.repl.buffer = ['1 + 1']
self.assertTrue(curtsiesrepl.code_finished_will_parse('\n'.join(self.repl.buffer)), (True, True))
self.repl.buffer = ['def foo(x):']
self.assertTrue(curtsiesrepl.code_finished_will_parse('\n'.join(self.repl.buffer)), (False, True))
self.repl.buffer = ['def foo(x)']
self.assertTrue(curtsiesrepl.code_finished_will_parse('\n'.join(self.repl.buffer)), (True, False))
self.repl.buffer = ['def foo(x):', 'return 1']
self.assertTrue(curtsiesrepl.code_finished_will_parse('\n'.join(self.repl.buffer)), (True, False))
self.repl.buffer = ['def foo(x):', ' return 1']
self.assertTrue(curtsiesrepl.code_finished_will_parse('\n'.join(self.repl.buffer)), (True, True))
self.repl.buffer = ['def foo(x):', ' return 1', '']
self.assertTrue(curtsiesrepl.code_finished_will_parse('\n'.join(self.repl.buffer)), (True, True))
def test_external_communication(self):
self.assertEqual(type(self.repl.help_text()), type(b''))
self.repl.send_current_block_to_external_editor()
self.repl.send_session_to_external_editor()
def test_external_communication_encoding(self):
with captured_output():
self.repl.display_lines.append(u'>>> "åß∂ƒ"')
self.repl.send_session_to_external_editor()
def test_get_last_word(self):
self.repl.rl_history.entries=['1','2 3','4 5 6']
self.repl._set_current_line('abcde')
self.repl.get_last_word()
self.assertEqual(self.repl.current_line,'abcde6')
self.repl.get_last_word()
self.assertEqual(self.repl.current_line,'abcde3')
@skip # this is the behavior of bash - not currently implemented
def test_get_last_word_with_prev_line(self):
self.repl.rl_history.entries=['1','2 3','4 5 6']
self.repl._set_current_line('abcde')
self.repl.up_one_line()
self.assertEqual(self.repl.current_line,'4 5 6')
self.repl.get_last_word()
self.assertEqual(self.repl.current_line,'4 5 63')
self.repl.get_last_word()
self.assertEqual(self.repl.current_line,'4 5 64')
self.repl.up_one_line()
self.assertEqual(self.repl.current_line,'2 3')
@contextmanager # from http://stackoverflow.com/a/17981937/398212 - thanks @rkennedy
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
def create_repl(**kwargs):
config = setup_config({'editor':'true'})
repl = curtsiesrepl.Repl(config=config, **kwargs)
os.environ['PAGER'] = 'true'
repl.width = 50
repl.height = 20
return repl
class TestFutureImports(unittest.TestCase):
def test_repl(self):
repl = create_repl()
with captured_output() as (out, err):
repl.push('from __future__ import division')
repl.push('1 / 2')
self.assertEqual(out.getvalue(), '0.5\n')
def test_interactive(self):
interp = code.InteractiveInterpreter(locals={})
with captured_output() as (out, err):
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as f:
f.write('from __future__ import division\n')
f.write('print(1/2)\n')
f.flush()
args.exec_code(interp, [f.name])
repl = create_repl(interp=interp)
repl.push('1 / 2')
self.assertEqual(out.getvalue(), '0.5\n0.5\n')
class TestPredictedIndent(unittest.TestCase):
def setUp(self):
self.repl = create_repl()
def test_simple(self):
self.assertEqual(self.repl.predicted_indent(''), 0)
self.assertEqual(self.repl.predicted_indent('class Foo:'), 4)
self.assertEqual(self.repl.predicted_indent('class Foo: pass'), 0)
self.assertEqual(self.repl.predicted_indent('def asdf():'), 4)
self.assertEqual(self.repl.predicted_indent('def asdf(): return 7'), 0)
@skip
def test_complex(self):
self.assertEqual(self.repl.predicted_indent('[a,'), 1)
self.assertEqual(self.repl.predicted_indent('reduce(asdfasdf,'), 7)
if __name__ == '__main__':
unittest.main()