github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/parser/parse_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  	"strings"
    24  
    25  	"github.com/getgauge/gauge/gauge"
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  func (s *MySuite) TestSimpleStepAfterStepValueExtraction(c *C) {
    30  	stepText := "a simple step"
    31  	stepValue, err := ExtractStepValueAndParams(stepText, false)
    32  
    33  	args := stepValue.Args
    34  	c.Assert(err, Equals, nil)
    35  	c.Assert(len(args), Equals, 0)
    36  	c.Assert(stepValue.StepValue, Equals, "a simple step")
    37  	c.Assert(stepValue.ParameterizedStepValue, Equals, "a simple step")
    38  }
    39  
    40  func (s *MySuite) TestStepWithColonAfterStepValueExtraction(c *C) {
    41  	stepText := "a : simple step \"hello\""
    42  	stepValue, err := ExtractStepValueAndParams(stepText, false)
    43  	args := stepValue.Args
    44  	c.Assert(err, Equals, nil)
    45  	c.Assert(len(args), Equals, 1)
    46  	c.Assert(stepValue.StepValue, Equals, "a : simple step {}")
    47  	c.Assert(stepValue.ParameterizedStepValue, Equals, "a : simple step <hello>")
    48  }
    49  
    50  func (s *MySuite) TestSimpleStepAfterStepValueExtractionForStepWithAParam(c *C) {
    51  	stepText := "Comment <a>"
    52  	stepValue, err := ExtractStepValueAndParams(stepText, false)
    53  
    54  	args := stepValue.Args
    55  	c.Assert(err, Equals, nil)
    56  	c.Assert(len(args), Equals, 1)
    57  	c.Assert(stepValue.StepValue, Equals, "Comment {}")
    58  	c.Assert(stepValue.ParameterizedStepValue, Equals, "Comment <a>")
    59  }
    60  
    61  func (s *MySuite) TestAddingTableParamAfterStepValueExtraction(c *C) {
    62  	stepText := "a simple step"
    63  	stepValue, err := ExtractStepValueAndParams(stepText, true)
    64  
    65  	args := stepValue.Args
    66  	c.Assert(err, Equals, nil)
    67  	c.Assert(len(args), Equals, 1)
    68  	c.Assert(args[0], Equals, string(gauge.TableArg))
    69  	c.Assert(stepValue.StepValue, Equals, "a simple step {}")
    70  	c.Assert(stepValue.ParameterizedStepValue, Equals, "a simple step <table>")
    71  }
    72  
    73  func (s *MySuite) TestAddingTableParamAfterStepValueExtractionForStepWithExistingParam(c *C) {
    74  	stepText := "a \"param1\" step with multiple params <param2> <file:specialParam>"
    75  	stepValue, err := ExtractStepValueAndParams(stepText, true)
    76  
    77  	args := stepValue.Args
    78  	c.Assert(err, Equals, nil)
    79  	c.Assert(len(args), Equals, 4)
    80  	c.Assert(args[0], Equals, "param1")
    81  	c.Assert(args[1], Equals, "param2")
    82  	c.Assert(args[2], Equals, "file:specialParam")
    83  	c.Assert(args[3], Equals, "table")
    84  	c.Assert(stepValue.StepValue, Equals, "a {} step with multiple params {} {} {}")
    85  	c.Assert(stepValue.ParameterizedStepValue, Equals, "a <param1> step with multiple params <param2> <file:specialParam> <table>")
    86  }
    87  
    88  func (s *MySuite) TestAfterStepValueExtractionForStepWithExistingParam(c *C) {
    89  	stepText := "a \"param1\" step with multiple params <param2> <file:specialParam>"
    90  	stepValue, err := ExtractStepValueAndParams(stepText, false)
    91  
    92  	args := stepValue.Args
    93  	c.Assert(err, Equals, nil)
    94  	c.Assert(len(args), Equals, 3)
    95  	c.Assert(args[0], Equals, "param1")
    96  	c.Assert(args[1], Equals, "param2")
    97  	c.Assert(args[2], Equals, "file:specialParam")
    98  	c.Assert(stepValue.StepValue, Equals, "a {} step with multiple params {} {}")
    99  	c.Assert(stepValue.ParameterizedStepValue, Equals, "a <param1> step with multiple params <param2> <file:specialParam>")
   100  }
   101  
   102  func (s *MySuite) TestCreateStepValueFromStep(c *C) {
   103  	step := &gauge.Step{Value: "simple step with {} and {}", Args: []*gauge.StepArg{staticArg("hello"), dynamicArg("desc")}}
   104  	stepValue := CreateStepValue(step)
   105  
   106  	args := stepValue.Args
   107  	c.Assert(len(args), Equals, 2)
   108  	c.Assert(args[0], Equals, "hello")
   109  	c.Assert(args[1], Equals, "desc")
   110  	c.Assert(stepValue.StepValue, Equals, "simple step with {} and {}")
   111  	c.Assert(stepValue.ParameterizedStepValue, Equals, "simple step with <hello> and <desc>")
   112  }
   113  
   114  func (s *MySuite) TestCreateStepValueFromStepWithSpecialParams(c *C) {
   115  	step := &gauge.Step{Value: "a step with {}, {} and {}", Args: []*gauge.StepArg{specialTableArg("hello"), specialStringArg("file:user.txt"), tableArgument()}}
   116  	stepValue := CreateStepValue(step)
   117  
   118  	args := stepValue.Args
   119  	c.Assert(len(args), Equals, 3)
   120  	c.Assert(args[0], Equals, "hello")
   121  	c.Assert(args[1], Equals, "file:user.txt")
   122  	c.Assert(args[2], Equals, "table")
   123  	c.Assert(stepValue.StepValue, Equals, "a step with {}, {} and {}")
   124  	c.Assert(stepValue.ParameterizedStepValue, Equals, "a step with <hello>, <file:user.txt> and <table>")
   125  }
   126  
   127  func (s *MySuite) TestSpecsFromArgsForMultipleIndexedArgsForOneSpec(c *C) {
   128  	specs, _ := parseSpecsInDirs(gauge.NewConceptDictionary(), []string{filepath.Join("testdata", "sample.spec:3"), filepath.Join("testdata", "sample.spec:6")}, gauge.NewBuildErrors())
   129  
   130  	c.Assert(len(specs), Equals, 1)
   131  	c.Assert(len(specs[0].Scenarios), Equals, 2)
   132  }
   133  
   134  func (s *MySuite) TestSpecsFromArgsForIndexedArgsForMultipleSpecs(c *C) {
   135  	sampleSpec := filepath.Join("testdata", "sample.spec")
   136  	sample2Spec := filepath.Join("testdata", "sample2.spec")
   137  	specs, _ := parseSpecsInDirs(gauge.NewConceptDictionary(), []string{sample2Spec, sampleSpec, sample2Spec + ":6"}, gauge.NewBuildErrors())
   138  
   139  	c.Assert(len(specs), Equals, 2)
   140  	c.Assert(len(specs[0].Scenarios), Equals, 2)
   141  	c.Assert(len(specs[1].Scenarios), Equals, 2)
   142  }
   143  
   144  func (s *MySuite) TestSpecsFromArgsMaintainsOrderOfSpecsPassed(c *C) {
   145  	sampleSpec := filepath.Join("testdata", "sample.spec")
   146  	sample2Spec := filepath.Join("testdata", "sample2.spec")
   147  	specs, _ := parseSpecsInDirs(gauge.NewConceptDictionary(), []string{sample2Spec, sampleSpec}, gauge.NewBuildErrors())
   148  
   149  	c.Assert(len(specs), Equals, 2)
   150  	c.Assert(specs[0].Heading.Value, Equals, "Sample 2")
   151  	c.Assert(specs[1].Heading.Value, Equals, "Sample")
   152  }
   153  
   154  func (s *MySuite) TestGetAllSpecsMaintainsOrderOfSpecs(c *C) {
   155  	sample2Spec := filepath.Join("testdata", "sample2.spec")
   156  	sampleSpec := filepath.Join("testdata", "sample.spec")
   157  	givenSpecs, indexedSpecs := getAllSpecFiles([]string{sample2Spec, sampleSpec})
   158  
   159  	c.Assert(len(givenSpecs), Equals, 2)
   160  	c.Assert(len(indexedSpecs), Equals, 2)
   161  
   162  	if !strings.HasSuffix(givenSpecs[0], sample2Spec) {
   163  		c.Fatalf("%s file order has changed", sample2Spec)
   164  	}
   165  	if !strings.HasSuffix(givenSpecs[1], sampleSpec) {
   166  		c.Fatalf("%s file order has changed", sampleSpec)
   167  	}
   168  
   169  	if !strings.HasSuffix(indexedSpecs[0].filePath, sample2Spec) {
   170  		c.Fatalf("%s file order has changed", sample2Spec)
   171  	}
   172  	c.Assert(len(indexedSpecs[0].indices), Equals, 0)
   173  
   174  	if !strings.HasSuffix(indexedSpecs[1].filePath, sampleSpec) {
   175  		c.Fatalf("%s file order has changed", sampleSpec)
   176  	}
   177  	c.Assert(len(indexedSpecs[1].indices), Equals, 0)
   178  }
   179  
   180  func (s *MySuite) TestGetAllSpecsAddIndicesForIndexedSpecs(c *C) {
   181  	file := filepath.Join("testdata", "sample.spec")
   182  	_, indexedSpecs := getAllSpecFiles([]string{file + ":1", file + ":5"})
   183  
   184  	c.Assert(len(indexedSpecs), Equals, 1)
   185  
   186  	if !strings.HasSuffix(indexedSpecs[0].filePath, file) {
   187  		c.Fatalf("%s file order has changed", file)
   188  	}
   189  	c.Assert(len(indexedSpecs[0].indices), Equals, 2)
   190  	c.Assert(indexedSpecs[0].indices[0], Equals, 1)
   191  	c.Assert(indexedSpecs[0].indices[1], Equals, 5)
   192  }
   193  
   194  func (s *MySuite) TestGetAllSpecsShouldDeDuplicateSpecs(c *C) {
   195  	sampleSpec := filepath.Join("testdata", "sample.spec")
   196  	sample2Spec := filepath.Join("testdata", "sample2.spec")
   197  
   198  	_, indexedSpecs := getAllSpecFiles([]string{sampleSpec, sample2Spec, sampleSpec, sample2Spec + ":2"})
   199  
   200  	c.Assert(len(indexedSpecs), Equals, 2)
   201  
   202  	if !strings.HasSuffix(indexedSpecs[0].filePath, sampleSpec) {
   203  		c.Fatalf("%s file order has changed", sampleSpec)
   204  	}
   205  
   206  	if !strings.HasSuffix(indexedSpecs[1].filePath, sample2Spec) {
   207  		c.Fatalf("%s file order has changed", sample2Spec)
   208  	}
   209  }
   210  
   211  func (s *MySuite) TestGetAllSpecsShouldDeDuplicateIndexedSpecs(c *C) {
   212  	sampleSpec := filepath.Join("testdata", "sample.spec")
   213  	sample2Spec := filepath.Join("testdata", "sample2.spec")
   214  
   215  	_, indexedSpecs := getAllSpecFiles([]string{sampleSpec + ":2", sample2Spec, sampleSpec})
   216  
   217  	c.Assert(len(indexedSpecs), Equals, 2)
   218  
   219  	if !strings.HasSuffix(indexedSpecs[0].filePath, sampleSpec) {
   220  		c.Fatalf("%s file order has changed", sampleSpec)
   221  	}
   222  
   223  	if !strings.HasSuffix(indexedSpecs[1].filePath, sample2Spec) {
   224  		c.Fatalf("%s file order has changed", sample2Spec)
   225  	}
   226  }
   227  
   228  func (s *MySuite) TestGetAllSpecsForIndexedNonExistingSpec(c *C) {
   229  	_, indexedSpecs := getAllSpecFiles([]string{"example.spec" + ":1"})
   230  
   231  	c.Assert(len(indexedSpecs), Equals, 0)
   232  }
   233  
   234  func (s *MySuite) TestToCheckIfItsIndexedSpec(c *C) {
   235  	c.Assert(isIndexedSpec("specs/hello_world:as"), Equals, false)
   236  	c.Assert(isIndexedSpec("specs/hello_world.spec:0"), Equals, true)
   237  	c.Assert(isIndexedSpec("specs/hello_world.spec:78809"), Equals, true)
   238  	c.Assert(isIndexedSpec("specs/hello_world.spec:09"), Equals, true)
   239  	c.Assert(isIndexedSpec("specs/hello_world.spec:09sa"), Equals, false)
   240  	c.Assert(isIndexedSpec("specs/hello_world.spec:09090"), Equals, true)
   241  	c.Assert(isIndexedSpec("specs/hello_world.spec"), Equals, false)
   242  	c.Assert(isIndexedSpec("specs/hello_world.spec:"), Equals, false)
   243  	c.Assert(isIndexedSpec("specs/hello_world.md"), Equals, false)
   244  }
   245  
   246  func (s *MySuite) TestToObtainIndexedSpecName(c *C) {
   247  	specName, scenarioNum := getIndexedSpecName("specs/hello_world.spec:67")
   248  	c.Assert(specName, Equals, "specs/hello_world.spec")
   249  	c.Assert(scenarioNum, Equals, 67)
   250  }
   251  func (s *MySuite) TestToObtainIndexedSpecName1(c *C) {
   252  	specName, scenarioNum := getIndexedSpecName("hello_world.spec:67342")
   253  	c.Assert(specName, Equals, "hello_world.spec")
   254  	c.Assert(scenarioNum, Equals, 67342)
   255  }
   256  
   257  func (s *MySuite) TestGetIndex(c *C) {
   258  	c.Assert(getIndex("hello.spec:67"), Equals, 10)
   259  	c.Assert(getIndex("specs/hello.spec:67"), Equals, 16)
   260  	c.Assert(getIndex("specs\\hello.spec:67"), Equals, 16)
   261  	c.Assert(getIndex(":67"), Equals, 0)
   262  	c.Assert(getIndex(""), Equals, 0)
   263  	c.Assert(getIndex("foo"), Equals, 0)
   264  	c.Assert(getIndex(":"), Equals, 0)
   265  	c.Assert(getIndex("f:7a.spec:9"), Equals, 9)
   266  }
   267  
   268  func staticArg(val string) *gauge.StepArg {
   269  	return &gauge.StepArg{ArgType: gauge.Static, Value: val}
   270  }
   271  
   272  func dynamicArg(val string) *gauge.StepArg {
   273  	return &gauge.StepArg{ArgType: gauge.Dynamic, Value: val}
   274  }
   275  
   276  func tableArgument() *gauge.StepArg {
   277  	return &gauge.StepArg{ArgType: gauge.TableArg}
   278  }
   279  
   280  func specialTableArg(val string) *gauge.StepArg {
   281  	return &gauge.StepArg{ArgType: gauge.SpecialTable, Name: val}
   282  }
   283  
   284  func specialStringArg(val string) *gauge.StepArg {
   285  	return &gauge.StepArg{ArgType: gauge.SpecialString, Name: val}
   286  }