|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import * as path from 'path'; |
| 8 | +import testAssetWorkspace from './testAssets/testAssetWorkspace'; |
| 9 | +import { activateCSharpExtension, openFileInWorkspaceAsync } from './integrationHelpers'; |
| 10 | +import { describe, beforeAll, beforeEach, afterAll, test, expect } from '@jest/globals'; |
| 11 | + |
| 12 | +describe(`[${testAssetWorkspace.description}] Test Go To Definition`, function () { |
| 13 | + beforeAll(async function () { |
| 14 | + await activateCSharpExtension(); |
| 15 | + }); |
| 16 | + |
| 17 | + beforeEach(async function () { |
| 18 | + await openFileInWorkspaceAsync(path.join('src', 'app', 'definition.cs')); |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(async () => { |
| 22 | + await testAssetWorkspace.cleanupWorkspace(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('Navigates to definition in same file', async () => { |
| 26 | + const requestPosition = new vscode.Position(10, 31); |
| 27 | + const definitionList = <vscode.Location[]>( |
| 28 | + await vscode.commands.executeCommand( |
| 29 | + 'vscode.executeDefinitionProvider', |
| 30 | + vscode.window.activeTextEditor!.document.uri, |
| 31 | + requestPosition |
| 32 | + ) |
| 33 | + ); |
| 34 | + expect(definitionList.length).toEqual(1); |
| 35 | + expect(definitionList[0].uri.path).toContain('definition.cs'); |
| 36 | + expect(definitionList[0].range).toStrictEqual(new vscode.Range(6, 29, 6, 32)); |
| 37 | + }); |
| 38 | + |
| 39 | + test('Navigates to definition in different file', async () => { |
| 40 | + const requestPosition = new vscode.Position(16, 19); |
| 41 | + const definitionList = <vscode.Location[]>( |
| 42 | + await vscode.commands.executeCommand( |
| 43 | + 'vscode.executeDefinitionProvider', |
| 44 | + vscode.window.activeTextEditor!.document.uri, |
| 45 | + requestPosition |
| 46 | + ) |
| 47 | + ); |
| 48 | + expect(definitionList.length).toEqual(1); |
| 49 | + expect(definitionList[0].uri.path).toContain('diagnostics.cs'); |
| 50 | + expect(definitionList[0].range).toStrictEqual(new vscode.Range(4, 17, 4, 23)); |
| 51 | + |
| 52 | + await navigate(requestPosition, definitionList, 'diagnostics.cs'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('Navigates to definition in decompiled source', async () => { |
| 56 | + await openFileInWorkspaceAsync(path.join('test', 'UnitTest1.cs')); |
| 57 | + |
| 58 | + // Get definitions |
| 59 | + const requestPosition = new vscode.Position(13, 9); |
| 60 | + const definitionList = <vscode.Location[]>( |
| 61 | + await vscode.commands.executeCommand( |
| 62 | + 'vscode.executeDefinitionProvider', |
| 63 | + vscode.window.activeTextEditor!.document.uri, |
| 64 | + requestPosition |
| 65 | + ) |
| 66 | + ); |
| 67 | + expect(definitionList.length).toEqual(1); |
| 68 | + const definitionPath = definitionList[0].uri; |
| 69 | + expect(definitionPath.fsPath).toContain('FactAttribute.cs'); |
| 70 | + |
| 71 | + // Navigate |
| 72 | + await navigate(requestPosition, definitionList, 'FactAttribute.cs'); |
| 73 | + expect(vscode.window.activeTextEditor?.document.getText()).toContain( |
| 74 | + '// Decompiled with ICSharpCode.Decompiler' |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + test('Navigates to definition in metadata as source', async () => { |
| 79 | + // Get definitions |
| 80 | + const requestPosition = new vscode.Position(10, 25); |
| 81 | + const definitionList = <vscode.Location[]>( |
| 82 | + await vscode.commands.executeCommand( |
| 83 | + 'vscode.executeDefinitionProvider', |
| 84 | + vscode.window.activeTextEditor!.document.uri, |
| 85 | + requestPosition |
| 86 | + ) |
| 87 | + ); |
| 88 | + expect(definitionList.length).toEqual(1); |
| 89 | + const definitionPath = definitionList[0].uri; |
| 90 | + expect(definitionPath.fsPath).toContain('Console.cs'); |
| 91 | + |
| 92 | + // Navigate |
| 93 | + await navigate(requestPosition, definitionList, 'Console.cs'); |
| 94 | + expect(vscode.window.activeTextEditor?.document.getText()).not.toContain( |
| 95 | + '// Decompiled with ICSharpCode.Decompiler' |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + test('Returns multiple definitions for partial types', async () => { |
| 100 | + const definitionList = <vscode.Location[]>( |
| 101 | + await vscode.commands.executeCommand( |
| 102 | + 'vscode.executeDefinitionProvider', |
| 103 | + vscode.window.activeTextEditor!.document.uri, |
| 104 | + new vscode.Position(4, 25) |
| 105 | + ) |
| 106 | + ); |
| 107 | + expect(definitionList.length).toEqual(2); |
| 108 | + expect(definitionList[0].uri.path).toContain('definition.cs'); |
| 109 | + expect(definitionList[0].range).toStrictEqual( |
| 110 | + new vscode.Range(new vscode.Position(4, 25), new vscode.Position(4, 35)) |
| 111 | + ); |
| 112 | + expect(definitionList[1].uri.path).toContain('definition.cs'); |
| 113 | + expect(definitionList[1].range).toStrictEqual( |
| 114 | + new vscode.Range(new vscode.Position(14, 25), new vscode.Position(14, 35)) |
| 115 | + ); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +async function navigate( |
| 120 | + originalPosition: vscode.Position, |
| 121 | + definitionLocations: vscode.Location[], |
| 122 | + expectedFileName: string |
| 123 | +): Promise<void> { |
| 124 | + const windowChanged = new Promise<void>((resolve, _) => { |
| 125 | + vscode.window.onDidChangeActiveTextEditor((_e) => { |
| 126 | + if (_e?.document.fileName.includes(expectedFileName)) { |
| 127 | + resolve(); |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + await vscode.commands.executeCommand( |
| 133 | + 'editor.action.goToLocations', |
| 134 | + vscode.window.activeTextEditor!.document.uri, |
| 135 | + originalPosition, |
| 136 | + definitionLocations, |
| 137 | + 'goto', |
| 138 | + 'Failed to navigate' |
| 139 | + ); |
| 140 | + |
| 141 | + // Navigation happens asynchronously when a different file is opened, so we need to wait for the window to change. |
| 142 | + await windowChanged; |
| 143 | + |
| 144 | + expect(vscode.window.activeTextEditor?.document.fileName).toContain(expectedFileName); |
| 145 | +} |
0 commit comments