Skip to content

Commit 5900d31

Browse files
committed
Testing: Add a test for directory completions
1 parent c295841 commit 5900d31

File tree

1 file changed

+84
-26
lines changed

1 file changed

+84
-26
lines changed

spyder/plugins/editor/widgets/tests/test_introspection.py

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ def test_dot_completions(completions_codeeditor, qtbot):
12091209
qtbot.wait(500)
12101210
assert completion.isVisible()
12111211

1212-
# Select a raondom entry in the completion widget
1212+
# Select a random entry in the completion widget
12131213
entry_index = random.randint(0, 30)
12141214
inserted_entry = completion.completion_list[entry_index]['insertText']
12151215
for _ in range(entry_index):
@@ -1225,7 +1225,7 @@ def test_dot_completions(completions_codeeditor, qtbot):
12251225
@pytest.mark.order(1)
12261226
@pytest.mark.parametrize(
12271227
"filename", ['000_test.txt', '.hidden', 'any_file.txt', 'abc.py',
1228-
'part.0.parquet', '/home', '/usr/bin', r'C:\\Users'])
1228+
'part.0.parquet'])
12291229
def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12301230
"""
12311231
Test that completions for files are handled as expected.
@@ -1249,40 +1249,20 @@ def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12491249
# This checks that we can insert file completions next to a dot when a
12501250
# filename has several dots.
12511251
qtbot.keyClicks(code_editor, "'part.0.'")
1252-
elif filename == '/home':
1253-
# This checks that we can insert file completions next to a /
1254-
qtbot.keyClicks(code_editor, "'/'")
1255-
elif filename == '/usr/bin':
1256-
# This checks that we can insert file completions next to a / placed at
1257-
# second-level
1258-
qtbot.keyClicks(code_editor, "'/usr/'")
1259-
elif filename == r'C:\\Users':
1260-
# This checks that we can insert file completions next to a \
1261-
qtbot.keyClicks(code_editor, r"'C:\\'")
12621252
else:
12631253
qtbot.keyClicks(code_editor, f"'{filename[0]}'")
12641254
code_editor.moveCursor(QTextCursor.PreviousCharacter)
12651255
qtbot.wait(500)
12661256

1267-
# Set text that will be completed
1268-
if filename == '/home':
1269-
completion_text = 'home'
1270-
elif filename == '/usr/bin':
1271-
completion_text = 'bin'
1272-
elif filename == r'C:\\Users':
1273-
completion_text = 'Users'
1274-
else:
1275-
completion_text = filename
1276-
12771257
mock_response.side_effect = lambda lang, method, params: {'params': [{
1278-
'label': f'{completion_text}',
1258+
'label': f'{filename}',
12791259
'kind': CompletionItemKind.FILE,
1280-
'sortText': (0, f'a{completion_text}'),
1281-
'insertText': f'{completion_text}',
1260+
'sortText': (0, f'a{filename}'),
1261+
'insertText': f'{filename}',
12821262
'data': {'doc_uri': path_as_uri(__file__)},
12831263
'detail': '',
12841264
'documentation': '',
1285-
'filterText': f'{completion_text}',
1265+
'filterText': f'{filename}',
12861266
'insertTextFormat': 1,
12871267
'provider': 'LSP',
12881268
'resolve': True
@@ -1296,5 +1276,83 @@ def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12961276
assert code_editor.get_text_with_eol() == f"'{filename}'"
12971277

12981278

1279+
@pytest.mark.slow
1280+
@pytest.mark.order(1)
1281+
@pytest.mark.parametrize(
1282+
"directory",
1283+
[
1284+
pytest.param(
1285+
'/home',
1286+
marks=pytest.mark.skipif(
1287+
not sys.platform.startswith('linux'),
1288+
reason='Only works on Linux'
1289+
)
1290+
),
1291+
pytest.param(
1292+
'C:\\Users',
1293+
marks=pytest.mark.skipif(
1294+
not os.name == 'nt',
1295+
reason='Only works on Windows'
1296+
)
1297+
),
1298+
pytest.param(
1299+
'C:\\Windows\\System32',
1300+
marks=pytest.mark.skipif(
1301+
not os.name == 'nt',
1302+
reason='Only works on Windows'
1303+
)
1304+
),
1305+
pytest.param(
1306+
'/Library/Frameworks',
1307+
marks=pytest.mark.skipif(
1308+
not sys.platform == 'darwin',
1309+
reason='Only works on macOS'
1310+
)
1311+
)
1312+
]
1313+
)
1314+
def test_directory_completions(directory, completions_codeeditor, qtbot):
1315+
"""
1316+
Test that directory completions work as expected.
1317+
"""
1318+
code_editor, _ = completions_codeeditor
1319+
completion = code_editor.completion_widget
1320+
1321+
qtbot.wait(500)
1322+
assert not completion.isVisible()
1323+
1324+
if directory == '/home':
1325+
qtbot.keyClicks(code_editor, "'/'")
1326+
elif directory == 'C:\\Users':
1327+
qtbot.keyClicks(code_editor, r"'C:\\'")
1328+
elif directory == 'C:\\Windows\\System32':
1329+
qtbot.keyClicks(code_editor, r"'C:\\Windows\\'")
1330+
else:
1331+
qtbot.keyClicks(code_editor, "'/Library/'")
1332+
1333+
code_editor.moveCursor(QTextCursor.PreviousCharacter)
1334+
with qtbot.waitSignal(completion.sig_show_completions,
1335+
timeout=10000):
1336+
qtbot.keyPress(code_editor, Qt.Key_Tab, delay=300)
1337+
1338+
qtbot.wait(500)
1339+
assert completion.isVisible()
1340+
1341+
# Select the corresponding entry in the completion widget
1342+
selected_entry = False
1343+
while not selected_entry:
1344+
item = completion.currentItem()
1345+
label = item.data(Qt.AccessibleTextRole).split()[0]
1346+
if directory.split(os.sep)[-1] in label:
1347+
selected_entry = True
1348+
else:
1349+
qtbot.keyPress(completion, Qt.Key_Down, delay=50)
1350+
1351+
# Insert completion and check that the inserted text is the expected one
1352+
qtbot.keyPress(completion, Qt.Key_Enter)
1353+
qtbot.wait(500)
1354+
assert code_editor.toPlainText() == f"'{directory}{os.sep}'"
1355+
1356+
12991357
if __name__ == '__main__':
13001358
pytest.main(['test_introspection.py', '--run-slow'])

0 commit comments

Comments
 (0)