Skip to content

Commit dc0f294

Browse files
committed
add test
1 parent 2c0958f commit dc0f294

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

bpython/test/test_curtsies_repl.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import tempfile
88
import io
9+
from functools import partial
910
from contextlib import contextmanager
1011
from six.moves import StringIO
1112

@@ -284,6 +285,68 @@ def test_variable_is_cleared(self):
284285
self.assertNotIn('b', self.repl.interp.locals)
285286

286287

288+
class TestCurtsiesReevaluateWithImport(TestCase):
289+
def setUp(self):
290+
self.repl = create_repl()
291+
self.open = partial(io.open, mode='wt', encoding='utf-8')
292+
self.dont_write_bytecode = sys.dont_write_bytecode
293+
sys.dont_write_bytecode = True
294+
295+
def tearDown(self):
296+
sys.dont_write_bytecode = self.dont_write_bytecode
297+
298+
def push(self, line):
299+
self.repl._current_line = line
300+
self.repl.on_enter()
301+
302+
def head(self, path):
303+
self.push('import sys')
304+
self.push('sys.path.append("%s")' % (path))
305+
306+
@staticmethod
307+
@contextmanager
308+
def tempfile():
309+
with tempfile.NamedTemporaryFile(suffix='.py') as temp:
310+
path, name = os.path.split(temp.name)
311+
yield temp.name, path, name.replace('.py', '')
312+
313+
def test_module_content_changed(self):
314+
with self.tempfile() as (fullpath, path, modname):
315+
with self.open(fullpath) as f:
316+
f.write('a = 0\n')
317+
self.head(path)
318+
self.push('import %s' % (modname))
319+
self.push('a = %s.a' % (modname))
320+
self.assertIn('a', self.repl.interp.locals)
321+
self.assertEqual(self.repl.interp.locals['a'], 0)
322+
with self.open(fullpath) as f:
323+
f.write('a = 1\n')
324+
self.repl.clear_modules_and_reevaluate()
325+
self.assertIn('a', self.repl.interp.locals)
326+
self.assertEqual(self.repl.interp.locals['a'], 1)
327+
328+
def test_import_module_with_rewind(self):
329+
with self.tempfile() as (fullpath, path, modname):
330+
with self.open(fullpath) as f:
331+
f.write('a = 0\n')
332+
self.head(path)
333+
self.push('import %s' % (modname))
334+
self.assertIn(modname, self.repl.interp.locals)
335+
self.repl.undo()
336+
self.assertNotIn(modname, self.repl.interp.locals)
337+
self.repl.clear_modules_and_reevaluate()
338+
self.assertNotIn(modname, self.repl.interp.locals)
339+
self.push('import %s' % (modname))
340+
self.push('a = %s.a' % (modname))
341+
self.assertIn('a', self.repl.interp.locals)
342+
self.assertEqual(self.repl.interp.locals['a'], 0)
343+
with self.open(fullpath) as f:
344+
f.write('a = 1\n')
345+
self.repl.clear_modules_and_reevaluate()
346+
self.assertIn('a', self.repl.interp.locals)
347+
self.assertEqual(self.repl.interp.locals['a'], 1)
348+
349+
287350
class TestCurtsiesPagerText(TestCase):
288351

289352
def setUp(self):

0 commit comments

Comments
 (0)