gitee.com/mirrors/gauge@v1.0.6/execution/result/specResult.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  // SpecResult represents the result of spec execution
    25  type SpecResult struct {
    26  	ProtoSpec            *gauge_messages.ProtoSpec
    27  	ScenarioFailedCount  int
    28  	ScenarioCount        int
    29  	IsFailed             bool
    30  	FailedDataTableRows  []int32
    31  	ExecutionTime        int64
    32  	Skipped              bool
    33  	ScenarioSkippedCount int
    34  	Errors               []*gauge_messages.Error
    35  }
    36  
    37  // SetFailure sets the result to failed
    38  func (specResult *SpecResult) SetFailure() {
    39  	specResult.IsFailed = true
    40  }
    41  
    42  func (specResult *SpecResult) SetSkipped(skipped bool) {
    43  	specResult.Skipped = skipped
    44  }
    45  
    46  func (specResult *SpecResult) AddSpecItems(resolvedItems []*gauge_messages.ProtoItem) {
    47  	specResult.ProtoSpec.Items = append(specResult.ProtoSpec.Items, resolvedItems...)
    48  }
    49  
    50  // AddScenarioResults adds the scenario result to the spec result.
    51  func (specResult *SpecResult) AddScenarioResults(scenarioResults []Result) {
    52  	for _, scenarioResult := range scenarioResults {
    53  		if scenarioResult.GetFailed() {
    54  			specResult.IsFailed = true
    55  			specResult.ScenarioFailedCount++
    56  		}
    57  		specResult.AddExecTime(scenarioResult.ExecTime())
    58  		specResult.ProtoSpec.Items = append(specResult.ProtoSpec.Items, &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Scenario, Scenario: scenarioResult.Item().(*gauge_messages.ProtoScenario)})
    59  	}
    60  	specResult.ScenarioCount += len(scenarioResults)
    61  }
    62  
    63  func (specResult *SpecResult) AddTableDrivenScenarioResult(r *ScenarioResult, t *gauge_messages.ProtoTable, scenarioRowIndex int, specRowIndex int, specTableDriven bool) {
    64  	if r.GetFailed() {
    65  		specResult.IsFailed = true
    66  		specResult.ScenarioFailedCount++
    67  	}
    68  	specResult.AddExecTime(r.ExecTime())
    69  	pItem := &gauge_messages.ProtoItem{
    70  		ItemType: gauge_messages.ProtoItem_TableDrivenScenario,
    71  		TableDrivenScenario: &gauge_messages.ProtoTableDrivenScenario{
    72  			Scenario:              r.Item().(*gauge_messages.ProtoScenario),
    73  			IsScenarioTableDriven: true,
    74  			ScenarioTableRowIndex: int32(scenarioRowIndex),
    75  			IsSpecTableDriven:     specTableDriven,
    76  			ScenarioDataTable:     t,
    77  			TableRowIndex:         int32(specRowIndex),
    78  			ScenarioTableRow:      r.ScenarioDataTableRow,
    79  		},
    80  	}
    81  	specResult.ProtoSpec.Items = append(specResult.ProtoSpec.Items, pItem)
    82  }
    83  
    84  // AddTableRelatedScenarioResult aggregates the data table driven spec results.
    85  func (specResult *SpecResult) AddTableRelatedScenarioResult(scenarioResults [][]Result, index int) {
    86  	numberOfScenarios := len(scenarioResults[0])
    87  
    88  	for scenarioIndex := 0; scenarioIndex < numberOfScenarios; scenarioIndex++ {
    89  		scenarioFailed := false
    90  		for _, eachRow := range scenarioResults {
    91  			protoScenario := eachRow[scenarioIndex].Item().(*gauge_messages.ProtoScenario)
    92  			specResult.AddExecTime(protoScenario.GetExecutionTime())
    93  			if protoScenario.GetExecutionStatus() == gauge_messages.ExecutionStatus_FAILED {
    94  				scenarioFailed = true
    95  				specResult.FailedDataTableRows = append(specResult.FailedDataTableRows, int32(index))
    96  			}
    97  			protoTableDrivenScenario := &gauge_messages.ProtoTableDrivenScenario{
    98  				Scenario:         protoScenario,
    99  				TableRowIndex:    int32(index),
   100  				ScenarioTableRow: eachRow[scenarioIndex].(*ScenarioResult).ScenarioDataTableRow,
   101  			}
   102  			protoItem := &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_TableDrivenScenario, TableDrivenScenario: protoTableDrivenScenario}
   103  			specResult.ProtoSpec.Items = append(specResult.ProtoSpec.Items, protoItem)
   104  		}
   105  		if scenarioFailed {
   106  			specResult.ScenarioFailedCount++
   107  			specResult.IsFailed = true
   108  		}
   109  	}
   110  	specResult.ProtoSpec.IsTableDriven = true
   111  	specResult.ScenarioCount += numberOfScenarios
   112  }
   113  
   114  func (specResult *SpecResult) AddExecTime(execTime int64) {
   115  	specResult.ExecutionTime += execTime
   116  }
   117  
   118  func (specResult *SpecResult) GetPreHook() []*gauge_messages.ProtoHookFailure {
   119  	return specResult.ProtoSpec.PreHookFailures
   120  }
   121  
   122  func (specResult *SpecResult) GetPostHook() []*gauge_messages.ProtoHookFailure {
   123  	return specResult.ProtoSpec.PostHookFailures
   124  }
   125  
   126  func (specResult *SpecResult) AddPreHook(f ...*gauge_messages.ProtoHookFailure) {
   127  	specResult.ProtoSpec.PreHookFailures = append(specResult.ProtoSpec.PreHookFailures, f...)
   128  }
   129  
   130  func (specResult *SpecResult) AddPostHook(f ...*gauge_messages.ProtoHookFailure) {
   131  	specResult.ProtoSpec.PostHookFailures = append(specResult.ProtoSpec.PostHookFailures, f...)
   132  }
   133  
   134  func (specResult *SpecResult) ExecTime() int64 {
   135  	return specResult.ExecutionTime
   136  }
   137  
   138  // GetFailed returns the state of the result
   139  func (specResult *SpecResult) GetFailed() bool {
   140  	return specResult.IsFailed
   141  }
   142  
   143  func (specResult *SpecResult) Item() interface{} {
   144  	return specResult.ProtoSpec
   145  }