|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------ */ |
| 6 | + |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import * as path from 'path'; |
| 9 | +import assert = require('assert'); |
| 10 | + |
| 11 | +export let doc: vscode.TextDocument; |
| 12 | +export let editor: vscode.TextEditor; |
| 13 | +export let documentEol: string; |
| 14 | +export let platformEol: string; |
| 15 | + |
| 16 | +/** |
| 17 | + * Activates the redhat.vscode-yaml extension |
| 18 | + */ |
| 19 | +export async function activate(docUri: vscode.Uri) { |
| 20 | + const ext = vscode.extensions.getExtension('redhat.vscode-yaml')!; |
| 21 | + const activation = await ext.activate(); |
| 22 | + try { |
| 23 | + doc = await vscode.workspace.openTextDocument(docUri); |
| 24 | + editor = await vscode.window.showTextDocument(doc); |
| 25 | + await sleep(2000); // Wait for server activation |
| 26 | + return activation; |
| 27 | + } catch (e) { |
| 28 | + console.error(e); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export async function sleep(ms: number) { |
| 33 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 34 | +} |
| 35 | + |
| 36 | +export const getDocPath = (p: string) => { |
| 37 | + return path.resolve(__dirname, '../../test/testFixture', p); |
| 38 | +}; |
| 39 | + |
| 40 | +export const getDocUri = (p: string) => { |
| 41 | + return vscode.Uri.file(getDocPath(p)); |
| 42 | +}; |
| 43 | + |
| 44 | +export const updateSettings = (setting: any, value: any) => { |
| 45 | + const yamlConfiguration = vscode.workspace.getConfiguration("yaml"); |
| 46 | + return yamlConfiguration.update(setting, value, false); |
| 47 | +} |
| 48 | + |
| 49 | +export const resetSettings = (setting: any, value: any) => { |
| 50 | + const yamlConfiguration = vscode.workspace.getConfiguration("yaml"); |
| 51 | + return yamlConfiguration.update(setting, value, false); |
| 52 | +} |
| 53 | + |
| 54 | +export async function setTestContent(content: string): Promise<boolean> { |
| 55 | + const all = new vscode.Range( |
| 56 | + doc.positionAt(0), |
| 57 | + doc.positionAt(doc.getText().length) |
| 58 | + ); |
| 59 | + return editor.edit(eb => eb.replace(all, content)); |
| 60 | +} |
| 61 | + |
| 62 | +export async function testCompletion( |
| 63 | + docUri: vscode.Uri, |
| 64 | + position: vscode.Position, |
| 65 | + expectedCompletionList: vscode.CompletionList |
| 66 | +) { |
| 67 | + |
| 68 | + // Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion |
| 69 | + const actualCompletionList = (await vscode.commands.executeCommand( |
| 70 | + 'vscode.executeCompletionItemProvider', |
| 71 | + docUri, |
| 72 | + position |
| 73 | + )) as vscode.CompletionList; |
| 74 | + |
| 75 | + assert.equal(actualCompletionList.items.length, expectedCompletionList.items.length); |
| 76 | + expectedCompletionList.items.forEach((expectedItem, i) => { |
| 77 | + const actualItem = actualCompletionList.items[i]; |
| 78 | + assert.equal(actualItem.label, expectedItem.label); |
| 79 | + assert.equal(actualItem.kind, expectedItem.kind); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +export async function testCompletionNotEmpty( |
| 84 | + docUri: vscode.Uri, |
| 85 | + position: vscode.Position |
| 86 | +) { |
| 87 | + |
| 88 | + // Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion |
| 89 | + const actualCompletionList = (await vscode.commands.executeCommand( |
| 90 | + 'vscode.executeCompletionItemProvider', |
| 91 | + docUri, |
| 92 | + position |
| 93 | + )) as vscode.CompletionList; |
| 94 | + |
| 95 | + assert.notEqual(actualCompletionList.items.length, 0); |
| 96 | +} |
| 97 | + |
| 98 | +export async function testHover( |
| 99 | + docUri: vscode.Uri, |
| 100 | + position: vscode.Position, |
| 101 | + expectedHover: vscode.Hover[] |
| 102 | +) { |
| 103 | + |
| 104 | + // Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion |
| 105 | + const actualHoverResults = (await vscode.commands.executeCommand( |
| 106 | + 'vscode.executeHoverProvider', |
| 107 | + docUri, |
| 108 | + position |
| 109 | + )) as vscode.Hover[]; |
| 110 | + |
| 111 | + assert.equal(actualHoverResults.length, expectedHover.length); |
| 112 | + expectedHover.forEach((expectedItem, i) => { |
| 113 | + const actualItem = actualHoverResults[i]; |
| 114 | + assert.equal((actualItem.contents[i] as vscode.MarkdownString).value, expectedItem.contents[i]); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +export async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) { |
| 119 | + const actualDiagnostics = vscode.languages.getDiagnostics(docUri); |
| 120 | + |
| 121 | + assert.equal(actualDiagnostics.length, expectedDiagnostics.length); |
| 122 | + |
| 123 | + expectedDiagnostics.forEach((expectedDiagnostic, i) => { |
| 124 | + const actualDiagnostic = actualDiagnostics[i] |
| 125 | + assert.equal(actualDiagnostic.message, expectedDiagnostic.message) |
| 126 | + assert.deepEqual(actualDiagnostic.range, expectedDiagnostic.range) |
| 127 | + assert.equal(actualDiagnostic.severity, expectedDiagnostic.severity) |
| 128 | + }); |
| 129 | +} |
0 commit comments