gitee.com/mirrors/gauge@v1.0.6/execution/resolve_test.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 execution
    19  
    20  import (
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/getgauge/gauge/gauge"
    25  	"github.com/getgauge/gauge/gauge_messages"
    26  	"github.com/getgauge/gauge/parser"
    27  	. "gopkg.in/check.v1"
    28  )
    29  
    30  func (s *MySuite) TestResolveConceptToProtoConceptItem(c *C) {
    31  	conceptDictionary := gauge.NewConceptDictionary()
    32  
    33  	specText := newSpecBuilder().specHeading("A spec heading").
    34  		scenarioHeading("First scenario").
    35  		step("create user \"456\" \"foo\" and \"9900\"").
    36  		String()
    37  	path, _ := filepath.Abs(filepath.Join("testdata", "concept.cpt"))
    38  	parser.AddConcepts([]string{path}, conceptDictionary)
    39  
    40  	spec, _, _ := new(parser.SpecParser).Parse(specText, conceptDictionary, "")
    41  
    42  	specExecutor := newSpecExecutor(spec, nil, nil, nil, 0)
    43  	specExecutor.errMap = getValidationErrorMap()
    44  	lookup, err := specExecutor.dataTableLookup()
    45  	c.Assert(err, IsNil)
    46  	cItem, err := resolveToProtoConceptItem(*spec.Scenarios[0].Steps[0], lookup, specExecutor.setSkipInfo)
    47  	c.Assert(err, IsNil)
    48  	protoConcept := cItem.GetConcept()
    49  
    50  	checkConceptParameterValuesInOrder(c, protoConcept, "456", "foo", "9900")
    51  	firstNestedStep := protoConcept.GetSteps()[0].GetConcept().GetSteps()[0].GetStep()
    52  	params := getParameters(firstNestedStep.GetFragments())
    53  	c.Assert(1, Equals, len(params))
    54  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
    55  	c.Assert(params[0].GetValue(), Equals, "456")
    56  
    57  	secondNestedStep := protoConcept.GetSteps()[0].GetConcept().GetSteps()[1].GetStep()
    58  	params = getParameters(secondNestedStep.GetFragments())
    59  	c.Assert(1, Equals, len(params))
    60  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
    61  	c.Assert(params[0].GetValue(), Equals, "foo")
    62  
    63  	secondStep := protoConcept.GetSteps()[1].GetStep()
    64  	params = getParameters(secondStep.GetFragments())
    65  	c.Assert(1, Equals, len(params))
    66  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
    67  	c.Assert(params[0].GetValue(), Equals, "9900")
    68  
    69  }
    70  
    71  func (s *MySuite) TestResolveNestedConceptToProtoConceptItem(c *C) {
    72  	conceptDictionary := gauge.NewConceptDictionary()
    73  
    74  	specText := newSpecBuilder().specHeading("A spec heading").
    75  		scenarioHeading("First scenario").
    76  		step("create user \"456\" \"foo\" and \"9900\"").
    77  		String()
    78  
    79  	path, _ := filepath.Abs(filepath.Join("testdata", "concept.cpt"))
    80  	parser.AddConcepts([]string{path}, conceptDictionary)
    81  	specParser := new(parser.SpecParser)
    82  	spec, _, _ := specParser.Parse(specText, conceptDictionary, "")
    83  
    84  	specExecutor := newSpecExecutor(spec, nil, nil, nil, 0)
    85  	specExecutor.errMap = getValidationErrorMap()
    86  	lookup, err := specExecutor.dataTableLookup()
    87  	c.Assert(err, IsNil)
    88  	cItem, err := resolveToProtoConceptItem(*spec.Scenarios[0].Steps[0], lookup, specExecutor.setSkipInfo)
    89  	c.Assert(err, IsNil)
    90  	protoConcept := cItem.GetConcept()
    91  	checkConceptParameterValuesInOrder(c, protoConcept, "456", "foo", "9900")
    92  
    93  	c.Assert(protoConcept.GetSteps()[0].GetItemType(), Equals, gauge_messages.ProtoItem_Concept)
    94  
    95  	nestedConcept := protoConcept.GetSteps()[0].GetConcept()
    96  	checkConceptParameterValuesInOrder(c, nestedConcept, "456", "foo")
    97  
    98  	firstNestedStep := nestedConcept.GetSteps()[0].GetStep()
    99  	params := getParameters(firstNestedStep.GetFragments())
   100  	c.Assert(1, Equals, len(params))
   101  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
   102  	c.Assert(params[0].GetValue(), Equals, "456")
   103  
   104  	secondNestedStep := nestedConcept.GetSteps()[1].GetStep()
   105  	params = getParameters(secondNestedStep.GetFragments())
   106  	c.Assert(1, Equals, len(params))
   107  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
   108  	c.Assert(params[0].GetValue(), Equals, "foo")
   109  
   110  	c.Assert(protoConcept.GetSteps()[1].GetItemType(), Equals, gauge_messages.ProtoItem_Step)
   111  	secondStepInConcept := protoConcept.GetSteps()[1].GetStep()
   112  	params = getParameters(secondStepInConcept.GetFragments())
   113  	c.Assert(1, Equals, len(params))
   114  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
   115  	c.Assert(params[0].GetValue(), Equals, "9900")
   116  }
   117  
   118  func TestResolveNestedConceptAndTableParamToProtoConceptItem(t *testing.T) {
   119  	conceptDictionary := gauge.NewConceptDictionary()
   120  
   121  	specText := newSpecBuilder().specHeading("A spec heading").
   122  		scenarioHeading("First scenario").
   123  		step("create user \"456\"").
   124  		String()
   125  	want := "456"
   126  	path, _ := filepath.Abs(filepath.Join("testdata", "conceptTable.cpt"))
   127  	parser.AddConcepts([]string{path}, conceptDictionary)
   128  	specParser := new(parser.SpecParser)
   129  	spec, _, _ := specParser.Parse(specText, conceptDictionary, "")
   130  
   131  	specExecutor := newSpecExecutor(spec, nil, nil, nil, 0)
   132  	specExecutor.errMap = getValidationErrorMap()
   133  	lookup, err := specExecutor.dataTableLookup()
   134  	if err != nil {
   135  		t.Errorf("Expected no error. Got : %s", err.Error())
   136  	}
   137  	cItem, err := resolveToProtoConceptItem(*spec.Scenarios[0].Steps[0], lookup, specExecutor.setSkipInfo)
   138  	if err != nil {
   139  		t.Errorf("Expected no error. Got : %s", err.Error())
   140  	}
   141  	protoConcept := cItem.GetConcept()
   142  	got := getParameters(protoConcept.GetSteps()[0].GetStep().GetFragments())[0].GetTable().GetRows()[1].Cells[0]
   143  
   144  	if want != got {
   145  		t.Errorf("Did not resolve dynamic param in table for concept. Got %s, want: %s", got, want)
   146  	}
   147  }
   148  
   149  func (s *MySuite) TestResolveToProtoConceptItemWithDataTable(c *C) {
   150  	conceptDictionary := gauge.NewConceptDictionary()
   151  
   152  	specText := newSpecBuilder().specHeading("A spec heading").
   153  		tableHeader("id", "name", "phone").
   154  		tableHeader("123", "foo", "8800").
   155  		tableHeader("666", "bar", "9900").
   156  		scenarioHeading("First scenario").
   157  		step("create user <id> <name> and <phone>").
   158  		String()
   159  
   160  	path, _ := filepath.Abs(filepath.Join("testdata", "concept.cpt"))
   161  	parser.AddConcepts([]string{path}, conceptDictionary)
   162  	specParser := new(parser.SpecParser)
   163  	spec, _, _ := specParser.Parse(specText, conceptDictionary, "")
   164  
   165  	specExecutor := newSpecExecutor(spec, nil, nil, nil, 0)
   166  
   167  	specExecutor.errMap = gauge.NewBuildErrors()
   168  	lookup, err := specExecutor.dataTableLookup()
   169  	c.Assert(err, IsNil)
   170  	cItem, err := resolveToProtoConceptItem(*spec.Scenarios[0].Steps[0], lookup, specExecutor.setSkipInfo)
   171  	c.Assert(err, IsNil)
   172  	protoConcept := cItem.GetConcept()
   173  	checkConceptParameterValuesInOrder(c, protoConcept, "123", "foo", "8800")
   174  
   175  	c.Assert(protoConcept.GetSteps()[0].GetItemType(), Equals, gauge_messages.ProtoItem_Concept)
   176  	nestedConcept := protoConcept.GetSteps()[0].GetConcept()
   177  	checkConceptParameterValuesInOrder(c, nestedConcept, "123", "foo")
   178  	firstNestedStep := nestedConcept.GetSteps()[0].GetStep()
   179  	params := getParameters(firstNestedStep.GetFragments())
   180  	c.Assert(1, Equals, len(params))
   181  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
   182  	c.Assert(params[0].GetValue(), Equals, "123")
   183  
   184  	secondNestedStep := nestedConcept.GetSteps()[1].GetStep()
   185  	params = getParameters(secondNestedStep.GetFragments())
   186  	c.Assert(1, Equals, len(params))
   187  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
   188  	c.Assert(params[0].GetValue(), Equals, "foo")
   189  
   190  	c.Assert(protoConcept.GetSteps()[1].GetItemType(), Equals, gauge_messages.ProtoItem_Step)
   191  	secondStepInConcept := protoConcept.GetSteps()[1].GetStep()
   192  	params = getParameters(secondStepInConcept.GetFragments())
   193  	c.Assert(1, Equals, len(params))
   194  	c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
   195  	c.Assert(params[0].GetValue(), Equals, "8800")
   196  }
   197  
   198  func checkConceptParameterValuesInOrder(c *C, concept *gauge_messages.ProtoConcept, paramValues ...string) {
   199  	params := getParameters(concept.GetConceptStep().Fragments)
   200  	c.Assert(len(params), Equals, len(paramValues))
   201  	for i, param := range params {
   202  		c.Assert(param.GetValue(), Equals, paramValues[i])
   203  	}
   204  }