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