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