import { IDocString , IStep , IDataTable , IElement } from './JSONSchema' ; import IAstMaker from '../IAstMaker' ; import * as messages from '@cucumber/messages' ; import IPredictableSupportCode from '../IPredictableSupportCode' ; import { traverseDataTable } from '../cucumber-ruby/JSONTraverse' ; import { traverseFeature as genericTraverseFeature , traverseTag , } from '../cucumber-generic/JSONTraverse' ; import { IFeature } from '../cucumber-generic/JSONSchema' ; function durationToMillis ( duration ) { return duration ? duration / 1000000 : 0 ; } export function traverseFeature ( feature , astMaker , newId , predictableSupportCode ) { return genericTraverseFeature ( feature , astMaker , newId , predictableSupportCode , traverseElement ) ; } export function traverseElement ( element , astMaker , newId , predictableSupportCode ) { const beforeHooks = [ ] ; const scenarioSteps = [ ] ; const afterHooks = [ ] ; let currentStepIs = 'before' ; for ( const step of element . steps ) { if ( currentStepIs === 'before' && ! step . hidden ) { currentStepIs = 'scenario' ; } else if ( currentStepIs === 'scenario' && step . hidden ) { currentStepIs = 'after' ; } switch ( currentStepIs ) { case 'before' : { beforeHooks . push ( step ) ; break ; } case 'scenario' : { scenarioSteps . push ( step ) ; break ; } case 'after' : { afterHooks . push ( step ) ; break ; } } } const tags = element . tags ? element . tags . map ( ( tag ) => traverseTag ( tag , astMaker ) ) : [ ] ; const featureChild = astMaker . makeScenarioFeatureChild ( newId ( ) , element . line , element . keyword , element . name , element . description , scenarioSteps . map ( ( step ) => traverseStep ( step , astMaker , newId , predictableSupportCode ) ) , tags ) ; for ( const hook of beforeHooks ) { traverseBeforeHook ( hook , featureChild . scenario , predictableSupportCode ) ; } for ( const hook of afterHooks ) { traverseAfterHook ( hook , featureChild . scenario , predictableSupportCode ) ; } return featureChild ; } export function traverseBeforeHook ( step , scenario , predictableSupportCode ) { predictableSupportCode . addPredictableBeforeHook ( step . match ?. location || '' , scenario . id , step . result . status , durationToMillis ( step . result . duration ) , step . result . error_message ) ; } export function traverseAfterHook ( step , scenario , predictableSupportCode ) { predictableSupportCode . addPredictableAfterHook ( step . match ?. location || '' , scenario . id , step . result . status , durationToMillis ( step . result . duration ) , step . result . error_message ) ; } export function traverseStep ( step , astMaker , newId , predictableSupportCode ) { const stepArguments = step . arguments || [ ] ; const docStringArgument = stepArguments . find ( ( arg ) => arg . content ) as IDocString ; const docString = docStringArgument ? traverseDocString ( docStringArgument , astMaker ) : null ; const datatableArgument = stepArguments . find ( ( arg ) => arg . rows ) as IDataTable ; const datatable = datatableArgument ? traverseDataTable ( datatableArgument . rows , astMaker ) : null ; const gherkinStep = astMaker . makeStep ( newId ( ) , step . line , step . keyword , step . name , docString , datatable ) ; if ( gherkinStep && step . match ) { predictableSupportCode . addPredictableStepDefinition ( step . match . location , gherkinStep . id , step . result . status , durationToMillis ( step . result . duration ) , step . result . error_message ) ; } return gherkinStep ; } export function traverseDocString ( docString , astMaker ) { return astMaker . makeDocstring ( null , docString . content ) ; }