Skip to content

Commit 1e47340

Browse files
authored
Merge pull request #7479 from dibarbet/far_tests
Add find references tests
2 parents ab263da + 49437dd commit 1e47340

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

test/integrationTests/integrationHelpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ export async function getCodeLensesAsync(): Promise<vscode.CodeLens[]> {
120120
});
121121
}
122122

123+
export function sortLocations(locations: vscode.Location[]): vscode.Location[] {
124+
return locations.sort((a, b) => {
125+
const uriCompare = a.uri.fsPath.localeCompare(b.uri.fsPath);
126+
if (uriCompare !== 0) {
127+
return uriCompare;
128+
}
129+
130+
return a.range.start.compareTo(b.range.start);
131+
});
132+
}
133+
123134
function isGivenSln(workspace: typeof vscode.workspace, expectedProjectFileName: string) {
124135
const primeWorkspace = workspace.workspaceFolders![0];
125136
const projectFileName = primeWorkspace.uri.fsPath.split(path.sep).pop();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 {
10+
activateCSharpExtension,
11+
closeAllEditorsAsync,
12+
openFileInWorkspaceAsync,
13+
sortLocations,
14+
} from './integrationHelpers';
15+
import { describe, beforeAll, beforeEach, afterAll, test, expect, afterEach } from '@jest/globals';
16+
17+
describe(`[${testAssetWorkspace.description}] Test Find References`, () => {
18+
beforeAll(async () => {
19+
await activateCSharpExtension();
20+
});
21+
22+
beforeEach(async () => {
23+
await openFileInWorkspaceAsync(path.join('src', 'app', 'reference.cs'));
24+
});
25+
26+
afterAll(async () => {
27+
await testAssetWorkspace.cleanupWorkspace();
28+
});
29+
30+
afterEach(async () => {
31+
await closeAllEditorsAsync();
32+
});
33+
34+
test('Finds references in same file', async () => {
35+
const requestPosition = new vscode.Position(13, 23);
36+
const referenceList = await getReferences(requestPosition);
37+
38+
expect(referenceList.length).toEqual(2);
39+
expect(referenceList[0].uri.path).toContain('reference.cs');
40+
expect(referenceList[0].range).toStrictEqual(new vscode.Range(6, 20, 6, 23));
41+
42+
expect(referenceList[1].uri.path).toContain('reference.cs');
43+
expect(referenceList[1].range).toStrictEqual(new vscode.Range(13, 22, 13, 25));
44+
});
45+
46+
test('Finds references in other files', async () => {
47+
const requestPosition = new vscode.Position(14, 17);
48+
const referenceList = await getReferences(requestPosition);
49+
50+
expect(referenceList.length).toEqual(2);
51+
expect(referenceList[0].uri.path).toContain('definition.cs');
52+
expect(referenceList[0].range).toStrictEqual(new vscode.Range(4, 25, 4, 35));
53+
54+
expect(referenceList[1].uri.path).toContain('reference.cs');
55+
expect(referenceList[1].range).toStrictEqual(new vscode.Range(14, 17, 14, 27));
56+
});
57+
});
58+
59+
async function getReferences(position: vscode.Position): Promise<vscode.Location[]> {
60+
const referenceList = <vscode.Location[]>(
61+
await vscode.commands.executeCommand(
62+
'vscode.executeReferenceProvider',
63+
vscode.window.activeTextEditor!.document.uri,
64+
position
65+
)
66+
);
67+
68+
return sortLocations(referenceList);
69+
}

test/integrationTests/testAssets/slnWithCsproj/src/app/reference.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Bar
1212
public Bar()
1313
{
1414
new Foo().Baz();
15+
Test.Definition d;
1516
}
1617
}
1718
}

0 commit comments

Comments
 (0)