##// END OF EJS Templates
add parent to Configurable...
add parent to Configurable this adds the notion of a parent and member config, so the config: c.Foo.Bar.attr = value will only set `Bar.attr = value` for `Bar` instances which are members of `Foo` instances. The mechanism for doing this is ```python f = Foo(config=cfg) f.b = Bar(parent=f) ``` This Instance config has higher priority than plain class config for Bar, but still lower priority than direct keyword arg trait assignment. The main implication this has is to change the standard creation of descendants: ```python self.bar = Bar(config=self.config) ``` into a direct parent expression ```python self.bar = Bar(parent=self) ``` This also means that most Configurables will actually have a handle on their parent object.

File last commit:

r11010:72ecc660
r11062:7f44a560
Show More
test_interactivshell.py
203 lines | 8.3 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 -*-
"""Tests for the key interactiveshell module.
Authors
-------
* Julian Taylor
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# stdlib
MinRK
match rl encoding in frontend test
r7683 import sys
Julian Taylor
add test case for readline multi-line history
r5177 import unittest
from IPython.testing.decorators import skipif
MinRK
match encoding in rlhistory tests...
r7647 from IPython.utils import py3compat
mr.Shu
added test_paste_magics_message
r8974 from IPython.testing import tools as tt
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)]
def test_runs_without_rl(self):
"""Test that function does not throw without readline"""
ip = get_ipython()
ip.has_readline = False
ip.readline = None
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 ip._replace_rlhist_multiline(u'source', 0)
Julian Taylor
add test case for readline multi-line history
r5177
@skipif(not get_ipython().has_readline, 'no readline')
Julian Taylor
_replace_rlhist_multiline: check if remove_history_item is available
r5273 def test_runs_without_remove_history_item(self):
"""Test that function does not throw on windows without
remove_history_item"""
ip = get_ipython()
Julian Taylor
skip multiline history tests on windows...
r5274 if hasattr(ip.readline, 'remove_history_item'):
del ip.readline.remove_history_item
Julian Taylor
_replace_rlhist_multiline: check if remove_history_item is available
r5273 ip._replace_rlhist_multiline(u'source', 0)
@skipif(not get_ipython().has_readline, 'no readline')
Julian Taylor
skip multiline history tests on windows...
r5274 @skipif(not hasattr(get_ipython().readline, 'remove_history_item'),
'no remove_history_item')
Julian Taylor
add test case for readline multi-line history
r5177 def test_replace_multiline_hist_disabled(self):
"""Test that multiline replace does nothing if disabled"""
ip = get_ipython()
ip.multiline_history = False
ghist = [u'line1', u'line2']
for h in ghist:
ip.readline.add_history(h)
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip.readline.get_current_history_length()
hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2',
hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ip.readline.get_current_history_length(),
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177 hist = self.rl_hist_entries(ip.readline, 2)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(hist, ghist)
Julian Taylor
add test case for readline multi-line history
r5177
@skipif(not get_ipython().has_readline, 'no readline')
Julian Taylor
skip multiline history tests on windows...
r5274 @skipif(not hasattr(get_ipython().readline, 'remove_history_item'),
'no remove_history_item')
Julian Taylor
add test case for readline multi-line history
r5177 def test_replace_multiline_hist_adds(self):
"""Test that multiline replace function adds history"""
ip = get_ipython()
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip.readline.get_current_history_length()
hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€', hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(hlen_b4_cell,
Julian Taylor
add test case for readline multi-line history
r5177 ip.readline.get_current_history_length())
@skipif(not get_ipython().has_readline, 'no readline')
Julian Taylor
skip multiline history tests on windows...
r5274 @skipif(not hasattr(get_ipython().readline, 'remove_history_item'),
'no remove_history_item')
Julian Taylor
add test case for readline multi-line history
r5177 def test_replace_multiline_hist_keeps_history(self):
"""Test that multiline replace does not delete history"""
ip = get_ipython()
ip.multiline_history = True
ghist = [u'line1', u'line2']
for h in ghist:
ip.readline.add_history(h)
#start cell
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip.readline.get_current_history_length()
# nothing added to rl history, should do nothing
hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2',
hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ip.readline.get_current_history_length(),
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell)
Julian Taylor
don't try to replace history when rl history unchanged...
r5254 hist = self.rl_hist_entries(ip.readline, 2)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(hist, ghist)
Julian Taylor
add test case for readline multi-line history
r5177
@skipif(not get_ipython().has_readline, 'no readline')
Julian Taylor
skip multiline history tests on windows...
r5274 @skipif(not hasattr(get_ipython().readline, 'remove_history_item'),
'no remove_history_item')
Julian Taylor
add test case for readline multi-line history
r5177 def test_replace_multiline_hist_replaces_twice(self):
"""Test that multiline entries are replaced twice"""
ip = get_ipython()
ip.multiline_history = True
ip.readline.add_history(u'line0')
#start cell
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip.readline.get_current_history_length()
Julian Taylor
add test case for readline multi-line history
r5177 ip.readline.add_history('l€ne1')
ip.readline.add_history('line2')
#replace cell with single line
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2',
hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177 ip.readline.add_history('l€ne3')
ip.readline.add_history('line4')
#replace cell with single line
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3\nline4',
hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ip.readline.get_current_history_length(),
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell)
Julian Taylor
add test case for readline multi-line history
r5177 hist = self.rl_hist_entries(ip.readline, 3)
MinRK
match encoding in rlhistory tests...
r7647 expected = [u'line0', u'l€ne1\nline2', u'l€ne3\nline4']
# perform encoding, in case of casting due to ASCII locale
MinRK
match rl encoding in frontend test
r7683 enc = sys.stdin.encoding or "utf-8"
expected = [ py3compat.unicode_to_str(e, enc) for e in expected ]
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(hist, expected)
Julian Taylor
add a multiline history test with empty cell entries
r5195
@skipif(not get_ipython().has_readline, 'no readline')
Julian Taylor
skip multiline history tests on windows...
r5274 @skipif(not hasattr(get_ipython().readline, 'remove_history_item'),
'no remove_history_item')
Julian Taylor
add a multiline history test with empty cell entries
r5195 def test_replace_multiline_hist_replaces_empty_line(self):
"""Test that multiline history skips empty line cells"""
ip = get_ipython()
ip.multiline_history = True
ip.readline.add_history(u'line0')
#start cell
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip.readline.get_current_history_length()
Julian Taylor
add a multiline history test with empty cell entries
r5195 ip.readline.add_history('l€ne1')
ip.readline.add_history('line2')
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2',
hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195 ip.readline.add_history('')
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195 ip.readline.add_history('l€ne3')
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3', hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195 ip.readline.add_history(' ')
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195 ip.readline.add_history('\t')
ip.readline.add_history('\t ')
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline('\t', hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195 ip.readline.add_history('line4')
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell = ip._replace_rlhist_multiline(u'line4', hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(ip.readline.get_current_history_length(),
Julian Taylor
make hlen_before_cell a function argument instead of class member
r5269 hlen_b4_cell)
Julian Taylor
add a multiline history test with empty cell entries
r5195 hist = self.rl_hist_entries(ip.readline, 4)
# expect no empty cells in history
MinRK
match encoding in rlhistory tests...
r7647 expected = [u'line0', u'l€ne1\nline2', u'l€ne3', u'line4']
# perform encoding, in case of casting due to ASCII locale
MinRK
match rl encoding in frontend test
r7683 enc = sys.stdin.encoding or "utf-8"
expected = [ py3compat.unicode_to_str(e, enc) for e in expected ]
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(hist, expected)
mr.Shu
added test_paste_magics_message
r8974
Thomas Kluyver
Failing test for gh-3246
r10752 class TerminalMagicsTestCase(unittest.TestCase):
mr.Shu
added test_paste_magics_message
r8974 def test_paste_magics_message(self):
"""Test that an IndentationError while using paste magics doesn't
trigger a message about paste magics and also the opposite."""
ip = get_ipython()
Thomas Kluyver
Fix test for IndentationError message
r10149 s = ('for a in range(5):\n'
'print(a)')
mr.Shu
added test_paste_magics_message
r8974
mr.Shu
Added test for TerminalMagics
r8976 tm = ip.magics_manager.registry['TerminalMagics']
mr.Shu
added test_paste_magics_message
r8974 with tt.AssertPrints("If you want to paste code into IPython, try the "
"%paste and %cpaste magic functions."):
ip.run_cell(s)
mr.Shu
Added test for TerminalMagics
r8976
with tt.AssertNotPrints("If you want to paste code into IPython, try the "
"%paste and %cpaste magic functions."):
tm.store_or_execute(s, name=None)
Thomas Kluyver
Failing test for gh-3246
r10752
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)