Best Gauge code snippet using parser.HandleParseResult
parse.go
Source:parse.go
...101 conceptsDictionary, conceptParseResult, err := CreateConceptsDictionary()102 if err != nil {103 return nil, nil, err104 }105 HandleParseResult(conceptParseResult)106 logger.Debugf(true, "%d concepts parsing completed.", len(conceptsDictionary.ConceptsMap))107 return conceptsDictionary, conceptParseResult, nil108}109func parseSpec(specFile string, conceptDictionary *gauge.ConceptDictionary) (*gauge.Specification, *ParseResult) {110 specFileContent, err := common.ReadFileContents(specFile)111 if err != nil {112 return nil, &ParseResult{ParseErrors: []ParseError{ParseError{FileName: specFile, Message: err.Error()}}, Ok: false}113 }114 spec, parseResult, err := new(SpecParser).Parse(specFileContent, conceptDictionary, specFile)115 if err != nil {116 logger.Fatalf(true, err.Error())117 }118 return spec, parseResult119}120type specFile struct {121 filePath string122 indices []int123}124// parseSpecsInDirs parses all the specs in list of dirs given.125// It also de-duplicates all specs passed through `specDirs` before parsing specs.126func parseSpecsInDirs(conceptDictionary *gauge.ConceptDictionary, specDirs []string, buildErrors *gauge.BuildErrors) ([]*gauge.Specification, bool) {127 passed := true128 givenSpecs, specFiles := getAllSpecFiles(specDirs)129 var specs []*gauge.Specification130 var specParseResults []*ParseResult131 allSpecs := make([]*gauge.Specification, len(specFiles))132 logger.Debug(true, "Started specifications parsing.")133 specs, specParseResults = ParseSpecFiles(givenSpecs, conceptDictionary, buildErrors)134 passed = !HandleParseResult(specParseResults...) && passed135 logger.Debugf(true, "%d specifications parsing completed.", len(specFiles))136 for _, spec := range specs {137 i, _ := getIndexFor(specFiles, spec.FileName)138 specFile := specFiles[i]139 if len(specFile.indices) > 0 {140 s, _ := spec.Filter(filter.NewScenarioFilterBasedOnSpan(specFile.indices))141 allSpecs[i] = s142 } else {143 allSpecs[i] = spec144 }145 }146 return allSpecs, !passed147}148func getAllSpecFiles(specDirs []string) (givenSpecs []string, specFiles []*specFile) {149 for _, specSource := range specDirs {150 if isIndexedSpec(specSource) {151 var specName string152 specName, index := getIndexedSpecName(specSource)153 files := util.GetSpecFiles([]string{specName})154 if len(files) < 1 {155 continue156 }157 specificationFile, created := addSpecFile(&specFiles, files[0])158 if created || len(specificationFile.indices) > 0 {159 specificationFile.indices = append(specificationFile.indices, index)160 }161 givenSpecs = append(givenSpecs, files[0])162 } else {163 files := util.GetSpecFiles([]string{specSource})164 for _, file := range files {165 specificationFile, _ := addSpecFile(&specFiles, file)166 specificationFile.indices = specificationFile.indices[0:0]167 }168 givenSpecs = append(givenSpecs, files...)169 }170 }171 return172}173func addSpecFile(specFiles *[]*specFile, file string) (*specFile, bool) {174 i, exists := getIndexFor(*specFiles, file)175 if !exists {176 specificationFile := &specFile{filePath: file}177 *specFiles = append(*specFiles, specificationFile)178 return specificationFile, true179 }180 return (*specFiles)[i], false181}182func getIndexFor(files []*specFile, file string) (int, bool) {183 for index, f := range files {184 if f.filePath == file {185 return index, true186 }187 }188 return -1, false189}190func isIndexedSpec(specSource string) bool {191 re := regexp.MustCompile(`(?i).(spec|md):[0-9]+$`)192 index := re.FindStringIndex(specSource)193 if index != nil {194 return index[0] != 0195 }196 return false197}198func getIndexedSpecName(indexedSpec string) (string, int) {199 index := getIndex(indexedSpec)200 specName := indexedSpec[:index]201 scenarioNum := indexedSpec[index+1:]202 scenarioNumber, _ := strconv.Atoi(scenarioNum)203 return specName, scenarioNumber204}205func getIndex(specSource string) int {206 re, _ := regexp.Compile(":[0-9]+$")207 index := re.FindStringSubmatchIndex(specSource)208 if index != nil {209 return index[0]210 }211 return 0212}213// ExtractStepValueAndParams parses a stepText string into a StepValue struct214func ExtractStepValueAndParams(stepText string, hasInlineTable bool) (*gauge.StepValue, error) {215 stepValueWithPlaceHolders, args, err := processStepText(stepText)216 if err != nil {217 return nil, err218 }219 extractedStepValue, _ := extractStepValueAndParameterTypes(stepValueWithPlaceHolders)220 if hasInlineTable {221 extractedStepValue += " " + gauge.ParameterPlaceholder222 args = append(args, string(gauge.TableArg))223 }224 parameterizedStepValue := getParameterizeStepValue(extractedStepValue, args)225 return &gauge.StepValue{Args: args, StepValue: extractedStepValue, ParameterizedStepValue: parameterizedStepValue}, nil226}227// CreateStepValue converts a Step to StepValue228func CreateStepValue(step *gauge.Step) gauge.StepValue {229 stepValue := gauge.StepValue{StepValue: step.Value}230 args := make([]string, 0)231 for _, arg := range step.Args {232 args = append(args, arg.ArgValue())233 }234 stepValue.Args = args235 stepValue.ParameterizedStepValue = getParameterizeStepValue(stepValue.StepValue, args)236 return stepValue237}238func getParameterizeStepValue(stepValue string, params []string) string {239 for _, param := range params {240 stepValue = strings.Replace(stepValue, gauge.ParameterPlaceholder, "<"+param+">", 1)241 }242 return stepValue243}244// HandleParseResult collates list of parse result and determines if gauge has to break flow.245func HandleParseResult(results ...*ParseResult) bool {246 var failed = false247 for _, result := range results {248 if !result.Ok {249 for _, err := range result.Errors() {250 logger.Errorf(true, err)251 }252 failed = true253 }254 if result.Warnings != nil {255 for _, warning := range result.Warnings {256 logger.Warningf(true, "[ParseWarning] %s", warning)257 }258 }259 }...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!