import * as assert from 'assert' ; import { IDeploymentTemplate , IPartialDeploymentTemplate } from "./diagnostics" ; import { parseTemplateWithMarkers } from "./parseTemplate" ; import { stringify } from './stringify' ; import { ITestPreparation , testWithPrep } from './testWithPrep' ; /**
 * Creates a test for completions in a particular template language expression
 */ export function createExpressionCompletionsTest ( expressionWithCursorMarker , expectedCompletions , template ? , options ? ) { const defaultTemplate = < IDeploymentTemplate > { $schema : "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#" , contentVersion : "1.0.0.0" , resources : [ ] , outputs : { o1 : { type : "object" , value : "<context>" } } } ; template = template ?? defaultTemplate ; createExpressionCompletionsTestEx ( template , '<context>' , expressionWithCursorMarker , expectedCompletions , options ) ; } /**
 * A generalization of createExpressionCompletionsTest that allows specifying a template and a location in that template
 * of where the expression completions will be evaluated
 */ export function createExpressionCompletionsTestEx ( template , /**
 * Indicates where in the template to place the expressionWithCursorMarker and run the completions on
 *  Example: if the template had the following in it:
 *    "output1": {
 *        "value": "<output1>",
 *        "type": "int"
 *    }
 *  then this should be '<output1>', which is the context of where the expression will be placed for the
 *  completions tests
 */ contextFind , expressionWithCursorMarker , expectedCompletions , options ? ) { const name = options ?. name ; testWithPrep ( `Expression completion: ${ name ? name + ': ' : '' }${ expressionWithCursorMarker }` , options ?. preps ?? [ ] , async ( ) => { let keepInClosure = name ; keepInClosure = keepInClosure ; template = stringify ( template ) . replace ( contextFind , expressionWithCursorMarker ) ; const { dt , markers : { cursor } } = parseTemplateWithMarkers ( template ) ; assert ( cursor , "Didn't find <!cursor!> marker in text" ) ; const pc = dt . getContextFromDocumentCharacterIndex ( cursor . index , undefined ) ; const completions = await pc . getCompletionItems ( options ?. triggerCharacter , 4 ) ; const completionItems = completions . items . filter ( c => ! ( options ?. ignoreCompletionNames ?? [ ] ) . includes ( c . label ) ) ; const completionNames = completionItems . map ( c => c . label ) . sort ( ) ; const completionInserts = completionItems . map ( c => c . insertText ) . sort ( ) ; let expectedNames = ( < unknown [ ] > expectedCompletions ) . map ( e => Array . isArray ( e ) ? < string > e [ 0 ] : < string > e ) ; expectedNames = expectedNames . sort ( ) ; let expectedInsertTexts ; if ( expectedCompletions . every ( e => Array . isArray ( e ) ) ) { expectedInsertTexts = ( < [ string , string ] [ ] > expectedCompletions ) . map ( e => e [ 1 ] ) . sort ( ) ; } assert . deepStrictEqual ( completionNames , expectedNames ) ; if ( expectedInsertTexts !== undefined ) { assert . deepStrictEqual ( completionInserts , expectedInsertTexts , "Completion insert texts didn't match" ) ; } } ) ; }