##// END OF EJS Templates
Try to elide long completion based on user input....
Try to elide long completion based on user input. If what the user has typed is already in the completion and the completion is really long, try to elide what the user has already typed from the displayed text of the completion. Keep the first 3 and last 3 of what is already present. This will behave weirdly if all the completion have a common prefix as I believe prompt toolkit will insert the common prefix (or do we?). I'll have to check how to consider the common prefix as being typed.

File last commit:

r25476:a9d2a686
r25689:d5704fdc
Show More
test_interactivshell.py
175 lines | 5.9 KiB | text/x-python | PythonLexer
/ IPython / terminal / tests / test_interactivshell.py
Julian Taylor
add test case for readline multi-line history
r5177 # -*- coding: utf-8 -*-
Thomas Kluyver
Remove the readline shell machinery...
r22436 """Tests for the TerminalInteractiveShell and related pieces."""
Min RK
disable `_ipython_display_` in terminal IPython...
r23307 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Julian Taylor
add test case for readline multi-line history
r5177
MinRK
match rl encoding in frontend test
r7683 import sys
Julian Taylor
add test case for readline multi-line history
r5177 import unittest
Inception95
Pop the last if path ends with slash
r25476 import os
Julian Taylor
add test case for readline multi-line history
r5177
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 from IPython.core.inputtransformer import InputTransformer
mr.Shu
added test_paste_magics_message
r8974 from IPython.testing import tools as tt
Min RK
disable `_ipython_display_` in terminal IPython...
r23307 from IPython.utils.capture import capture_output
Julian Taylor
add test case for readline multi-line history
r5177
Steve Bartz
Do not duplicate '=' when tab completing a kwarg with the cursor on '='
r23622 from IPython.terminal.ptutils import _elide, _adjust_completion_text_based_on_context
Matthias Bussonnier
Fix Crash: Eliding string with consecutive dots....
r23591 import nose.tools as nt
class TestElide(unittest.TestCase):
def test_elide(self):
_elide('concatenate((a1, a2, ...), axis') # do not raise
_elide('concatenate((a1, a2, ..), . axis') # do not raise
nt.assert_equal(_elide('aaaa.bbbb.ccccc.dddddd.eeeee.fffff.gggggg.hhhhhh'), 'aaaa.b…g.hhhhhh')
Inception95
Pop the last if path ends with slash
r25476
test_string = os.sep.join(['', 10*'a', 10*'b', 10*'c', ''])
expect_stirng = os.sep + 'a' + '\N{HORIZONTAL ELLIPSIS}' + 'b' + os.sep + 10*'c'
nt.assert_equal(_elide(test_string), expect_stirng)
Matthias Bussonnier
Fix Crash: Eliding string with consecutive dots....
r23591
Steve Bartz
Do not duplicate '=' when tab completing a kwarg with the cursor on '='
r23622
class TestContextAwareCompletion(unittest.TestCase):
def test_adjust_completion_text_based_on_context(self):
# Adjusted case
nt.assert_equal(_adjust_completion_text_based_on_context('arg1=', 'func1(a=)', 7), 'arg1')
# Untouched cases
nt.assert_equal(_adjust_completion_text_based_on_context('arg1=', 'func1(a)', 7), 'arg1=')
nt.assert_equal(_adjust_completion_text_based_on_context('arg1=', 'func1(a', 7), 'arg1=')
nt.assert_equal(_adjust_completion_text_based_on_context('%magic', 'func1(a=)', 7), '%magic')
nt.assert_equal(_adjust_completion_text_based_on_context('func2', 'func1(a=)', 7), 'func2')
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 # Decorator for interaction loop tests -----------------------------------------
class mock_input_helper(object):
"""Machinery for tests of the main interact loop.
Used by the mock_input decorator.
"""
def __init__(self, testgen):
self.testgen = testgen
self.exception = None
self.ip = get_ipython()
def __enter__(self):
Thomas Kluyver
Remove the readline shell machinery...
r22436 self.orig_prompt_for_code = self.ip.prompt_for_code
self.ip.prompt_for_code = self.fake_input
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 return self
def __exit__(self, etype, value, tb):
Thomas Kluyver
Remove the readline shell machinery...
r22436 self.ip.prompt_for_code = self.orig_prompt_for_code
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528
Thomas Kluyver
Remove the readline shell machinery...
r22436 def fake_input(self):
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 try:
return next(self.testgen)
except StopIteration:
Thomas Kluyver
Remove the readline shell machinery...
r22436 self.ip.keep_running = False
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 return u''
except:
self.exception = sys.exc_info()
Thomas Kluyver
Remove the readline shell machinery...
r22436 self.ip.keep_running = False
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 return u''
def mock_input(testfunc):
"""Decorator for tests of the main interact loop.
Write the test as a generator, yield-ing the input strings, which IPython
will see as if they were typed in at the prompt.
"""
def test_method(self):
testgen = testfunc(self)
with mock_input_helper(testgen) as mih:
Thomas Kluyver
Remove the readline shell machinery...
r22436 mih.ip.interact()
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528
if mih.exception is not None:
# Re-raise captured exception
etype, value, tb = mih.exception
import traceback
traceback.print_tb(tb, file=sys.stdout)
del tb # Avoid reference loop
raise value
return test_method
# Test classes -----------------------------------------------------------------
Julian Taylor
add test case for readline multi-line history
r5177 class InteractiveShellTestCase(unittest.TestCase):
def rl_hist_entries(self, rl, n):
"""Get last n readline history entries as a list"""
return [rl.get_history_item(rl.get_current_history_length() - x)
for x in range(n - 1, -1, -1)]
mr.Shu
added test_paste_magics_message
r8974
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 @mock_input
def test_inputtransformer_syntaxerror(self):
ip = get_ipython()
Thomas Kluyver
Update transformer test in terminal test case
r24405 ip.input_transformers_post.append(syntax_error_transformer)
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528
try:
#raise Exception
with tt.AssertPrints('4', suppress=False):
yield u'print(2*2)'
with tt.AssertPrints('SyntaxError: input contains', suppress=False):
yield u'print(2345) # syntaxerror'
with tt.AssertPrints('16', suppress=False):
yield u'print(4*4)'
finally:
Thomas Kluyver
Update transformer test in terminal test case
r24405 ip.input_transformers_post.remove(syntax_error_transformer)
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528
Min RK
terminal IPython display formatter is plain-text-only...
r22530 def test_plain_text_only(self):
ip = get_ipython()
formatter = ip.display_formatter
assert formatter.active_types == ['text/plain']
Min RK
disable `_ipython_display_` in terminal IPython...
r23307 assert not formatter.ipython_display_formatter.enabled
class Test(object):
def __repr__(self):
return "<Test %i>" % id(self)
def _repr_html_(self):
return '<html>'
# verify that HTML repr isn't computed
obj = Test()
data, _ = formatter.format(obj)
self.assertEqual(data, {'text/plain': repr(obj)})
class Test2(Test):
def _ipython_display_(self):
from IPython.display import display
display('<custom>')
# verify that _ipython_display_ shortcut isn't called
obj = Test2()
with capture_output() as captured:
data, _ = formatter.format(obj)
self.assertEqual(data, {'text/plain': repr(obj)})
assert captured.stdout == ''
Thomas Kluyver
Update test for new transformation API
r24170 def syntax_error_transformer(lines):
"""Transformer that throws SyntaxError if 'syntaxerror' is in the code."""
for line in lines:
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 pos = line.find('syntaxerror')
if pos >= 0:
e = SyntaxError('input contains "syntaxerror"')
e.text = line
e.offset = pos + 1
raise e
Thomas Kluyver
Update test for new transformation API
r24170 return lines
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528
Thomas Kluyver
Failing test for gh-3246
r10752 class TerminalMagicsTestCase(unittest.TestCase):
def test_paste_magics_blankline(self):
"""Test that code with a blank line doesn't get split (gh-3246)."""
ip = get_ipython()
s = ('def pasted_func(a):\n'
' b = a+1\n'
'\n'
' return b')
tm = ip.magics_manager.registry['TerminalMagics']
tm.store_or_execute(s, name=None)
self.assertEqual(ip.user_ns['pasted_func'](54), 55)