github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/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 "github.com/getgauge/gauge/gauge_messages"
    21  
    22  type ScenarioResult struct {
    23  	ProtoScenario *gauge_messages.ProtoScenario
    24  }
    25  
    26  func NewScenarioResult(sce *gauge_messages.ProtoScenario) *ScenarioResult {
    27  	return &ScenarioResult{ProtoScenario: sce}
    28  }
    29  
    30  func (s *ScenarioResult) SetFailure() {
    31  	s.ProtoScenario.ExecutionStatus = gauge_messages.ExecutionStatus_FAILED
    32  	s.ProtoScenario.Failed = true
    33  }
    34  
    35  func (s *ScenarioResult) GetFailed() bool {
    36  	return s.ProtoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_FAILED
    37  }
    38  
    39  func (s *ScenarioResult) AddItems(protoItems []*gauge_messages.ProtoItem) {
    40  	s.ProtoScenario.ScenarioItems = append(s.ProtoScenario.ScenarioItems, protoItems...)
    41  }
    42  
    43  func (s *ScenarioResult) AddContexts(contextProtoItems []*gauge_messages.ProtoItem) {
    44  	s.ProtoScenario.Contexts = append(s.ProtoScenario.Contexts, contextProtoItems...)
    45  }
    46  
    47  func (s *ScenarioResult) AddTearDownSteps(tearDownProtoItems []*gauge_messages.ProtoItem) {
    48  	s.ProtoScenario.TearDownSteps = append(s.ProtoScenario.TearDownSteps, tearDownProtoItems...)
    49  }
    50  
    51  func (s *ScenarioResult) UpdateExecutionTime() {
    52  	s.updateExecutionTimeFromItems(s.ProtoScenario.GetContexts())
    53  	s.updateExecutionTimeFromItems(s.ProtoScenario.GetScenarioItems())
    54  }
    55  
    56  func (s *ScenarioResult) AddExecTime(execTime int64) {
    57  	currentScenarioExecTime := s.ProtoScenario.GetExecutionTime()
    58  	s.ProtoScenario.ExecutionTime = currentScenarioExecTime + execTime
    59  }
    60  
    61  func (s *ScenarioResult) ExecTime() int64 {
    62  	return s.ProtoScenario.ExecutionTime
    63  }
    64  
    65  func (s *ScenarioResult) updateExecutionTimeFromItems(protoItems []*gauge_messages.ProtoItem) {
    66  	for _, item := range protoItems {
    67  		if item.GetItemType() == gauge_messages.ProtoItem_Step {
    68  			stepExecTime := item.GetStep().GetStepExecutionResult().GetExecutionResult().GetExecutionTime()
    69  			s.AddExecTime(stepExecTime)
    70  		} else if item.GetItemType() == gauge_messages.ProtoItem_Concept {
    71  			conceptExecTime := item.GetConcept().GetConceptExecutionResult().GetExecutionResult().GetExecutionTime()
    72  			s.AddExecTime(conceptExecTime)
    73  		}
    74  	}
    75  }
    76  
    77  func (s *ScenarioResult) GetPreHook() []*gauge_messages.ProtoHookFailure {
    78  	if s.ProtoScenario.PreHookFailure == nil {
    79  		return []*gauge_messages.ProtoHookFailure{}
    80  	}
    81  	return []*gauge_messages.ProtoHookFailure{s.ProtoScenario.PreHookFailure}
    82  }
    83  
    84  func (s *ScenarioResult) GetPostHook() []*gauge_messages.ProtoHookFailure {
    85  	if s.ProtoScenario.PostHookFailure == nil {
    86  		return []*gauge_messages.ProtoHookFailure{}
    87  	}
    88  	return []*gauge_messages.ProtoHookFailure{s.ProtoScenario.PostHookFailure}
    89  }
    90  
    91  func (s *ScenarioResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) {
    92  	s.ProtoScenario.PreHookFailure = f[0]
    93  }
    94  
    95  func (s *ScenarioResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) {
    96  	s.ProtoScenario.PostHookFailure = f[0]
    97  }
    98  
    99  func (s *ScenarioResult) Item() interface{} {
   100  	return s.ProtoScenario
   101  }