Skip to content

Commit 48ff7af

Browse files
committed
Add codelens integration test
1 parent c372c4d commit 48ff7af

File tree

4 files changed

+122
-28
lines changed

4 files changed

+122
-28
lines changed

src/lsptoolshost/commands.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ async function peekReferencesCallback(uriStr: string, serverPosition: languageCl
7272
uri,
7373
vscodeApiPosition
7474
);
75+
7576
if (references && Array.isArray(references)) {
7677
// The references could come back after the document has moved to a new state (that may not even contain the position).
7778
// This is fine - the VSCode API is resilient to that scenario and will not crash.
78-
vscode.commands.executeCommand('editor.action.showReferences', uri, vscodeApiPosition, references);
79+
await vscode.commands.executeCommand('editor.action.showReferences', uri, vscodeApiPosition, references);
7980
}
8081
}
8182

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 lsp from 'vscode-languageserver-protocol';
7+
import * as vscode from 'vscode';
8+
import * as path from 'path';
9+
import { describe, beforeAll, beforeEach, afterAll, test, expect } from '@jest/globals';
10+
import testAssetWorkspace from './testAssets/testAssetWorkspace';
11+
import { activateCSharpExtension, getCodeLensesAsync, openFileInWorkspaceAsync } from './integrationHelpers';
12+
13+
describe(`[${testAssetWorkspace.description}] Test CodeLens`, function () {
14+
beforeAll(async function () {
15+
await activateCSharpExtension();
16+
});
17+
18+
beforeEach(async function () {
19+
const fileName = path.join('src', 'app', 'reference.cs');
20+
await openFileInWorkspaceAsync(fileName);
21+
});
22+
23+
afterAll(async () => {
24+
await testAssetWorkspace.cleanupWorkspace();
25+
});
26+
27+
test('CodeLens references are displayed', async () => {
28+
const codeLenses = await getCodeLensesAsync();
29+
expect(codeLenses).toHaveLength(4);
30+
31+
const fooPosition: lsp.Position = { line: 4, character: 17 };
32+
const fooRange = new vscode.Range(
33+
new vscode.Position(fooPosition.line, fooPosition.character),
34+
new vscode.Position(fooPosition.line, fooPosition.character + 3)
35+
);
36+
37+
const fooBazPosition: lsp.Position = { line: 6, character: 20 };
38+
const fooBazRange = new vscode.Range(
39+
new vscode.Position(fooBazPosition.line, fooBazPosition.character),
40+
new vscode.Position(fooBazPosition.line, fooBazPosition.character + 3)
41+
);
42+
43+
const barPosition: lsp.Position = { line: 9, character: 17 };
44+
const barRange = new vscode.Range(
45+
new vscode.Position(barPosition.line, barPosition.character),
46+
new vscode.Position(barPosition.line, barPosition.character + 3)
47+
);
48+
49+
const barBarPosition: lsp.Position = { line: 11, character: 15 };
50+
const barBarRange = new vscode.Range(
51+
new vscode.Position(barBarPosition.line, barBarPosition.character),
52+
new vscode.Position(barBarPosition.line, barBarPosition.character + 3)
53+
);
54+
55+
// Foo references
56+
expect(codeLenses[0].command?.command).toBe('roslyn.client.peekReferences');
57+
expect(codeLenses[0].command?.title).toBe('1 reference');
58+
expect(codeLenses[0].command?.arguments![1]).toEqual(fooPosition);
59+
expect(codeLenses[0].range).toEqual(fooRange);
60+
61+
// For.Baz references
62+
expect(codeLenses[1].command?.command).toBe('roslyn.client.peekReferences');
63+
expect(codeLenses[1].command?.title).toBe('1 reference');
64+
expect(codeLenses[1].command?.arguments![1]).toEqual(fooBazPosition);
65+
expect(codeLenses[1].range).toEqual(fooBazRange);
66+
67+
// Bar references
68+
expect(codeLenses[2].command?.command).toBe('roslyn.client.peekReferences');
69+
expect(codeLenses[2].command?.title).toBe('1 reference');
70+
expect(codeLenses[2].command?.arguments![1]).toEqual(barPosition);
71+
expect(codeLenses[2].range).toEqual(barRange);
72+
73+
// Bar.Bar references
74+
expect(codeLenses[3].command?.command).toBe('roslyn.client.peekReferences');
75+
expect(codeLenses[3].command?.title).toBe('0 references');
76+
expect(codeLenses[3].command?.arguments![1]).toEqual(barBarPosition);
77+
expect(codeLenses[3].range).toEqual(barBarRange);
78+
});
79+
80+
test('CodeLens references selected', async () => {
81+
const codeLenses = await getCodeLensesAsync();
82+
const peekCommand = codeLenses[0].command!;
83+
84+
// There's no way to directly test that the peek window correctly opens, so just assert that nothing threw an error.
85+
await expect(
86+
vscode.commands.executeCommand<Promise<void>>(
87+
peekCommand.command,
88+
peekCommand.arguments![0],
89+
peekCommand.arguments![1]
90+
)
91+
).resolves.toBeUndefined();
92+
});
93+
});

test/integrationTests/integrationHelpers.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ export function isSlnWithGenerator(workspace: typeof vscode.workspace) {
8080
return isGivenSln(workspace, 'slnWithGenerator');
8181
}
8282

83+
export async function getCodeLensesAsync(): Promise<vscode.CodeLens[]> {
84+
const activeEditor = vscode.window.activeTextEditor;
85+
if (!activeEditor) {
86+
throw new Error('No active editor');
87+
}
88+
89+
// The number of code lens items to resolve. Set to a high number so we get pretty much everything in the document.
90+
const resolvedItemCount = 100;
91+
92+
const codeLenses = <vscode.CodeLens[]>(
93+
await vscode.commands.executeCommand(
94+
'vscode.executeCodeLensProvider',
95+
activeEditor.document.uri,
96+
resolvedItemCount
97+
)
98+
);
99+
return codeLenses.sort((a, b) => {
100+
const rangeCompare = a.range.start.compareTo(b.range.start);
101+
if (rangeCompare !== 0) {
102+
return rangeCompare;
103+
}
104+
105+
return a.command!.title.localeCompare(b.command!.command);
106+
});
107+
}
108+
83109
function isGivenSln(workspace: typeof vscode.workspace, expectedProjectFileName: string) {
84110
const primeWorkspace = workspace.workspaceFolders![0];
85111
const projectFileName = primeWorkspace.uri.fsPath.split(path.sep).pop();

test/integrationTests/unitTests.integration.test.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as vscode from 'vscode';
77
import * as path from 'path';
88
import { describe, beforeAll, beforeEach, afterAll, test, expect } from '@jest/globals';
99
import testAssetWorkspace from './testAssets/testAssetWorkspace';
10-
import { activateCSharpExtension, openFileInWorkspaceAsync } from './integrationHelpers';
10+
import { activateCSharpExtension, getCodeLensesAsync, openFileInWorkspaceAsync } from './integrationHelpers';
1111
import { TestProgress } from '../../src/lsptoolshost/roslynProtocol';
1212

1313
describe(`[${testAssetWorkspace.description}] Test Unit Testing`, function () {
@@ -124,29 +124,3 @@ describe(`[${testAssetWorkspace.description}] Test Unit Testing`, function () {
124124
expect(testResults?.testsSkipped).toEqual(0);
125125
});
126126
});
127-
128-
async function getCodeLensesAsync(): Promise<vscode.CodeLens[]> {
129-
const activeEditor = vscode.window.activeTextEditor;
130-
if (!activeEditor) {
131-
throw new Error('No active editor');
132-
}
133-
134-
// The number of code lens items to resolve. Set to a high number so we get pretty much everything in the document.
135-
const resolvedItemCount = 100;
136-
137-
const codeLenses = <vscode.CodeLens[]>(
138-
await vscode.commands.executeCommand(
139-
'vscode.executeCodeLensProvider',
140-
activeEditor.document.uri,
141-
resolvedItemCount
142-
)
143-
);
144-
return codeLenses.sort((a, b) => {
145-
const rangeCompare = a.range.start.compareTo(b.range.start);
146-
if (rangeCompare !== 0) {
147-
return rangeCompare;
148-
}
149-
150-
return a.command!.title.localeCompare(b.command!.command);
151-
});
152-
}

0 commit comments

Comments
 (0)