gitee.com/mirrors/gauge@v1.0.6/execution/result/stepResult.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  // StepResult represents the result of step execution
    23  type StepResult struct {
    24  	ProtoStep  *gauge_messages.ProtoStep
    25  	StepFailed bool
    26  }
    27  
    28  // NewStepResult is a constructor for StepResult
    29  func NewStepResult(ps *gauge_messages.ProtoStep) *StepResult {
    30  	return &StepResult{ProtoStep: ps}
    31  }
    32  
    33  func (s *StepResult) GetPreHook() []*gauge_messages.ProtoHookFailure {
    34  	if s.ProtoStep.StepExecutionResult.PreHookFailure == nil {
    35  		return []*gauge_messages.ProtoHookFailure{}
    36  	}
    37  	return []*gauge_messages.ProtoHookFailure{s.ProtoStep.StepExecutionResult.PreHookFailure}
    38  }
    39  
    40  func (s *StepResult) GetPostHook() []*gauge_messages.ProtoHookFailure {
    41  	if s.ProtoStep.StepExecutionResult.PostHookFailure == nil {
    42  		return []*gauge_messages.ProtoHookFailure{}
    43  	}
    44  	return []*gauge_messages.ProtoHookFailure{s.ProtoStep.StepExecutionResult.PostHookFailure}
    45  }
    46  
    47  func (s *StepResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) {
    48  	s.ProtoStep.StepExecutionResult.PreHookFailure = f[0]
    49  }
    50  
    51  func (s *StepResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) {
    52  	s.ProtoStep.StepExecutionResult.PostHookFailure = f[0]
    53  }
    54  
    55  // SetFailure sets the result to failed
    56  func (s *StepResult) SetFailure() {
    57  	s.ProtoStep.StepExecutionResult.ExecutionResult.Failed = true
    58  }
    59  
    60  // GetFailed returns the state of the result
    61  func (s *StepResult) GetFailed() bool {
    62  	return s.ProtoStep.StepExecutionResult.ExecutionResult.GetFailed()
    63  }
    64  
    65  // GetFailed returns true if the actual step failed, and not step hook.
    66  func (s *StepResult) GetStepFailed() bool {
    67  	return s.StepFailed
    68  }
    69  
    70  // GetStackTrace returns the stacktrace for step failure
    71  func (s *StepResult) GetStackTrace() string {
    72  	return s.ProtoStep.GetStepExecutionResult().GetExecutionResult().GetStackTrace()
    73  }
    74  
    75  // GetErrorMessage returns the error message for step failure
    76  func (s *StepResult) GetErrorMessage() string {
    77  	return s.ProtoStep.GetStepExecutionResult().GetExecutionResult().GetErrorMessage()
    78  }
    79  
    80  // GetStepActualText returns the Actual text of step from step result
    81  func (s *StepResult) GetStepActualText() string {
    82  	return s.ProtoStep.GetActualText()
    83  }
    84  
    85  // SetStepFailure sets the actual step as failed. StepResult.ProtoStep.GetFailed() returns true even if hook failed and not actual step.
    86  func (s *StepResult) SetStepFailure() {
    87  	s.StepFailed = true
    88  }
    89  
    90  func (s *StepResult) Item() interface{} {
    91  	return s.ProtoStep
    92  }
    93  
    94  // ExecTime returns the time taken to execute the step
    95  func (s *StepResult) ExecTime() int64 {
    96  	return s.ProtoStep.StepExecutionResult.ExecutionResult.GetExecutionTime()
    97  }
    98  
    99  // AddExecTime increments the execution time by the given value
   100  func (s *StepResult) AddExecTime(t int64) {
   101  	if s.ProtoStep.StepExecutionResult.ExecutionResult == nil {
   102  		s.ProtoStep.StepExecutionResult.ExecutionResult = &gauge_messages.ProtoExecutionResult{Failed: false}
   103  	}
   104  	currentTime := s.ProtoStep.StepExecutionResult.ExecutionResult.GetExecutionTime()
   105  	s.ProtoStep.StepExecutionResult.ExecutionResult.ExecutionTime = currentTime + t
   106  }
   107  
   108  // ProtoStepExecResult returns the step execution result used at the proto layer
   109  func (s *StepResult) ProtoStepExecResult() *gauge_messages.ProtoStepExecutionResult {
   110  	return s.ProtoStep.StepExecutionResult
   111  }
   112  
   113  // SetProtoExecResult sets the execution result
   114  func (s *StepResult) SetProtoExecResult(r *gauge_messages.ProtoExecutionResult) {
   115  	s.ProtoStep.StepExecutionResult.ExecutionResult = r
   116  }