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