##// END OF EJS Templates
update regex
update regex

File last commit:

r13690:5f5083c1
r17755:a20c271b
Show More
test_io.py
123 lines | 3.7 KiB | text/x-python | PythonLexer
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # encoding: utf-8
"""Tests for io.py"""
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2008-2011 The IPython Development Team
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 from __future__ import print_function
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 from __future__ import absolute_import
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 import io as stdlib_io
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import sys
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 from subprocess import Popen, PIPE
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 import unittest
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
import nose.tools as nt
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 from IPython.testing.decorators import skipif
from IPython.utils.io import Tee, capture_output, unicode_std_stream
Thomas Kluyver
Use StringIO.StringIO on Python 2....
r13366 from IPython.utils.py3compat import doctest_refactor_print, PY3
if PY3:
from io import StringIO
else:
from StringIO import StringIO
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
def test_tee_simple():
"Very simple check with stdout only"
chan = StringIO()
text = 'Hello'
tee = Tee(chan, channel='stdout')
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print(text, file=chan)
Thomas Kluyver
Various fixes to tests in IPython.utils.
r4891 nt.assert_equal(chan.getvalue(), text+"\n")
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 class TeeTestCase(unittest.TestCase):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
def tchan(self, channel, check='close'):
trap = StringIO()
chan = StringIO()
text = 'Hello'
std_ori = getattr(sys, channel)
setattr(sys, channel, trap)
tee = Tee(chan, channel=channel)
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print(text, end='', file=chan)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 setattr(sys, channel, std_ori)
trap_val = trap.getvalue()
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(chan.getvalue(), text)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if check=='close':
tee.close()
else:
del tee
def test(self):
for chan in ['stdout', 'stderr']:
for check in ['close', 'del']:
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.tchan(chan, check)
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800
def test_io_init():
"""Test that io.stdin/out/err exist at startup"""
for name in ('stdin', 'stdout', 'stderr'):
Thomas Kluyver
Various fixes to tests in IPython.utils.
r4891 cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name)
p = Popen([sys.executable, '-c', cmd],
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 stdout=PIPE)
p.wait()
Thomas Kluyver
Various fixes to tests in IPython.utils.
r4891 classname = p.stdout.read().strip().decode('ascii')
# __class__ is a reference to the class object in Python 3, so we can't
# just test for string equality.
assert 'IPython.utils.io.IOStream' in classname, classname
MinRK
move capture_output util from parallel tests to utils.io
r7324
def test_capture_output():
"""capture_output() context works"""
with capture_output() as io:
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print('hi, stdout')
print('hi, stderr', file=sys.stderr)
MinRK
move capture_output util from parallel tests to utils.io
r7324
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(io.stdout, 'hi, stdout\n')
nt.assert_equal(io.stderr, 'hi, stderr\n')
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690
def test_UnicodeStdStream():
# Test wrapping a bytes-level stdout
if PY3:
stdoutb = stdlib_io.BytesIO()
stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii')
else:
stdout = stdoutb = stdlib_io.BytesIO()
orig_stdout = sys.stdout
sys.stdout = stdout
try:
sample = u"@łe¶ŧ←"
unicode_std_stream().write(sample)
output = stdoutb.getvalue().decode('utf-8')
nt.assert_equal(output, sample)
assert not stdout.closed
finally:
sys.stdout = orig_stdout
@skipif(not PY3, "Not applicable on Python 2")
def test_UnicodeStdStream_nowrap():
# If we replace stdout with a StringIO, it shouldn't get wrapped.
orig_stdout = sys.stdout
sys.stdout = StringIO()
try:
nt.assert_is(unicode_std_stream(), sys.stdout)
assert not sys.stdout.closed
finally:
sys.stdout = orig_stdout