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