import * as assert from 'assert' ; import { CodeAction , Command , Uri } from 'vscode' ; import { addMissingParameters , assignTemplateGraphToDeploymentTemplate , DeploymentTemplateDoc , filterByType , getNormalizedDocumentKey , getVSCodeRangeFromSpan , IAddMissingParametersArgs , INotifyTemplateGraphArgs , isWin32 , LinkedFileLoadState , NormalizedMap , Span } from '../extension.bundle' ; import { TextDocumentFake } from './fakes/TextDocumentFake' ; import { TextEditorFake } from './fakes/TextEditorFake' ; import { getEmptyCodeActionContext } from './support/getEmptyCodeActionContext' ; import { parseTemplateWithMarkers } from "./support/parseTemplate" ; import { removeComments } from './support/removeComments' ; import { testWithLanguageServer } from './support/testWithLanguageServer' ; suite ( "Add missing parameters for nested/linked templates" , ( ) => { enum Params { "all" , "onlyRequired" } function createAddMissingParamsTest ( testName , template , linkedTemplate , whichParams , expectedResult ) { testWithLanguageServer ( testName , async ( ) => { const tabSize = 4 ; const { dt : expectedDt } = parseTemplateWithMarkers ( expectedResult , undefined , { fromFile : true } ) ; const expectedResultText = expectedDt . documentText ; const root = isWin32 ? 'c:\\' : '/' ; const templateUri = Uri . file ( `${ root }mainTemplate.json` ) ; const childUri = Uri . file ( `${ root }childTemplate.json` ) ; const { dt , markers : { bang } } = parseTemplateWithMarkers ( template , undefined , { fromFile : true , documentUri : templateUri , tabSize } ) ; assert ( bang , "Didn't find bang marker" ) ; const span = new Span ( bang . index , 0 ) ; const range = getVSCodeRangeFromSpan ( dt , span ) ; if ( linkedTemplate ) { const { dt : childDt } = parseTemplateWithMarkers ( linkedTemplate , undefined , { fromFile : true , documentUri : childUri , tabSize } ) ; const graph = { rootTemplateUri : templateUri . toString ( ) , linkedTemplates : [ { fullUri : childUri . toString ( ) , columnNumberInParent : range . start . character , id : "0" , lineNumberInParent : range . start . line , loadErrorMessage : undefined , loadState : LinkedFileLoadState . SuccessfullyLoaded , originalPath : "childTemplate.json" , parameterValues : { } } ] , fullValidationStatus : { fullValidationEnabled : true , allParametersHaveDefaults : true , hasParameterFile : true , } , isComplete : true , rootTemplateDocVersion : 0 , } ; const allLoadedTemplates = new NormalizedMap < Uri , DeploymentTemplateDoc > ( getNormalizedDocumentKey ) ; allLoadedTemplates . set ( templateUri , dt ) ; allLoadedTemplates . set ( childUri , childDt ) ; assignTemplateGraphToDeploymentTemplate ( graph , dt , { getOpenedDeploymentTemplate : ( uri ) : DeploymentTemplateDoc | undefined => { return allLoadedTemplates . get ( uri ) ; } , setOpenedDeploymentDocument : ( _uri , _doc ) : void => { assert . fail ( "NYI" ) ; } } ) ; } const codeActions = dt . getCodeActions ( undefined , range , getEmptyCodeActionContext ( ) ) ; assert . equal ( codeActions . length , 2 , "Expecting a 2 add missing params code actions" ) ; const expectedName = whichParams === Params . all ? "Add all missing parameters" : "Add missing required parameters" ; const codeAction = filterByType ( codeActions , CodeAction ) . find ( ca => ca . title === expectedName ) ; const args = < IAddMissingParametersArgs > codeAction ! . command ! . arguments ! [ 1 ] ; const doc = new TextDocumentFake ( dt . documentText , templateUri ) ; const editor = new TextEditorFake ( doc ) ; editor . options = { insertSpaces : true , tabSize , } ; await addMissingParameters ( args . parameterDefinitionsSource , args . parameterValuesSource , editor , args . parentParameterDefinitionsSource , whichParams === Params . onlyRequired ) ; const actualResult = editor . document . getText ( ) ; const actualResultNoComments = removeComments ( actualResult ) ; assert . strictEqual ( actualResultNoComments , expectedResultText ) ; } ) ; } createAddMissingParamsTest ( "Simple linked template - main.linked01.json" , "templates/linkedTemplates/addMissingParams/main.simple-linked.json" , "templates/linkedTemplates/addMissingParams/childTemplate.json" , Params . all , "templates/linkedTemplates/addMissingParams/main.simple-linked.expected.json" ) ; createAddMissingParamsTest ( "Pass through top-level parameters to linked template" , "templates/linkedTemplates/addMissingParams/main.linked.passthru.json" , "templates/linkedTemplates/addMissingParams/childTemplate.json" , Params . all , "templates/linkedTemplates/addMissingParams/main.linked.passthru.expected.json" ) ; createAddMissingParamsTest ( "Pass through top-level parameters to nested template" , "templates/linkedTemplates/addMissingParams/main.nested.passthru.json" , undefined , Params . all , "templates/linkedTemplates/addMissingParams/main.nested.passthru.expected.json" ) ; } ) ;