1- import unittest
2- import sys
1+ import code
32import os
3+ import sys
4+ import tempfile
5+ from contextlib import contextmanager
6+ from StringIO import StringIO
7+
8+ import unittest
9+ try :
10+ from unittest import skip
11+ except ImportError :
12+ def skip (f ):
13+ return lambda self : None
14+
415py3 = (sys .version_info [0 ] == 3 )
516
6- from bpython .curtsiesfrontend import repl
17+ from bpython .curtsiesfrontend import repl as curtsiesrepl
718from bpython import config
19+ from bpython import args
820
921def setup_config (conf ):
1022 config_struct = config .Struct ()
@@ -18,11 +30,7 @@ def setup_config(conf):
1830class TestCurtsiesRepl (unittest .TestCase ):
1931
2032 def setUp (self ):
21- self .config = setup_config ({'editor' :'true' })
22- self .repl = repl .Repl (config = self .config )
23- os .environ ['PAGER' ] = 'true'
24- self .repl .width = 50
25- self .repl .height = 20
33+ self .repl = create_repl ()
2634
2735 def test_buffer_finished_will_parse (self ):
2836 self .repl .buffer = ['1 + 1' ]
@@ -43,6 +51,48 @@ def test_external_communication(self):
4351 self .repl .send_current_block_to_external_editor ()
4452 self .repl .send_session_to_external_editor ()
4553
54+ @contextmanager # from http://stackoverflow.com/a/17981937/398212 - thanks @rkennedy
55+ def captured_output ():
56+ new_out , new_err = StringIO (), StringIO ()
57+ old_out , old_err = sys .stdout , sys .stderr
58+ try :
59+ sys .stdout , sys .stderr = new_out , new_err
60+ yield sys .stdout , sys .stderr
61+ finally :
62+ sys .stdout , sys .stderr = old_out , old_err
63+
64+ def create_repl (** kwargs ):
65+ config = setup_config ({'editor' :'true' })
66+ repl = curtsiesrepl .Repl (config = config , ** kwargs )
67+ os .environ ['PAGER' ] = 'true'
68+ repl .width = 50
69+ repl .height = 20
70+ return repl
71+
72+ class TestFutureImports (unittest .TestCase ):
73+
74+ def test_repl (self ):
75+ repl = create_repl ()
76+ with captured_output () as (out , err ):
77+ repl .push ('from __future__ import division' )
78+ repl .push ('1 / 2' )
79+ self .assertEqual (out .getvalue (), '0.5\n ' )
80+
81+ @skip ('Failing - this is issue #369' )
82+ def test_interactive (self ):
83+ interp = code .InteractiveInterpreter (locals = {})
84+ with captured_output () as (out , err ):
85+ with tempfile .NamedTemporaryFile (suffix = '.py' ) as f :
86+ f .write ('from __future__ import division\n ' )
87+ f .write ('print 1/2\n ' )
88+ f .flush ()
89+ args .exec_code (interp , [f .name ])
90+
91+ repl = create_repl (interp = interp )
92+ repl .push ('1 / 2' )
93+
94+ self .assertEqual (out .getvalue (), '0.5\n 0.5\n ' )
95+
4696
4797if __name__ == '__main__' :
4898 unittest .main ()
0 commit comments