|
| 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 | +}); |
0 commit comments