forked from bpython/bpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_curtsies_repl.py
More file actions
98 lines (81 loc) · 3.23 KB
/
test_curtsies_repl.py
File metadata and controls
98 lines (81 loc) · 3.23 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
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_buffer_finished_will_parse(self):
self.repl.buffer = ['1 + 1']
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, True))
self.repl.buffer = ['def foo(x):']
self.assertTrue(self.repl.buffer_finished_will_parse(), (False, True))
self.repl.buffer = ['def foo(x)']
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, False))
self.repl.buffer = ['def foo(x):', 'return 1']
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, False))
self.repl.buffer = ['def foo(x):', ' return 1']
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, True))
self.repl.buffer = ['def foo(x):', ' return 1', '']
self.assertTrue(self.repl.buffer_finished_will_parse(), (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()
@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')
@skip('Failing - this is issue #369')
def test_interactive(self):
interp = code.InteractiveInterpreter(locals={})
with captured_output() as (out, err):
with tempfile.NamedTemporaryFile(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')
if __name__ == '__main__':
unittest.main()