Skip to content

Commit

Permalink
Removing Add method from ConceptDictionary and refactoring test
Browse files Browse the repository at this point in the history
  • Loading branch information
kashishm committed Jan 20, 2016
1 parent 45a20bd commit d012e94
Show file tree
Hide file tree
Showing 20 changed files with 337 additions and 665 deletions.
55 changes: 21 additions & 34 deletions execution/specExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package execution

import (
"fmt"
"path/filepath"

"github.com/getgauge/gauge/gauge_messages"
"github.com/getgauge/gauge/parser"
. "gopkg.in/check.v1"
Expand Down Expand Up @@ -93,34 +95,35 @@ func (specBuilder *specBuilder) text(comment string) *specBuilder {
}

func (s *MySuite) TestResolveConceptToProtoConceptItem(c *C) {
conceptDictionary := new(parser.ConceptDictionary)
conceptDictionary := parser.NewConceptDictionary()

specText := SpecBuilder().specHeading("A spec heading").
scenarioHeading("First scenario").
step("create user \"456\" \"foo\" and \"9900\"").
String()
path, _ := filepath.Abs(filepath.Join("testdata", "concept.cpt"))
parser.AddConcepts(path, conceptDictionary)

conceptText := SpecBuilder().
specHeading("create user <user-id> <user-name> and <user-phone>").
step("assign id <user-id> and name <user-name>").
step("assign phone <user-phone>").String()

concepts, _ := new(parser.ConceptParser).Parse(conceptText)
conceptDictionary.Add(concepts, "file.cpt")
spec, _ := new(parser.SpecParser).Parse(specText, conceptDictionary)

specExecutor := newSpecExecutor(spec, nil, nil, indexRange{start: 0, end: 0}, nil, nil)
specExecutor.errMap = &validationErrMaps{make(map[*parser.Specification][]*stepValidationError), make(map[*parser.Scenario][]*stepValidationError), make(map[*parser.Step]*stepValidationError)}
protoConcept := specExecutor.resolveToProtoConceptItem(*spec.Scenarios[0].Steps[0]).GetConcept()

checkConceptParameterValuesInOrder(c, protoConcept, "456", "foo", "9900")

firstStep := protoConcept.GetSteps()[0].GetStep()
params := getParameters(firstStep.GetFragments())
c.Assert(2, Equals, len(params))
fmt.Println(protoConcept)
firstNestedStep := protoConcept.GetSteps()[0].GetConcept().GetSteps()[0].GetStep()
params := getParameters(firstNestedStep.GetFragments())
c.Assert(1, Equals, len(params))
c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
c.Assert(params[0].GetValue(), Equals, "456")

secondNestedStep := protoConcept.GetSteps()[0].GetConcept().GetSteps()[1].GetStep()
params = getParameters(secondNestedStep.GetFragments())
c.Assert(1, Equals, len(params))
c.Assert(params[0].GetParameterType(), Equals, gauge_messages.Parameter_Dynamic)
c.Assert(params[0].GetValue(), Equals, "foo")

secondStep := protoConcept.GetSteps()[1].GetStep()
params = getParameters(secondStep.GetFragments())
c.Assert(1, Equals, len(params))
Expand All @@ -130,23 +133,15 @@ func (s *MySuite) TestResolveConceptToProtoConceptItem(c *C) {
}

func (s *MySuite) TestResolveNestedConceptToProtoConceptItem(c *C) {
conceptDictionary := new(parser.ConceptDictionary)
conceptDictionary := parser.NewConceptDictionary()

specText := SpecBuilder().specHeading("A spec heading").
scenarioHeading("First scenario").
step("create user \"456\" \"foo\" and \"9900\"").
String()

conceptText := SpecBuilder().
specHeading("create user <user-id> <user-name> and <user-phone>").
step("assign id <user-id> and name <user-name>").
step("assign phone <user-phone>").
specHeading("assign id <userid> and name <username>").
step("add id <userid>").
step("add name <username>").String()

concepts, _ := new(parser.ConceptParser).Parse(conceptText)
conceptDictionary.Add(concepts, "file.cpt")
path, _ := filepath.Abs(filepath.Join("testdata", "concept.cpt"))
parser.AddConcepts(path, conceptDictionary)
specParser := new(parser.SpecParser)
spec, _ := specParser.Parse(specText, conceptDictionary)

Expand Down Expand Up @@ -182,7 +177,7 @@ func (s *MySuite) TestResolveNestedConceptToProtoConceptItem(c *C) {
}

func (s *MySuite) TestResolveToProtoConceptItemWithDataTable(c *C) {
conceptDictionary := new(parser.ConceptDictionary)
conceptDictionary := parser.NewConceptDictionary()

specText := SpecBuilder().specHeading("A spec heading").
tableHeader("id", "name", "phone").
Expand All @@ -192,16 +187,8 @@ func (s *MySuite) TestResolveToProtoConceptItemWithDataTable(c *C) {
step("create user <id> <name> and <phone>").
String()

conceptText := SpecBuilder().
specHeading("create user <user-id> <user-name> and <user-phone>").
step("assign id <user-id> and name <user-name>").
step("assign phone <user-phone>").
specHeading("assign id <userid> and name <username>").
step("add id <userid>").
step("add name <username>").String()

concepts, _ := new(parser.ConceptParser).Parse(conceptText)
conceptDictionary.Add(concepts, "file.cpt")
path, _ := filepath.Abs(filepath.Join("testdata", "concept.cpt"))
parser.AddConcepts(path, conceptDictionary)
specParser := new(parser.SpecParser)
spec, _ := specParser.Parse(specText, conceptDictionary)

Expand Down
7 changes: 7 additions & 0 deletions execution/testdata/concept.cpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# create user <user-id> <user-name> and <user-phone>
* assign id <user-id> and name <user-name>
* assign phone <user-phone>

# assign id <userid> and name <username>
* add id <userid>
* add name <username>
11 changes: 7 additions & 4 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package formatter

import (
"testing"

"github.com/getgauge/gauge/parser"
. "gopkg.in/check.v1"
"testing"
)

func Test(t *testing.T) { TestingT(t) }
Expand Down Expand Up @@ -50,7 +51,7 @@ func (s *MySuite) TestFormatSpecification(c *C) {
Scenario Heading
----------------
* Example step
* Step with inline table
* Step with inline table`+" "+`
|id |name|
|---|----|
|<1>|foo |
Expand All @@ -59,10 +60,12 @@ Scenario Heading
}

func (s *MySuite) TestFormatConcepts(c *C) {
dictionary := new(parser.ConceptDictionary)
dictionary := parser.NewConceptDictionary()
step1 := &parser.Step{Value: "sdsf", LineText: "sdsf", IsConcept: true, LineNo: 1, PreComments: []*parser.Comment{&parser.Comment{Value: "COMMENT", LineNo: 1}}}
step2 := &parser.Step{Value: "dsfdsfdsf", LineText: "dsfdsfdsf", IsConcept: true, LineNo: 2, Items: []parser.Item{&parser.Step{Value: "sfd", LineText: "sfd", IsConcept: false}, &parser.Step{Value: "sdfsdf" + "T", LineText: "sdfsdf" + "T", IsConcept: false}}}
dictionary.Add([]*parser.Step{step1, step2}, "file.cpt")

dictionary.ConceptsMap[step1.Value] = &parser.Concept{ConceptStep: step1, FileName: "file.cpt"}
dictionary.ConceptsMap[step2.Value] = &parser.Concept{ConceptStep: step2, FileName: "file.cpt"}

formatted := FormatConcepts(dictionary)
c.Assert(formatted["file.cpt"], Equals, `COMMENT
Expand Down
30 changes: 10 additions & 20 deletions parser/conceptParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
type ConceptDictionary struct {
ConceptsMap map[string]*Concept
constructionMap map[string][]*Step
referenceMap map[*Step][]*Step
}

type Concept struct {
Expand Down Expand Up @@ -232,25 +231,6 @@ func AddConcepts(conceptFile string, conceptDictionary *ConceptDictionary) *Pars
if parseResults != nil && parseResults.Error != nil {
return parseResults.Error
}
return conceptDictionary.Add(concepts, conceptFile)
}

func NewConceptDictionary() *ConceptDictionary {
return &ConceptDictionary{ConceptsMap: make(map[string]*Concept, 0)}
}

func (conceptDictionary *ConceptDictionary) isConcept(step *Step) bool {
_, ok := conceptDictionary.ConceptsMap[step.Value]
return ok

}
func (conceptDictionary *ConceptDictionary) Add(concepts []*Step, conceptFile string) *ParseError {
if conceptDictionary.ConceptsMap == nil {
conceptDictionary.ConceptsMap = make(map[string]*Concept)
}
if conceptDictionary.constructionMap == nil {
conceptDictionary.constructionMap = make(map[string][]*Step)
}
for _, conceptStep := range concepts {
if _, exists := conceptDictionary.ConceptsMap[conceptStep.Value]; exists {
return &ParseError{Message: "Duplicate concept definition found", LineNo: conceptStep.LineNo, LineText: conceptStep.LineText}
Expand All @@ -262,6 +242,16 @@ func (conceptDictionary *ConceptDictionary) Add(concepts []*Step, conceptFile st
return conceptDictionary.validateConcepts()
}

func NewConceptDictionary() *ConceptDictionary {
return &ConceptDictionary{ConceptsMap: make(map[string]*Concept, 0), constructionMap: make(map[string][]*Step, 0)}
}

func (conceptDictionary *ConceptDictionary) isConcept(step *Step) bool {
_, ok := conceptDictionary.ConceptsMap[step.Value]
return ok

}

func (conceptDictionary *ConceptDictionary) search(stepValue string) *Concept {
if concept, ok := conceptDictionary.ConceptsMap[stepValue]; ok {
return concept
Expand Down
Loading

0 comments on commit d012e94

Please sign in to comment.