gitee.com/mirrors/gauge@v1.0.6/execution/result/scenarioResult.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package result 19 20 import ( 21 "github.com/getgauge/gauge/gauge_messages" 22 ) 23 24 type ScenarioResult struct { 25 ProtoScenario *gauge_messages.ProtoScenario 26 ScenarioDataTableRow *gauge_messages.ProtoTable 27 ScenarioDataTableRowIndex int 28 ScenarioDataTable *gauge_messages.ProtoTable 29 } 30 31 func NewScenarioResult(sce *gauge_messages.ProtoScenario) *ScenarioResult { 32 return &ScenarioResult{ProtoScenario: sce} 33 } 34 35 // SetFailure sets the scenarioResult as failed 36 func (s ScenarioResult) SetFailure() { 37 s.ProtoScenario.ExecutionStatus = gauge_messages.ExecutionStatus_FAILED 38 s.ProtoScenario.Failed = true 39 } 40 41 // GetFailed returns the state of the scenario result 42 func (s ScenarioResult) GetFailed() bool { 43 return s.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_FAILED 44 } 45 46 func (s ScenarioResult) AddItems(protoItems []*gauge_messages.ProtoItem) { 47 s.ProtoScenario.ScenarioItems = append(s.ProtoScenario.ScenarioItems, protoItems...) 48 } 49 50 func (s ScenarioResult) AddContexts(contextProtoItems []*gauge_messages.ProtoItem) { 51 s.ProtoScenario.Contexts = append(s.ProtoScenario.Contexts, contextProtoItems...) 52 } 53 54 func (s ScenarioResult) AddTearDownSteps(tearDownProtoItems []*gauge_messages.ProtoItem) { 55 s.ProtoScenario.TearDownSteps = append(s.ProtoScenario.TearDownSteps, tearDownProtoItems...) 56 } 57 58 func (s ScenarioResult) UpdateExecutionTime() { 59 s.updateExecutionTimeFromItems(s.ProtoScenario.GetContexts()) 60 s.updateExecutionTimeFromItems(s.ProtoScenario.GetScenarioItems()) 61 } 62 63 func (s ScenarioResult) AddExecTime(execTime int64) { 64 currentScenarioExecTime := s.ProtoScenario.GetExecutionTime() 65 s.ProtoScenario.ExecutionTime = currentScenarioExecTime + execTime 66 } 67 68 // ExecTime returns the time taken for scenario execution 69 func (s ScenarioResult) ExecTime() int64 { 70 return s.ProtoScenario.ExecutionTime 71 } 72 73 func (s ScenarioResult) updateExecutionTimeFromItems(protoItems []*gauge_messages.ProtoItem) { 74 for _, item := range protoItems { 75 if item.GetItemType() == gauge_messages.ProtoItem_Step { 76 stepExecTime := item.GetStep().GetStepExecutionResult().GetExecutionResult().GetExecutionTime() 77 s.AddExecTime(stepExecTime) 78 } else if item.GetItemType() == gauge_messages.ProtoItem_Concept { 79 conceptExecTime := item.GetConcept().GetConceptExecutionResult().GetExecutionResult().GetExecutionTime() 80 s.AddExecTime(conceptExecTime) 81 } 82 } 83 } 84 85 func (s ScenarioResult) GetPreHook() []*gauge_messages.ProtoHookFailure { 86 if s.ProtoScenario.PreHookFailure == nil { 87 return []*gauge_messages.ProtoHookFailure{} 88 } 89 return []*gauge_messages.ProtoHookFailure{s.ProtoScenario.PreHookFailure} 90 } 91 92 func (s ScenarioResult) GetPostHook() []*gauge_messages.ProtoHookFailure { 93 if s.ProtoScenario.PostHookFailure == nil { 94 return []*gauge_messages.ProtoHookFailure{} 95 } 96 return []*gauge_messages.ProtoHookFailure{s.ProtoScenario.PostHookFailure} 97 } 98 99 func (s ScenarioResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) { 100 s.ProtoScenario.PreHookFailure = f[0] 101 } 102 103 func (s ScenarioResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) { 104 s.ProtoScenario.PostHookFailure = f[0] 105 } 106 107 func (s ScenarioResult) Item() interface{} { 108 return s.ProtoScenario 109 }