forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
135 lines (113 loc) · 4.08 KB
/
processor.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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 (
"bytes"
"fmt"
"strings"
"github.com/getgauge/gauge/gauge"
)
func processSpec(parser *SpecParser, token *Token) (*ParseError, bool) {
if len(token.Value) < 1 {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: "Spec heading should have at least one character"}, true
}
return nil, false
}
func processTearDown(parser *SpecParser, token *Token) (*ParseError, bool) {
if len(token.Value) < 3 {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: "Teardown should have at least three underscore characters"}, true
}
return nil, false
}
func processDataTable(parser *SpecParser, token *Token) (*ParseError, bool) {
if len(strings.TrimSpace(strings.Replace(token.Value, "table:", "", 1))) == 0 {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: "Table location not specified"}, true
}
resolvedArg, err := newSpecialTypeResolver().resolve(token.Value)
if resolvedArg == nil || err != nil {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: fmt.Sprintf("Could not resolve table from %s", token.LineText)}, true
}
return nil, false
}
func processScenario(parser *SpecParser, token *Token) (*ParseError, bool) {
if len(token.Value) < 1 {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: "Scenario heading should have at least one character"}, true
}
parser.clearState()
return nil, false
}
func processComment(parser *SpecParser, token *Token) (*ParseError, bool) {
parser.clearState()
addStates(&parser.currentState, commentScope)
return nil, false
}
func processTag(parser *SpecParser, token *Token) (*ParseError, bool) {
parser.clearState()
tokens := splitAndTrimTags(token.Value)
for _, tagValue := range tokens {
if len(tagValue) > 0 {
token.Args = append(token.Args, tagValue)
}
}
return nil, false
}
func processTable(parser *SpecParser, token *Token) (*ParseError, bool) {
var buffer bytes.Buffer
shouldEscape := false
for i, element := range token.Value {
if i == 0 {
continue
}
if shouldEscape {
buffer.WriteRune(element)
shouldEscape = false
continue
}
if element == '\\' {
shouldEscape = true
continue
} else if element == '|' {
trimmedValue := strings.TrimSpace(buffer.String())
if token.Kind == gauge.TableHeader {
if len(trimmedValue) == 0 {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: "Table header should not be blank"}, true
}
if arrayContains(token.Args, trimmedValue) {
return &ParseError{LineNo: parser.lineNo, LineText: token.Value, Message: "Table header cannot have repeated column values"}, true
}
}
token.Args = append(token.Args, trimmedValue)
buffer.Reset()
} else {
buffer.WriteRune(element)
}
}
if !isInState(parser.currentState, tableScope) {
addStates(&parser.currentState, tableScope)
} else {
addStates(&parser.currentState, tableDataScope)
}
return nil, false
}
func processConceptStep(spec *gauge.Specification, step *gauge.Step, conceptDictionary *gauge.ConceptDictionary) {
if conceptFromDictionary := conceptDictionary.Search(step.Value); conceptFromDictionary != nil {
createConceptStep(spec, conceptFromDictionary.ConceptStep, step)
}
}
func splitAndTrimTags(tag string) []string {
listOfTags := strings.Split(tag, ",")
for i, aTag := range listOfTags {
listOfTags[i] = strings.TrimSpace(aTag)
}
return listOfTags
}