Best Gauge code snippet using validation.NewStepValidationError
validate.go
Source:validate.go
...86// NewSpecValidationError generates new spec validation error with error message and filename.87func NewSpecValidationError(m string, f string) SpecValidationError {88 return SpecValidationError{message: m, fileName: f}89}90// NewStepValidationError generates new step validation error with error message, filename and error type.91func NewStepValidationError(s *gauge.Step, m string, f string, e *gm.StepValidateResponse_ErrorType, suggestion string) StepValidationError {92 return StepValidationError{step: s, message: m, fileName: f, errorType: e, suggestion: suggestion}93}94// Validate validates specs and if it has any errors, it exits.95func Validate(args []string) {96 if len(args) == 0 {97 args = append(args, util.GetSpecDirs()...)98 }99 res := ValidateSpecs(args, false)100 if len(res.Errs) > 0 {101 os.Exit(1)102 }103 if res.SpecCollection.Size() < 1 {104 logger.Infof(true, "No specifications found in %s.", strings.Join(args, ", "))105 res.Runner.Kill()106 if res.ParseOk {107 os.Exit(0)108 }109 os.Exit(1)110 }111 res.Runner.Kill()112 if res.ErrMap.HasErrors() {113 os.Exit(1)114 }115 logger.Infof(true, "No errors found.")116}117//TODO : duplicate in execute.go. Need to fix runner init.118func startAPI(debug bool) runner.Runner {119 sc := api.StartAPI(debug, reporter.Current())120 select {121 case runner := <-sc.RunnerChan:122 return runner123 case err := <-sc.ErrorChan:124 logger.Fatalf(true, "Failed to start gauge API: %s", err.Error())125 }126 return nil127}128type ValidationResult struct {129 SpecCollection *gauge.SpecCollection130 ErrMap *gauge.BuildErrors131 Runner runner.Runner132 Errs []error133 ParseOk bool134}135// NewValidationResult creates a new Validation result136func NewValidationResult(s *gauge.SpecCollection, errMap *gauge.BuildErrors, r runner.Runner, parseOk bool, e ...error) *ValidationResult {137 return &ValidationResult{SpecCollection: s, ErrMap: errMap, Runner: r, ParseOk: parseOk, Errs: e}138}139// ValidateSpecs parses the specs, creates a new validator and call the runner to get the validation result.140func ValidateSpecs(args []string, debug bool) *ValidationResult {141 conceptDict, res, err := parser.ParseConcepts()142 if err != nil {143 logger.Fatalf(true, "Unable to validate : %s", err.Error())144 }145 errMap := gauge.NewBuildErrors()146 s, specsFailed := parser.ParseSpecs(args, conceptDict, errMap)147 r := startAPI(debug)148 vErrs := NewValidator(s, r, conceptDict).Validate()149 errMap = getErrMap(errMap, vErrs)150 s = parser.GetSpecsForDataTableRows(s, errMap)151 printValidationFailures(vErrs)152 showSuggestion(vErrs)153 if !res.Ok {154 r.Kill()155 return NewValidationResult(nil, nil, nil, false, errors.New("Parsing failed."))156 }157 if specsFailed {158 return NewValidationResult(gauge.NewSpecCollection(s, false), errMap, r, false)159 }160 return NewValidationResult(gauge.NewSpecCollection(s, false), errMap, r, true)161}162func getErrMap(errMap *gauge.BuildErrors, validationErrors validationErrors) *gauge.BuildErrors {163 for spec, valErrors := range validationErrors {164 for _, err := range valErrors {165 switch err.(type) {166 case StepValidationError:167 errMap.StepErrs[err.(StepValidationError).step] = err.(StepValidationError)168 case SpecValidationError:169 errMap.SpecErrs[spec] = append(errMap.SpecErrs[spec], err.(SpecValidationError))170 }171 }172 skippedScnInSpec := 0173 for _, scenario := range spec.Scenarios {174 fillScenarioErrors(scenario, errMap, scenario.Steps)175 if _, ok := errMap.ScenarioErrs[scenario]; ok {176 skippedScnInSpec++177 }178 }179 if len(spec.Scenarios) > 0 && skippedScnInSpec == len(spec.Scenarios) {180 errMap.SpecErrs[spec] = append(errMap.SpecErrs[spec], errMap.ScenarioErrs[spec.Scenarios[0]]...)181 }182 fillSpecErrors(spec, errMap, append(spec.Contexts, spec.TearDownSteps...))183 }184 return errMap185}186func fillScenarioErrors(scenario *gauge.Scenario, errMap *gauge.BuildErrors, steps []*gauge.Step) {187 for _, step := range steps {188 if step.IsConcept {189 fillScenarioErrors(scenario, errMap, step.ConceptSteps)190 }191 if err, ok := errMap.StepErrs[step]; ok {192 errMap.ScenarioErrs[scenario] = append(errMap.ScenarioErrs[scenario], err)193 }194 }195}196func fillSpecErrors(spec *gauge.Specification, errMap *gauge.BuildErrors, steps []*gauge.Step) {197 for _, context := range steps {198 if context.IsConcept {199 fillSpecErrors(spec, errMap, context.ConceptSteps)200 }201 if err, ok := errMap.StepErrs[context]; ok {202 errMap.SpecErrs[spec] = append(errMap.SpecErrs[spec], err)203 for _, scenario := range spec.Scenarios {204 if _, ok := errMap.ScenarioErrs[scenario]; !ok {205 errMap.ScenarioErrs[scenario] = append(errMap.ScenarioErrs[scenario], err)206 }207 }208 }209 }210}211func printValidationFailures(validationErrors validationErrors) {212 for _, e := range FilterDuplicates(validationErrors) {213 logger.Errorf(true, "[ValidationError] %s", e.Error())214 }215}216func FilterDuplicates(validationErrors validationErrors) []error {217 filteredErrs := make([]error, 0)218 exists := make(map[string]bool)219 for _, errs := range validationErrors {220 for _, e := range errs {221 var val string222 if vErr, ok := e.(StepValidationError); ok {223 val = vErr.step.Value + vErr.step.FileName + strconv.Itoa(e.(StepValidationError).step.LineNo)224 } else if vErr, ok := e.(SpecValidationError); ok {225 val = vErr.message + vErr.fileName226 } else {227 continue228 }229 if _, ok := exists[val]; !ok {230 exists[val] = true231 filteredErrs = append(filteredErrs, e)232 }233 }234 }235 return filteredErrs236}237type validationErrors map[*gauge.Specification][]error238func NewValidator(s []*gauge.Specification, r runner.Runner, c *gauge.ConceptDictionary) *validator {239 return &validator{specsToExecute: s, runner: r, conceptsDictionary: c}240}241func (v *validator) Validate() validationErrors {242 validationStatus := make(validationErrors)243 specValidator := &SpecValidator{runner: v.runner, conceptsDictionary: v.conceptsDictionary, stepValidationCache: make(map[string]error)}244 for _, spec := range v.specsToExecute {245 specValidator.specification = spec246 validationErrors := specValidator.validate()247 if len(validationErrors) != 0 {248 validationStatus[spec] = validationErrors249 }250 }251 if len(validationStatus) > 0 {252 return validationStatus253 }254 return nil255}256func (v *SpecValidator) validate() []error {257 queue := &gauge.ItemQueue{Items: v.specification.AllItems()}258 v.specification.Traverse(v, queue)259 return v.validationErrors260}261// Validates a step. If validation result from runner is not valid then it creates a new validation error.262// If the error type is StepValidateResponse_STEP_IMPLEMENTATION_NOT_FOUND then gives suggestion with step implementation stub.263func (v *SpecValidator) Step(s *gauge.Step) {264 if s.IsConcept {265 for _, c := range s.ConceptSteps {266 v.Step(c)267 }268 return269 }270 val, ok := v.stepValidationCache[s.Value]271 if !ok {272 err := v.validateStep(s)273 if err != nil {274 v.validationErrors = append(v.validationErrors, err)275 }276 v.stepValidationCache[s.Value] = err277 return278 }279 if val != nil {280 valErr := val.(StepValidationError)281 if s.Parent == nil {282 v.validationErrors = append(v.validationErrors,283 NewStepValidationError(s, valErr.message, v.specification.FileName, valErr.errorType, valErr.suggestion))284 } else {285 cpt := v.conceptsDictionary.Search(s.Parent.Value)286 v.validationErrors = append(v.validationErrors,287 NewStepValidationError(s, valErr.message, cpt.FileName, valErr.errorType, valErr.suggestion))288 }289 }290}291var invalidResponse gm.StepValidateResponse_ErrorType = -1292func (v *SpecValidator) validateStep(s *gauge.Step) error {293 stepValue, err := parser.ExtractStepValueAndParams(s.LineText, s.HasInlineTable)294 if err != nil {295 return nil296 }297 protoStepValue := gauge.ConvertToProtoStepValue(stepValue)298 m := &gm.Message{MessageType: gm.Message_StepValidateRequest,299 StepValidateRequest: &gm.StepValidateRequest{StepText: s.Value, NumberOfParameters: int32(len(s.Args)), StepValue: protoStepValue}}300 r, err := v.runner.ExecuteMessageWithTimeout(m)301 if err != nil {302 return NewStepValidationError(s, err.Error(), v.specification.FileName, &invalidResponse, "")303 }304 if r.GetMessageType() == gm.Message_StepValidateResponse {305 res := r.GetStepValidateResponse()306 if !res.GetIsValid() {307 msg := getMessage(res.GetErrorType().String())308 suggestion := res.GetSuggestion()309 if s.Parent == nil {310 vErr := NewStepValidationError(s, msg, v.specification.FileName, &res.ErrorType, suggestion)311 return vErr312 }313 cpt := v.conceptsDictionary.Search(s.Parent.Value)314 vErr := NewStepValidationError(s, msg, cpt.FileName, &res.ErrorType, suggestion)315 return vErr316 }317 return nil318 }319 return NewStepValidationError(s, "Invalid response from runner for Validation request", v.specification.FileName, &invalidResponse, "")320}321func getMessage(message string) string {322 lower := strings.ToLower(strings.Replace(message, "_", " ", -1))323 return strings.ToUpper(lower[:1]) + lower[1:]324}325func (v *SpecValidator) TearDown(step *gauge.TearDown) {326}327func (v *SpecValidator) Heading(heading *gauge.Heading) {328}329func (v *SpecValidator) Tags(tags *gauge.Tags) {330}331func (v *SpecValidator) Table(dataTable *gauge.Table) {332}333func (v *SpecValidator) Scenario(scenario *gauge.Scenario) {...
NewStepValidationError
Using AI Code Generation
1import (2func main() {3 ts := entity.NewTimeSeries("test", period.NewPeriod1M())4 ts.Append(10, 11, 12, 13, 14, 15)5 ts.Append(16, 17, 18, 19, 20, 21)6 ts.Append(22, 23, 24, 25, 26, 27)7 ts.Append(28, 29, 30, 31, 32, 33)8 ts.Append(34, 35, 36, 37, 38, 39)9 ts.Append(40, 41, 42, 43, 44, 45)10 ts.Append(46, 47, 48, 49, 50, 51)11 ts.Append(52, 53, 54, 55, 56, 57)12 ts.Append(58, 59, 60, 61, 62, 63)13 ts.Append(64, 65, 66, 67, 68, 69)14 ts.Append(70, 71, 72, 73, 74, 75)15 ts.Append(76, 77, 78, 79, 80, 81)16 ts.Append(82, 83, 84, 85, 86, 87)17 ts.Append(88, 89, 90, 91, 92, 93)18 ts.Append(94, 95, 96, 97, 98, 99)19 ts.Append(100, 101, 102, 103, 104, 105)20 ts.Append(106, 107, 108, 109, 110, 111)21 ts.Append(112, 113, 114, 115, 116, 117)22 ts.Append(118, 119, 120, 121, 122, 123)23 ts.Append(
NewStepValidationError
Using AI Code Generation
1import (2func main() {3 validation := validation.Validation{}4 err := validation.NewStepValidationError()5 fmt.Println(err)6}
NewStepValidationError
Using AI Code Generation
1func NewStepValidationError() *StepValidationError {2 return &StepValidationError{message: "Step Validation Error"}3}4func NewStepValidationError() *StepValidationError {5 return &StepValidationError{message: "Step Validation Error"}6}7func NewStepValidationError() *StepValidationError {8 return &StepValidationError{message: "Step Validation Error"}9}10func NewStepValidationError() *StepValidationError {11 return &StepValidationError{message: "Step Validation Error"}12}13func NewStepValidationError() *StepValidationError {14 return &StepValidationError{message: "Step Validation Error"}15}16func NewStepValidationError() *StepValidationError {17 return &StepValidationError{message: "Step Validation Error"}18}19func NewStepValidationError() *StepValidationError {20 return &StepValidationError{message: "Step Validation Error"}21}22func NewStepValidationError() *StepValidationError {23 return &StepValidationError{message: "Step Validation Error"}24}25func NewStepValidationError() *StepValidationError {26 return &StepValidationError{message: "Step Validation Error"}27}28func NewStepValidationError() *StepValidationError {
NewStepValidationError
Using AI Code Generation
1func main() {2 err := validation.NewStepValidationError("my-step", "my-field", "my-error")3 fmt.Println(err)4}5func main() {6 err := validation.NewStepValidationError("my-step", "my-field", "my-error")7 fmt.Println(err)8}9func main() {10 err := validation.NewStepValidationError("my-step", "my-field", "my-error")11 fmt.Println(err)12}
NewStepValidationError
Using AI Code Generation
1func (s *Step) Validate() error {2 if s.Id == "" {3 return validation.NewStepValidationError("id", "id is required")4 }5 if s.Name == "" {6 return validation.NewStepValidationError("name", "name is required")7 }8}9func (s *Step) Validate() error {10 if s.Id == "" {11 return validation.NewStepValidationError("id", "id is required")12 }13 if s.Name == "" {14 return validation.NewStepValidationError("name", "name is required")15 }16}17func (s *Step) Validate() error {18 if s.Id == "" {19 return validation.NewStepValidationError("id", "id is required")20 }21 if s.Name == "" {22 return validation.NewStepValidationError("name", "name is required")23 }24}25func (s *Step) Validate() error {26 if s.Id == "" {27 return validation.NewStepValidationError("id", "id is required")28 }29 if s.Name == "" {30 return validation.NewStepValidationError("name", "name is required")31 }32}33func (s *Step) Validate() error {34 if s.Id == "" {35 return validation.NewStepValidationError("id", "id is required")36 }37 if s.Name == "" {38 return validation.NewStepValidationError("name", "name is required")39 }40}41func (s *Step) Validate() error {42 if s.Id == "" {43 return validation.NewStepValidationError("id",
NewStepValidationError
Using AI Code Generation
1import (2func main() {3 validation := new(validation.Validation)4 validation.NewStepValidationError("Error Message")5 fmt.Println(validation.GetStepValidationErrors())6}7import (8func main() {9 validation := new(validation.Validation)10 validation.AddStepValidationError("Error Message")11 fmt.Println(validation.GetStepValidationErrors())12}13import (14func main() {15 validation := new(validation.Validation)16 validation.AddStepValidationError("Error Message")17 fmt.Println(validation.GetStepValidationErrors())18}19import (20func main() {21 validation := new(validation.Validation)22 validation.AddStepValidationError("Error Message")23 fmt.Println(validation.GetStepValidationErrors())24}25import (26func main() {27 validation := new(validation.Validation)28 validation.AddStepValidationError("Error Message")29 fmt.Println(validation.GetStepValidationErrors())30}31import (32func main() {33 validation := new(validation.Validation)34 validation.AddStepValidationError("Error Message")35 fmt.Println(validation.GetStepValidationErrors())36}37import (38func main() {39 validation := new(validation.Validation)40 validation.AddStepValidationError("Error Message")41 fmt.Println(validation.HasStepValidationError())42}
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!!