|
| 1 | +// Licensed under the MIT License. |
| 2 | + |
| 3 | +'use strict'; |
| 4 | +import * as assert from 'assert'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { closeActiveWindows, initialize, initializeTest } from '../initialize'; |
| 8 | + |
| 9 | +const decoratorsPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'definition', 'navigation'); |
| 10 | +const fileDefinitions = path.join(decoratorsPath, 'definitions.py'); |
| 11 | +const fileUsages = path.join(decoratorsPath, 'usages.py'); |
| 12 | + |
| 13 | +// tslint:disable-next-line:max-func-body-length |
| 14 | +suite('Definition Navigation', () => { |
| 15 | + suiteSetup(initialize); |
| 16 | + setup(initializeTest); |
| 17 | + suiteTeardown(closeActiveWindows); |
| 18 | + teardown(closeActiveWindows); |
| 19 | + |
| 20 | + const assertFile = (expectedLocation: string, location: vscode.Uri) => { |
| 21 | + const relLocation = vscode.workspace.asRelativePath(location); |
| 22 | + const expectedRelLocation = vscode.workspace.asRelativePath(expectedLocation); |
| 23 | + assert.equal(expectedRelLocation, relLocation, 'Position is in wrong file'); |
| 24 | + }; |
| 25 | + |
| 26 | + const formatPosition = (position: vscode.Position) => { |
| 27 | + return `${position.line},${position.character}`; |
| 28 | + }; |
| 29 | + |
| 30 | + const assertRange = (expectedRange: vscode.Range, range: vscode.Range) => { |
| 31 | + assert.equal(formatPosition(expectedRange.start), formatPosition(range.start), 'Start position is incorrect'); |
| 32 | + assert.equal(formatPosition(expectedRange.end), formatPosition(range.end), 'End position is incorrect'); |
| 33 | + }; |
| 34 | + |
| 35 | + const buildTest = (startFile: string, startPosition: vscode.Position, expectedFile: string, expectedRange: vscode.Range) => { |
| 36 | + return async () => { |
| 37 | + const textDocument = await vscode.workspace.openTextDocument(startFile); |
| 38 | + await vscode.window.showTextDocument(textDocument); |
| 39 | + assert(vscode.window.activeTextEditor, 'No active editor'); |
| 40 | + |
| 41 | + const locations = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, startPosition); |
| 42 | + assert.equal(1, locations!.length, 'Wrong number of results'); |
| 43 | + |
| 44 | + const def = locations![0]; |
| 45 | + assertFile(expectedFile, def.uri); |
| 46 | + assertRange(expectedRange, def.range!); |
| 47 | + }; |
| 48 | + }; |
| 49 | + |
| 50 | + test('From own definition', buildTest( |
| 51 | + fileDefinitions, |
| 52 | + new vscode.Position(2, 6), |
| 53 | + fileDefinitions, |
| 54 | + new vscode.Range(2, 0, 11, 17) |
| 55 | + )); |
| 56 | + |
| 57 | + test('Nested function', buildTest( |
| 58 | + fileDefinitions, |
| 59 | + new vscode.Position(11, 16), |
| 60 | + fileDefinitions, |
| 61 | + new vscode.Range(6, 4, 10, 16) |
| 62 | + )); |
| 63 | + |
| 64 | + test('Decorator usage', buildTest( |
| 65 | + fileDefinitions, |
| 66 | + new vscode.Position(13, 1), |
| 67 | + fileDefinitions, |
| 68 | + new vscode.Range(2, 0, 11, 17) |
| 69 | + )); |
| 70 | + |
| 71 | + test('Function decorated by stdlib', buildTest( |
| 72 | + fileDefinitions, |
| 73 | + new vscode.Position(29, 6), |
| 74 | + fileDefinitions, |
| 75 | + new vscode.Range(21, 0, 27, 17) |
| 76 | + )); |
| 77 | + |
| 78 | + test('Function decorated by local decorator', buildTest( |
| 79 | + fileDefinitions, |
| 80 | + new vscode.Position(30, 6), |
| 81 | + fileDefinitions, |
| 82 | + new vscode.Range(14, 0, 18, 7) |
| 83 | + )); |
| 84 | + |
| 85 | + test('Module imported decorator usage', buildTest( |
| 86 | + fileUsages, |
| 87 | + new vscode.Position(3, 15), |
| 88 | + fileDefinitions, |
| 89 | + new vscode.Range(2, 0, 11, 17) |
| 90 | + )); |
| 91 | + |
| 92 | + test('Module imported function decorated by stdlib', buildTest( |
| 93 | + fileUsages, |
| 94 | + new vscode.Position(11, 19), |
| 95 | + fileDefinitions, |
| 96 | + new vscode.Range(21, 0, 27, 17) |
| 97 | + )); |
| 98 | + |
| 99 | + test('Module imported function decorated by local decorator', buildTest( |
| 100 | + fileUsages, |
| 101 | + new vscode.Position(12, 19), |
| 102 | + fileDefinitions, |
| 103 | + new vscode.Range(14, 0, 18, 7) |
| 104 | + )); |
| 105 | + |
| 106 | + test('Specifically imported decorator usage', buildTest( |
| 107 | + fileUsages, |
| 108 | + new vscode.Position(7, 1), |
| 109 | + fileDefinitions, |
| 110 | + new vscode.Range(2, 0, 11, 17) |
| 111 | + )); |
| 112 | + |
| 113 | + test('Specifically imported function decorated by stdlib', buildTest( |
| 114 | + fileUsages, |
| 115 | + new vscode.Position(14, 6), |
| 116 | + fileDefinitions, |
| 117 | + new vscode.Range(21, 0, 27, 17) |
| 118 | + )); |
| 119 | + |
| 120 | + test('Specifically imported function decorated by local decorator', buildTest( |
| 121 | + fileUsages, |
| 122 | + new vscode.Position(15, 6), |
| 123 | + fileDefinitions, |
| 124 | + new vscode.Range(14, 0, 18, 7) |
| 125 | + )); |
| 126 | +}); |
0 commit comments