github.com/getgauge/gauge@v1.6.9/execution/result/scenarioResult.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package result 8 9 import ( 10 "github.com/getgauge/gauge-proto/go/gauge_messages" 11 ) 12 13 type ScenarioResult struct { 14 ProtoScenario *gauge_messages.ProtoScenario 15 ScenarioDataTableRow *gauge_messages.ProtoTable 16 ScenarioDataTableRowIndex int 17 ScenarioDataTable *gauge_messages.ProtoTable 18 } 19 20 func NewScenarioResult(sce *gauge_messages.ProtoScenario) *ScenarioResult { 21 return &ScenarioResult{ProtoScenario: sce} 22 } 23 24 // SetFailure sets the scenarioResult as failed 25 func (s ScenarioResult) SetFailure() { 26 s.ProtoScenario.ExecutionStatus = gauge_messages.ExecutionStatus_FAILED 27 } 28 29 // GetFailed returns the state of the scenario result 30 func (s ScenarioResult) GetFailed() bool { 31 return s.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_FAILED 32 } 33 34 func (s ScenarioResult) SetSkippedScenario() { 35 s.ProtoScenario.ExecutionStatus = gauge_messages.ExecutionStatus_SKIPPED 36 } 37 38 func (s ScenarioResult) GetSkippedScenario() bool { 39 return s.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_SKIPPED 40 } 41 42 func (s ScenarioResult) AddItems(protoItems []*gauge_messages.ProtoItem) { 43 s.ProtoScenario.ScenarioItems = append(s.ProtoScenario.ScenarioItems, protoItems...) 44 } 45 46 func (s ScenarioResult) AddContexts(contextProtoItems []*gauge_messages.ProtoItem) { 47 s.ProtoScenario.Contexts = append(s.ProtoScenario.Contexts, contextProtoItems...) 48 } 49 50 func (s ScenarioResult) AddTearDownSteps(tearDownProtoItems []*gauge_messages.ProtoItem) { 51 s.ProtoScenario.TearDownSteps = append(s.ProtoScenario.TearDownSteps, tearDownProtoItems...) 52 } 53 54 func (s ScenarioResult) UpdateExecutionTime() { 55 s.updateExecutionTimeFromItems(s.ProtoScenario.GetContexts()) 56 s.updateExecutionTimeFromItems(s.ProtoScenario.GetScenarioItems()) 57 } 58 59 func (s ScenarioResult) AddExecTime(execTime int64) { 60 currentScenarioExecTime := s.ProtoScenario.GetExecutionTime() 61 s.ProtoScenario.ExecutionTime = currentScenarioExecTime + execTime 62 } 63 64 // ExecTime returns the time taken for scenario execution 65 func (s ScenarioResult) ExecTime() int64 { 66 return s.ProtoScenario.ExecutionTime 67 } 68 69 func (s ScenarioResult) updateExecutionTimeFromItems(protoItems []*gauge_messages.ProtoItem) { 70 for _, item := range protoItems { 71 if item.GetItemType() == gauge_messages.ProtoItem_Step { 72 stepExecTime := item.GetStep().GetStepExecutionResult().GetExecutionResult().GetExecutionTime() 73 s.AddExecTime(stepExecTime) 74 } else if item.GetItemType() == gauge_messages.ProtoItem_Concept { 75 conceptExecTime := item.GetConcept().GetConceptExecutionResult().GetExecutionResult().GetExecutionTime() 76 s.AddExecTime(conceptExecTime) 77 } 78 } 79 } 80 81 func (s ScenarioResult) GetPreHook() []*gauge_messages.ProtoHookFailure { 82 if s.ProtoScenario.PreHookFailure == nil { 83 return []*gauge_messages.ProtoHookFailure{} 84 } 85 return []*gauge_messages.ProtoHookFailure{s.ProtoScenario.PreHookFailure} 86 } 87 88 func (s ScenarioResult) GetPostHook() []*gauge_messages.ProtoHookFailure { 89 if s.ProtoScenario.PostHookFailure == nil { 90 return []*gauge_messages.ProtoHookFailure{} 91 } 92 return []*gauge_messages.ProtoHookFailure{s.ProtoScenario.PostHookFailure} 93 } 94 95 func (s ScenarioResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) { 96 s.ProtoScenario.PreHookFailure = f[0] 97 } 98 99 func (s ScenarioResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) { 100 s.ProtoScenario.PostHookFailure = f[0] 101 } 102 103 func (s ScenarioResult) Item() interface{} { 104 return s.ProtoScenario 105 }