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