test_prefilterfrontend.py
180 lines
| 4.8 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. | ||||
#------------------------------------------------------------------------------- | ||||
from cStringIO import StringIO | ||||
import string | ||||
Gael Varoquaux
|
r1505 | |||
Gael Varoquaux
|
r1504 | from IPython.ipapi import get as get_ipython0 | ||
Gael Varoquaux
|
r1505 | from IPython.frontend.prefilterfrontend import PrefilterFrontEnd | ||
Gael Varoquaux
|
r1535 | from copy import deepcopy | ||
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
|
r1505 | ipython0 = get_ipython0().IP | ||
Gael Varoquaux
|
r1458 | self.out = StringIO() | ||
Gael Varoquaux
|
r1504 | PrefilterFrontEnd.__init__(self, ipython0=ipython0) | ||
Gael Varoquaux
|
r1505 | # Clean up the namespace for isolation between tests | ||
user_ns = self.ipython0.user_ns | ||||
# We need to keep references to things so that they don't | ||||
# get garbage collected (this stinks). | ||||
self.shadow_ns = dict() | ||||
for i in self.ipython0.magic_who_ls(): | ||||
self.shadow_ns[i] = user_ns.pop(i) | ||||
# 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. | ||||
""" | ||||
def my_func(*args, **kwargs): | ||||
ipython0 = get_ipython0().IP | ||||
Gael Varoquaux
|
r1535 | user_ns = deepcopy(ipython0.user_ns) | ||
global_ns = deepcopy(ipython0.global_ns) | ||||
try: | ||||
func(*args, **kwargs) | ||||
finally: | ||||
ipython0.user_ns = user_ns | ||||
ipython0.global_ns = global_ns | ||||
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() | ||
assert 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() | ||
assert out_value == '' | ||||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
assert 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() | ||
assert out_value == '' | ||||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
assert 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() | ||
assert 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() | ||
assert 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() | ||||
Gael Varoquaux
|
r1505 | f.input_buffer += '%who' | ||
gvaroquaux
|
r1460 | f._on_enter() | ||
Gael Varoquaux
|
r1505 | out_value = f.out.getvalue() | ||
assert 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 | ||
gvaroquaux
|
r1461 | def test_completion(): | ||
""" Test command-line completion. | ||||
""" | ||||
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() | ||
assert out_value == '\nzzza zzzb ' | ||||
gvaroquaux
|
r1464 | assert f.input_buffer == 'zzz' | ||
gvaroquaux
|
r1461 | |||
Gael Varoquaux
|
r1458 | |||
if __name__ == '__main__': | ||||
gvaroquaux
|
r1461 | test_magic() | ||
test_help() | ||||
Gael Varoquaux
|
r1458 | test_execution() | ||
test_multiline() | ||||
test_capture() | ||||
gvaroquaux
|
r1464 | test_completion() | ||