Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d4ab62

Browse files
committed
fix #1121
1 parent 8c90be4 commit 1d4ab62

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/test/definitions/code.test.ts

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Note: This example test is leveraging the Mocha test framework.
2+
// Please refer to their documentation on https://mochajs.org/ for help.
3+
4+
5+
// Place this right on top
6+
import { initialize, PYTHON_PATH, closeActiveWindows } from '../initialize';
7+
// The module 'assert' provides assertion methods from node
8+
import * as assert from 'assert';
9+
import { EOL } from 'os';
10+
// You can import and use all API from the 'vscode' module
11+
// as well as import your extension to test it
12+
import * as vscode from 'vscode';
13+
import * as path from 'path';
14+
import * as settings from '../../client/common/configSettings';
15+
16+
let pythonSettings = settings.PythonSettings.getInstance();
17+
let autoCompPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'definition');
18+
const fileOne = path.join(autoCompPath, 'one.py');
19+
const fileTwo = path.join(autoCompPath, 'two.py');
20+
const fileThree = path.join(autoCompPath, 'three.py');
21+
const fileEncoding = path.join(autoCompPath, 'four.py');
22+
const fileEncodingUsed = path.join(autoCompPath, 'five.py');
23+
24+
suite('Code Definition', () => {
25+
suiteSetup(done => {
26+
initialize().then(() => {
27+
pythonSettings.pythonPath = PYTHON_PATH;
28+
done();
29+
}, done);
30+
});
31+
32+
suiteTeardown(done => {
33+
closeActiveWindows().then(done, done);
34+
});
35+
teardown(done => {
36+
closeActiveWindows().then(done, done);
37+
});
38+
39+
test('Go to method', done => {
40+
let textEditor: vscode.TextEditor;
41+
let textDocument: vscode.TextDocument;
42+
vscode.workspace.openTextDocument(fileOne).then(document => {
43+
textDocument = document;
44+
return vscode.window.showTextDocument(textDocument);
45+
}).then(editor => {
46+
assert(vscode.window.activeTextEditor, 'No active editor');
47+
textEditor = editor;
48+
const position = new vscode.Position(30, 5);
49+
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
50+
}).then(def => {
51+
assert.equal(def.length, 1, 'Definition length is incorrect');
52+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '17,4', 'Start position is incorrect');
53+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '21,11', 'End position is incorrect');
54+
}).then(done, done);
55+
});
56+
57+
test('Go to function', done => {
58+
let textEditor: vscode.TextEditor;
59+
let textDocument: vscode.TextDocument;
60+
vscode.workspace.openTextDocument(fileOne).then(document => {
61+
textDocument = document;
62+
return vscode.window.showTextDocument(textDocument);
63+
}).then(editor => {
64+
assert(vscode.window.activeTextEditor, 'No active editor');
65+
textEditor = editor;
66+
const position = new vscode.Position(45, 5);
67+
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
68+
}).then(def => {
69+
assert.equal(def.length, 1, 'Definition length is incorrect');
70+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '32,0', 'Start position is incorrect');
71+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '33,21', 'End position is incorrect');
72+
}).then(done, done);
73+
});
74+
75+
test('Across files', done => {
76+
let textEditor: vscode.TextEditor;
77+
let textDocument: vscode.TextDocument;
78+
vscode.workspace.openTextDocument(fileThree).then(document => {
79+
textDocument = document;
80+
return vscode.window.showTextDocument(textDocument);
81+
}).then(editor => {
82+
assert(vscode.window.activeTextEditor, 'No active editor');
83+
textEditor = editor;
84+
const position = new vscode.Position(1, 5);
85+
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
86+
}).then(def => {
87+
assert.equal(def.length, 1, 'Definition length is incorrect');
88+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '0,0', 'Start position is incorrect');
89+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '5,11', 'End position is incorrect');
90+
assert.equal(def[0].uri.fsPath, fileTwo, 'File is incorrect');
91+
}).then(done, done);
92+
});
93+
94+
test('With Unicode Characters', done => {
95+
let textEditor: vscode.TextEditor;
96+
let textDocument: vscode.TextDocument;
97+
vscode.workspace.openTextDocument(fileEncoding).then(document => {
98+
textDocument = document;
99+
return vscode.window.showTextDocument(textDocument);
100+
}).then(editor => {
101+
assert(vscode.window.activeTextEditor, 'No active editor');
102+
textEditor = editor;
103+
const position = new vscode.Position(25, 6);
104+
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
105+
}).then(def => {
106+
assert.equal(def.length, 1, 'Definition length is incorrect');
107+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '10,4', 'Start position is incorrect');
108+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '16,35', 'End position is incorrect');
109+
assert.equal(def[0].uri.fsPath, fileEncoding, 'File is incorrect');
110+
}).then(done, done);
111+
});
112+
113+
test('Across files with Unicode Characters', done => {
114+
let textEditor: vscode.TextEditor;
115+
let textDocument: vscode.TextDocument;
116+
vscode.workspace.openTextDocument(fileEncodingUsed).then(document => {
117+
textDocument = document;
118+
return vscode.window.showTextDocument(textDocument);
119+
}).then(editor => {
120+
assert(vscode.window.activeTextEditor, 'No active editor');
121+
textEditor = editor;
122+
const position = new vscode.Position(1, 11);
123+
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
124+
}).then(def => {
125+
assert.equal(def.length, 1, 'Definition length is incorrect');
126+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '18,0', 'Start position is incorrect');
127+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '23,16', 'End position is incorrect');
128+
assert.equal(def[0].uri.fsPath, fileEncoding, 'File is incorrect');
129+
}).then(done, done);
130+
});
131+
});

0 commit comments

Comments
 (0)