forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.test.ts
More file actions
293 lines (276 loc) · 16.5 KB
/
hover.test.ts
File metadata and controls
293 lines (276 loc) · 16.5 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
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
// The module 'assert' provides assertion methods from node
import * as assert from 'assert';
import { EOL } from 'os';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
import * as path from 'path';
import { initialize, closeActiveWindows, initializeTest } from '../initialize';
import { normalizeMarkedString } from '../textUtils';
const autoCompPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'autocomp');
const hoverPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'hover');
const fileOne = path.join(autoCompPath, 'one.py');
const fileThree = path.join(autoCompPath, 'three.py');
const fileEncoding = path.join(autoCompPath, 'four.py');
const fileEncodingUsed = path.join(autoCompPath, 'five.py');
const fileHover = path.join(autoCompPath, 'hoverTest.py');
const fileStringFormat = path.join(hoverPath, 'stringFormat.py');
suite('Hover Definition', () => {
suiteSetup(() => initialize());
setup(() => initializeTest());
suiteTeardown(() => closeActiveWindows());
teardown(() => closeActiveWindows());
test('Method', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileOne).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(30, 5);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '30,4', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '30,11', 'End position is incorrect');
assert.equal(def[0].contents.length, 1, 'Invalid content items');
const expectedContent = '```python' + EOL + 'def method1()' + EOL + '```' + EOL + 'This is method1';
assert.equal(normalizeMarkedString(def[0].contents[0]), expectedContent, 'function signature incorrect');
}).then(done, done);
});
test('Across files', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileThree).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(1, 12);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '1,9', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '1,12', 'End position is incorrect');
assert.equal(normalizeMarkedString(def[0].contents[0]), '```python' + EOL + 'def fun()' + EOL + '```' + EOL + 'This is fun', 'Invalid conents');
}).then(done, done);
});
test('With Unicode Characters', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileEncoding).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(25, 6);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '25,4', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '25,7', 'End position is incorrect');
assert.equal(normalizeMarkedString(def[0].contents[0]), '```python' + EOL + 'def bar()' + EOL + '```' + EOL +
'说明 - keep this line, it works' + EOL + 'delete following line, it works' +
EOL + '如果存在需要等待审批或正在执行的任务,将不刷新页面', 'Invalid conents');
}).then(done, done);
});
test('Across files with Unicode Characters', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileEncodingUsed).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(1, 11);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '1,5', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '1,16', 'End position is incorrect');
assert.equal(normalizeMarkedString(def[0].contents[0]), '```python' + EOL +
'def showMessage()' + EOL +
'```' + EOL +
'Кюм ут жэмпэр пошжим льаборэж, коммюны янтэрэсщэт нам ед, декта игнота ныморэ жят эи. ' + EOL +
'Шэа декам экшырки эи, эи зыд эррэм докэндё, векж факэтэ пэрчыквюэрёж ку.', 'Invalid conents');
}).then(done, done);
});
test('Nothing for keywords (class)', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileOne).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(5, 1);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 0, 'Definition length is incorrect');
}).then(done, done);
});
test('Nothing for keywords (for)', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileHover).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(3, 1);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 0, 'Definition length is incorrect');
}).then(done, done);
});
test('Highlighting Class', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileHover).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(11, 15);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '11,12', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '11,18', 'End position is incorrect');
let documentation = "```python" + EOL +
"class Random(x=None)" + EOL +
"```" + EOL +
"Random number generator base class used by bound module functions." + EOL +
"" + EOL +
"Used to instantiate instances of Random to get generators that don't" + EOL +
"share state." + EOL +
"" + EOL +
"Class Random can also be subclassed if you want to use a different basic" + EOL +
"generator of your own devising: in that case, override the following" + EOL + EOL +
"`methods` random(), seed(), getstate(), and setstate()." + EOL + EOL +
"Optionally, implement a getrandbits() method so that randrange()" + EOL +
"can cover arbitrarily large ranges.";
assert.equal(normalizeMarkedString(def[0].contents[0]), documentation, 'Invalid conents');
}).then(done, done);
});
test('Highlight Method', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileHover).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(12, 10);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '12,5', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '12,12', 'End position is incorrect');
assert.equal(normalizeMarkedString(def[0].contents[0]), '```python' + EOL +
'def randint(a, b)' + EOL +
'```' + EOL +
'Return random integer in range [a, b], including both end points.', 'Invalid conents');
}).then(done, done);
});
test('Highlight Function', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileHover).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(8, 14);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '8,11', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '8,15', 'End position is incorrect');
assert.equal(normalizeMarkedString(def[0].contents[0]), '```python' + EOL +
'def acos(x)' + EOL +
'```' + EOL +
'Return the arc cosine (measured in radians) of x.', 'Invalid conents');
}).then(done, done);
});
test('Highlight Multiline Method Signature', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileHover).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(14, 14);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '14,9', 'Start position is incorrect');
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '14,15', 'End position is incorrect');
assert.equal(normalizeMarkedString(def[0].contents[0]), '```python' + EOL +
'class Thread(group=None, target=None, name=None, args=(), kwargs=None, verbose=None)' + EOL +
'```' + EOL +
'Thread(self, group=None, target=None, name=None,' + EOL +
'args=(), kwargs=None, verbose=None)' + EOL +
'' + EOL +
'A class that represents a thread of control.' + EOL +
'' + EOL +
'This class can be safely subclassed in a limited fashion.', 'Invalid content items');
}).then(done, done);
});
test('Variable', done => {
let textEditor: vscode.TextEditor;
let textDocument: vscode.TextDocument;
vscode.workspace.openTextDocument(fileHover).then(document => {
textDocument = document;
return vscode.window.showTextDocument(textDocument);
}).then(editor => {
assert(vscode.window.activeTextEditor, 'No active editor');
textEditor = editor;
const position = new vscode.Position(6, 2);
return vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
}).then(def => {
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(def[0].contents.length, 1, 'Only expected one result');
const contents = normalizeMarkedString(def[0].contents[0]);
if (contents.indexOf("```python") === -1) {
assert.fail(contents, "", "First line is incorrect", "compare");
}
if (contents.indexOf("Random number generator base class used by bound module functions.") === -1) {
assert.fail(contents, "", "'Random number generator' message missing", "compare");
}
if (contents.indexOf("Class Random can also be subclassed if you want to use a different basic") === -1) {
assert.fail(contents, "", "'Class Random message' missing", "compare");
}
}).then(done, done);
});
test('format().capitalize()', async () => {
const textDocument = await vscode.workspace.openTextDocument(fileStringFormat);
await vscode.window.showTextDocument(textDocument);
const position = new vscode.Position(5, 41);
const def = await vscode.commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', textDocument.uri, position);
assert.equal(def.length, 1, 'Definition length is incorrect');
assert.equal(def[0].contents.length, 1, 'Only expected one result');
const contents = normalizeMarkedString(def[0].contents[0]);
if (contents.indexOf("def capitalize") === -1) {
assert.fail(contents, "", "'def capitalize' is missing", "compare");
}
if (contents.indexOf("Return a capitalized version of S") === -1 &&
contents.indexOf("Return a copy of the string S with only its first character") === -1) {
assert.fail(contents, "", "'Return a capitalized version of S/Return a copy of the string S with only its first character' message missing", "compare");
}
});
});