##// END OF EJS Templates
Merge pull request #8429 from Carreau/backport-notebook-89...
Merge pull request #8429 from Carreau/backport-notebook-89 Backport jupyter/notebook#89

File last commit:

r15516:43a46eb6
r21379:78b7cfd9 merge
Show More
test_text.py
189 lines | 6.3 KiB | text/x-python | PythonLexer
Fernando Perez
Add failing test: columnize called with very long entries....
r4538 # encoding: utf-8
"""Tests for IPython.utils.text"""
Thomas Kluyver
Convert print statements to print function calls...
r13348 from __future__ import print_function
Fernando Perez
Add failing test: columnize called with very long entries....
r4538
#-----------------------------------------------------------------------------
# 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
#-----------------------------------------------------------------------------
import os
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 import math
Matthias BUSSONNIER
re-write columnize, with intermediate step....
r7387 import random
Thomas Kluyver
Fix EvalFormatter test for Python 3.4...
r13990 import sys
Fernando Perez
Add failing test: columnize called with very long entries....
r4538
import nose.tools as nt
from IPython.utils import text
#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
def test_columnize():
Fernando Perez
Fix bug where tab-completion with very long filenames would crash the qt console....
r4539 """Basic columnize tests."""
size = 5
items = [l*size for l in 'abc']
out = text.columnize(items, displaywidth=80)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(out, 'aaaaa bbbbb ccccc\n')
Matthias BUSSONNIER
re-write columnize, with intermediate step....
r7387 out = text.columnize(items, displaywidth=12)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(out, 'aaaaa ccccc\nbbbbb\n')
Matthias BUSSONNIER
re-write columnize, with intermediate step....
r7387 out = text.columnize(items, displaywidth=10)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\n')
Matthias BUSSONNIER
re-write columnize, with intermediate step....
r7387
def test_columnize_random():
"""Test with random input to hopfully catch edge case """
for nitems in [random.randint(2,70) for i in range(2,20)]:
displaywidth = random.randint(20,200)
rand_len = [random.randint(2,displaywidth) for i in range(nitems)]
items = ['x'*l for l in rand_len]
out = text.columnize(items, displaywidth=displaywidth)
longer_line = max([len(x) for x in out.split('\n')])
longer_element = max(rand_len)
if longer_line > displaywidth:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("Columnize displayed something lager than displaywidth : %s " % longer_line)
print("longer element : %s " % longer_element)
print("displaywidth : %s " % displaywidth)
print("number of element : %s " % nitems)
print("size of each element :\n %s" % rand_len)
Matthias BUSSONNIER
re-write columnize, with intermediate step....
r7387 assert False
Fernando Perez
Fix bug where tab-completion with very long filenames would crash the qt console....
r4539
Matthias BUSSONNIER
re-write columnize, with intermediate step....
r7387 def test_columnize_medium():
"""Test with inputs than shouldn't be wider tahn 80 """
size = 40
items = [l*size for l in 'abc']
out = text.columnize(items, displaywidth=80)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(out, '\n'.join(items+['']))
Fernando Perez
Fix bug where tab-completion with very long filenames would crash the qt console....
r4539
def test_columnize_long():
"""Test columnize with inputs longer than the display window"""
size = 11
items = [l*size for l in 'abc']
out = text.columnize(items, displaywidth=size-1)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(out, '\n'.join(items+['']))
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654
Thomas Kluyver
Add DollarFormatter and tests.
r5354 def eval_formatter_check(f):
Thomas Kluyver
Use DollarFormatter to fill in names in ! shell calls....
r5355 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"café", b="café")
Thomas Kluyver
Various fixes to tests in IPython.utils.
r4891 s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "12 3 hello")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "12 6 4 3 2 2 1")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 s = f.format('{[n//i for i in range(1,8)]}', **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "[12, 6, 4, 3, 2, 2, 1]")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 s = f.format("{stuff!s}", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, ns['stuff'])
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 s = f.format("{stuff!r}", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, repr(ns['stuff']))
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654
Thomas Kluyver
Use DollarFormatter to fill in names in ! shell calls....
r5355 # Check with unicode:
s = f.format("{u}", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, ns['u'])
Thomas Kluyver
Use DollarFormatter to fill in names in ! shell calls....
r5355 # This decodes in a platform dependent manner, but it shouldn't error out
s = f.format("{b}", **ns)
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 nt.assert_raises(NameError, f.format, '{dne}', **ns)
Thomas Kluyver
Add DollarFormatter and tests.
r5354 def eval_formatter_slicing_check(f):
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
s = f.format(" {stuff.split()[:]} ", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, " ['hello', 'there'] ")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 s = f.format(" {stuff.split()[::-1]} ", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, " ['there', 'hello'] ")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 s = f.format("{stuff[::2]}", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, ns['stuff'][::2])
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654
nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
Thomas Kluyver
Add DollarFormatter and tests.
r5354 def eval_formatter_no_slicing_check(f):
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
s = f.format('{n:x} {pi**2:+f}', **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "c +9.869604")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654
Thomas Kluyver
Sort out the different implementations of EvalFormatter, so we usually use the simple one.
r5501 s = f.format('{stuff[slice(1,4)]}', **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, 'ell')
Thomas Kluyver
Sort out the different implementations of EvalFormatter, so we usually use the simple one.
r5501
Thomas Kluyver
Fix EvalFormatter test for Python 3.4...
r13990 if sys.version_info >= (3, 4):
# String formatting has changed in Python 3.4, so this now works.
s = f.format("{a[:]}", a=[1, 2])
nt.assert_equal(s, "[1, 2]")
else:
nt.assert_raises(SyntaxError, f.format, "{a[:]}")
MinRK
update EvalFormatter to allow arbitrary expressions...
r4654
Thomas Kluyver
Add DollarFormatter and tests.
r5354 def test_eval_formatter():
f = text.EvalFormatter()
eval_formatter_check(f)
eval_formatter_no_slicing_check(f)
def test_full_eval_formatter():
f = text.FullEvalFormatter()
eval_formatter_check(f)
eval_formatter_slicing_check(f)
def test_dollar_formatter():
f = text.DollarFormatter()
eval_formatter_check(f)
eval_formatter_slicing_check(f)
ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
s = f.format("$n", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "12")
Thomas Kluyver
Add DollarFormatter and tests.
r5354 s = f.format("$n.real", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "12")
Thomas Kluyver
Add DollarFormatter and tests.
r5354 s = f.format("$n/{stuff[:5]}", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "12/hello")
Thomas Kluyver
Allow 2033 to get literal $ in shell commands.
r5365 s = f.format("$n $$HOME", **ns)
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "12 $HOME")
Thomas Kluyver
Add test for to specify name of an environment variable.
r5375 s = f.format("${foo}", foo="HOME")
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(s, "$HOME")
Fernando Perez
Add robust email-quote-stripping function to text utilities.
r7713
Fernando Perez
Fix logic for longest_substring when only one input....
r7715 def test_long_substr():
data = ['hi']
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(text.long_substr(data), 'hi')
Fernando Perez
Fix logic for longest_substring when only one input....
r7715
def test_long_substr2():
data = ['abc', 'abd', 'abf', 'ab']
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(text.long_substr(data), 'ab')
Fernando Perez
Fix logic for longest_substring when only one input....
r7715
Bradley M. Froehle
Fix: longest_substr([]) -> ''...
r8147 def test_long_substr_empty():
data = []
nt.assert_equal(text.long_substr(data), '')
Fernando Perez
Fix logic for longest_substring when only one input....
r7715
Fernando Perez
Add robust email-quote-stripping function to text utilities.
r7713 def test_strip_email():
src = """\
>> >>> def f(x):
>> ... return x+1
Fernando Perez
Fix continuation prompt in test.
r7714 >> ...
Fernando Perez
Add robust email-quote-stripping function to text utilities.
r7713 >> >>> zz = f(2.5)"""
cln = """\
>>> def f(x):
... return x+1
Fernando Perez
Fix continuation prompt in test.
r7714 ...
Fernando Perez
Add robust email-quote-stripping function to text utilities.
r7713 >>> zz = f(2.5)"""
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(text.strip_email_quotes(src), cln)
Fernando Perez
Fix logic for longest_substring when only one input....
r7715
def test_strip_email2():
src = '> > > list()'
cln = 'list()'
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(text.strip_email_quotes(src), cln)
Thomas Kluyver
Add tests for things in utils
r15516
def test_LSString():
lss = text.LSString("abc\ndef")
nt.assert_equal(lss.l, ['abc', 'def'])
nt.assert_equal(lss.s, 'abc def')
def test_SList():
sl = text.SList(['a 11', 'b 1', 'a 2'])
nt.assert_equal(sl.n, 'a 11\nb 1\na 2')
nt.assert_equal(sl.s, 'a 11 b 1 a 2')
nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2']))
nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a']))
nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11']))