##// END OF EJS Templates
Fix continuation prompt in test.
Fernando Perez -
Show More
@@ -1,154 +1,154 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.text"""
2 """Tests for IPython.utils.text"""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2011 The IPython Development Team
5 # Copyright (C) 2011 The IPython Development Team
6 #
6 #
7 # Distributed under the terms of the BSD License. The full license is in
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 import os
15 import os
16 import math
16 import math
17 import random
17 import random
18
18
19 import nose.tools as nt
19 import nose.tools as nt
20
20
21 from nose import with_setup
21 from nose import with_setup
22
22
23 from IPython.testing import decorators as dec
23 from IPython.testing import decorators as dec
24 from IPython.utils import text
24 from IPython.utils import text
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Globals
27 # Globals
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 def test_columnize():
30 def test_columnize():
31 """Basic columnize tests."""
31 """Basic columnize tests."""
32 size = 5
32 size = 5
33 items = [l*size for l in 'abc']
33 items = [l*size for l in 'abc']
34 out = text.columnize(items, displaywidth=80)
34 out = text.columnize(items, displaywidth=80)
35 nt.assert_equals(out, 'aaaaa bbbbb ccccc\n')
35 nt.assert_equals(out, 'aaaaa bbbbb ccccc\n')
36 out = text.columnize(items, displaywidth=12)
36 out = text.columnize(items, displaywidth=12)
37 nt.assert_equals(out, 'aaaaa ccccc\nbbbbb\n')
37 nt.assert_equals(out, 'aaaaa ccccc\nbbbbb\n')
38 out = text.columnize(items, displaywidth=10)
38 out = text.columnize(items, displaywidth=10)
39 nt.assert_equals(out, 'aaaaa\nbbbbb\nccccc\n')
39 nt.assert_equals(out, 'aaaaa\nbbbbb\nccccc\n')
40
40
41 def test_columnize_random():
41 def test_columnize_random():
42 """Test with random input to hopfully catch edge case """
42 """Test with random input to hopfully catch edge case """
43 for nitems in [random.randint(2,70) for i in range(2,20)]:
43 for nitems in [random.randint(2,70) for i in range(2,20)]:
44 displaywidth = random.randint(20,200)
44 displaywidth = random.randint(20,200)
45 rand_len = [random.randint(2,displaywidth) for i in range(nitems)]
45 rand_len = [random.randint(2,displaywidth) for i in range(nitems)]
46 items = ['x'*l for l in rand_len]
46 items = ['x'*l for l in rand_len]
47 out = text.columnize(items, displaywidth=displaywidth)
47 out = text.columnize(items, displaywidth=displaywidth)
48 longer_line = max([len(x) for x in out.split('\n')])
48 longer_line = max([len(x) for x in out.split('\n')])
49 longer_element = max(rand_len)
49 longer_element = max(rand_len)
50 if longer_line > displaywidth:
50 if longer_line > displaywidth:
51 print "Columnize displayed something lager than displaywidth : %s " % longer_line
51 print "Columnize displayed something lager than displaywidth : %s " % longer_line
52 print "longer element : %s " % longer_element
52 print "longer element : %s " % longer_element
53 print "displaywidth : %s " % displaywidth
53 print "displaywidth : %s " % displaywidth
54 print "number of element : %s " % nitems
54 print "number of element : %s " % nitems
55 print "size of each element :\n %s" % rand_len
55 print "size of each element :\n %s" % rand_len
56 assert False
56 assert False
57
57
58 def test_columnize_medium():
58 def test_columnize_medium():
59 """Test with inputs than shouldn't be wider tahn 80 """
59 """Test with inputs than shouldn't be wider tahn 80 """
60 size = 40
60 size = 40
61 items = [l*size for l in 'abc']
61 items = [l*size for l in 'abc']
62 out = text.columnize(items, displaywidth=80)
62 out = text.columnize(items, displaywidth=80)
63 nt.assert_equals(out, '\n'.join(items+['']))
63 nt.assert_equals(out, '\n'.join(items+['']))
64
64
65 def test_columnize_long():
65 def test_columnize_long():
66 """Test columnize with inputs longer than the display window"""
66 """Test columnize with inputs longer than the display window"""
67 size = 11
67 size = 11
68 items = [l*size for l in 'abc']
68 items = [l*size for l in 'abc']
69 out = text.columnize(items, displaywidth=size-1)
69 out = text.columnize(items, displaywidth=size-1)
70 nt.assert_equals(out, '\n'.join(items+['']))
70 nt.assert_equals(out, '\n'.join(items+['']))
71
71
72 def eval_formatter_check(f):
72 def eval_formatter_check(f):
73 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"cafΓ©", b="cafΓ©")
73 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"cafΓ©", b="cafΓ©")
74 s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
74 s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
75 nt.assert_equals(s, "12 3 hello")
75 nt.assert_equals(s, "12 3 hello")
76 s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns)
76 s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns)
77 nt.assert_equals(s, "12 6 4 3 2 2 1")
77 nt.assert_equals(s, "12 6 4 3 2 2 1")
78 s = f.format('{[n//i for i in range(1,8)]}', **ns)
78 s = f.format('{[n//i for i in range(1,8)]}', **ns)
79 nt.assert_equals(s, "[12, 6, 4, 3, 2, 2, 1]")
79 nt.assert_equals(s, "[12, 6, 4, 3, 2, 2, 1]")
80 s = f.format("{stuff!s}", **ns)
80 s = f.format("{stuff!s}", **ns)
81 nt.assert_equals(s, ns['stuff'])
81 nt.assert_equals(s, ns['stuff'])
82 s = f.format("{stuff!r}", **ns)
82 s = f.format("{stuff!r}", **ns)
83 nt.assert_equals(s, repr(ns['stuff']))
83 nt.assert_equals(s, repr(ns['stuff']))
84
84
85 # Check with unicode:
85 # Check with unicode:
86 s = f.format("{u}", **ns)
86 s = f.format("{u}", **ns)
87 nt.assert_equals(s, ns['u'])
87 nt.assert_equals(s, ns['u'])
88 # This decodes in a platform dependent manner, but it shouldn't error out
88 # This decodes in a platform dependent manner, but it shouldn't error out
89 s = f.format("{b}", **ns)
89 s = f.format("{b}", **ns)
90
90
91 nt.assert_raises(NameError, f.format, '{dne}', **ns)
91 nt.assert_raises(NameError, f.format, '{dne}', **ns)
92
92
93 def eval_formatter_slicing_check(f):
93 def eval_formatter_slicing_check(f):
94 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
94 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
95 s = f.format(" {stuff.split()[:]} ", **ns)
95 s = f.format(" {stuff.split()[:]} ", **ns)
96 nt.assert_equals(s, " ['hello', 'there'] ")
96 nt.assert_equals(s, " ['hello', 'there'] ")
97 s = f.format(" {stuff.split()[::-1]} ", **ns)
97 s = f.format(" {stuff.split()[::-1]} ", **ns)
98 nt.assert_equals(s, " ['there', 'hello'] ")
98 nt.assert_equals(s, " ['there', 'hello'] ")
99 s = f.format("{stuff[::2]}", **ns)
99 s = f.format("{stuff[::2]}", **ns)
100 nt.assert_equals(s, ns['stuff'][::2])
100 nt.assert_equals(s, ns['stuff'][::2])
101
101
102 nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
102 nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
103
103
104 def eval_formatter_no_slicing_check(f):
104 def eval_formatter_no_slicing_check(f):
105 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
105 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
106
106
107 s = f.format('{n:x} {pi**2:+f}', **ns)
107 s = f.format('{n:x} {pi**2:+f}', **ns)
108 nt.assert_equals(s, "c +9.869604")
108 nt.assert_equals(s, "c +9.869604")
109
109
110 s = f.format('{stuff[slice(1,4)]}', **ns)
110 s = f.format('{stuff[slice(1,4)]}', **ns)
111 nt.assert_equals(s, 'ell')
111 nt.assert_equals(s, 'ell')
112
112
113 nt.assert_raises(SyntaxError, f.format, "{a[:]}")
113 nt.assert_raises(SyntaxError, f.format, "{a[:]}")
114
114
115 def test_eval_formatter():
115 def test_eval_formatter():
116 f = text.EvalFormatter()
116 f = text.EvalFormatter()
117 eval_formatter_check(f)
117 eval_formatter_check(f)
118 eval_formatter_no_slicing_check(f)
118 eval_formatter_no_slicing_check(f)
119
119
120 def test_full_eval_formatter():
120 def test_full_eval_formatter():
121 f = text.FullEvalFormatter()
121 f = text.FullEvalFormatter()
122 eval_formatter_check(f)
122 eval_formatter_check(f)
123 eval_formatter_slicing_check(f)
123 eval_formatter_slicing_check(f)
124
124
125 def test_dollar_formatter():
125 def test_dollar_formatter():
126 f = text.DollarFormatter()
126 f = text.DollarFormatter()
127 eval_formatter_check(f)
127 eval_formatter_check(f)
128 eval_formatter_slicing_check(f)
128 eval_formatter_slicing_check(f)
129
129
130 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
130 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
131 s = f.format("$n", **ns)
131 s = f.format("$n", **ns)
132 nt.assert_equals(s, "12")
132 nt.assert_equals(s, "12")
133 s = f.format("$n.real", **ns)
133 s = f.format("$n.real", **ns)
134 nt.assert_equals(s, "12")
134 nt.assert_equals(s, "12")
135 s = f.format("$n/{stuff[:5]}", **ns)
135 s = f.format("$n/{stuff[:5]}", **ns)
136 nt.assert_equals(s, "12/hello")
136 nt.assert_equals(s, "12/hello")
137 s = f.format("$n $$HOME", **ns)
137 s = f.format("$n $$HOME", **ns)
138 nt.assert_equals(s, "12 $HOME")
138 nt.assert_equals(s, "12 $HOME")
139 s = f.format("${foo}", foo="HOME")
139 s = f.format("${foo}", foo="HOME")
140 nt.assert_equals(s, "$HOME")
140 nt.assert_equals(s, "$HOME")
141
141
142
142
143 def test_strip_email():
143 def test_strip_email():
144 src = """\
144 src = """\
145 >> >>> def f(x):
145 >> >>> def f(x):
146 >> ... return x+1
146 >> ... return x+1
147 >> ...
147 >> ...
148 >> >>> zz = f(2.5)"""
148 >> >>> zz = f(2.5)"""
149 cln = """\
149 cln = """\
150 >>> def f(x):
150 >>> def f(x):
151 ... return x+1
151 ... return x+1
152 ...
152 ...
153 >>> zz = f(2.5)"""
153 >>> zz = f(2.5)"""
154 nt.assert_equals(text.strip_email_quotes(src), cln)
154 nt.assert_equals(text.strip_email_quotes(src), cln)
General Comments 0
You need to be logged in to leave comments. Login now