import { SpecCompleteResponse } from "../../model/spec-complete-response" ; import { TestSuiteInfo , TestInfo } from "vscode-test-adapter-api" ; import * as path from "path" ; export class SpecResponseToTestSuiteInfoMapper { public constructor ( private readonly projectRootPath ) { } public map ( specs ) { const rootSuiteNode = { type : "suite" , id : "root" , label : "root" , fullName : "root" , children : [ ] , } as TestSuiteInfo ; for ( const spec of specs ) { const suiteNames = this . filterSuiteNames ( spec . suite ) ; const suiteNode = this . getOrCreateLowerSuiteNode ( rootSuiteNode , spec , suiteNames ) ; this . createTest ( spec , suiteNode , suiteNames . join ( " " ) ) ; } return rootSuiteNode ; } private getOrCreateLowerSuiteNode ( node , spec , suiteNames ) { for ( const suiteName of suiteNames ) { let nextNode = this . findNodeByKey ( node , suiteName ) ; if ( ! nextNode ) { const locationHint = suiteNames . reduce ( ( previousValue , currentValue , index ) => { if ( previousValue === suiteName ) { spec . suite = [ suiteName ] ; return suiteName ; } spec . suite = suiteNames ; return [ previousValue , currentValue ] . join ( " " ) ; } ) ; nextNode = this . createSuite ( spec , locationHint ) ; node . children . push ( nextNode ) ; } node = nextNode ; } return node ; } private findNodeByKey ( node , suiteLookup ) { if ( node . type === "test" ) { return undefined ; } if ( node . label === suiteLookup ) { return node ; } else { for ( const child of node . children ) { if ( child . label === suiteLookup ) { return child as TestSuiteInfo ; } } } return undefined ; } private filterSuiteNames ( suiteNames ) { if ( suiteNames . length > 0 && "Jasmine__TopLevel__Suite" === suiteNames [ 0 ] ) { suiteNames = suiteNames . slice ( 1 ) ; } return suiteNames ; } private createTest ( specComplete , suiteNode , suiteLookup ) { suiteNode . children . push ( { id : specComplete . id , fullName : suiteLookup + " " + specComplete . description , label : specComplete . description , file : specComplete . filePath ? path . join ( this . projectRootPath , specComplete . filePath as string ) : undefined , type : "test" , line : specComplete . line ? ( specComplete . line as number ) : undefined , } as TestInfo ) ; } private createSuite ( specComplete , suiteLookup ) { return { id : suiteLookup , fullName : specComplete . suite [ specComplete . suite . length - 1 ] , label : specComplete . suite [ specComplete . suite . length - 1 ] , file : specComplete . filePath ? path . join ( this . projectRootPath , specComplete . filePath as string ) : undefined , type : "suite" , children : [ ] , } as TestSuiteInfo ; } }