github.com/getgauge/gauge@v1.6.9/gauge/protoConverters_test.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 gauge
     8  
     9  import (
    10  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    11  	. "gopkg.in/check.v1"
    12  )
    13  
    14  func (s *MySuite) TestCopyingFragments(c *C) {
    15  	text1 := &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Text, Text: "step with"}
    16  	staticParam := &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Parameter, Parameter: &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Static, Value: "param0"}}
    17  	text2 := &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Text, Text: "and"}
    18  	dynamicParam := &gauge_messages.Fragment{FragmentType: gauge_messages.Fragment_Parameter, Parameter: &gauge_messages.Parameter{ParameterType: gauge_messages.Parameter_Dynamic, Value: "param1"}}
    19  	fragments := []*gauge_messages.Fragment{text1, staticParam, text2, dynamicParam}
    20  
    21  	copiedFragments := makeFragmentsCopy(fragments)
    22  
    23  	compareFragments(fragments, copiedFragments, c)
    24  
    25  	fragments[1].Parameter.Value = "changedParam"
    26  	fragments[2].Text = "changed text"
    27  
    28  	c.Assert(copiedFragments[1].Parameter.GetValue(), Equals, "param0")
    29  	c.Assert(copiedFragments[2].GetText(), Equals, "and")
    30  }
    31  
    32  func (s *MySuite) TestCopyingProtoTable(c *C) {
    33  	headers := &gauge_messages.ProtoTableRow{Cells: []string{"id", "name", "description"}}
    34  	row1 := &gauge_messages.ProtoTableRow{Cells: []string{"123", "abc", "first description"}}
    35  	row2 := &gauge_messages.ProtoTableRow{Cells: []string{"456", "def", "second description"}}
    36  
    37  	table := &gauge_messages.ProtoTable{Headers: headers, Rows: []*gauge_messages.ProtoTableRow{row1, row2}}
    38  	copiedTable := makeTableCopy(table)
    39  
    40  	compareTable(table, copiedTable, c)
    41  	table.Headers.Cells[0] = "new id"
    42  	table.Rows[0].Cells[0] = "789"
    43  	table.Rows[1].Cells[1] = "xyz"
    44  
    45  	c.Assert(copiedTable.Headers.Cells[0], Equals, "id")
    46  	c.Assert(copiedTable.Rows[0].Cells[0], Equals, "123")
    47  	c.Assert(copiedTable.Rows[1].Cells[1], Equals, "def")
    48  
    49  }
    50  
    51  func (s *MySuite) TestCopyingStepValue(c *C) {
    52  	stepValue := &StepValue{[]string{"param1"}, "foo with {}", "foo with <param>"}
    53  	protoStepValue := ConvertToProtoStepValue(stepValue)
    54  
    55  	c.Assert(protoStepValue.GetStepValue(), Equals, stepValue.StepValue)
    56  	c.Assert(protoStepValue.GetParameterizedStepValue(), Equals, stepValue.ParameterizedStepValue)
    57  	c.Assert(protoStepValue.GetParameters(), DeepEquals, stepValue.Args)
    58  }
    59  
    60  func (s *MySuite) TestNewProtoScenario(c *C) {
    61  	sceHeading := "sce heading"
    62  	sce := &Scenario{Heading: &Heading{Value: sceHeading}, Span: &Span{Start: 1, End: 4}}
    63  
    64  	protoSce := NewProtoScenario(sce)
    65  
    66  	c.Assert(protoSce.GetScenarioHeading(), Equals, sceHeading)
    67  	c.Assert(protoSce.GetExecutionStatus(), Equals, gauge_messages.ExecutionStatus_NOTEXECUTED)
    68  	c.Assert(protoSce.Span.Start, Equals, int64(1))
    69  	c.Assert(protoSce.Span.End, Equals, int64(4))
    70  }
    71  
    72  func (s *MySuite) TestConvertToProtoSpecWithDataTable(c *C) {
    73  	spec := &Specification{
    74  		Heading: &Heading{
    75  			Value: "Spec Heading",
    76  		},
    77  		FileName:  "example.spec",
    78  		DataTable: DataTable{Table: &Table{headerIndexMap: make(map[string]int)}},
    79  	}
    80  	protoSpec := ConvertToProtoSpec(spec)
    81  
    82  	c.Assert(protoSpec.GetIsTableDriven(), Equals, true)
    83  }
    84  
    85  func (s *MySuite) TestConvertToProtoSpecWithoutDataTable(c *C) {
    86  	spec := &Specification{
    87  		Heading: &Heading{
    88  			Value: "Spec Heading",
    89  		},
    90  		FileName: "example.spec",
    91  	}
    92  	protoSpec := ConvertToProtoSpec(spec)
    93  
    94  	c.Assert(protoSpec.GetIsTableDriven(), Equals, false)
    95  }
    96  
    97  func (s *MySuite) TestConvertToProtoStep(c *C) {
    98  	step := &Step{
    99  		LineText: "line text",
   100  		Value:    "value",
   101  	}
   102  	actual := convertToProtoStep(step)
   103  
   104  	expected := &gauge_messages.ProtoStep{ActualText: step.LineText, ParsedText: step.Value, Fragments: []*gauge_messages.Fragment{}}
   105  	c.Assert(actual, DeepEquals, expected)
   106  }
   107  
   108  func (s *MySuite) TestConvertToProtoConcept(c *C) {
   109  	step := &Step{
   110  		LineText:  "line text",
   111  		Value:     "value",
   112  		IsConcept: true,
   113  		ConceptSteps: []*Step{
   114  			{LineText: "line text1", Value: "value1", ConceptSteps: []*Step{}},
   115  			{LineText: "line text2", Value: "value2", IsConcept: true,
   116  				ConceptSteps: []*Step{{LineText: "line text3", Value: "value3", ConceptSteps: []*Step{}}},
   117  			},
   118  		},
   119  	}
   120  	actual := convertToProtoConcept(step)
   121  
   122  	expected := &gauge_messages.ProtoItem{
   123  		ItemType: gauge_messages.ProtoItem_Concept,
   124  		Concept: &gauge_messages.ProtoConcept{
   125  			ConceptStep: newProtoStep("line text", "value"),
   126  			Steps: []*gauge_messages.ProtoItem{
   127  				newStepItem("line text1", "value1"),
   128  				{
   129  					ItemType: gauge_messages.ProtoItem_Concept,
   130  					Concept: &gauge_messages.ProtoConcept{
   131  						ConceptStep: newProtoStep("line text2", "value2"),
   132  						Steps:       []*gauge_messages.ProtoItem{newStepItem("line text3", "value3")},
   133  					},
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	c.Assert(actual, DeepEquals, expected)
   140  }
   141  
   142  func newStepItem(lineText, value string) *gauge_messages.ProtoItem {
   143  	return &gauge_messages.ProtoItem{
   144  		ItemType: gauge_messages.ProtoItem_Step,
   145  		Step:     newProtoStep(lineText, value),
   146  	}
   147  
   148  }
   149  
   150  func newProtoStep(lineText, value string) *gauge_messages.ProtoStep {
   151  	return &gauge_messages.ProtoStep{
   152  		ActualText: lineText,
   153  		ParsedText: value,
   154  		Fragments:  []*gauge_messages.Fragment{},
   155  	}
   156  }
   157  
   158  func compareFragments(fragmentList1 []*gauge_messages.Fragment, fragmentList2 []*gauge_messages.Fragment, c *C) {
   159  	c.Assert(len(fragmentList1), Equals, len(fragmentList2))
   160  	for i := range fragmentList1 {
   161  		compareFragment(fragmentList1[i], fragmentList2[i], c)
   162  	}
   163  }
   164  
   165  func compareFragment(fragment1 *gauge_messages.Fragment, fragment2 *gauge_messages.Fragment, c *C) {
   166  	c.Assert(fragment1.GetFragmentType(), Equals, fragment2.GetFragmentType())
   167  	c.Assert(fragment1.GetText(), Equals, fragment2.GetText())
   168  	parameter1 := fragment1.GetParameter()
   169  	parameter2 := fragment2.GetParameter()
   170  	compareParameter(parameter1, parameter2, c)
   171  }
   172  
   173  func compareParameter(parameter1 *gauge_messages.Parameter, parameter2 *gauge_messages.Parameter, c *C) {
   174  	if parameter1 != nil && parameter2 != nil {
   175  		c.Assert(parameter1.GetParameterType(), Equals, parameter2.GetParameterType())
   176  		c.Assert(parameter1.GetName(), Equals, parameter2.GetName())
   177  		c.Assert(parameter1.GetValue(), Equals, parameter2.GetValue())
   178  		compareTable(parameter1.GetTable(), parameter2.GetTable(), c)
   179  
   180  	} else if (parameter1 == nil && parameter2 != nil) || (parameter1 != nil && parameter2 == nil) {
   181  		c.Log("One parameter was nil and the other wasn't")
   182  		c.Fail()
   183  	}
   184  }
   185  
   186  func compareTable(table1 *gauge_messages.ProtoTable, table2 *gauge_messages.ProtoTable, c *C) {
   187  	compareTableRow(table1.GetHeaders(), table2.GetHeaders(), c)
   188  	c.Assert(len(table1.GetRows()), Equals, len(table2.GetRows()))
   189  	for i, row := range table1.GetRows() {
   190  		compareTableRow(row, table2.GetRows()[i], c)
   191  	}
   192  }
   193  
   194  func compareTableRow(row1 *gauge_messages.ProtoTableRow, row2 *gauge_messages.ProtoTableRow, c *C) {
   195  	c.Assert(row1.GetCells(), DeepEquals, row2.GetCells())
   196  }
   197  
   198  func (s *MySuite) TestProtoConvertingExecutionArgs(c *C) {
   199  	executionArgs := []*ExecutionArg{}
   200  	executionArg := &ExecutionArg{
   201  		Name:  "parallel",
   202  		Value: []string{"true"},
   203  	}
   204  	executionArgs = append(executionArgs, executionArg)
   205  	actual := ConvertToProtoExecutionArg(executionArgs)
   206  
   207  	expectedArgs := []*gauge_messages.ExecutionArg{}
   208  	expectedArg := &gauge_messages.ExecutionArg{
   209  		FlagName:  executionArgs[0].Name,
   210  		FlagValue: executionArgs[0].Value,
   211  	}
   212  	expectedArgs = append(expectedArgs, expectedArg)
   213  
   214  	c.Assert(actual, DeepEquals, expectedArgs)
   215  }