|
| 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 { describe, beforeAll, beforeEach, afterAll, test, expect } from '@jest/globals'; |
| 9 | +import testAssetWorkspace from './testAssets/testAssetWorkspace'; |
| 10 | +import { activateCSharpExtension, openFileInWorkspaceAsync } from './integrationHelpers'; |
| 11 | + |
| 12 | +describe(`[${testAssetWorkspace.description}] Test Completion`, function () { |
| 13 | + beforeAll(async function () { |
| 14 | + await activateCSharpExtension(); |
| 15 | + }); |
| 16 | + |
| 17 | + beforeEach(async function () { |
| 18 | + const fileName = path.join('src', 'app', 'completion.cs'); |
| 19 | + await openFileInWorkspaceAsync(fileName); |
| 20 | + }); |
| 21 | + |
| 22 | + afterAll(async () => { |
| 23 | + await testAssetWorkspace.cleanupWorkspace(); |
| 24 | + }); |
| 25 | + |
| 26 | + test('Returns completion items', async () => { |
| 27 | + const completionList = await getCompletionsAsync(new vscode.Position(8, 12), undefined, 10); |
| 28 | + expect(completionList.items.length).toBeGreaterThan(0); |
| 29 | + expect(completionList.items.map((item) => item.label)).toContain('Console'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('Resolve adds documentation', async () => { |
| 33 | + const completionList = await getCompletionsAsync(new vscode.Position(8, 12), undefined, 10); |
| 34 | + const documentation = completionList.items.slice(0, 10).filter((item) => item.documentation); |
| 35 | + expect(documentation.length).toEqual(10); |
| 36 | + }); |
| 37 | + |
| 38 | + test('Override completion is applied', async () => { |
| 39 | + const completionList = await getCompletionsAsync(new vscode.Position(12, 24), ' ', 10); |
| 40 | + expect(completionList.items.length).toBeGreaterThan(0); |
| 41 | + const methodOverrideItem = completionList.items.find( |
| 42 | + (item) => item.label === 'Method(singleCsproj2.NeedsImport n)' |
| 43 | + ); |
| 44 | + expect(methodOverrideItem).toBeDefined(); |
| 45 | + expect(methodOverrideItem!.kind).toEqual(vscode.CompletionItemKind.Method); |
| 46 | + expect(methodOverrideItem!.command).toBeDefined(); |
| 47 | + expect(methodOverrideItem!.command!.command).toEqual('roslyn.client.completionComplexEdit'); |
| 48 | + |
| 49 | + await vscode.commands.executeCommand( |
| 50 | + methodOverrideItem!.command!.command, |
| 51 | + methodOverrideItem!.command!.arguments![0], |
| 52 | + methodOverrideItem!.command!.arguments![1], |
| 53 | + methodOverrideItem!.command!.arguments![2], |
| 54 | + methodOverrideItem!.command!.arguments![3] |
| 55 | + ); |
| 56 | + |
| 57 | + const usingLine = vscode.window.activeTextEditor!.document.lineAt(1).text; |
| 58 | + const methodOverrideLine = vscode.window.activeTextEditor!.document.lineAt(13).text; |
| 59 | + const methodOverrideImplLine = vscode.window.activeTextEditor!.document.lineAt(15).text; |
| 60 | + expect(usingLine).toContain('using singleCsproj2;'); |
| 61 | + expect(methodOverrideLine).toContain('override void Method(NeedsImport n)'); |
| 62 | + expect(methodOverrideImplLine).toContain('base.Method(n);'); |
| 63 | + }); |
| 64 | + |
| 65 | + async function getCompletionsAsync( |
| 66 | + position: vscode.Position, |
| 67 | + triggerCharacter: string | undefined, |
| 68 | + completionsToResolve: number |
| 69 | + ): Promise<vscode.CompletionList> { |
| 70 | + const activeEditor = vscode.window.activeTextEditor; |
| 71 | + if (!activeEditor) { |
| 72 | + throw new Error('No active editor'); |
| 73 | + } |
| 74 | + |
| 75 | + return await vscode.commands.executeCommand( |
| 76 | + 'vscode.executeCompletionItemProvider', |
| 77 | + activeEditor.document.uri, |
| 78 | + position, |
| 79 | + triggerCharacter, |
| 80 | + completionsToResolve |
| 81 | + ); |
| 82 | + } |
| 83 | +}); |
0 commit comments