github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/gauge/protoConverters.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 gauge
    19  
    20  import (
    21  	"github.com/getgauge/gauge/execution/result"
    22  	"github.com/getgauge/gauge/gauge_messages"
    23  	"github.com/golang/protobuf/proto"
    24  )
    25  
    26  func ConvertToProtoItem(item Item) *gauge_messages.ProtoItem {
    27  	switch item.Kind() {
    28  	case ScenarioKind:
    29  		return convertToProtoScenarioItem(item.(*Scenario))
    30  	case StepKind:
    31  		return convertToProtoStepItem(item.(*Step))
    32  	case CommentKind:
    33  		return convertToProtoCommentItem(item.(*Comment))
    34  	case DataTableKind:
    35  		return convertToProtoDataTableItem(item.(*DataTable))
    36  	case TagKind:
    37  		return convertToProtoTagItem(item.(*Tags))
    38  	}
    39  	return nil
    40  }
    41  
    42  func convertToProtoTagItem(tags *Tags) *gauge_messages.ProtoItem {
    43  	return &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Tags.Enum(), Tags: convertToProtoTags(tags)}
    44  }
    45  
    46  func convertToProtoStepItem(step *Step) *gauge_messages.ProtoItem {
    47  	if step.IsConcept {
    48  		return convertToProtoConcept(step)
    49  	}
    50  	return &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Step.Enum(), Step: convertToProtoStep(step)}
    51  }
    52  
    53  func convertToProtoStepItems(steps []*Step) []*gauge_messages.ProtoItem {
    54  	protoItems := make([]*gauge_messages.ProtoItem, 0)
    55  	for _, step := range steps {
    56  		protoItems = append(protoItems, convertToProtoStepItem(step))
    57  	}
    58  	return protoItems
    59  }
    60  
    61  func convertToProtoScenarioItem(scenario *Scenario) *gauge_messages.ProtoItem {
    62  	scenarioItems := make([]*gauge_messages.ProtoItem, 0)
    63  	for _, item := range scenario.Items {
    64  		scenarioItems = append(scenarioItems, ConvertToProtoItem(item))
    65  	}
    66  	protoScenario := NewProtoScenario(scenario)
    67  	return &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Scenario.Enum(), Scenario: protoScenario}
    68  }
    69  
    70  func convertToProtoConcept(concept *Step) *gauge_messages.ProtoItem {
    71  	protoConcept := &gauge_messages.ProtoConcept{ConceptStep: convertToProtoStep(concept), Steps: convertToProtoStepItems(concept.ConceptSteps)}
    72  	protoConceptItem := &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Concept.Enum(), Concept: protoConcept}
    73  	return protoConceptItem
    74  }
    75  
    76  func convertToProtoStep(step *Step) *gauge_messages.ProtoStep {
    77  	return &gauge_messages.ProtoStep{ActualText: proto.String(step.LineText), ParsedText: proto.String(step.Value), Fragments: makeFragmentsCopy(step.Fragments)}
    78  }
    79  
    80  func convertToProtoTags(tags *Tags) *gauge_messages.ProtoTags {
    81  	return &gauge_messages.ProtoTags{Tags: getAllTags(tags)}
    82  
    83  }
    84  
    85  func getAllTags(tags *Tags) []string {
    86  	allTags := make([]string, 0)
    87  	for _, tag := range tags.Values {
    88  		allTags = append(allTags, *proto.String(tag))
    89  	}
    90  	return allTags
    91  }
    92  
    93  func makeFragmentsCopy(fragments []*gauge_messages.Fragment) []*gauge_messages.Fragment {
    94  	copiedFragments := make([]*gauge_messages.Fragment, 0)
    95  	for _, fragment := range fragments {
    96  		copiedFragments = append(copiedFragments, makeFragmentCopy(fragment))
    97  	}
    98  	return copiedFragments
    99  }
   100  
   101  func makeFragmentCopy(fragment *gauge_messages.Fragment) *gauge_messages.Fragment {
   102  	if fragment.GetFragmentType() == gauge_messages.Fragment_Text {
   103  		return &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Text.Enum(), Text: proto.String(fragment.GetText())}
   104  	} else {
   105  		return &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Parameter.Enum(), Parameter: makeParameterCopy(fragment.Parameter)}
   106  	}
   107  }
   108  
   109  func makeParameterCopy(parameter *gauge_messages.Parameter) *gauge_messages.Parameter {
   110  	switch parameter.GetParameterType() {
   111  	case gauge_messages.Parameter_Static:
   112  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Static.Enum(), Value: proto.String(parameter.GetValue()), Name: proto.String(parameter.GetName())}
   113  	case gauge_messages.Parameter_Dynamic:
   114  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Dynamic.Enum(), Value: proto.String(parameter.GetValue()), Name: proto.String(parameter.GetName())}
   115  	case gauge_messages.Parameter_Table:
   116  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Table.Enum(), Table: makeTableCopy(parameter.GetTable()), Name: proto.String(parameter.GetName())}
   117  	case gauge_messages.Parameter_Special_String:
   118  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Special_String.Enum(), Value: proto.String(parameter.GetValue()), Name: proto.String(parameter.GetName())}
   119  	case gauge_messages.Parameter_Special_Table:
   120  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Special_Table.Enum(), Table: makeTableCopy(parameter.GetTable()), Name: proto.String(parameter.GetName())}
   121  	}
   122  	return parameter
   123  }
   124  
   125  func makeTableCopy(table *gauge_messages.ProtoTable) *gauge_messages.ProtoTable {
   126  	copiedTable := &gauge_messages.ProtoTable{}
   127  	copiedTable.Headers = makeProtoTableRowCopy(table.GetHeaders())
   128  
   129  	copiedRows := make([]*gauge_messages.ProtoTableRow, 0)
   130  	for _, tableRow := range table.GetRows() {
   131  		copiedRows = append(copiedRows, makeProtoTableRowCopy(tableRow))
   132  	}
   133  	copiedTable.Rows = copiedRows
   134  	return copiedTable
   135  }
   136  
   137  func makeProtoTableRowCopy(tableRow *gauge_messages.ProtoTableRow) *gauge_messages.ProtoTableRow {
   138  	copiedCells := make([]string, 0)
   139  	return &gauge_messages.ProtoTableRow{Cells: append(copiedCells, tableRow.GetCells()...)}
   140  }
   141  
   142  func convertToProtoCommentItem(comment *Comment) *gauge_messages.ProtoItem {
   143  	return &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Comment.Enum(), Comment: &gauge_messages.ProtoComment{Text: proto.String(comment.Value)}}
   144  }
   145  
   146  func convertToProtoDataTableItem(dataTable *DataTable) *gauge_messages.ProtoItem {
   147  	return &gauge_messages.ProtoItem{ItemType: gauge_messages.ProtoItem_Table.Enum(), Table: convertToProtoTableParam(&dataTable.Table)}
   148  }
   149  
   150  func convertToProtoParameter(arg *StepArg) *gauge_messages.Parameter {
   151  	switch arg.ArgType {
   152  	case Static:
   153  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Static.Enum(), Value: proto.String(arg.Value), Name: proto.String(arg.Name)}
   154  	case Dynamic:
   155  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Dynamic.Enum(), Value: proto.String(arg.Value), Name: proto.String(arg.Name)}
   156  	case TableArg:
   157  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Table.Enum(), Table: convertToProtoTableParam(&arg.Table), Name: proto.String(arg.Name)}
   158  	case SpecialString:
   159  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Special_String.Enum(), Value: proto.String(arg.Value), Name: proto.String(arg.Name)}
   160  	case SpecialTable:
   161  		return &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Special_Table.Enum(), Table: convertToProtoTableParam(&arg.Table), Name: proto.String(arg.Name)}
   162  	}
   163  	return nil
   164  }
   165  
   166  func convertToProtoTableParam(table *Table) *gauge_messages.ProtoTable {
   167  	protoTableParam := &gauge_messages.ProtoTable{Rows: make([]*gauge_messages.ProtoTableRow, 0)}
   168  	protoTableParam.Headers = &gauge_messages.ProtoTableRow{Cells: table.Headers}
   169  	for _, row := range table.Rows() {
   170  		protoTableParam.Rows = append(protoTableParam.Rows, &gauge_messages.ProtoTableRow{Cells: row})
   171  	}
   172  	return protoTableParam
   173  }
   174  
   175  func ConvertToProtoSuiteResult(suiteResult *result.SuiteResult) *gauge_messages.ProtoSuiteResult {
   176  	protoSuiteResult := &gauge_messages.ProtoSuiteResult{
   177  		PreHookFailure:    suiteResult.PreSuite,
   178  		PostHookFailure:   suiteResult.PostSuite,
   179  		Failed:            proto.Bool(suiteResult.IsFailed),
   180  		SpecsFailedCount:  proto.Int32(int32(suiteResult.SpecsFailedCount)),
   181  		ExecutionTime:     proto.Int64(suiteResult.ExecutionTime),
   182  		SpecResults:       convertToProtoSpecResult(suiteResult.SpecResults),
   183  		SuccessRate:       proto.Float32(getSuccessRate(len(suiteResult.SpecResults), suiteResult.SpecsFailedCount+suiteResult.SpecsSkippedCount)),
   184  		Environment:       proto.String(suiteResult.Environment),
   185  		Tags:              proto.String(suiteResult.Tags),
   186  		ProjectName:       proto.String(suiteResult.ProjectName),
   187  		Timestamp:         proto.String(suiteResult.Timestamp),
   188  		SpecsSkippedCount: proto.Int32(int32(suiteResult.SpecsSkippedCount)),
   189  	}
   190  	return protoSuiteResult
   191  }
   192  
   193  func getSuccessRate(totalSpecs int, failedSpecs int) float32 {
   194  	if totalSpecs == 0 {
   195  		return 0
   196  	}
   197  	return (float32)(100.0 * (totalSpecs - failedSpecs) / totalSpecs)
   198  }
   199  
   200  func convertToProtoSpecResult(specResults []*result.SpecResult) []*gauge_messages.ProtoSpecResult {
   201  	protoSpecResults := make([]*gauge_messages.ProtoSpecResult, 0)
   202  	for _, specResult := range specResults {
   203  		protoSpecResult := &gauge_messages.ProtoSpecResult{
   204  			ProtoSpec:            specResult.ProtoSpec,
   205  			ScenarioCount:        proto.Int32(int32(specResult.ScenarioCount)),
   206  			ScenarioFailedCount:  proto.Int32(int32(specResult.ScenarioFailedCount)),
   207  			Failed:               proto.Bool(specResult.IsFailed),
   208  			FailedDataTableRows:  specResult.FailedDataTableRows,
   209  			ExecutionTime:        proto.Int64(specResult.ExecutionTime),
   210  			Skipped:              proto.Bool(specResult.Skipped),
   211  			ScenarioSkippedCount: proto.Int32(int32(specResult.ScenarioSkippedCount)),
   212  		}
   213  		protoSpecResults = append(protoSpecResults, protoSpecResult)
   214  	}
   215  	return protoSpecResults
   216  }
   217  
   218  func ConvertToProtoSpec(spec *Specification) *gauge_messages.ProtoSpec {
   219  	protoSpec := newProtoSpec(spec)
   220  	protoItems := make([]*gauge_messages.ProtoItem, 0)
   221  	for _, item := range spec.Items {
   222  		protoItems = append(protoItems, ConvertToProtoItem(item))
   223  	}
   224  	protoSpec.Items = protoItems
   225  	return protoSpec
   226  }
   227  
   228  func ConvertToProtoStepValue(stepValue *StepValue) *gauge_messages.ProtoStepValue {
   229  	return &gauge_messages.ProtoStepValue{
   230  		StepValue:              proto.String(stepValue.StepValue),
   231  		ParameterizedStepValue: proto.String(stepValue.ParameterizedStepValue),
   232  		Parameters:             stepValue.Args,
   233  	}
   234  }
   235  
   236  func newProtoSpec(specification *Specification) *gauge_messages.ProtoSpec {
   237  	return &gauge_messages.ProtoSpec{
   238  		Items:         make([]*gauge_messages.ProtoItem, 0),
   239  		SpecHeading:   proto.String(specification.Heading.Value),
   240  		IsTableDriven: proto.Bool(false),
   241  		FileName:      proto.String(specification.FileName),
   242  		Tags:          getTags(specification.Tags),
   243  	}
   244  
   245  }
   246  
   247  func NewSpecResult(specification *Specification) *result.SpecResult {
   248  	return &result.SpecResult{
   249  		ProtoSpec:           newProtoSpec(specification),
   250  		FailedDataTableRows: make([]int32, 0),
   251  	}
   252  }
   253  
   254  func NewProtoScenario(scenario *Scenario) *gauge_messages.ProtoScenario {
   255  	return &gauge_messages.ProtoScenario{
   256  		ScenarioHeading: proto.String(scenario.Heading.Value),
   257  		Failed:          proto.Bool(false),
   258  		Tags:            getTags(scenario.Tags),
   259  		Contexts:        make([]*gauge_messages.ProtoItem, 0),
   260  		ExecutionTime:   proto.Int64(0),
   261  	}
   262  }
   263  
   264  func getTags(tags *Tags) []string {
   265  	if tags != nil {
   266  		return tags.Values
   267  	} else {
   268  		return make([]string, 0)
   269  	}
   270  }