test_prefilterfrontend.py
132 lines
| 3.4 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 IPython.frontend.prefilterfrontend import PrefilterFrontEnd | ||||
from cStringIO import StringIO | ||||
import string | ||||
Gael Varoquaux
|
r1472 | import sys | ||
Gael Varoquaux
|
r1504 | from IPython.ipapi import get as get_ipython0 | ||
Gael Varoquaux
|
r1458 | |||
class TestPrefilterFrontEnd(PrefilterFrontEnd): | ||||
input_prompt_template = string.Template('') | ||||
output_prompt_template = string.Template('') | ||||
gvaroquaux
|
r1460 | def __init__(self): | ||
Gael Varoquaux
|
r1504 | ipython0 = get_ipython0() | ||
Gael Varoquaux
|
r1458 | self.out = StringIO() | ||
Gael Varoquaux
|
r1504 | PrefilterFrontEnd.__init__(self, ipython0=ipython0) | ||
Gael Varoquaux
|
r1458 | |||
def write(self, string): | ||||
self.out.write(string) | ||||
def _on_enter(self): | ||||
gvaroquaux
|
r1462 | self.input_buffer += '\n' | ||
Gael Varoquaux
|
r1458 | PrefilterFrontEnd._on_enter(self) | ||
def test_execution(): | ||||
""" Test execution of a command. | ||||
""" | ||||
gvaroquaux
|
r1460 | f = TestPrefilterFrontEnd() | ||
gvaroquaux
|
r1462 | f.input_buffer = 'print 1\n' | ||
Gael Varoquaux
|
r1458 | f._on_enter() | ||
assert f.out.getvalue() == '1\n' | ||||
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() | ||
assert f.out.getvalue() == '' | ||||
f._on_enter() | ||||
assert f.out.getvalue() == '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() | ||
assert f.out.getvalue() == '' | ||||
f._on_enter() | ||||
assert f.out.getvalue() == '1\n' | ||||
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() | ||
assert f.out.getvalue() == '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() | ||
assert f.out.getvalue() == '1' | ||||
gvaroquaux
|
r1460 | |||
Gael Varoquaux
|
r1458 | |||
gvaroquaux
|
r1460 | def test_magic(): | ||
""" Test the magic expansion and history. | ||||
This test is fairly fragile and will break when magics change. | ||||
""" | ||||
f = TestPrefilterFrontEnd() | ||||
gvaroquaux
|
r1462 | f.input_buffer += '%who\n' | ||
gvaroquaux
|
r1460 | f._on_enter() | ||
assert f.out.getvalue() == 'Interactive namespace is empty.\n' | ||||
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() | ||
assert f.out.getvalue().split()[-1] == 'foobar' | ||||
gvaroquaux
|
r1462 | |||
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() | ||
assert f.out.getvalue() == '\nzzza zzzb ' | ||||
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() | ||