forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigation.test.ts
More file actions
134 lines (114 loc) · 5.29 KB
/
navigation.test.ts
File metadata and controls
134 lines (114 loc) · 5.29 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
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import * as path from 'path';
import * as vscode from 'vscode';
import { isLanguageServerTest } from '../../client/common/constants';
import { closeActiveWindows, initialize, initializeTest } from '../initialize';
const decoratorsPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'definition', 'navigation');
const fileDefinitions = path.join(decoratorsPath, 'definitions.py');
const fileUsages = path.join(decoratorsPath, 'usages.py');
// tslint:disable-next-line:max-func-body-length
suite('Definition Navigation', () => {
suiteSetup(initialize);
setup(initializeTest);
suiteTeardown(closeActiveWindows);
teardown(closeActiveWindows);
const assertFile = (expectedLocation: string, location: vscode.Uri) => {
const relLocation = vscode.workspace.asRelativePath(location);
const expectedRelLocation = vscode.workspace.asRelativePath(expectedLocation);
assert.equal(expectedRelLocation, relLocation, 'Position is in wrong file');
};
const formatPosition = (position: vscode.Position) => {
return `${position.line},${position.character}`;
};
const assertRange = (expectedRange: vscode.Range, range: vscode.Range) => {
assert.equal(formatPosition(expectedRange.start), formatPosition(range.start), 'Start position is incorrect');
assert.equal(formatPosition(expectedRange.end), formatPosition(range.end), 'End position is incorrect');
};
const buildTest = (startFile: string, startPosition: vscode.Position, expectedFiles: string[], expectedRanges: vscode.Range[]) => {
return async () => {
const textDocument = await vscode.workspace.openTextDocument(startFile);
await vscode.window.showTextDocument(textDocument);
assert(vscode.window.activeTextEditor, 'No active editor');
const locations = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, startPosition);
assert.equal(expectedFiles.length, locations!.length, 'Wrong number of results');
for (let i = 0; i < locations!.length; i += 1) {
assertFile(expectedFiles[i], locations![i].uri);
assertRange(expectedRanges[i], locations![i].range!);
}
};
};
test('From own definition', buildTest(
fileDefinitions,
new vscode.Position(2, 6),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(2, 4, 2, 16)] : [new vscode.Range(2, 0, 11, 17)]
));
test('Nested function', buildTest(
fileDefinitions,
new vscode.Position(11, 16),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(6, 8, 6, 15)] : [new vscode.Range(6, 4, 10, 16)]
));
test('Decorator usage', buildTest(
fileDefinitions,
new vscode.Position(13, 1),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(2, 4, 2, 16)] : [new vscode.Range(2, 0, 11, 17)]
));
test('Function decorated by stdlib', buildTest(
fileDefinitions,
new vscode.Position(29, 6),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(21, 4, 21, 22)] : [new vscode.Range(21, 0, 27, 17)]
));
test('Function decorated by local decorator', buildTest(
fileDefinitions,
new vscode.Position(30, 6),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(14, 4, 14, 9)] : [new vscode.Range(14, 0, 18, 7)]
));
test('Module imported decorator usage', buildTest(
fileUsages,
new vscode.Position(3, 15),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(2, 4, 2, 16)] : [new vscode.Range(2, 0, 11, 17)]
));
test('Module imported function decorated by stdlib', buildTest(
fileUsages,
new vscode.Position(11, 19),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(21, 4, 21, 22)] : [new vscode.Range(21, 0, 27, 17)]
));
test('Module imported function decorated by local decorator', buildTest(
fileUsages,
new vscode.Position(12, 19),
[fileDefinitions],
isLanguageServerTest() ? [new vscode.Range(14, 4, 14, 9)] : [new vscode.Range(14, 0, 18, 7)]
));
test('Specifically imported decorator usage', buildTest(
fileUsages,
new vscode.Position(7, 1),
isLanguageServerTest() ? [fileDefinitions] : [fileDefinitions],
isLanguageServerTest()
? [new vscode.Range(2, 4, 2, 16)]
: [new vscode.Range(2, 0, 11, 17)]
));
test('Specifically imported function decorated by stdlib', buildTest(
fileUsages,
new vscode.Position(14, 6),
isLanguageServerTest() ? [fileDefinitions] : [fileDefinitions],
isLanguageServerTest()
? [new vscode.Range(21, 4, 21, 22)]
: [new vscode.Range(21, 0, 27, 17)]
));
test('Specifically imported function decorated by local decorator', buildTest(
fileUsages,
new vscode.Position(15, 6),
isLanguageServerTest() ? [fileDefinitions] : [fileDefinitions],
isLanguageServerTest()
? [new vscode.Range(14, 4, 14, 9)]
: [new vscode.Range(14, 0, 18, 7)]
));
});