forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
117 lines (100 loc) · 3.61 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package parser
import (
"strings"
"github.com/getgauge/common"
"github.com/getgauge/gauge/gauge"
"github.com/getgauge/gauge/logger"
)
func ParseSpecFiles(specFiles []string, conceptDictionary *gauge.ConceptDictionary) ([]*gauge.Specification, []*ParseResult) {
parseResultsChan := make(chan *ParseResult, len(specFiles))
specsChan := make(chan *gauge.Specification, len(specFiles))
var parseResults []*ParseResult
var specs []*gauge.Specification
for _, specFile := range specFiles {
go parseSpec(specFile, conceptDictionary, specsChan, parseResultsChan)
}
for _ = range specFiles {
parseResults = append(parseResults, <-parseResultsChan)
spec := <-specsChan
if spec != nil {
specs = append(specs, spec)
}
}
return specs, parseResults
}
func parseSpec(specFile string, conceptDictionary *gauge.ConceptDictionary, specChannel chan *gauge.Specification, parseResultChan chan *ParseResult) {
specFileContent, err := common.ReadFileContents(specFile)
if err != nil {
specChannel <- nil
parseResultChan <- &ParseResult{ParseErrors: []*ParseError{&ParseError{FileName: specFile, Message: err.Error()}}, Ok: false}
return
}
spec, parseResult := new(SpecParser).Parse(specFileContent, conceptDictionary, specFile)
specChannel <- spec
parseResultChan <- parseResult
}
func ExtractStepValueAndParams(stepText string, hasInlineTable bool) (*gauge.StepValue, error) {
stepValueWithPlaceHolders, args, err := processStepText(stepText)
if err != nil {
return nil, err
}
extractedStepValue, _ := extractStepValueAndParameterTypes(stepValueWithPlaceHolders)
if hasInlineTable {
extractedStepValue += " " + gauge.ParameterPlaceholder
args = append(args, string(gauge.TableArg))
}
parameterizedStepValue := getParameterizeStepValue(extractedStepValue, args)
return &gauge.StepValue{args, extractedStepValue, parameterizedStepValue}, nil
}
func CreateStepValue(step *gauge.Step) gauge.StepValue {
stepValue := gauge.StepValue{StepValue: step.Value}
args := make([]string, 0)
for _, arg := range step.Args {
switch arg.ArgType {
case gauge.Static, gauge.Dynamic:
args = append(args, arg.Value)
case gauge.TableArg:
args = append(args, "table")
case gauge.SpecialString, gauge.SpecialTable:
args = append(args, arg.Name)
}
}
stepValue.Args = args
stepValue.ParameterizedStepValue = getParameterizeStepValue(stepValue.StepValue, args)
return stepValue
}
func getParameterizeStepValue(stepValue string, params []string) string {
for _, param := range params {
stepValue = strings.Replace(stepValue, gauge.ParameterPlaceholder, "<"+param+">", 1)
}
return stepValue
}
func HandleParseResult(results ...*ParseResult) bool {
var failed = false
for _, result := range results {
if !result.Ok {
for _, err := range result.Errors() {
logger.Errorf(err)
}
failed = true
}
if result.Warnings != nil {
for _, warning := range result.Warnings {
logger.Warning("[ParseWarning] %s", warning)
}
}
}
return failed
}