|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { after, before, beforeEach, describe, it } from 'mocha'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import { provideCompletionItemMiddleware } from '../../extension'; |
| 6 | +import { PuppetStatusBarFeature } from '../../feature/PuppetStatusBarFeature'; |
| 7 | +import { ISettings, settingsFromWorkspace } from '../../settings'; |
| 8 | + |
| 9 | +describe('Extension Tests', () => { |
| 10 | + let vscodeCommandsRegisterCommandStub: sinon.SinonStub; |
| 11 | + let puppetStatusBarFeatureStub: sinon.SinonStub; |
| 12 | + let settings: ISettings; |
| 13 | + let context: vscode.ExtensionContext; |
| 14 | + let registerDebugAdapterDescriptorFactoryStub: sinon.SinonStub; |
| 15 | + let document: vscode.TextDocument; |
| 16 | + let position: vscode.Position; |
| 17 | + let completionContext: vscode.CompletionContext; |
| 18 | + let token: vscode.CancellationToken; |
| 19 | + let next: sinon.SinonStub |
| 20 | + const sandbox = sinon.createSandbox(); |
| 21 | + |
| 22 | + before(() => { |
| 23 | + vscodeCommandsRegisterCommandStub = sandbox.stub(vscode.commands, 'registerCommand'); |
| 24 | + settings = settingsFromWorkspace(); |
| 25 | + context = { |
| 26 | + subscriptions: [], |
| 27 | + asAbsolutePath: (relativePath: string) => { |
| 28 | + return `/absolute/path/to/${relativePath}`; |
| 29 | + }, |
| 30 | + globalState: { |
| 31 | + get: sandbox.stub(), |
| 32 | + } |
| 33 | + } as vscode.ExtensionContext; |
| 34 | + puppetStatusBarFeatureStub = sandbox.createStubInstance(PuppetStatusBarFeature); |
| 35 | + registerDebugAdapterDescriptorFactoryStub = sandbox.stub(vscode.debug, 'registerDebugAdapterDescriptorFactory'); |
| 36 | + }); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + document = {} as vscode.TextDocument; |
| 40 | + position = new vscode.Position(0, 0); |
| 41 | + completionContext = {} as vscode.CompletionContext; |
| 42 | + token = new vscode.CancellationTokenSource().token; |
| 43 | + next = sandbox.stub(); |
| 44 | + }); |
| 45 | + |
| 46 | + after(() => { |
| 47 | + sandbox.restore(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should add command to completion items', async () => { |
| 51 | + const completionItems = [ |
| 52 | + new vscode.CompletionItem('item1', vscode.CompletionItemKind.Property), |
| 53 | + new vscode.CompletionItem('item2', vscode.CompletionItemKind.Text), |
| 54 | + ]; |
| 55 | + completionItems[0].detail = 'Property'; |
| 56 | + completionItems[1].detail = 'Text'; |
| 57 | + next.returns(completionItems); |
| 58 | + |
| 59 | + const result = await provideCompletionItemMiddleware.provideCompletionItem(document, position, context, token, next); |
| 60 | + |
| 61 | + assert.ok(Array.isArray(result)); |
| 62 | + assert.strictEqual(result.length, 2); |
| 63 | + assert.strictEqual(result[0].command?.command, 'editor.action.formatDocumentAndMoveCursor'); |
| 64 | + assert.strictEqual(result[1].command?.command, 'editor.action.formatDocument'); |
| 65 | + }); |
| 66 | +}); |
0 commit comments