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