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