Show More
@@ -1,193 +1,209 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 | import sys | |
20 |
|
20 | |||
21 | import nose.tools as nt |
|
21 | import nose.tools as nt | |
22 | import path |
|
22 | import path | |
23 |
|
23 | |||
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 'abcd'] | |
|
34 | ||||
34 | out = text.columnize(items, displaywidth=80) |
|
35 | out = text.columnize(items, displaywidth=80) | |
35 | nt.assert_equal(out, 'aaaaa bbbbb ccccc\n') |
|
36 | nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n') | |
|
37 | out = text.columnize(items, displaywidth=25) | |||
|
38 | nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n') | |||
36 | out = text.columnize(items, displaywidth=12) |
|
39 | out = text.columnize(items, displaywidth=12) | |
37 | nt.assert_equal(out, 'aaaaa ccccc\nbbbbb\n') |
|
40 | nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n') | |
38 | out = text.columnize(items, displaywidth=10) |
|
41 | out = text.columnize(items, displaywidth=10) | |
39 | nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\n') |
|
42 | nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n') | |
|
43 | ||||
|
44 | out = text.columnize(items, row_first=True, displaywidth=80) | |||
|
45 | nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n') | |||
|
46 | out = text.columnize(items, row_first=True, displaywidth=25) | |||
|
47 | nt.assert_equal(out, 'aaaaa bbbbb\nccccc ddddd\n') | |||
|
48 | out = text.columnize(items, row_first=True, displaywidth=12) | |||
|
49 | nt.assert_equal(out, 'aaaaa bbbbb\nccccc ddddd\n') | |||
|
50 | out = text.columnize(items, row_first=True, displaywidth=10) | |||
|
51 | nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n') | |||
|
52 | ||||
40 |
|
53 | |||
41 | def test_columnize_random(): |
|
54 | def test_columnize_random(): | |
42 | """Test with random input to hopfully catch edge case """ |
|
55 | """Test with random input to hopfully catch edge case """ | |
|
56 | for row_first in [True, False]: | |||
43 | for nitems in [random.randint(2,70) for i in range(2,20)]: |
|
57 | for nitems in [random.randint(2,70) for i in range(2,20)]: | |
44 | displaywidth = random.randint(20,200) |
|
58 | displaywidth = random.randint(20,200) | |
45 | rand_len = [random.randint(2,displaywidth) for i in range(nitems)] |
|
59 | rand_len = [random.randint(2,displaywidth) for i in range(nitems)] | |
46 | items = ['x'*l for l in rand_len] |
|
60 | items = ['x'*l for l in rand_len] | |
47 | out = text.columnize(items, displaywidth=displaywidth) |
|
61 | out = text.columnize(items, row_first=row_first, displaywidth=displaywidth) | |
48 | longer_line = max([len(x) for x in out.split('\n')]) |
|
62 | longer_line = max([len(x) for x in out.split('\n')]) | |
49 | longer_element = max(rand_len) |
|
63 | longer_element = max(rand_len) | |
50 | if longer_line > displaywidth: |
|
64 | if longer_line > displaywidth: | |
51 | print("Columnize displayed something lager than displaywidth : %s " % longer_line) |
|
65 | print("Columnize displayed something lager than displaywidth : %s " % longer_line) | |
52 | print("longer element : %s " % longer_element) |
|
66 | print("longer element : %s " % longer_element) | |
53 | print("displaywidth : %s " % displaywidth) |
|
67 | print("displaywidth : %s " % displaywidth) | |
54 | print("number of element : %s " % nitems) |
|
68 | print("number of element : %s " % nitems) | |
55 | print("size of each element :\n %s" % rand_len) |
|
69 | print("size of each element :\n %s" % rand_len) | |
56 | assert False |
|
70 | assert False, "row_first={0}".format(row_first) | |
57 |
|
71 | |||
58 | def test_columnize_medium(): |
|
72 | def test_columnize_medium(): | |
59 |
"""Test with inputs than shouldn't be wider t |
|
73 | """Test with inputs than shouldn't be wider than 80""" | |
60 | size = 40 |
|
74 | size = 40 | |
61 | items = [l*size for l in 'abc'] |
|
75 | items = [l*size for l in 'abc'] | |
62 | out = text.columnize(items, displaywidth=80) |
|
76 | for row_first in [True, False]: | |
63 | nt.assert_equal(out, '\n'.join(items+[''])) |
|
77 | out = text.columnize(items, row_first=row_first, displaywidth=80) | |
|
78 | nt.assert_equal(out, '\n'.join(items+['']), "row_first={0}".format(row_first)) | |||
64 |
|
79 | |||
65 | def test_columnize_long(): |
|
80 | def test_columnize_long(): | |
66 | """Test columnize with inputs longer than the display window""" |
|
81 | """Test columnize with inputs longer than the display window""" | |
67 | size = 11 |
|
82 | size = 11 | |
68 | items = [l*size for l in 'abc'] |
|
83 | items = [l*size for l in 'abc'] | |
69 | out = text.columnize(items, displaywidth=size-1) |
|
84 | for row_first in [True, False]: | |
70 | nt.assert_equal(out, '\n'.join(items+[''])) |
|
85 | out = text.columnize(items, row_first=row_first, displaywidth=size-1) | |
|
86 | nt.assert_equal(out, '\n'.join(items+['']), "row_first={0}".format(row_first)) | |||
71 |
|
87 | |||
72 | def eval_formatter_check(f): |
|
88 | def eval_formatter_check(f): | |
73 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"cafΓ©", b="cafΓ©") |
|
89 | 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) |
|
90 | s = f.format("{n} {n//4} {stuff.split()[0]}", **ns) | |
75 | nt.assert_equal(s, "12 3 hello") |
|
91 | nt.assert_equal(s, "12 3 hello") | |
76 | s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns) |
|
92 | s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns) | |
77 | nt.assert_equal(s, "12 6 4 3 2 2 1") |
|
93 | nt.assert_equal(s, "12 6 4 3 2 2 1") | |
78 | s = f.format('{[n//i for i in range(1,8)]}', **ns) |
|
94 | s = f.format('{[n//i for i in range(1,8)]}', **ns) | |
79 | nt.assert_equal(s, "[12, 6, 4, 3, 2, 2, 1]") |
|
95 | nt.assert_equal(s, "[12, 6, 4, 3, 2, 2, 1]") | |
80 | s = f.format("{stuff!s}", **ns) |
|
96 | s = f.format("{stuff!s}", **ns) | |
81 | nt.assert_equal(s, ns['stuff']) |
|
97 | nt.assert_equal(s, ns['stuff']) | |
82 | s = f.format("{stuff!r}", **ns) |
|
98 | s = f.format("{stuff!r}", **ns) | |
83 | nt.assert_equal(s, repr(ns['stuff'])) |
|
99 | nt.assert_equal(s, repr(ns['stuff'])) | |
84 |
|
100 | |||
85 | # Check with unicode: |
|
101 | # Check with unicode: | |
86 | s = f.format("{u}", **ns) |
|
102 | s = f.format("{u}", **ns) | |
87 | nt.assert_equal(s, ns['u']) |
|
103 | nt.assert_equal(s, ns['u']) | |
88 | # This decodes in a platform dependent manner, but it shouldn't error out |
|
104 | # This decodes in a platform dependent manner, but it shouldn't error out | |
89 | s = f.format("{b}", **ns) |
|
105 | s = f.format("{b}", **ns) | |
90 |
|
106 | |||
91 | nt.assert_raises(NameError, f.format, '{dne}', **ns) |
|
107 | nt.assert_raises(NameError, f.format, '{dne}', **ns) | |
92 |
|
108 | |||
93 | def eval_formatter_slicing_check(f): |
|
109 | def eval_formatter_slicing_check(f): | |
94 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
110 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) | |
95 | s = f.format(" {stuff.split()[:]} ", **ns) |
|
111 | s = f.format(" {stuff.split()[:]} ", **ns) | |
96 | nt.assert_equal(s, " ['hello', 'there'] ") |
|
112 | nt.assert_equal(s, " ['hello', 'there'] ") | |
97 | s = f.format(" {stuff.split()[::-1]} ", **ns) |
|
113 | s = f.format(" {stuff.split()[::-1]} ", **ns) | |
98 | nt.assert_equal(s, " ['there', 'hello'] ") |
|
114 | nt.assert_equal(s, " ['there', 'hello'] ") | |
99 | s = f.format("{stuff[::2]}", **ns) |
|
115 | s = f.format("{stuff[::2]}", **ns) | |
100 | nt.assert_equal(s, ns['stuff'][::2]) |
|
116 | nt.assert_equal(s, ns['stuff'][::2]) | |
101 |
|
117 | |||
102 | nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns) |
|
118 | nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns) | |
103 |
|
119 | |||
104 | def eval_formatter_no_slicing_check(f): |
|
120 | def eval_formatter_no_slicing_check(f): | |
105 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
121 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) | |
106 |
|
122 | |||
107 | s = f.format('{n:x} {pi**2:+f}', **ns) |
|
123 | s = f.format('{n:x} {pi**2:+f}', **ns) | |
108 | nt.assert_equal(s, "c +9.869604") |
|
124 | nt.assert_equal(s, "c +9.869604") | |
109 |
|
125 | |||
110 | s = f.format('{stuff[slice(1,4)]}', **ns) |
|
126 | s = f.format('{stuff[slice(1,4)]}', **ns) | |
111 | nt.assert_equal(s, 'ell') |
|
127 | nt.assert_equal(s, 'ell') | |
112 |
|
128 | |||
113 | if sys.version_info >= (3, 4): |
|
129 | if sys.version_info >= (3, 4): | |
114 | # String formatting has changed in Python 3.4, so this now works. |
|
130 | # String formatting has changed in Python 3.4, so this now works. | |
115 | s = f.format("{a[:]}", a=[1, 2]) |
|
131 | s = f.format("{a[:]}", a=[1, 2]) | |
116 | nt.assert_equal(s, "[1, 2]") |
|
132 | nt.assert_equal(s, "[1, 2]") | |
117 | else: |
|
133 | else: | |
118 | nt.assert_raises(SyntaxError, f.format, "{a[:]}") |
|
134 | nt.assert_raises(SyntaxError, f.format, "{a[:]}") | |
119 |
|
135 | |||
120 | def test_eval_formatter(): |
|
136 | def test_eval_formatter(): | |
121 | f = text.EvalFormatter() |
|
137 | f = text.EvalFormatter() | |
122 | eval_formatter_check(f) |
|
138 | eval_formatter_check(f) | |
123 | eval_formatter_no_slicing_check(f) |
|
139 | eval_formatter_no_slicing_check(f) | |
124 |
|
140 | |||
125 | def test_full_eval_formatter(): |
|
141 | def test_full_eval_formatter(): | |
126 | f = text.FullEvalFormatter() |
|
142 | f = text.FullEvalFormatter() | |
127 | eval_formatter_check(f) |
|
143 | eval_formatter_check(f) | |
128 | eval_formatter_slicing_check(f) |
|
144 | eval_formatter_slicing_check(f) | |
129 |
|
145 | |||
130 | def test_dollar_formatter(): |
|
146 | def test_dollar_formatter(): | |
131 | f = text.DollarFormatter() |
|
147 | f = text.DollarFormatter() | |
132 | eval_formatter_check(f) |
|
148 | eval_formatter_check(f) | |
133 | eval_formatter_slicing_check(f) |
|
149 | eval_formatter_slicing_check(f) | |
134 |
|
150 | |||
135 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) |
|
151 | ns = dict(n=12, pi=math.pi, stuff='hello there', os=os) | |
136 | s = f.format("$n", **ns) |
|
152 | s = f.format("$n", **ns) | |
137 | nt.assert_equal(s, "12") |
|
153 | nt.assert_equal(s, "12") | |
138 | s = f.format("$n.real", **ns) |
|
154 | s = f.format("$n.real", **ns) | |
139 | nt.assert_equal(s, "12") |
|
155 | nt.assert_equal(s, "12") | |
140 | s = f.format("$n/{stuff[:5]}", **ns) |
|
156 | s = f.format("$n/{stuff[:5]}", **ns) | |
141 | nt.assert_equal(s, "12/hello") |
|
157 | nt.assert_equal(s, "12/hello") | |
142 | s = f.format("$n $$HOME", **ns) |
|
158 | s = f.format("$n $$HOME", **ns) | |
143 | nt.assert_equal(s, "12 $HOME") |
|
159 | nt.assert_equal(s, "12 $HOME") | |
144 | s = f.format("${foo}", foo="HOME") |
|
160 | s = f.format("${foo}", foo="HOME") | |
145 | nt.assert_equal(s, "$HOME") |
|
161 | nt.assert_equal(s, "$HOME") | |
146 |
|
162 | |||
147 |
|
163 | |||
148 | def test_long_substr(): |
|
164 | def test_long_substr(): | |
149 | data = ['hi'] |
|
165 | data = ['hi'] | |
150 | nt.assert_equal(text.long_substr(data), 'hi') |
|
166 | nt.assert_equal(text.long_substr(data), 'hi') | |
151 |
|
167 | |||
152 |
|
168 | |||
153 | def test_long_substr2(): |
|
169 | def test_long_substr2(): | |
154 | data = ['abc', 'abd', 'abf', 'ab'] |
|
170 | data = ['abc', 'abd', 'abf', 'ab'] | |
155 | nt.assert_equal(text.long_substr(data), 'ab') |
|
171 | nt.assert_equal(text.long_substr(data), 'ab') | |
156 |
|
172 | |||
157 | def test_long_substr_empty(): |
|
173 | def test_long_substr_empty(): | |
158 | data = [] |
|
174 | data = [] | |
159 | nt.assert_equal(text.long_substr(data), '') |
|
175 | nt.assert_equal(text.long_substr(data), '') | |
160 |
|
176 | |||
161 | def test_strip_email(): |
|
177 | def test_strip_email(): | |
162 | src = """\ |
|
178 | src = """\ | |
163 | >> >>> def f(x): |
|
179 | >> >>> def f(x): | |
164 | >> ... return x+1 |
|
180 | >> ... return x+1 | |
165 | >> ... |
|
181 | >> ... | |
166 | >> >>> zz = f(2.5)""" |
|
182 | >> >>> zz = f(2.5)""" | |
167 | cln = """\ |
|
183 | cln = """\ | |
168 | >>> def f(x): |
|
184 | >>> def f(x): | |
169 | ... return x+1 |
|
185 | ... return x+1 | |
170 | ... |
|
186 | ... | |
171 | >>> zz = f(2.5)""" |
|
187 | >>> zz = f(2.5)""" | |
172 | nt.assert_equal(text.strip_email_quotes(src), cln) |
|
188 | nt.assert_equal(text.strip_email_quotes(src), cln) | |
173 |
|
189 | |||
174 |
|
190 | |||
175 | def test_strip_email2(): |
|
191 | def test_strip_email2(): | |
176 | src = '> > > list()' |
|
192 | src = '> > > list()' | |
177 | cln = 'list()' |
|
193 | cln = 'list()' | |
178 | nt.assert_equal(text.strip_email_quotes(src), cln) |
|
194 | nt.assert_equal(text.strip_email_quotes(src), cln) | |
179 |
|
195 | |||
180 | def test_LSString(): |
|
196 | def test_LSString(): | |
181 | lss = text.LSString("abc\ndef") |
|
197 | lss = text.LSString("abc\ndef") | |
182 | nt.assert_equal(lss.l, ['abc', 'def']) |
|
198 | nt.assert_equal(lss.l, ['abc', 'def']) | |
183 | nt.assert_equal(lss.s, 'abc def') |
|
199 | nt.assert_equal(lss.s, 'abc def') | |
184 | lss = text.LSString(os.getcwd()) |
|
200 | lss = text.LSString(os.getcwd()) | |
185 | nt.assert_is_instance(lss.p[0], path.path) |
|
201 | nt.assert_is_instance(lss.p[0], path.path) | |
186 |
|
202 | |||
187 | def test_SList(): |
|
203 | def test_SList(): | |
188 | sl = text.SList(['a 11', 'b 1', 'a 2']) |
|
204 | sl = text.SList(['a 11', 'b 1', 'a 2']) | |
189 | nt.assert_equal(sl.n, 'a 11\nb 1\na 2') |
|
205 | nt.assert_equal(sl.n, 'a 11\nb 1\na 2') | |
190 | nt.assert_equal(sl.s, 'a 11 b 1 a 2') |
|
206 | nt.assert_equal(sl.s, 'a 11 b 1 a 2') | |
191 | nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2'])) |
|
207 | nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2'])) | |
192 | nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a'])) |
|
208 | nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a'])) | |
193 | nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11'])) |
|
209 | nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11'])) |
General Comments 0
You need to be logged in to leave comments.
Login now