github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/parser/resolver_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 parser
    19  
    20  import (
    21  	"path/filepath"
    22  
    23  	"github.com/getgauge/gauge/gauge"
    24  	"github.com/getgauge/gauge/util"
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  func (s *MySuite) TestParsingFileSpecialType(c *C) {
    29  	resolver := newSpecialTypeResolver()
    30  	resolver.predefinedResolvers["file"] = func(value string) (*gauge.StepArg, error) {
    31  		return &gauge.StepArg{Value: "dummy", ArgType: gauge.Static}, nil
    32  	}
    33  
    34  	stepArg, _ := resolver.resolve("file:foo")
    35  	c.Assert(stepArg.Value, Equals, "dummy")
    36  	c.Assert(stepArg.ArgType, Equals, gauge.Static)
    37  	c.Assert(stepArg.Name, Equals, "file:foo")
    38  }
    39  
    40  func (s *MySuite) TestParsingFileAsSpecialParamWithWindowsPathAsValue(c *C) {
    41  	resolver := newSpecialTypeResolver()
    42  	resolver.predefinedResolvers["file"] = func(value string) (*gauge.StepArg, error) {
    43  		return &gauge.StepArg{Value: "hello", ArgType: gauge.SpecialString}, nil
    44  	}
    45  
    46  	stepArg, _ := resolver.resolve(`file:C:\Users\abc`)
    47  	c.Assert(stepArg.Value, Equals, "hello")
    48  	c.Assert(stepArg.ArgType, Equals, gauge.SpecialString)
    49  	if util.IsWindows() {
    50  		c.Assert(stepArg.Name, Equals, `file:C:\\Users\\abc`)
    51  	} else {
    52  		c.Assert(stepArg.Name, Equals, `file:C:\Users\abc`)
    53  	}
    54  }
    55  
    56  func (s *MySuite) TestParsingInvalidSpecialType(c *C) {
    57  	resolver := newSpecialTypeResolver()
    58  
    59  	_, err := resolver.resolve("unknown:foo")
    60  	c.Assert(err.Error(), Equals, "Resolver not found for special param <unknown:foo>")
    61  }
    62  
    63  func (s *MySuite) TestConvertCsvToTable(c *C) {
    64  	table, _ := convertCsvToTable("id,name\n1,foo\n2,bar")
    65  
    66  	idColumn := table.Get("id")
    67  	c.Assert(idColumn[0].Value, Equals, "1")
    68  	c.Assert(idColumn[1].Value, Equals, "2")
    69  
    70  	nameColumn := table.Get("name")
    71  	c.Assert(nameColumn[0].Value, Equals, "foo")
    72  	c.Assert(nameColumn[1].Value, Equals, "bar")
    73  }
    74  
    75  func (s *MySuite) TestConvertEmptyCsvToTable(c *C) {
    76  	table, _ := convertCsvToTable("")
    77  	c.Assert(len(table.Columns), Equals, 0)
    78  }
    79  
    80  func (s *MySuite) TestParsingUnknownSpecialType(c *C) {
    81  	resolver := newSpecialTypeResolver()
    82  
    83  	_, err := resolver.getStepArg("unknown", "foo", "unknown:foo")
    84  	c.Assert(err.Error(), Equals, "Resolver not found for special param <unknown:foo>")
    85  }
    86  
    87  func (s *MySuite) TestPopulatingConceptLookup(c *C) {
    88  	parser := new(SpecParser)
    89  	specText := SpecBuilder().specHeading("A spec heading").
    90  		tableHeader("id", "name", "phone").
    91  		tableHeader("123", "foo", "888").
    92  		scenarioHeading("First scenario").
    93  		step("create user <id> <name> and <phone>").
    94  		String()
    95  
    96  	conceptDictionary := gauge.NewConceptDictionary()
    97  	path, _ := filepath.Abs(filepath.Join("testdata", "dynamic_param_concept.cpt"))
    98  	AddConcepts([]string{path}, conceptDictionary)
    99  	spec, _ := parser.Parse(specText, conceptDictionary, "")
   100  	concept := spec.Scenarios[0].Steps[0]
   101  
   102  	dataTableLookup := new(gauge.ArgLookup).FromDataTableRow(&spec.DataTable.Table, 0)
   103  	PopulateConceptDynamicParams(concept, dataTableLookup)
   104  
   105  	c.Assert(concept.GetArg("user-id").Value, Equals, "123")
   106  	c.Assert(concept.GetArg("user-name").Value, Equals, "foo")
   107  	c.Assert(concept.GetArg("user-phone").Value, Equals, "888")
   108  
   109  }
   110  
   111  func (s *MySuite) TestPopulatingNestedConceptLookup(c *C) {
   112  	parser := new(SpecParser)
   113  	specText := SpecBuilder().specHeading("A spec heading").
   114  		tableHeader("id", "name", "phone").
   115  		tableHeader("123", "prateek", "8800").
   116  		scenarioHeading("First scenario").
   117  		step("create user <id> <name> and <phone>").
   118  		step("create user \"456\" \"foo\" and \"9900\"").
   119  		String()
   120  
   121  	conceptDictionary := gauge.NewConceptDictionary()
   122  	path, _ := filepath.Abs(filepath.Join("testdata", "dynamic_param_concept.cpt"))
   123  	AddConcepts([]string{path}, conceptDictionary)
   124  	spec, _ := parser.Parse(specText, conceptDictionary, "")
   125  	concept1 := spec.Scenarios[0].Steps[0]
   126  
   127  	dataTableLookup := new(gauge.ArgLookup).FromDataTableRow(&spec.DataTable.Table, 0)
   128  	PopulateConceptDynamicParams(concept1, dataTableLookup)
   129  
   130  	c.Assert(concept1.GetArg("user-id").Value, Equals, "123")
   131  	c.Assert(concept1.GetArg("user-name").Value, Equals, "prateek")
   132  	c.Assert(concept1.GetArg("user-phone").Value, Equals, "8800")
   133  
   134  	nestedConcept := concept1.ConceptSteps[0]
   135  	c.Assert(nestedConcept.GetArg("userid").Value, Equals, "123")
   136  	c.Assert(nestedConcept.GetArg("username").Value, Equals, "prateek")
   137  
   138  	concept2 := spec.Scenarios[0].Steps[1]
   139  	c.Assert(concept2.GetArg("user-id").Value, Equals, "456")
   140  	c.Assert(concept2.GetArg("user-name").Value, Equals, "foo")
   141  	c.Assert(concept2.GetArg("user-phone").Value, Equals, "9900")
   142  
   143  	nestedConcept2 := concept2.ConceptSteps[0]
   144  	c.Assert(nestedConcept2.GetArg("userid").Value, Equals, "456")
   145  	c.Assert(nestedConcept2.GetArg("username").Value, Equals, "foo")
   146  
   147  }
   148  
   149  func (s *MySuite) TestPopulatingNestedConceptsWithStaticParametersLookup(c *C) {
   150  	parser := new(SpecParser)
   151  	specText := SpecBuilder().specHeading("A spec heading").
   152  		scenarioHeading("First scenario").
   153  		step("create user \"456\" \"foo\" and \"123456\"").
   154  		String()
   155  
   156  	conceptDictionary := gauge.NewConceptDictionary()
   157  	path, _ := filepath.Abs(filepath.Join("testdata", "static_param_concept.cpt"))
   158  	AddConcepts([]string{path}, conceptDictionary)
   159  
   160  	spec, _ := parser.Parse(specText, conceptDictionary, "")
   161  	concept1 := spec.Scenarios[0].Steps[0]
   162  
   163  	dataTableLookup := new(gauge.ArgLookup).FromDataTableRow(&spec.DataTable.Table, 0)
   164  	PopulateConceptDynamicParams(concept1, dataTableLookup)
   165  
   166  	c.Assert(concept1.GetArg("user-id").Value, Equals, "456")
   167  	c.Assert(concept1.GetArg("user-name").Value, Equals, "foo")
   168  	c.Assert(concept1.GetArg("user-phone").Value, Equals, "123456")
   169  
   170  	nestedConcept := concept1.ConceptSteps[0]
   171  	c.Assert(nestedConcept.GetArg("userid").Value, Equals, "456")
   172  	c.Assert(nestedConcept.GetArg("username").Value, Equals, "static-value")
   173  }
   174  
   175  func (s *MySuite) TestEachConceptUsageIsUpdatedWithRespectiveParams(c *C) {
   176  	parser := new(SpecParser)
   177  	specText := SpecBuilder().specHeading("A spec heading").
   178  		scenarioHeading("First scenario").
   179  		step("create user \"sdf\" \"name\" and \"1234\"").
   180  		String()
   181  
   182  	conceptDictionary := gauge.NewConceptDictionary()
   183  	path, _ := filepath.Abs(filepath.Join("testdata", "static_param_concept.cpt"))
   184  	AddConcepts([]string{path}, conceptDictionary)
   185  	spec, _ := parser.Parse(specText, conceptDictionary, "")
   186  	concept1 := spec.Scenarios[0].Steps[0]
   187  
   188  	nestedConcept := concept1.ConceptSteps[0]
   189  	nestedConcept1 := concept1.ConceptSteps[1]
   190  
   191  	c.Assert(nestedConcept.GetArg("username").Value, Equals, "static-value")
   192  	c.Assert(nestedConcept1.GetArg("username").Value, Equals, "static-value1")
   193  	c.Assert(nestedConcept.GetArg("userid").Value, Equals, "sdf")
   194  	c.Assert(nestedConcept1.GetArg("userid").Value, Equals, "sdf")
   195  }