forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_smart_selection.py
More file actions
360 lines (301 loc) · 9.27 KB
/
test_smart_selection.py
File metadata and controls
360 lines (301 loc) · 9.27 KB
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
import importlib
import textwrap
import normalizeSelection
def test_part_dictionary():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
not_dictionary = 'hi'
my_dict = {
"key1": "value1",
"key2": "value2"
}
print('only send the dictionary')
"""
)
expected = textwrap.dedent(
"""\
my_dict = {
"key1": "value1",
"key2": "value2"
}
"""
)
result = normalizeSelection.traverse_file(src, 3, 3, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_nested_loop():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
for i in range(1, 6):
for j in range(1, 6):
for x in range(1, 5):
for y in range(1, 5):
for z in range(1,10):
print(i, j, x, y, z)
"""
)
expected = textwrap.dedent(
"""\
for i in range(1, 6):
for j in range(1, 6):
for x in range(1, 5):
for y in range(1, 5):
for z in range(1,10):
print(i, j, x, y, z)
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_smart_shift_enter_multiple_statements():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
import textwrap
import ast
print("Porsche")
print("Genesis")
print("Audi");print("BMW");print("Mercedes")
print("dont print me")
"""
)
# Expected to printing statement line by line,
# for when multiple print statements are ran
# from the same line.
expected = textwrap.dedent(
"""\
print("Audi")
print("BMW")
print("Mercedes")
"""
)
result = normalizeSelection.traverse_file(src, 8, 8, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_two_layer_dictionary():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
print("dont print me")
two_layered_dictionary = {
'inner_dict_one': {
'Audi': 'Germany',
'BMW': 'Germnay',
'Genesis': 'Korea',
},
'inner_dict_two': {
'Mercedes': 'Germany',
'Porsche': 'Germany',
'Lamborghini': 'Italy',
'Ferrari': 'Italy',
'Maserati': 'Italy'
}
}
"""
)
expected = textwrap.dedent(
"""\
two_layered_dictionary = {
'inner_dict_one': {
'Audi': 'Germany',
'BMW': 'Germnay',
'Genesis': 'Korea',
},
'inner_dict_two': {
'Mercedes': 'Germany',
'Porsche': 'Germany',
'Lamborghini': 'Italy',
'Ferrari': 'Italy',
'Maserati': 'Italy'
}
}
"""
)
result = normalizeSelection.traverse_file(src, 6, 7, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_run_whole_func():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
print("Decide which dog you will choose")
def my_dogs():
print("Corgi")
print("Husky")
print("Corgi2")
print("Husky2")
print("no dogs")
"""
)
expected = textwrap.dedent(
"""\
def my_dogs():
print("Corgi")
print("Husky")
print("Corgi2")
print("Husky2")
print("no dogs")
"""
)
result = normalizeSelection.traverse_file(src, 2, 2, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_small_forloop():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
for i in range(1, 6):
print(i)
print("Please also send this print statement")
"""
)
expected = textwrap.dedent(
"""\
for i in range(1, 6):
print(i)
print("Please also send this print statement")
"""
)
# Cover the whole for loop block with multiple inner statements
# Make sure to contain all of the print statements included.
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def inner_for_loop_component():
"""Pressing shift+enter inside a for loop, specifically on a viable expression by itself, such as print(i) should only return that exact expression."""
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
for i in range(1, 6):
print(i)
print("Please also send this print statement")
"""
)
result = normalizeSelection.traverse_file(src, 2, 2, was_highlighted=False)
expected = textwrap.dedent(
"""\
print(i)
"""
)
assert result["normalized_smart_result"] == expected
def test_dict_comprehension():
"""Having the mouse cursor on the first line, and pressing shift+enter should return the whole dictionary comp, respecting user's code style."""
src = textwrap.dedent(
"""\
my_dict_comp = {temp_mover:
temp_mover for temp_mover in range(1, 7)}
"""
)
expected = textwrap.dedent(
"""\
my_dict_comp = {temp_mover:
temp_mover for temp_mover in range(1, 7)}
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_send_whole_generator():
"""Pressing shift+enter on the first line, which is the '(' should be returning the whole generator expression instead of just the '('."""
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
(
my_first_var
for my_first_var in range(1, 10)
if my_first_var % 2 == 0
)
"""
)
expected = textwrap.dedent(
"""\
(
my_first_var
for my_first_var in range(1, 10)
if my_first_var % 2 == 0
)
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_multiline_lambda():
"""Shift+enter on part of the lambda expression should return the whole lambda expression, regardless of whether all the component of lambda expression is on the same or not."""
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
my_lambda = lambda x: (
x + 1
)
"""
)
expected = textwrap.dedent(
"""\
my_lambda = lambda x: (
x + 1
)
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_send_whole_class():
"""Shift+enter on a class definition should send the whole class definition."""
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
class Stub(object):
def __init__(self):
self.calls = []
def add_call(self, name, args=None, kwargs=None):
self.calls.append((name, args, kwargs))
print("We should be here after running whole class")
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
expected = textwrap.dedent(
"""\
class Stub(object):
def __init__(self):
self.calls = []
def add_call(self, name, args=None, kwargs=None):
self.calls.append((name, args, kwargs))
"""
)
assert result["normalized_smart_result"] == expected
def test_send_whole_if_statement():
"""Shift+enter on an if statement should send the whole if statement including statements inside and else."""
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
if True:
print('send this')
else:
print('also send this')
print('cursor here afterwards')
"""
)
expected = textwrap.dedent(
"""\
if True:
print('send this')
else:
print('also send this')
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected
def test_send_try():
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
try:
1+1
except:
print("error")
print("Not running this")
"""
)
expected = textwrap.dedent(
"""\
try:
1+1
except:
print("error")
"""
)
result = normalizeSelection.traverse_file(src, 1, 1, was_highlighted=False)
assert result["normalized_smart_result"] == expected