import * as assert from 'assert' ; import { __debugMarkPositionInString } from '../../extension.bundle' ; import { getDocumentMarkers } from "./parseTemplate" ; export async function testStringAtEachIndex < T > ( textWithMarkers , expectedValues , getTestResult ) { for ( let marker of Object . keys ( expectedValues ) ) { for ( let i = 1 ; ; ++ i ) { const newString = textWithMarkers . replace ( `<!${ marker }!>` , `<!${ marker }$${ i }!>` ) ; if ( newString === textWithMarkers ) { break ; } else { textWithMarkers = newString ; } } } const { markers , unmarkedText } = getDocumentMarkers ( textWithMarkers ) ; let expectedResultHere ; for ( let i = 0 ; i < unmarkedText . length ; ++ i ) { const currentMarker = Object . getOwnPropertyNames ( markers ) . find ( key => markers [ key ] . index === i ) ; if ( currentMarker ) { const nonUniqueMarkerName = currentMarker . replace ( /\$[0-9]+$/ , '' ) ; if ( ! ( nonUniqueMarkerName in expectedValues ) ) { assert . fail ( `Unexpected marker name "${ nonUniqueMarkerName }"` ) ; } expectedResultHere = expectedValues [ nonUniqueMarkerName ] ; } else { if ( i === 0 ) { assert . fail ( "Must have a marker at the very beginning of the string" ) ; } } const actualResultHere = await getTestResult ( unmarkedText , i ) ; assert . equal ( actualResultHere , expectedResultHere , `At index ${ i }: ${ __debugMarkPositionInString ( unmarkedText , i ) }` ) ; } }