github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/api/infoGatherer/specDetails_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 infoGatherer
    19  
    20  import (
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"github.com/getgauge/gauge/config"
    27  	"github.com/getgauge/gauge/gauge"
    28  	"github.com/getgauge/gauge/util"
    29  	. "gopkg.in/check.v1"
    30  )
    31  
    32  func Test(t *testing.T) { TestingT(t) }
    33  
    34  const specDir = "specs"
    35  
    36  var _ = Suite(&MySuite{})
    37  
    38  var concept1 []byte
    39  var concept2 []byte
    40  var spec1 []byte
    41  
    42  type MySuite struct {
    43  	specsDir   string
    44  	projectDir string
    45  }
    46  
    47  func (s *MySuite) SetUpTest(c *C) {
    48  	s.projectDir, _ = ioutil.TempDir("_testdata", "gaugeTest")
    49  	s.specsDir, _ = util.CreateDirIn(s.projectDir, specDir)
    50  	config.ProjectRoot = s.projectDir
    51  
    52  	s.buildTestData()
    53  }
    54  
    55  func (s *MySuite) TearDownTest(c *C) {
    56  	os.RemoveAll(s.projectDir)
    57  }
    58  
    59  func (s *MySuite) buildTestData() {
    60  	concept1 = make([]byte, 0)
    61  	concept1 = append(concept1, `# foo bar
    62  * first step with "foo"
    63  * say "hello" to me
    64  * a "final" step
    65  `...)
    66  
    67  	concept2 = make([]byte, 0)
    68  	concept2 = append(concept2, `# bar
    69  * first step with "foo"
    70  * say "hello" to me
    71  * a "final" step
    72  `...)
    73  
    74  	spec1 = make([]byte, 0)
    75  	spec1 = append(spec1, `Specification Heading
    76  =====================
    77  Scenario 1
    78  ----------
    79  * say hello
    80  * say "hello" to me
    81  `...)
    82  }
    83  
    84  func (s *MySuite) TestGetParsedSpecs(c *C) {
    85  	_, err := util.CreateFileIn(s.specsDir, "spec1.spec", spec1)
    86  	c.Assert(err, Equals, nil)
    87  	specInfoGatherer := &SpecInfoGatherer{SpecDirs: []string{specDir}}
    88  
    89  	specFiles := util.FindSpecFilesIn(s.specsDir)
    90  	specs := specInfoGatherer.getParsedSpecs(specFiles)
    91  
    92  	c.Assert(len(specs), Equals, 1)
    93  	c.Assert(specs[0].Heading.Value, Equals, "Specification Heading")
    94  }
    95  
    96  func (s *MySuite) TestGetParsedConcepts(c *C) {
    97  	_, err := util.CreateFileIn(s.specsDir, "concept.cpt", concept1)
    98  	c.Assert(err, Equals, nil)
    99  	specInfoGatherer := &SpecInfoGatherer{SpecDirs: []string{s.projectDir + string(filepath.Separator) + specDir}}
   100  
   101  	conceptsMap := specInfoGatherer.getParsedConcepts()
   102  
   103  	c.Assert(len(conceptsMap), Equals, 1)
   104  	c.Assert(conceptsMap["foo bar"], NotNil)
   105  	c.Assert(specInfoGatherer.conceptDictionary, NotNil)
   106  }
   107  
   108  func (s *MySuite) TestGetParsedStepValues(c *C) {
   109  	steps := []*gauge.Step{
   110  		&gauge.Step{Value: "Step with a {}", LineText: "Step with a <table>", IsConcept: true, HasInlineTable: true},
   111  		&gauge.Step{Value: "A context step", LineText: "A context step", IsConcept: false},
   112  		&gauge.Step{Value: "Say {} to {}", LineText: "Say \"hello\" to \"gauge\"", IsConcept: false,
   113  			Args: []*gauge.StepArg{
   114  				&gauge.StepArg{Name: "first", Value: "hello", ArgType: gauge.Static},
   115  				&gauge.StepArg{Name: "second", Value: "gauge", ArgType: gauge.Static}},
   116  		},
   117  	}
   118  
   119  	stepValues := getParsedStepValues(steps)
   120  
   121  	c.Assert(len(stepValues), Equals, 2)
   122  	c.Assert(stepValues[0].StepValue, Equals, "A context step")
   123  	c.Assert(stepValues[1].StepValue, Equals, "Say {} to {}")
   124  }
   125  
   126  func (s *MySuite) TestInitSpecsCache(c *C) {
   127  	_, err := util.CreateFileIn(s.specsDir, "spec1.spec", spec1)
   128  	c.Assert(err, Equals, nil)
   129  	specInfoGatherer := &SpecInfoGatherer{SpecDirs: []string{specDir}}
   130  	specInfoGatherer.waitGroup.Add(1)
   131  
   132  	specInfoGatherer.initSpecsCache()
   133  
   134  	c.Assert(len(specInfoGatherer.specsCache), Equals, 1)
   135  }
   136  
   137  func (s *MySuite) TestInitConceptsCache(c *C) {
   138  	_, err := util.CreateFileIn(s.specsDir, "concept1.cpt", concept1)
   139  	c.Assert(err, Equals, nil)
   140  	_, err = util.CreateFileIn(s.specsDir, "concept2.cpt", concept2)
   141  	c.Assert(err, Equals, nil)
   142  	specInfoGatherer := &SpecInfoGatherer{SpecDirs: []string{s.projectDir + string(filepath.Separator) + specDir}}
   143  	specInfoGatherer.waitGroup.Add(1)
   144  
   145  	specInfoGatherer.initConceptsCache()
   146  
   147  	c.Assert(len(specInfoGatherer.conceptsCache), Equals, 2)
   148  }