FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bpython/bpython/test/test_line_properties.py at master · godhelpjun/bpython · GitHub
godhelpjun
/
bpython
Public
forked from
bpython/bpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bpython
/
bpython
/
test
/
test_line_properties.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
363 lines (292 loc) · 12.2 KB
Breadcrumbs
bpython
/
bpython
/
test
/
test_line_properties.py
Copy path
File metadata and controls
363 lines (292 loc) · 12.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# encoding: utf-8
import
re
from
bpython
.
test
import
unittest
from
bpython
.
line
import
current_word
,
current_dict_key
,
current_dict
, \
current_string
,
current_object
,
current_object_attribute
, \
current_from_import_from
,
current_from_import_import
,
current_import
, \
current_method_definition_name
,
current_single_word
, \
current_expression_attribute
,
current_dotted_attribute
def
cursor
(
s
):
"""'ab|c' -> (2, 'abc')"""
cursor_offset
=
s
.
index
(
'|'
)
line
=
s
[:
cursor_offset
]
+
s
[
cursor_offset
+
1
:]
return
cursor_offset
,
line
def
decode
(
s
):
"""'a<bd|c>d' -> ((3, 'abcd'), (1, 3, 'bdc'))"""
if
not
s
.
count
(
'|'
)
==
1
:
raise
ValueError
(
'match helper needs | to occur once'
)
if
s
.
count
(
'<'
)
!=
s
.
count
(
'>'
)
or
s
.
count
(
'<'
)
not
in
(
0
,
1
):
raise
ValueError
(
'match helper needs <, and > to occur just once'
)
matches
=
list
(
re
.
finditer
(
r'[<>|]'
,
s
))
assert
len
(
matches
)
in
[
1
,
3
], [
m
.
group
()
for
m
in
matches
]
d
=
{}
for
i
,
m
in
enumerate
(
matches
):
d
[
m
.
group
(
0
)]
=
m
.
start
()
-
i
s
=
s
[:
m
.
start
()
-
i
]
+
s
[
m
.
end
()
-
i
:]
assert
len
(
d
)
in
[
1
,
3
],
'need all the parts just once! %r'
%
d
if
'<'
in
d
:
return
(
d
[
'|'
],
s
), (
d
[
'<'
],
d
[
'>'
],
s
[
d
[
'<'
]:
d
[
'>'
]])
else
:
return
(
d
[
'|'
],
s
),
None
def
line_with_cursor
(
cursor_offset
,
line
):
return
line
[:
cursor_offset
]
+
'|'
+
line
[
cursor_offset
:]
def
encode
(
cursor_offset
,
line
,
result
):
"""encode(3, 'abdcd', (1, 3, 'bdc')) -> a<bd|c>d'
Written for prettier assert error messages
"""
encoded_line
=
line_with_cursor
(
cursor_offset
,
line
)
if
result
is
None
:
return
encoded_line
start
,
end
,
value
=
result
assert
line
[
start
:
end
]
==
value
if
start
<
cursor_offset
:
encoded_line
=
encoded_line
[:
start
]
+
'<'
+
encoded_line
[
start
:]
else
:
encoded_line
=
(
encoded_line
[:
start
+
1
]
+
'<'
+
encoded_line
[
start
+
1
:])
if
end
<
cursor_offset
:
encoded_line
=
encoded_line
[:
end
+
1
]
+
'>'
+
encoded_line
[
end
+
1
:]
else
:
encoded_line
=
encoded_line
[:
end
+
2
]
+
'>'
+
encoded_line
[
end
+
2
:]
return
encoded_line
class
LineTestCase
(
unittest
.
TestCase
):
def
assertAccess
(
self
,
s
):
r"""Asserts that self.func matches as described
by s, which uses a little language to describe matches:
abcd<efg>hijklmnopqrstuvwx|yz
/|\ /|\ /|\
| | |
the function should the current cursor position
match this "efg" is between the x and y
"""
(
cursor_offset
,
line
),
match
=
decode
(
s
)
result
=
self
.
func
(
cursor_offset
,
line
)
self
.
assertEqual
(
result
,
match
,
"%s(%r) result
\n
%r (%r) doesn't match expected
\n
%r (%r)"
%
(
self
.
func
.
__name__
,
line_with_cursor
(
cursor_offset
,
line
),
encode
(
cursor_offset
,
line
,
result
),
result
,
s
,
match
))
class
TestHelpers
(
LineTestCase
):
def
test_I
(
self
):
self
.
assertEqual
(
cursor
(
'asd|fgh'
), (
3
,
'asdfgh'
))
def
test_decode
(
self
):
self
.
assertEqual
(
decode
(
'a<bd|c>d'
), ((
3
,
'abdcd'
), (
1
,
4
,
'bdc'
)))
self
.
assertEqual
(
decode
(
'a|<bdc>d'
), ((
1
,
'abdcd'
), (
1
,
4
,
'bdc'
)))
self
.
assertEqual
(
decode
(
'a<bdc>d|'
), ((
5
,
'abdcd'
), (
1
,
4
,
'bdc'
)))
def
test_encode
(
self
):
self
.
assertEqual
(
encode
(
3
,
'abdcd'
, (
1
,
4
,
'bdc'
)),
'a<bd|c>d'
)
self
.
assertEqual
(
encode
(
1
,
'abdcd'
, (
1
,
4
,
'bdc'
)),
'a|<bdc>d'
)
self
.
assertEqual
(
encode
(
4
,
'abdcd'
, (
1
,
4
,
'bdc'
)),
'a<bdc|>d'
)
self
.
assertEqual
(
encode
(
5
,
'abdcd'
, (
1
,
4
,
'bdc'
)),
'a<bdc>d|'
)
def
test_assert_access
(
self
):
def
dumb_func
(
cursor_offset
,
line
):
return
(
0
,
2
,
'ab'
)
self
.
func
=
dumb_func
self
.
assertAccess
(
'<a|b>d'
)
class
TestCurrentWord
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_word
def
test_simple
(
self
):
self
.
assertAccess
(
'|'
)
self
.
assertAccess
(
'|asdf'
)
self
.
assertAccess
(
'<a|sdf>'
)
self
.
assertAccess
(
'<asdf|>'
)
self
.
assertAccess
(
'<asdfg|>'
)
self
.
assertAccess
(
'asdf + <asdfg|>'
)
self
.
assertAccess
(
'<asdfg|> + asdf'
)
def
test_inside
(
self
):
self
.
assertAccess
(
'<asd|>'
)
self
.
assertAccess
(
'<asd|fg>'
)
def
test_dots
(
self
):
self
.
assertAccess
(
'<Object.attr1|>'
)
self
.
assertAccess
(
'<Object.attr1.attr2|>'
)
self
.
assertAccess
(
'<Object.att|r1.attr2>'
)
self
.
assertAccess
(
'stuff[stuff] + {123: 456} + <Object.attr1.attr2|>'
)
self
.
assertAccess
(
'stuff[<asd|fg>]'
)
self
.
assertAccess
(
'stuff[asdf[<asd|fg>]'
)
def
test_non_dots
(
self
):
self
.
assertAccess
(
'].asdf|'
)
self
.
assertAccess
(
').asdf|'
)
self
.
assertAccess
(
'foo[0].asdf|'
)
self
.
assertAccess
(
'foo().asdf|'
)
self
.
assertAccess
(
'foo().|'
)
self
.
assertAccess
(
'foo().asdf.|'
)
self
.
assertAccess
(
'foo[0].asdf.|'
)
def
test_open_paren
(
self
):
self
.
assertAccess
(
'<foo(|>'
)
# documenting current behavior - TODO is this intended?
class
TestCurrentDictKey
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_dict_key
def
test_simple
(
self
):
self
.
assertAccess
(
'asdf|'
)
self
.
assertAccess
(
'asdf|'
)
self
.
assertAccess
(
'asdf[<>|'
)
self
.
assertAccess
(
'asdf[<>|]'
)
self
.
assertAccess
(
'object.dict[<abc|>'
)
self
.
assertAccess
(
'asdf|'
)
self
.
assertAccess
(
'asdf[<(>|]'
)
self
.
assertAccess
(
'asdf[<(1>|]'
)
self
.
assertAccess
(
'asdf[<(1,>|]'
)
self
.
assertAccess
(
'asdf[<(1, >|]'
)
self
.
assertAccess
(
'asdf[<(1, 2)>|]'
)
# TODO self.assertAccess('d[d[<12|>')
self
.
assertAccess
(
"d[<'a>|"
)
class
TestCurrentDict
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_dict
def
test_simple
(
self
):
self
.
assertAccess
(
'asdf|'
)
self
.
assertAccess
(
'asdf|'
)
self
.
assertAccess
(
'<asdf>[|'
)
self
.
assertAccess
(
'<asdf>[|]'
)
self
.
assertAccess
(
'<object.dict>[abc|'
)
self
.
assertAccess
(
'asdf|'
)
class
TestCurrentString
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_string
def
test_closed
(
self
):
self
.
assertAccess
(
'"<as|df>"'
)
self
.
assertAccess
(
'"<asdf|>"'
)
self
.
assertAccess
(
'"<|asdf>"'
)
self
.
assertAccess
(
"'<asdf|>'"
)
self
.
assertAccess
(
"'<|asdf>'"
)
self
.
assertAccess
(
"'''<asdf|>'''"
)
self
.
assertAccess
(
'"""<asdf|>"""'
)
self
.
assertAccess
(
'asdf.afd("a") + "<asdf|>"'
)
def
test_open
(
self
):
self
.
assertAccess
(
'"<as|df>'
)
self
.
assertAccess
(
'"<asdf|>'
)
self
.
assertAccess
(
'"<|asdf>'
)
self
.
assertAccess
(
"'<asdf|>"
)
self
.
assertAccess
(
"'<|asdf>"
)
self
.
assertAccess
(
"'''<asdf|>"
)
self
.
assertAccess
(
'"""<asdf|>'
)
self
.
assertAccess
(
'asdf.afd("a") + "<asdf|>'
)
class
TestCurrentObject
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_object
def
test_simple
(
self
):
self
.
assertAccess
(
'<Object>.attr1|'
)
self
.
assertAccess
(
'<Object>.|'
)
self
.
assertAccess
(
'Object|'
)
self
.
assertAccess
(
'Object|.'
)
self
.
assertAccess
(
'<Object>.|'
)
self
.
assertAccess
(
'<Object.attr1>.attr2|'
)
self
.
assertAccess
(
'<Object>.att|r1.attr2'
)
self
.
assertAccess
(
'stuff[stuff] + {123: 456} + <Object.attr1>.attr2|'
)
self
.
assertAccess
(
'stuff[asd|fg]'
)
self
.
assertAccess
(
'stuff[asdf[asd|fg]'
)
class
TestCurrentAttribute
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_object_attribute
def
test_simple
(
self
):
self
.
assertAccess
(
'Object.<attr1|>'
)
self
.
assertAccess
(
'Object.attr1.<attr2|>'
)
self
.
assertAccess
(
'Object.<att|r1>.attr2'
)
self
.
assertAccess
(
'stuff[stuff] + {123: 456} + Object.attr1.<attr2|>'
)
self
.
assertAccess
(
'stuff[asd|fg]'
)
self
.
assertAccess
(
'stuff[asdf[asd|fg]'
)
self
.
assertAccess
(
'Object.attr1.<|attr2>'
)
self
.
assertAccess
(
'Object.<attr1|>.attr2'
)
class
TestCurrentFromImportFrom
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_from_import_from
def
test_simple
(
self
):
self
.
assertAccess
(
'from <sys|> import path'
)
self
.
assertAccess
(
'from <sys> import path|'
)
self
.
assertAccess
(
'if True|: from sys import path'
)
self
.
assertAccess
(
'if True: |from sys import path'
)
self
.
assertAccess
(
'if True: from <sys> import p|ath'
)
self
.
assertAccess
(
'if True: from sys imp|ort path'
)
self
.
assertAccess
(
'if True: from sys import |path'
)
self
.
assertAccess
(
'if True: from sys import path.stu|ff'
)
self
.
assertAccess
(
'if True: from <sys.path> import sep|'
)
self
.
assertAccess
(
'from <os.p|>'
)
class
TestCurrentFromImportImport
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_from_import_import
def
test_simple
(
self
):
self
.
assertAccess
(
'from sys import <path|>'
)
self
.
assertAccess
(
'from sys import <p|ath>'
)
self
.
assertAccess
(
'from sys import |path'
)
self
.
assertAccess
(
'from sys| import path'
)
self
.
assertAccess
(
'from s|ys import path'
)
self
.
assertAccess
(
'from |sys import path'
)
self
.
assertAccess
(
'from xml.dom import <N|ode>'
)
# because syntax error
self
.
assertAccess
(
'from xml.dom import Node.as|d'
)
class
TestCurrentImport
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_import
def
test_simple
(
self
):
self
.
assertAccess
(
'import <path|>'
)
self
.
assertAccess
(
'import <p|ath>'
)
self
.
assertAccess
(
'import |path'
)
self
.
assertAccess
(
'import path, <another|>'
)
self
.
assertAccess
(
'import path another|'
)
self
.
assertAccess
(
'if True: import <path|>'
)
self
.
assertAccess
(
'if True: import <xml.dom.minidom|>'
)
self
.
assertAccess
(
'if True: import <xml.do|m.minidom>'
)
self
.
assertAccess
(
'if True: import <xml.do|m.minidom> as something'
)
class
TestMethodDefinitionName
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_method_definition_name
def
test_simple
(
self
):
self
.
assertAccess
(
'def <foo|>'
)
self
.
assertAccess
(
' def bar(x, y)|:'
)
self
.
assertAccess
(
' def <bar|>(x, y)'
)
class
TestSingleWord
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_single_word
def
test_simple
(
self
):
self
.
assertAccess
(
'foo.bar|'
)
self
.
assertAccess
(
'.foo|'
)
self
.
assertAccess
(
' <foo|>'
)
class
TestCurrentExpressionAttribute
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_expression_attribute
def
test_simple
(
self
):
self
.
assertAccess
(
'Object.<attr1|>.'
)
self
.
assertAccess
(
'Object.<|attr1>.'
)
self
.
assertAccess
(
'Object.(|)'
)
self
.
assertAccess
(
'Object.another.(|)'
)
self
.
assertAccess
(
'asdf asdf asdf.(abc|)'
)
def
test_without_dot
(
self
):
self
.
assertAccess
(
'Object|'
)
self
.
assertAccess
(
'Object|.'
)
self
.
assertAccess
(
'|Object.'
)
def
test_with_whitespace
(
self
):
self
.
assertAccess
(
'Object. <attr|>'
)
self
.
assertAccess
(
'Object .<attr|>'
)
self
.
assertAccess
(
'Object . <attr|>'
)
self
.
assertAccess
(
'Object .asdf attr|'
)
self
.
assertAccess
(
'Object .<asdf|> attr'
)
self
.
assertAccess
(
'Object. asdf attr|'
)
self
.
assertAccess
(
'Object. <asdf|> attr'
)
self
.
assertAccess
(
'Object . asdf attr|'
)
self
.
assertAccess
(
'Object . <asdf|> attr'
)
def
test_indexing
(
self
):
self
.
assertAccess
(
'abc[def].<ghi|>'
)
self
.
assertAccess
(
'abc[def].<|ghi>'
)
self
.
assertAccess
(
'abc[def].<gh|i>'
)
self
.
assertAccess
(
'abc[def].gh |i'
)
self
.
assertAccess
(
'abc[def]|'
)
def
test_strings
(
self
):
self
.
assertAccess
(
'"hey".<a|>'
)
self
.
assertAccess
(
'"hey"|'
)
self
.
assertAccess
(
'"hey"|.a'
)
self
.
assertAccess
(
'"hey".<a|b>'
)
self
.
assertAccess
(
'"hey".asdf d|'
)
self
.
assertAccess
(
'"hey".<|>'
)
class
TestCurrentDottedAttribute
(
LineTestCase
):
def
setUp
(
self
):
self
.
func
=
current_dotted_attribute
def
test_simple
(
self
):
self
.
assertAccess
(
'<obj.attr>|'
)
self
.
assertAccess
(
'(<obj.attr>|'
)
self
.
assertAccess
(
'[<obj.attr>|'
)
self
.
assertAccess
(
'm.body[0].value|'
)
self
.
assertAccess
(
'm.body[0].attr.value|'
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Back
|
FazBrowse Home
|
New Git URL