test_prefilterfrontend.py
252 lines
| 6.7 KiB
| text/x-python
|
PythonLexer
Gael Varoquaux
|
r1458 | # encoding: utf-8 | ||
""" | ||||
Test process execution and IO redirection. | ||||
""" | ||||
__docformat__ = "restructuredtext en" | ||||
#------------------------------------------------------------------------------- | ||||
# Copyright (C) 2008 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. | ||||
#------------------------------------------------------------------------------- | ||||
Fernando Perez
|
r1966 | from copy import copy, deepcopy | ||
Gael Varoquaux
|
r1458 | from cStringIO import StringIO | ||
import string | ||||
Gael Varoquaux
|
r1505 | |||
Gael Varoquaux
|
r1887 | from nose.tools import assert_equal | ||
Gael Varoquaux
|
r1505 | from IPython.frontend.prefilterfrontend import PrefilterFrontEnd | ||
Brian Granger
|
r2027 | from IPython.core.ipapi import get as get_ipython0 | ||
Fernando Perez
|
r1966 | from IPython.testing.plugin.ipdoctest import default_argv | ||
Gael Varoquaux
|
r1887 | |||
def safe_deepcopy(d): | ||||
""" Deep copy every key of the given dict, when possible. Elsewhere | ||||
do a copy. | ||||
""" | ||||
copied_d = dict() | ||||
for key, value in d.iteritems(): | ||||
try: | ||||
copied_d[key] = deepcopy(value) | ||||
except: | ||||
try: | ||||
copied_d[key] = copy(value) | ||||
except: | ||||
copied_d[key] = value | ||||
return copied_d | ||||
Gael Varoquaux
|
r1458 | |||
class TestPrefilterFrontEnd(PrefilterFrontEnd): | ||||
input_prompt_template = string.Template('') | ||||
output_prompt_template = string.Template('') | ||||
Gael Varoquaux
|
r1505 | banner = '' | ||
Gael Varoquaux
|
r1458 | |||
gvaroquaux
|
r1460 | def __init__(self): | ||
Gael Varoquaux
|
r1458 | self.out = StringIO() | ||
Fernando Perez
|
r1966 | PrefilterFrontEnd.__init__(self,argv=default_argv()) | ||
Gael Varoquaux
|
r1505 | # Some more code for isolation (yeah, crazy) | ||
self._on_enter() | ||||
self.out.flush() | ||||
self.out.reset() | ||||
self.out.truncate() | ||||
def write(self, string, *args, **kwargs): | ||||
Gael Varoquaux
|
r1458 | self.out.write(string) | ||
def _on_enter(self): | ||||
gvaroquaux
|
r1462 | self.input_buffer += '\n' | ||
Gael Varoquaux
|
r1458 | PrefilterFrontEnd._on_enter(self) | ||
gvaroquaux
|
r1531 | def isolate_ipython0(func): | ||
""" Decorator to isolate execution that involves an iptyhon0. | ||||
Gael Varoquaux
|
r1887 | |||
Notes | ||||
Fernando Perez
|
r1966 | ----- | ||
Gael Varoquaux
|
r1887 | |||
Apply only to functions with no arguments. Nose skips functions | ||||
with arguments. | ||||
gvaroquaux
|
r1531 | """ | ||
Gael Varoquaux
|
r1887 | def my_func(): | ||
iplib = get_ipython0() | ||||
if iplib is None: | ||||
return func() | ||||
ipython0 = iplib.IP | ||||
global_ns = safe_deepcopy(ipython0.user_global_ns) | ||||
user_ns = safe_deepcopy(ipython0.user_ns) | ||||
Gael Varoquaux
|
r1535 | try: | ||
Gael Varoquaux
|
r1887 | out = func() | ||
Gael Varoquaux
|
r1535 | finally: | ||
ipython0.user_ns = user_ns | ||||
Gael Varoquaux
|
r1887 | ipython0.user_global_ns = global_ns | ||
# Undo the hack at creation of PrefilterFrontEnd | ||||
Brian Granger
|
r2058 | from IPython.core import iplib | ||
Gael Varoquaux
|
r1887 | iplib.InteractiveShell.isthreaded = False | ||
return out | ||||
gvaroquaux
|
r1531 | |||
Gael Varoquaux
|
r1887 | my_func.__name__ = func.__name__ | ||
gvaroquaux
|
r1531 | return my_func | ||
@isolate_ipython0 | ||||
Gael Varoquaux
|
r1458 | def test_execution(): | ||
""" Test execution of a command. | ||||
""" | ||||
gvaroquaux
|
r1460 | f = TestPrefilterFrontEnd() | ||
Gael Varoquaux
|
r1505 | f.input_buffer = 'print 1' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1887 | assert_equal(out_value, '1\n') | ||
Gael Varoquaux
|
r1458 | |||
gvaroquaux
|
r1531 | @isolate_ipython0 | ||
Gael Varoquaux
|
r1458 | def test_multiline(): | ||
""" Test execution of a multiline command. | ||||
""" | ||||
gvaroquaux
|
r1460 | f = TestPrefilterFrontEnd() | ||
gvaroquaux
|
r1462 | f.input_buffer = 'if True:' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
gvaroquaux
|
r1462 | f.input_buffer += 'print 1' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '1\n' | ||
gvaroquaux
|
r1460 | f = TestPrefilterFrontEnd() | ||
gvaroquaux
|
r1462 | f.input_buffer='(1 +' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
gvaroquaux
|
r1462 | f.input_buffer += '0)' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '1\n' | ||
Gael Varoquaux
|
r1458 | |||
gvaroquaux
|
r1531 | @isolate_ipython0 | ||
Gael Varoquaux
|
r1458 | def test_capture(): | ||
""" Test the capture of output in different channels. | ||||
""" | ||||
gvaroquaux
|
r1460 | # Test on the OS-level stdout, stderr. | ||
f = TestPrefilterFrontEnd() | ||||
gvaroquaux
|
r1462 | f.input_buffer = \ | ||
'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()' | ||||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '1' | ||
gvaroquaux
|
r1460 | f = TestPrefilterFrontEnd() | ||
gvaroquaux
|
r1462 | f.input_buffer = \ | ||
'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()' | ||||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '1' | ||
gvaroquaux
|
r1460 | |||
Gael Varoquaux
|
r1458 | |||
gvaroquaux
|
r1531 | @isolate_ipython0 | ||
gvaroquaux
|
r1460 | def test_magic(): | ||
""" Test the magic expansion and history. | ||||
This test is fairly fragile and will break when magics change. | ||||
""" | ||||
f = TestPrefilterFrontEnd() | ||||
Fernando Perez
|
r1966 | # Before checking the interactive namespace, make sure it's clear (it can | ||
# otherwise pick up things stored in the user's local db) | ||||
f.input_buffer += '%reset -f' | ||||
f._on_enter() | ||||
f.complete_current_input() | ||||
# Now, run the %who magic and check output | ||||
Gael Varoquaux
|
r1505 | f.input_buffer += '%who' | ||
gvaroquaux
|
r1460 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1887 | assert_equal(out_value, 'Interactive namespace is empty.\n') | ||
gvaroquaux
|
r1461 | |||
gvaroquaux
|
r1531 | @isolate_ipython0 | ||
gvaroquaux
|
r1461 | def test_help(): | ||
""" Test object inspection. | ||||
""" | ||||
f = TestPrefilterFrontEnd() | ||||
gvaroquaux
|
r1462 | f.input_buffer += "def f():" | ||
gvaroquaux
|
r1460 | f._on_enter() | ||
gvaroquaux
|
r1462 | f.input_buffer += "'foobar'" | ||
gvaroquaux
|
r1461 | f._on_enter() | ||
gvaroquaux
|
r1462 | f.input_buffer += "pass" | ||
gvaroquaux
|
r1461 | f._on_enter() | ||
f._on_enter() | ||||
gvaroquaux
|
r1462 | f.input_buffer += "f?" | ||
gvaroquaux
|
r1461 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | assert 'traceback' not in f.last_result | ||
## XXX: ipython doctest magic breaks this. I have no clue why | ||||
#out_value = f.out.getvalue() | ||||
#assert out_value.split()[-1] == 'foobar' | ||||
gvaroquaux
|
r1461 | |||
gvaroquaux
|
r1462 | |||
gvaroquaux
|
r1531 | @isolate_ipython0 | ||
Gael Varoquaux
|
r1887 | def test_completion_simple(): | ||
""" Test command-line completion on trivial examples. | ||||
gvaroquaux
|
r1461 | """ | ||
f = TestPrefilterFrontEnd() | ||||
gvaroquaux
|
r1462 | f.input_buffer = 'zzza = 1' | ||
gvaroquaux
|
r1461 | f._on_enter() | ||
gvaroquaux
|
r1462 | f.input_buffer = 'zzzb = 2' | ||
gvaroquaux
|
r1461 | f._on_enter() | ||
gvaroquaux
|
r1462 | f.input_buffer = 'zz' | ||
gvaroquaux
|
r1464 | f.complete_current_input() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '\nzzza zzzb ' | ||
yield assert_equal, f.input_buffer, 'zzz' | ||||
Gael Varoquaux
|
r1887 | |||
@isolate_ipython0 | ||||
def test_completion_parenthesis(): | ||||
""" Test command-line completion when a parenthesis is open. | ||||
""" | ||||
f = TestPrefilterFrontEnd() | ||||
f.input_buffer = 'zzza = 1' | ||||
f._on_enter() | ||||
f.input_buffer = 'zzzb = 2' | ||||
f._on_enter() | ||||
f.input_buffer = 'map(zz' | ||||
f.complete_current_input() | ||||
out_value = f.out.getvalue() | ||||
Gael Varoquaux
|
r1947 | yield assert_equal, out_value, '\nzzza zzzb ' | ||
yield assert_equal, f.input_buffer, 'map(zzz' | ||||
Gael Varoquaux
|
r1887 | |||
@isolate_ipython0 | ||||
def test_completion_indexing(): | ||||
""" Test command-line completion when indexing on objects. | ||||
""" | ||||
f = TestPrefilterFrontEnd() | ||||
f.input_buffer = 'a = [0]' | ||||
f._on_enter() | ||||
f.input_buffer = 'a[0].' | ||||
f.complete_current_input() | ||||
assert_equal(f.input_buffer, 'a[0].__') | ||||
@isolate_ipython0 | ||||
def test_completion_equal(): | ||||
""" Test command-line completion when the delimiter is "=", not " ". | ||||
""" | ||||
f = TestPrefilterFrontEnd() | ||||
f.input_buffer = 'a=1.' | ||||
f.complete_current_input() | ||||
assert_equal(f.input_buffer, 'a=1.__') | ||||
gvaroquaux
|
r1461 | |||
Gael Varoquaux
|
r1458 | |||
if __name__ == '__main__': | ||||
gvaroquaux
|
r1461 | test_magic() | ||
test_help() | ||||
Gael Varoquaux
|
r1458 | test_execution() | ||
test_multiline() | ||||
test_capture() | ||||
Gael Varoquaux
|
r1887 | test_completion_simple() | ||
test_completion_complex() | ||||