import * as vscode from 'vscode' ; import * as path from 'path' ; import assert = require ( 'assert' ) ; import { CommonLanguageClient } from 'vscode-languageclient/lib/common/commonClient' ; import { MessageTransports , ProtocolRequestType , ProtocolRequestType0 , RequestType , RequestType0 } from 'vscode-languageclient' ; export let doc ; export let editor ; export let documentEol ; export let platformEol ; /**
 * Activates the redhat.vscode-yaml extension
 */ export async function activate ( docUri ) { const ext = vscode . extensions . getExtension ( 'redhat.vscode-yaml' ) ; const activation = await ext . activate ( ) ; try { doc = await vscode . workspace . openTextDocument ( docUri ) ; editor = await vscode . window . showTextDocument ( doc ) ; await sleep ( 2000 ) ; return activation ; } catch ( e ) { console . error ( e ) ; } } export async function sleep ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } export const getDocPath = ( p ) : string => { return path . resolve ( __dirname , path . join ( '..' , '..' , 'test' , 'testFixture' , p ) ) ; } ; export const getDocUri = ( p ) : vscode . Uri => { return vscode . Uri . file ( getDocPath ( p ) ) ; } ; export const updateSettings = ( setting , value ) : Thenable < void > => { const yamlConfiguration = vscode . workspace . getConfiguration ( 'yaml' , null ) ; return yamlConfiguration . update ( setting , value , false ) ; } ; export const resetSettings = ( setting , value ) : Thenable < void > => { const yamlConfiguration = vscode . workspace . getConfiguration ( 'yaml' , null ) ; return yamlConfiguration . update ( setting , value , false ) ; } ; export async function setTestContent ( content ) { const all = new vscode . Range ( doc . positionAt ( 0 ) , doc . positionAt ( doc . getText ( ) . length ) ) ; return editor . edit ( ( eb ) => eb . replace ( all , content ) ) ; } export async function testCompletion ( docUri , position , expectedCompletionList ) { const actualCompletionList = ( await vscode . commands . executeCommand ( 'vscode.executeCompletionItemProvider' , docUri , position ) ) as vscode . CompletionList ; const sortedActualCompletionList = actualCompletionList . items . sort ( ( a , b ) => ( a . label > b . label ? 1 : - 1 ) ) ; assert . equal ( actualCompletionList . items . length , expectedCompletionList . items . length , "Completion List doesn't have expected size" ) ; expectedCompletionList . items . sort ( ( a , b ) => ( a . label > b . label ? 1 : - 1 ) ) . forEach ( ( expectedItem , i ) => { const actualItem = sortedActualCompletionList [ i ] ; assert . equal ( actualItem . label , expectedItem . label ) ; assert . equal ( actualItem . kind , expectedItem . kind ) ; } ) ; } export async function testCompletionNotEmpty ( docUri , position ) { const actualCompletionList = ( await vscode . commands . executeCommand ( 'vscode.executeCompletionItemProvider' , docUri , position ) ) as vscode . CompletionList ; assert . notEqual ( actualCompletionList . items . length , 0 ) ; } export async function testHover ( docUri , position , expectedHover ) { const actualHoverResults = ( await vscode . commands . executeCommand ( 'vscode.executeHoverProvider' , docUri , position ) ) as vscode . Hover [ ] ; assert . equal ( actualHoverResults . length , expectedHover . length ) ; expectedHover . forEach ( ( expectedItem , i ) => { const actualItem = actualHoverResults [ i ] ; assert . equal ( ( actualItem . contents [ i ] as vscode . MarkdownString ) . value , expectedItem . contents [ i ] ) ; } ) ; } export async function testDiagnostics ( docUri , expectedDiagnostics ) { const actualDiagnostics = vscode . languages . getDiagnostics ( docUri ) ; assert . equal ( actualDiagnostics . length , expectedDiagnostics . length ) ; expectedDiagnostics . forEach ( ( expectedDiagnostic , i ) => { const actualDiagnostic = actualDiagnostics [ i ] ; assert . equal ( actualDiagnostic . message , expectedDiagnostic . message ) ; assert . deepEqual ( actualDiagnostic . range , expectedDiagnostic . range ) ; assert . equal ( actualDiagnostic . severity , expectedDiagnostic . severity ) ; } ) ; } export class TestMemento implements vscode . Memento { keys ( ) { throw new Error ( 'Method not implemented.' ) ; } get < T > ( key ) ; get < T > ( key , defaultValue ) ; get < T > ( key , defaultValue ? ) { throw new Error ( 'Method not implemented.' ) ; } update ( key , value ) { throw new Error ( 'Method not implemented.' ) ; } } export class TestLanguageClient extends CommonLanguageClient { constructor ( ) { super ( 'test' , 'test' , { } ) ; } protected getLocale ( ) { throw new Error ( 'Method not implemented.' ) ; } protected createMessageTransports ( ) { throw new Error ( 'Method not implemented.' ) ; } sendRequest < R , PR , E , RO > ( type , token ? ) ; sendRequest < P , R , PR , E , RO > ( type , params , token ? ) ; sendRequest < R , E > ( type , token ? ) ; sendRequest < P , R , E > ( type , params , token ? ) ; sendRequest < R > ( method , token ? ) ; sendRequest < R > ( method , param , token ? ) ; sendRequest < R > ( method , param ? , token ? ) { return Promise . resolve ( void 0 ) ; } }